Download Amortized_simulation/main_mixtureGaussian2.py from tonyshelby/NeuralSW: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/tonyshelby/NeuralSW/resolve/main/Amortized_simulation/main_mixtureGaussian2.py
- Command line
-
hf download hf://tonyshelby/NeuralSW/Amortized_simulation/main_mixtureGaussian2.py
-
curl -L -o main_mixtureGaussian2.py https://huggingface.co/tonyshelby/NeuralSW/resolve/main/Amortized_simulation/main_mixtureGaussian2.py
1.49 kB
| import math | |
| 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 | |
| n_components = 8 | |
| radius = 5.0 | |
| std = 0.3 | |
| embedding_dim = 256 | |
| num_cosines = 256 | |
| angles = torch.arange(n_components, dtype=torch.float32) * (2 * math.pi / n_components) | |
| means = (radius * torch.stack([torch.cos(angles), torch.sin(angles)], dim=1)).to(device) # 8 components on a ring | |
| covs = (std ** 2) * torch.eye(2, device=device).unsqueeze(0).repeat(n_components, 1, 1) # isotropic per component | |
| weights = torch.full((n_components,), 1.0 / n_components) # equal weights | |
| 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, | |
| n_samples=64, | |
| L=100, | |
| epochs=5000, | |
| lr=1e-4, | |
| embedding_dim=embedding_dim, | |
| num_cosines=num_cosines, | |
| lambda_reg=1e-4, | |
| ) | |
| 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", | |
| seed=123, # fixed test directions/data so this is comparable across runs and architectures | |
| ) | |