File size: 1,485 Bytes
b389bd5
c0f6452
b389bd5
 
 
 
 
 
 
c0f6452
b389bd5
c0f6452
b389bd5
c0f6452
 
b389bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
)