brendanhasz / probflow

A Python package for building Bayesian models with TensorFlow or PyTorch
http://probflow.readthedocs.io
MIT License
170 stars 17 forks source link

Predictive interval fails for Dense model example #64

Open edebrouwer opened 3 years ago

edebrouwer commented 3 years ago

Hi, while trying to run the example of dense regression for fully connected NN, I run into an issue when I try to sample from the posterior of the model. I use exactly the commands as shown in the documentation.

My backend is pytorch,

Thank you so much !


# Compute 95% confidence intervals
lb, ub = model.predictive_interval(x_test, ci=0.95, batch_size=1)

# Plot em!
plt.fill_between(x_test[:, 0], lb[:, 0], ub[:, 0],
                 alpha=0.2, label='95% ci')
plt.plot(x, y, '.', label='Data')```

I get :

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-61-eb2905895d26> in <module>
      3 
      4 # Plot em!
----> 5 plt.fill_between(x_test[:, 0], lb[:, 0], ub[:, 0],
      6                  alpha=0.2, label='95% ci')
      7 plt.plot(x, y, '.', label='Data')

~/anaconda3/envs/causalode/lib/python3.8/site-packages/matplotlib/pyplot.py in fill_between(x, y1, y2, where, interpolate, step, data, **kwargs)
   2636         x, y1, y2=0, where=None, interpolate=False, step=None, *,
   2637         data=None, **kwargs):
-> 2638     return gca().fill_between(
   2639         x, y1, y2=y2, where=where, interpolate=interpolate, step=step,
   2640         **({"data": data} if data is not None else {}), **kwargs)

~/anaconda3/envs/causalode/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448 
   1449         bound = new_sig.bind(ax, *args, **kwargs)

~/anaconda3/envs/causalode/lib/python3.8/site-packages/matplotlib/axes/_axes.py in fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
   5299     def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
   5300                      step=None, **kwargs):
-> 5301         return self._fill_between_x_or_y(
   5302             "x", x, y1, y2,
   5303             where=where, interpolate=interpolate, step=step, **kwargs)

~/anaconda3/envs/causalode/lib/python3.8/site-packages/matplotlib/axes/_axes.py in _fill_between_x_or_y(self, ind_dir, ind, dep1, dep2, where, interpolate, step, **kwargs)
   5221                     f"must have the same size as {ind} in {func_name}(). This "
   5222                     "will become an error %(removal)s.")
-> 5223         where = where & ~functools.reduce(
   5224             np.logical_or, map(np.ma.getmask, [ind, dep1, dep2]))
   5225 

ValueError: operands could not be broadcast together with shapes (101,) (101000,) 
brendanhasz commented 3 years ago

Hi! Which matplotlib version are you using? If you're using a newer one, it seems like something's broke with the latest release of matplotlib (I actually wasn't able to even import the package past 3.3.4 😕 , was getting this error). Try using any matplotlib version <=3.3.4, that seemed to work for me (see this google colab for a working example and the versions used). Specifically I tested 3.2.2, 3.3.0, and 3.3.4 and they all worked; while 3.4.0 and 3.4.2 did not.

That said, you've made me realize there was a separate unrelated bug in that example! Fixed it here https://github.com/brendanhasz/probflow/pull/65 Apparently torch doesn't like casting a torch tensor to a torch tensor (I think maybe it was cutting off the gradient propagation? But not sure tbh. But anyway the updated example works). So basically just make sure you have the x = torch.tensor(x) line only in your top-level module (which in that example is the DenseRegression class).

Thanks for letting me know about the error and hope that helps! 😄