Download Amortized_simulation/main_mixtureGaussian.py from tonyshelby/NeuralSW: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/tonyshelby/NeuralSW/resolve/main/Amortized_simulation/main_mixtureGaussian.py
- Command line
-
hf download hf://tonyshelby/NeuralSW/Amortized_simulation/main_mixtureGaussian.py
-
curl -L -o main_mixtureGaussian.py https://huggingface.co/tonyshelby/NeuralSW/resolve/main/Amortized_simulation/main_mixtureGaussian.py
1.12 kB
| import torch | |
| from sw_common import ( | |
| set_seed, | |
| train_amortized_phi, | |
| evaluate_and_plot, | |
| sample_mixture_gaussian, | |
| mixture_gaussian_true_quantiles, | |
| ) | |
| set_seed(42) | |
| device = 'cuda' | |
| n_dim = 2 | |
| means = torch.tensor([[1.0, 2.0], [4.0, 5.0], [7.0, 8.0]], device=device) # 3 components' means | |
| covs = torch.tensor([[[1.0, 0.5], [0.5, 2.0]], # Covariance for component 1 | |
| [[0.8, 0.2], [0.2, 1.0]], # Covariance for component 2 | |
| [[1.5, 0.4], [0.4, 1.2]]], device=device) # Covariance for component 3 | |
| weights = torch.tensor([0.4, 0.3, 0.3]) # Weights for the 3 components | |
| def sample_fn(n_samples): | |
| return sample_mixture_gaussian(n_samples, means, covs, weights, n_dim, device) | |
| def true_quantile_fn(test_thetas, taus_test): | |
| return mixture_gaussian_true_quantiles(test_thetas, taus_test, means, covs, weights) | |
| model = train_amortized_phi(sample_fn, d=n_dim, device=device) | |
| evaluate_and_plot( | |
| model, d=n_dim, device=device, | |
| true_quantile_fn=true_quantile_fn, | |
| sample_fn=sample_fn, | |
| title="Amortized Quantile Function — Gaussian Mixture", | |
| ) | |