-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
147 lines (125 loc) · 5.37 KB
/
models.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# code adapted from : https://github.com/tonyduan/mdn
# MIT license
# please cite accordingly
import torch
import torch.nn as nn
from torch.distributions import Normal, OneHotCategorical
#---------------------------------------------------------------------------------------
class MixtureDensityNetwork(nn.Module):
"""
Mixture density network.
[ Bishop, 1994 ]
Parameters
----------
dim_in: int; dimensionality of the covariates
dim_out: int; dimensionality of the response variable
n_components: int; number of components in the mixture model
"""
def __init__(self, dim_in, dim_out, n_components):
super().__init__()
self.pi_network = CategoricalNetwork(dim_in, n_components)
self.normal_network = MixtureDiagNormalNetwork(dim_in, dim_out,
n_components)
def forward(self, x):
return self.pi_network(x), self.normal_network(x)
def loss(self, x, y):
pi, normal = self.forward(x)
loglik = normal.log_prob(y.unsqueeze(1).expand_as(normal.loc))
loglik = torch.sum(loglik, dim=2)
loss = -torch.logsumexp(torch.log(pi.probs) + loglik, dim=1)
return loss
def sample(self, x):
pi, normal = self.forward(x)
samples = torch.sum(pi.sample().unsqueeze(2) * normal.sample(), dim=1)
return samples
#---------------------------------------------------------------------------------------
class MixtureDiagNormalNetwork(nn.Module):
def __init__(self, in_dim, out_dim, n_components, num_channel=16, hidden_dim=50,\
mask_size=11):
super().__init__()
self.n_components = n_components
self.deconv1 = torch.nn.Conv1d(1, num_channel, mask_size, stride=2)
self.deconv2 = torch.nn.Conv1d(num_channel, num_channel, mask_size, stride=2)
self.deconv3 = torch.nn.Conv1d(num_channel, num_channel, mask_size, stride=2)
self.deconv4 = torch.nn.Conv1d(num_channel, 1, mask_size, stride=2)
self.batch_norm1 = torch.nn.Sequential(
torch.nn.BatchNorm1d(num_channel),
torch.nn.LeakyReLU()
)
self.batch_norm2 = torch.nn.Sequential(
torch.nn.BatchNorm1d(num_channel),
torch.nn.LeakyReLU()
)
self.batch_norm3 = torch.nn.Sequential(
torch.nn.BatchNorm1d(num_channel),
torch.nn.LeakyReLU()
)
self.batch_norm4 = torch.nn.Sequential(
torch.nn.BatchNorm1d(1),
torch.nn.LeakyReLU()
)
self.mlp = torch.nn.Sequential(
torch.nn.Linear(442, hidden_dim),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim, 2 * out_dim * n_components),
)
def forward(self, x):
x = x[:,None,:]
x = self.deconv1(x)
x = self.batch_norm1(x)
x = self.deconv2(x)
x = self.batch_norm2(x)
x = self.deconv3(x)
x = self.batch_norm3(x)
x = self.deconv4(x)
x = self.batch_norm4(x)[:,0,:]
params = self.mlp(x)
mean, sd = torch.split(params, params.shape[1] // 2, dim=1)
mean = torch.stack(mean.split(mean.shape[1] // self.n_components, 1))
sd = torch.stack(sd.split(sd.shape[1] // self.n_components, 1))
return Normal(mean.transpose(0, 1), torch.exp(sd).transpose(0, 1))
#---------------------------------------------------------------------------------------
class CategoricalNetwork(nn.Module):
def __init__(self, in_dim, out_dim, num_channel=16, hidden_dim=50, mask_size=11):
super().__init__()
self.deconv1 = torch.nn.Conv1d(1, num_channel, mask_size, stride=2)
self.deconv2 = torch.nn.Conv1d(num_channel, num_channel, mask_size, stride=2)
self.deconv3 = torch.nn.Conv1d(num_channel, num_channel, mask_size, stride=2)
self.deconv4 = torch.nn.Conv1d(num_channel, 1, mask_size, stride=2)
self.batch_norm1 = torch.nn.Sequential(
torch.nn.BatchNorm1d(num_channel),
torch.nn.LeakyReLU()
)
self.batch_norm2 = torch.nn.Sequential(
torch.nn.BatchNorm1d(num_channel),
torch.nn.LeakyReLU()
)
self.batch_norm3 = torch.nn.Sequential(
torch.nn.BatchNorm1d(num_channel),
torch.nn.LeakyReLU()
)
self.batch_norm4 = torch.nn.Sequential(
torch.nn.BatchNorm1d(1),
torch.nn.LeakyReLU()
)
self.mlp = torch.nn.Sequential(
torch.nn.Linear(442, hidden_dim),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim, out_dim),
)
def forward(self, x):
x = x[:,None,:]
x = self.deconv1(x)
x = self.batch_norm1(x)
x = self.deconv2(x)
x = self.batch_norm2(x)
x = self.deconv3(x)
x = self.batch_norm3(x)
x = self.deconv4(x)
x = self.batch_norm4(x)[:,0,:]
params = self.mlp(x)
return OneHotCategorical(logits=params)