Spaces:
Running on Zero
Running on Zero
Download diffcve/modules/distributions.py from hugging-apps/diffcve-2607: direct link, hf CLI and curl.
- Browser
- Download file 724 Bytes
-
https://huggingface.co/spaces/hugging-apps/diffcve-2607/resolve/main/diffcve/modules/distributions.py
- Command line
-
hf download hf://spaces/hugging-apps/diffcve-2607/diffcve/modules/distributions.py
-
curl -L -o distributions.py https://huggingface.co/spaces/hugging-apps/diffcve-2607/resolve/main/diffcve/modules/distributions.py
724 Bytes
| import torch | |
| class DiagonalGaussianDistribution(object): | |
| def __init__(self, parameters, deterministic=False): | |
| self.parameters = parameters | |
| self.mean, self.logvar = torch.chunk(parameters, 2, dim=1) | |
| self.logvar = torch.clamp(self.logvar, -30.0, 20.0) | |
| self.deterministic = deterministic | |
| self.std = torch.exp(0.5 * self.logvar) | |
| self.var = torch.exp(self.logvar) | |
| if self.deterministic: | |
| self.var = self.std = torch.zeros_like(self.mean).to(device=self.parameters.device) | |
| def sample(self): | |
| x = self.mean + self.std * torch.randn(self.mean.shape).to(device=self.parameters.device) | |
| return x | |
| def mode(self): | |
| return self.mean | |