-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
138 lines (92 loc) · 3.94 KB
/
train.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
from datetime import datetime
from typing import Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchtext.data.iterator import Iterator
from torchtext.vocab import Vocab
from utils import to_onehot, seq_to_str
from bleu import moses_multi_bleu
def _train(epoch: int, enc: nn.Module, dec: nn.Module, disc: nn.Module, prior_size: int,
dl: Iterator, vocab: Vocab, device: str, validate: bool = False) -> Tuple[float, float, float, float]:
if not validate:
enc.train()
dec.train()
disc.train()
else:
enc.eval()
dec.eval()
disc.eval()
epoch_g_loss = 0.0
epoch_ae_loss = 0.0
epoch_disc_loss = 0.0
strs = []
dec_strs = []
n_batches = len(dl)
for batch_idx, batch in enumerate(dl):
seq = batch.text
seq = seq[1:]
label = batch.label
label = to_onehot(label, 2, device)
(seq_len, batch_size) = seq.shape
batch_zeros = torch.zeros((batch_size, 1)).to(device)
batch_ones = torch.ones((batch_size, 1)).to(device)
# ======== train/validate Discriminator ========
if not validate:
enc.zero_grad()
disc.zero_grad()
z = torch.randn((batch_size, prior_size)).to(device)
z_label = to_onehot(torch.randint(0, 2, (batch_size, )).long(), 2, device)
latent = enc(seq)
fake_pred = disc(latent, label)
true_pred = disc(z, z_label)
fake_loss = F.binary_cross_entropy_with_logits(fake_pred, batch_zeros)
true_loss = F.binary_cross_entropy_with_logits(true_pred, batch_ones)
disc_loss = 0.5 * (fake_loss + true_loss)
if not validate:
disc_loss.backward()
disc.optim.step()
# ======== train/validate Autoencoder ========
if not validate:
enc.zero_grad()
dec.zero_grad()
disc.zero_grad()
latent = enc(seq)
x = torch.zeros(1, batch_size).to(device).long() + vocab.stoi['<sos>']
h = None
output = None
for i in range(seq_len):
o, h = dec(x, latent, h, label)
x = seq[i].view(1, -1)
output = o if output is None else torch.cat((output, o), 0)
ae_loss = F.nll_loss(output, seq.view(-1))
fake_pred_z = disc(latent, label)
enc_loss = F.binary_cross_entropy_with_logits(fake_pred_z, batch_ones)
g_loss = ae_loss + enc_loss
if not validate:
g_loss.backward()
dec.optim.step()
enc.optim.step()
# ----------------------------------------------------
epoch_g_loss += g_loss.item()
epoch_ae_loss += ae_loss.item()
epoch_disc_loss += disc_loss.item()
_, w_idxs = output.topk(1, dim=1)
dec_seq = w_idxs.view(seq_len, batch_size)
strs.extend(seq_to_str(seq.detach(), vocab))
dec_strs.extend(seq_to_str(dec_seq.detach(), vocab))
epoch_g_loss /= n_batches
epoch_ae_loss /= n_batches
epoch_disc_loss /= n_batches
bleu = moses_multi_bleu(np.array(dec_strs), np.array(strs))
mode = 'Valid' if validate else 'Train'
print("Epoch {:3} {:5}: BLEU: {:.2f}, AE: {:.5f}, G: {:.5f}, D: {:.5f} at {}".format(
epoch, mode, bleu, epoch_ae_loss, epoch_g_loss, epoch_disc_loss, datetime.now().strftime("%H:%M:%S")))
return epoch_ae_loss, epoch_g_loss, epoch_disc_loss, bleu
def train(epoch: int, enc: nn.Module, dec: nn.Module, disc: nn.Module, prior_size: int,
dl: Iterator, vocab: Vocab, device: str) -> Tuple[float, float, float, float]:
return _train(epoch, enc, dec, disc, prior_size, dl, vocab, device, validate=False)
def validate(epoch: int, enc: nn.Module, dec: nn.Module, disc: nn.Module, prior_size: int,
dl: Iterator, vocab: Vocab, device: str) -> Tuple[float, float, float, float]:
return _train(epoch, enc, dec, disc, prior_size, dl, vocab, device, validate=True)