File size: 998 Bytes
c0f6452 b389bd5 c0f6452 b389bd5 c0f6452 b389bd5 c0f6452 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 | 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",
)
|