Created
November 14, 2023 18:32
-
-
Save KeAWang/e3d1cbb57c3dfe53618ee4f0ea7ed685 to your computer and use it in GitHub Desktop.
Constrain and unconstrain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
def constrain(x, min, max, temperature:float=1.): | |
return (max - min) * torch.sigmoid(x / temperature) + min | |
def unconstrain(y, min, max, temperature:float=1, EPS:float=1e-8): | |
assert torch.all(y >= min) and torch.all(y <= max) | |
# ensure both numerator and denominator are positive | |
numerator = y - min | |
denominator = max - min | |
return temperature * torch.logit(numerator / denominator, eps=EPS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment