-
Notifications
You must be signed in to change notification settings - Fork 328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Incorrect Calculation of Mode for TanhNormal Distribution #2186
Comments
Hello Basically, before it was fast, now it's accurate |
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(100000)
plt.hist(sample.numpy(), bins=64, range=(-1, 1))
plt.show() Now results in |
Hello Thank you for your prompt response and for fixing the implementation error in the mode of TanhNormal distribution calculations. I really appreciate it. |
Given the time it takes to compute the mode now, wouldn't it make sense to make it a method (not a property) and raise a deprecation warning in the current property? |
I updated the PR to rely on Adam which is faster and more accurate than LBFGS, SGD and Newton Raphson (crazy right?) |
Does this mean we should implement the calculation of the correct mode as a separate method and revert the mode property to its original implementation? Now that you mentioned it, that seem more practical for me.Sorry for my confusing issue. |
yes if you look at the current status of the PR this is how I went about it |
Fixed by #2198 |
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.
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.
Thank you again for your continuous support and hard work on maintaining the torchrl library.
The text was updated successfully, but these errors were encountered: