facebookincubator / flowtorch

This library would form a permanent home for reusable components for deep probabilistic programming. The library would form and harness a community of users and contributors by focusing initially on complete infra and documentation for how to use and create components.
https://flowtorch.ai
MIT License
301 stars 21 forks source link

Issue with log_prob and composing sigmoid bijectors #108

Closed mlkrock closed 2 years ago

mlkrock commented 2 years ago

Issue Description

Hello, I think I stumbled across a potential issue when i was trying to compare to tensorflow, you can see it here. https://github.com/tensorflow/probability/issues/1556

Steps to Reproduce

setup

import torch
import flowtorch.bijectors as B
import flowtorch.distributions as D
import flowtorch.parameters as P
base_dist = torch.distributions.Independent(torch.distributions.Normal(torch.zeros(2), torch.ones(2)), 1)
bijectors = B.Compose(bijectors  =[B.Sigmoid(), B.Sigmoid()])
flow = D.Flow(base_dist, bijectors)

Expected Behavior

flow.log_prob(torch.zeros(2)) gives -7280.1641, but I think it should give a nan since sigmoid(sigmoid(x)) is in [0.5, 0.73] flow.log_prob(torch.zeros(2)+0.5) gives -7452.0645 but flow.log_prob(torch.zeros(2)+0.51)gives -2.6445. So it does reflect the domain [0.5,0.73] in terms of the magnitude of these probabilities, but should it give nans when outside the domain, or are these extremely small probabilities not a bug ?

System Info

stefanwebb commented 2 years ago

Hi @mlkrock, I suspect this is due to our implementation clipping the sigmoid function to prevent overflow or underflow from causing NaNs during optimization:

https://github.com/facebookincubator/flowtorch/blob/main/flowtorch/bijectors/sigmoid.py#L18 https://github.com/facebookincubator/flowtorch/blob/coupling/flowtorch/ops/__init__.py#L19

I.e. it's by design :)