Description
Describe the bug
I'd like to express my gratitude for the swift and diligent maintenance of the torchrl library.
I have identified a potential issue with the implementation of the mode method in the TanhNormal distribution within TorchRL.
The calculation of the mode appears to be incorrect due to the nature of the tanh function applied to a normal distribution.
- current implementation
I think the mode of the TanhNormal distribution should accurately reflect the peak of the probability density function after applying the tanh transformation to the underlying normal distribution. Given the non-linearity of the tanh function, the mode calculation should account for this complexity.
To Reproduce
The current implementation of the mode method does not correctly compute the mode, resulting in inaccurate values.
For example, in the following scenario, the mode is expected to be around 1, but the method returns approximately 0.197.
import torch
from torchrl.modules import TanhNormal
import matplotlib.pyplot as plt
torch.random.manual_seed(0)
loc = torch.tensor([0.2], dtype=torch.float32)
scale = torch.tensor([1.0], dtype=torch.float32)
dist = TanhNormal(loc, scale, min=-1, max=1)
print("mode: ", dist.mode.item()) # mode: 0.1973753273487091
sample = dist.sample_n(10000)
plt.hist(sample.numpy(), bins=500, range=(-1, 1))
plt.show()
Thank you again for your continuous support and hard work on maintaining the torchrl library.