Skip to content

Commit 238f05a

Browse files
SebastianAmentfacebook-github-bot
authored andcommitted
Use expm1 for log-normal transforms (#2540)
Summary: Pull Request resolved: #2540 Replacing `exp(x) - 1` with the numerically more stable `expm1`. Reviewed By: Balandat Differential Revision: D62789111 fbshipit-source-id: 5d438ca572daebc14cf201a8ee8024ab3b74a207
1 parent 200cb2a commit 238f05a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

botorch/models/transforms/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def norm_to_lognorm(mu: Tensor, Cov: Tensor) -> tuple[Tensor, Tensor]:
6060
diag = torch.diagonal(Cov, dim1=-1, dim2=-2)
6161
b = mu + 0.5 * diag
6262
mu_ln = torch.exp(b)
63-
Cov_ln = (torch.exp(Cov) - 1) * torch.exp(b.unsqueeze(-1) + b.unsqueeze(-2))
63+
Cov_ln = torch.special.expm1(Cov) * torch.exp(b.unsqueeze(-1) + b.unsqueeze(-2))
6464
return mu_ln, Cov_ln
6565

6666

@@ -88,7 +88,7 @@ def norm_to_lognorm_variance(mu: Tensor, var: Tensor) -> Tensor:
8888
The `batch_shape x n` variance vector of the log-Normal distribution.
8989
"""
9090
b = mu + 0.5 * var
91-
return (torch.exp(var) - 1) * torch.exp(2 * b)
91+
return torch.special.expm1(var) * torch.exp(2 * b)
9292

9393

9494
def expand_and_copy_tensor(X: Tensor, batch_shape: torch.Size) -> Tensor:

0 commit comments

Comments
 (0)