torch.distributions.Bernoulli boolean support weird behaviour

Hello,

I’m trying to use the torch.distributions.Bernoulli(prob).log_prob(). I encounter an error where I have to use a boolean support for the input of log_prob. The error is

Expected value argument (Tensor of shape (32, 784)) to be within the support (Boolean()) of the distribution Bernoulli(probs: torch.Size([32, 784])), but found invalid values:
tensor([[0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        ...,
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.]], device='cuda:0')

Oddly, when I wrapped the log_prob’s input i.e. torch.bernoulli(targets), it somehow works which i don’t expect because the returned dtype for it is torch.float32. Are there any fixes for this? Thanks.

The code I’m using for it is wrapped in this function below,

def likelihood(outputs, targets):
    dist = torch.distributions.Bernoulli(probs=outputs)
    return dist.log_prob(targets).exp()

The outputs and targets are already within [0, 1] value range because the outputs are from a sigmoid activation while the targets are from a binary dataset.

Could you post a minimal and executable code snippet reproducing the issue as I’m currently unsure what exactly your inputs represent?