-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
124 lines (100 loc) · 5.06 KB
/
model.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import torch
import torch.nn as nn
import torch.nn.functional as F
from latent_encoder import LatentEncoder
from deterministic_encoder import DeterministicEncoder
from decoder import Decoder
class LatentModel(nn.Module):
def __init__(self,
x_dim,
y_dim,
hidden_dim = 32,
latent_dim = 32,
latent_self_attn_type = 'dot',
det_self_attn_type = 'dot',
det_cross_attn_type = 'multihead',
n_multiheads = 8,
n_lat_enc_layer = 2,
n_det_enc_layer = 2,
n_decoder_layer = 2,
rep ='mlp',
use_deterministic_enc = False,
min_std = 0.01,
p_drop = 0,
isnorm = True,
p_attn_drop = 0,
attn_layers = 2,
use_self_attn = False,
context_in_target = True
):
super().__init__()
self.laten_encoder = LatentEncoder(x_dim+y_dim,
x_dim,
hidden_dim=hidden_dim,
latent_dim=latent_dim,
self_attn_type=latent_self_attn_type,
encoder_layer=n_lat_enc_layer,
n_multiheads = n_multiheads,
min_std=min_std,
isnorm = isnorm,
p_encoder=p_drop,
p_attn=p_attn_drop,
rep = 'identity',
use_self_attn=use_self_attn,
attn_layers=attn_layers
)
self.deterministic_encoder = DeterministicEncoder(x_dim+y_dim,
x_dim,
isnorm = isnorm,
hidden_dim=hidden_dim,
n_encoder_layer=n_det_enc_layer,
rep=rep,
self_attn_type=det_self_attn_type,
cross_attn_type=det_cross_attn_type,
p_encoder=p_drop,
p_attention=p_attn_drop,
attn_layers=attn_layers,
use_self_attn=use_self_attn,
n_multiheads = n_multiheads
)
self.decoder = Decoder(x_dim,
y_dim,
hidden_dim = hidden_dim,
latent_dim=latent_dim,
n_decoder_layer=n_decoder_layer,
use_deterministic_path=use_deterministic_enc,
min_std=min_std,
isnorm=isnorm,
dropout_p=p_drop
)
self.use_deterministic_enc = use_deterministic_enc
self.context_in_target = context_in_target
def forward(self, c_x, c_y, t_x, t_y = None, training = False):
dist_prior = self.laten_encoder(c_x, c_y)
if t_y is not None:
dist_posterior = self.laten_encoder(t_x, t_y)
z = dist_posterior.sample() #(B, ld)
else:
z = dist_prior.sample() #(B, ld)
n_target = t_x.shape[1]
z = z.unsqueeze(1).repeat(1, n_target,1) #(B, n_target, 1)
if self.use_deterministic_enc:
r = self.deterministic_encoder(c_x, c_y, t_x) #(B, n_target=m, H)
representation = torch.cat([r, z], axis = -1) #(B, ld+hd)
else:
representation = z
dist, μ, σ = self.decoder(representation, t_x)
#at test time, target y is not Known so we return None
if t_y is not None:
log_p = dist.log_prob(t_y) #(B, m, 1)
kl_loss = torch.distributions.kl_divergence(dist_posterior, dist_prior).sum(dim = -1, keepdim = True)
kl_loss = kl_loss.repeat([1, n_target])[:,:, None]
loss = -(torch.mean(log_p - kl_loss/n_target))
mse_loss = F.mse_loss(dist.loc, t_y, reduction = 'none')[:,:c_x.size(1)].mean()
else:
kl_loss = None
log_p = None
mse_loss = None
loss = None
y_pred = μ
return y_pred, dict(loss = loss, loss_p = log_p, loss_kl = kl_loss, mse_loss = mse_loss), dist