pyro-ppl / pyro

Deep universal probabilistic programming with Python and PyTorch
http://pyro.ai
Apache License 2.0
8.59k stars 987 forks source link

[bug] pyro.contrib.bnn.HiddenLayer missing attributes '_batch_shape' and '_event_shape' #2937

Open lbasora opened 3 years ago

lbasora commented 3 years ago

Issue Description

The class HiddenLayer is missing attributes '_batch_shape' and '_event_shape'. I provide a very simple code snippet to reproduce the error for '_batch_shape', but I've got the error when training a full model with such distribution layer (in the call to svi.step()).

Environment

For any bugs, please provide the following:

Code Snippet

import torch
import torch.nn.functional as nnf
from pyro.contrib.bnn import HiddenLayer

h = 5
x = torch.linspace(0, 0.5, 1000).view(-1, 1)
a1_mean, a1_scale = torch.zeros(x.shape[-1], h), torch.ones(x.shape[-1], h)
dist = HiddenLayer(
    x,
    a1_mean,
    a1_scale,
    non_linearity=nnf.relu,
    KL_factor=1.0,
    include_hidden_bias=False,
)
print(dist.batch_shape)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-c6853d534b82> in <module>
     14     include_hidden_bias=False,
     15 )
---> 16 print(dist.batch_shape)

~/.conda/envs/py39/lib/python3.9/site-packages/torch/distributions/distribution.py in batch_shape(self)
     80         Returns the shape over which parameters are batched.
     81         """
---> 82         return self._batch_shape
     83 
     84     @property

AttributeError: 'HiddenLayer' object has no attribute '_batch_shape'
martinjankowiak commented 3 years ago

hi @lbasora . this particular distribution isn't being maintained anymore. it should probably be deprecated. we recommend using TyXe for doing bayesian neural networks in Pyro

lbasora commented 3 years ago

Hi @martinjankowiak . Thanks very much for pointing me to the TyXe library. This is exactly the functionality I was looking for.