matthewwicker / deepbayes

Optimizers for performing approximate Bayesian inference on neural network parameters with Tensorflow and JAX
13 stars 3 forks source link

A few bugs in DeepBayes #1

Open hughiephan opened 9 months ago

hughiephan commented 9 months ago

Hi assistant professor @matthewwicker , I was trying to re-implemented the paper "Individual Fairness in Bayesian Neural Networks" but I get the following error

File /opt/conda/lib/python3.10/site-packages/deepbayes/optimizers/blrvi.py:68, in VariationalOnlineGuassNewton.step(self, features, labels, lrate)
     65     sample = tf.math.add(self.posterior_mean[i], sample)
     66     init_weights.append(sample)
---> 68 self.model.set_weights(np.asarray(init_weights))
     70 with tf.GradientTape(persistent=True) as tape:
     71     # Get the probabilities
     72     predictions = self.model(features)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (4,) + inhomogeneous part.

I believe doing something like numpy.array([....], dtype=object) should fix it. My notebook with above error could be found here: https://www.kaggle.com/code/hughiephan/fgsm-bayesian-neural-networks-failed

hughiephan commented 9 months ago

I create a pull request to tackle above problem https://github.com/matthewwicker/deepbayes/pull/2 . But there are still some errors as well (not directly related to numpy)

InvalidArgumentError: {{function_node __wrapped__Squeeze_device_/job:localhost/replica:0/task:0/device:CPU:0}} Can not squeeze dim[1], expected a dimension of 1, got 2 [Op:Squeeze] name: 

Could be related to your tutorials as well, X_train and X_test both needs to be reshape like this X_train = X_train.astype("float32").reshape(-1, 1, 28*28) instead of X_train = X_train.astype("float64").reshape(-1, 28*28)

hughiephan commented 9 months ago

Above PR is also fixing the following bug

VariationalOnlineGuassNewton' object has no attribute 'set_weights'

Just a quick change from model.set_weights to model.model.set_weights

hughiephan commented 9 months ago

The error is also the same for model._predict in verifiers.py and we need to change it to model.model._predict . But one problem is that _predict is for your custom PosteriorModel class and won't work with the default Sequential model where it is using predict function (not _predict)

hughiephan commented 9 months ago

Working version: https://www.kaggle.com/hughiephan/robustness-with-deep-bayesian

hughiephan commented 8 months ago

Regarding the error The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (4,) + inhomogeneous part, as mentioned above, we need to modify the codebase where numpy.array([....]), was used and change it to numpy.array([....], dtype=object). Example of that can be seen here: https://github.com/matthewwicker/deepbayes/pull/2/files#diff-2f2d1176939dbe25f9ffdef7cff879a97ad35f8c6caeffcd0227b94bca657403

Other than that, I believe a new fully-working version will be published around next month where all the problems here will be fixed.