rl
rl copied to clipboard
[BUG] TruncatedNormal crashing when computing entropy
Describe the bug
Calling .entropy() on a TruncatedNormal distribution causes the code to crash.
To Reproduce
First crash happened using a PPO agent with entropy bonus turned on and actor parametrized with a TruncatedNormal.
A simple snippet to reproduce is the following:
import torch
from torchrl.modules.distributions import IndependentNormal, TruncatedNormal
if __name__ == '__main__':
loc, scale = torch.zeros(1), torch.ones(1)
d1 = IndependentNormal(loc, scale)
print(d1.entropy())
d2 = TruncatedNormal(loc, scale)
print(d2.entropy())
tensor(1.4189)
Traceback (most recent call last):
File "/home/diego/Desktop/test.py", line 10, in <module>
print(d2.entropy())
File "/home/diego/miniconda3/envs/pytorch/lib/python3.10/site-packages/torch/distributions/independent.py", line 103, in entropy
entropy = self.base_dist.entropy()
TypeError: 'Tensor' object is not callable
Expected behavior
The entropy value should be returned.
System info
- Python 3.10.12
- torch 2.0.1
import torchrl, numpy, sys
print(torchrl.__version__, numpy.__version__, sys.version, sys.platform)
0.1.1 1.25.1 3.10.12 (main, Jul 5 2023, 18:54:27) [GCC 11.2.0] linux
Reason and Possible fixes
In the TruncatedStandardNormal class, the self._entropy attribute is a constant tensor computed at initialization. For some reason, calling TruncatedStandardNormal.entropy returns the self._entropy attribute, rather than the entropy() property:
import torch
from torchrl.modules.distributions.truncated_normal import TruncatedStandardNormal
loc, scale = torch.zeros(1), torch.ones(1)
print(TruncatedStandardNormal(loc, scale).entropy)
print(TruncatedStandardNormal(loc, scale).entropy())
tensor([-0.0104])
Traceback (most recent call last):
File "/home/diego/Desktop/test.py", line 5, in <module>
print(TruncatedStandardNormal(loc, scale).entropy())
TypeError: 'Tensor' object is not callable
Checklist
- [x] I have checked that there is no similar issue in the repo (required)
- [x] I have read the documentation (required)
- [x] I have provided a minimal working example to reproduce the bug (required)
You're absolutely right, entropy should be a callable and not a property. Let me fix that