-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
200 lines (162 loc) · 7.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from noise import noisy
def reparameterize(mu, logvar):
std = torch.exp(0.5*logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
def log_prob(z, mu, logvar):
var = torch.exp(logvar)
logp = - (z-mu)**2 / (2*var) - torch.log(2*np.pi*var) / 2
return logp.sum(dim=1)
def loss_kl(mu, logvar):
return -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp()) / len(mu)
class Classifier(nn.Module):
def __init__(self, args):
super().__init__()
self.l1 = torch.nn.Linear(args.dim_z, 64)
self.l2 = torch.nn.Linear(64, 32)
self.l3 = torch.nn.Linear(32, 16)
self.l4 = torch.nn.Linear(16, 4)
self.l5 = torch.nn.Linear(4, 1)
def forward(self, x):
z1 = torch.relu(self.l1(x))
z2 = torch.relu(self.l2(z1))
z3 = torch.relu(self.l3(z2))
z4 = torch.relu(self.l4(z3))
z5 = torch.sigmoid(self.l5(z4))
return z5
class TextModel(nn.Module):
"""Container module with word embedding and projection layers"""
def __init__(self, vocab, args, initrange=0.1):
super().__init__()
self.vocab = vocab
self.args = args
self.embed = nn.Embedding(vocab.size, args.dim_emb)
self.proj = nn.Linear(args.dim_h, vocab.size)
self.embed.weight.data.uniform_(-initrange, initrange)
self.proj.bias.data.zero_()
self.proj.weight.data.uniform_(-initrange, initrange)
class DAE(TextModel):
"""Denoising Auto-Encoder"""
def __init__(self, vocab, args):
super().__init__(vocab, args)
self.drop = nn.Dropout(args.dropout)
self.E = nn.LSTM(args.dim_emb, args.dim_h, args.nlayers,
dropout=args.dropout if args.nlayers > 1 else 0, bidirectional=True)
self.G = nn.LSTM(args.dim_emb, args.dim_h, args.nlayers,
dropout=args.dropout if args.nlayers > 1 else 0)
self.h2mu = nn.Linear(args.dim_h*2, args.dim_z)
self.h2logvar = nn.Linear(args.dim_h*2, args.dim_z)
self.z2emb = nn.Linear(args.dim_z, args.dim_emb)
self.opt = optim.Adam(self.parameters(), lr=args.lr, betas=(0.5, 0.999))
def flatten(self):
self.E.flatten_parameters()
self.G.flatten_parameters()
def encode(self, input):
input = self.drop(self.embed(input))
_, (h, _) = self.E(input)
h = torch.cat([h[-2], h[-1]], 1)
return self.h2mu(h), self.h2logvar(h)
def decode(self, z, input, hidden=None):
input = self.drop(self.embed(input)) + self.z2emb(z)
output, hidden = self.G(input, hidden)
output = self.drop(output)
logits = self.proj(output.view(-1, output.size(-1)))
return logits.view(output.size(0), output.size(1), -1), hidden
def generate(self, z, max_len, alg):
assert alg in ['greedy' , 'sample' , 'top5']
sents = []
input = torch.zeros(1, len(z), dtype=torch.long, device=z.device).fill_(self.vocab.go)
hidden = None
for l in range(max_len):
sents.append(input)
logits, hidden = self.decode(z, input, hidden)
if alg == 'greedy':
input = logits.argmax(dim=-1)
elif alg == 'sample':
input = torch.multinomial(logits.squeeze(dim=0).exp(), num_samples=1).t()
elif alg == 'top5':
not_top5_indices=logits.topk(logits.shape[-1]-5,dim=2,largest=False).indices
logits_exp=logits.exp()
logits_exp[:,:,not_top5_indices]=0.
input = torch.multinomial(logits_exp.squeeze(dim=0), num_samples=1).t()
return torch.cat(sents)
def forward(self, input, is_train=False):
_input = noisy(self.vocab, input, *self.args.noise) if is_train else input
mu, logvar = self.encode(_input)
z = reparameterize(mu, logvar)
logits, _ = self.decode(z, input)
return mu, logvar, z, logits
def loss_rec(self, logits, targets):
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1),
ignore_index=self.vocab.pad, reduction='none').view(targets.size())
return loss.sum(dim=0)
def loss(self, losses):
return losses['rec']
def autoenc(self, inputs, targets, is_train=False):
_, _, _, logits = self(inputs, is_train)
return {'rec': self.loss_rec(logits, targets).mean()}
def step(self, losses):
self.opt.zero_grad()
losses['loss'].backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
#nn.utils.clip_grad_norm_(self.parameters(), clip)
self.opt.step()
def nll_is(self, inputs, targets, m):
"""compute negative log-likelihood by importance sampling:
p(x;theta) = E_{q(z|x;phi)}[p(z)p(x|z;theta)/q(z|x;phi)]
"""
mu, logvar = self.encode(inputs)
tmp = []
for _ in range(m):
z = reparameterize(mu, logvar)
logits, _ = self.decode(z, inputs)
v = log_prob(z, torch.zeros_like(z), torch.zeros_like(z)) - \
self.loss_rec(logits, targets) - log_prob(z, mu, logvar)
tmp.append(v.unsqueeze(-1))
ll_is = torch.logsumexp(torch.cat(tmp, 1), 1) - np.log(m)
return -ll_is
class VAE(DAE):
"""Variational Auto-Encoder"""
def __init__(self, vocab, args):
super().__init__(vocab, args)
def loss(self, losses):
return losses['rec'] + self.args.lambda_kl * losses['kl']
def autoenc(self, inputs, targets, is_train=False):
mu, logvar, _, logits = self(inputs, is_train)
return {'rec': self.loss_rec(logits, targets).mean(),
'kl': loss_kl(mu, logvar)}
class AAE(DAE):
"""Adversarial Auto-Encoder"""
def __init__(self, vocab, args):
super().__init__(vocab, args)
self.D = nn.Sequential(nn.Linear(args.dim_z, args.dim_d), nn.ReLU(),
nn.Linear(args.dim_d, 1), nn.Sigmoid())
self.optD = optim.Adam(self.D.parameters(), lr=args.lr, betas=(0.5, 0.999))
def loss_adv(self, z):
zn = torch.randn_like(z)
zeros = torch.zeros(len(z), 1, device=z.device)
ones = torch.ones(len(z), 1, device=z.device)
loss_d = F.binary_cross_entropy(self.D(z.detach()), zeros) + \
F.binary_cross_entropy(self.D(zn), ones)
loss_g = F.binary_cross_entropy(self.D(z), ones)
return loss_d, loss_g
def loss(self, losses):
return losses['rec'] + self.args.lambda_adv * losses['adv'] + \
self.args.lambda_p * losses['|lvar|']
def autoenc(self, inputs, targets, is_train=False):
_, logvar, z, logits = self(inputs, is_train)
loss_d, adv = self.loss_adv(z)
return {'rec': self.loss_rec(logits, targets).mean(),
'adv': adv,
'|lvar|': logvar.abs().sum(dim=1).mean(),
'loss_d': loss_d}
def step(self, losses):
super().step(losses)
self.optD.zero_grad()
losses['loss_d'].backward()
self.optD.step()