Download Amortized_simulation/main_Gaussian.py from tonyshelby/NeuralSW: direct link, hf CLI and curl.
- Browser
- Download file 998 Bytes
-
https://huggingface.co/tonyshelby/NeuralSW/resolve/main/Amortized_simulation/main_Gaussian.py
- Command line
-
hf download hf://tonyshelby/NeuralSW/Amortized_simulation/main_Gaussian.py
-
curl -L -o main_Gaussian.py https://huggingface.co/tonyshelby/NeuralSW/resolve/main/Amortized_simulation/main_Gaussian.py
998 Bytes
| import numpy as np | |
| import torch | |
| from scipy.stats import norm | |
| from sw_common import set_seed, train_amortized_phi, evaluate_and_plot | |
| set_seed(42) | |
| device = 'cuda' | |
| mean = torch.tensor([1.0, 2.0], device=device) | |
| cov = torch.tensor([[1.0, 0.8], | |
| [0.8, 2.0]], device=device) | |
| dist = torch.distributions.MultivariateNormal(mean, covariance_matrix=cov) | |
| d = 2 | |
| def sample_fn(n_samples): | |
| return dist.sample((n_samples,)) | |
| def true_quantile_fn(test_thetas, taus_test): | |
| means = (test_thetas @ mean).cpu().numpy() | |
| variances = torch.einsum("li,ij,lj->l", test_thetas, cov, test_thetas).cpu().numpy() | |
| taus = taus_test.numpy().ravel() | |
| return np.stack([norm.ppf(taus, loc=means[l], scale=np.sqrt(variances[l])) for l in range(len(means))]) | |
| model = train_amortized_phi(sample_fn, d=d, device=device) | |
| evaluate_and_plot( | |
| model, d=d, device=device, | |
| true_quantile_fn=true_quantile_fn, | |
| sample_fn=sample_fn, | |
| title="Amortized Quantile Function — Gaussian", | |
| ) | |