-
Notifications
You must be signed in to change notification settings - Fork 10
/
pretrain.py
156 lines (135 loc) · 5.7 KB
/
pretrain.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
import torch
import torch.nn as nn
from torch.nn.utils import clip_grad_norm_
from data_utils import DataLoader
import losses
import constants
import time
import os
import h5py
def init_parameters(model):
for p in model.parameters():
p.data.uniform_(-0.1, 0.1)
def save_pretrain_checkpoint(state, args):
torch.save(state, args.pretrain_checkpoint)
def validate(valData, model, lossF, args, cuda0, cuda1, loss_cuda):
"""
ValData (DataLoader)
"""
autoencoder, rclayer = model
# switch to evaluation mode
autoencoder.eval()
rclayer.eval()
num_iteration = valData.size // args.batch
if valData.size % args.batch > 0:
num_iteration += 1
total_genloss = 0
for iteration in range(num_iteration):
gendata = valData.getbatch_generative()
with torch.no_grad():
loss, _ = losses.reconstructionLoss(
gendata, autoencoder, rclayer, lossF, args, cuda0, cuda1, loss_cuda)
# gendata.trg.size(1) is the batch size
total_genloss += loss.item() * gendata.trg.size(1)
# switch back to training mode
autoencoder.train()
rclayer.train()
return total_genloss / valData.size
def pretrain_ae(model, args, cuda0, cuda1, loss_cuda):
'''
Pretrain autoencoder
cuda0 for autoencoder
cuda1 for relayer
loss_cuda for reconstruction loss
'''
# define criterion, model, optimizer
autoencoder, rclayer = model
ae_optimizer = torch.optim.Adam(
autoencoder.parameters(), lr=args.learning_rate)
rc_optimizer = torch.optim.Adam(
rclayer.parameters(), lr=args.learning_rate)
V, D = losses.load_dis_matrix(args)
V, D = V.to(loss_cuda), D.to(loss_cuda)
def rclossF(o, t):
return losses.KLDIVloss(o, t, V, D, loss_cuda)
start_iteration = 0
is_pretrain = False
# load model state and optmizer state
if os.path.isfile(args.pretrain_checkpoint):
print("=> loading pretrain checkpoint '{}'".format(
args.pretrain_checkpoint))
pretrain_checkpoint = torch.load(args.pretrain_checkpoint)
start_iteration = pretrain_checkpoint["iteration"] + 1
is_pretrain = pretrain_checkpoint["pretrain"]
autoencoder.load_state_dict(pretrain_checkpoint["autoencoder"])
rclayer.load_state_dict(pretrain_checkpoint["rclayer"])
ae_optimizer.load_state_dict(pretrain_checkpoint["ae_optimizer"])
rc_optimizer.load_state_dict(pretrain_checkpoint["rc_optimizer"])
else:
print("=> No checkpoint found at '{}'".format(
args.pretrain_checkpoint))
if is_pretrain:
print("-"*7 + " Loaded pretrain model" + "-"*7)
return
# load data for pretrain
print("=> Reading training data...")
trainsrc = os.path.join(args.data, "train.src")
traintrg = os.path.join(args.data, "train.trg")
trainmta = os.path.join(args.data, "train.mta")
trainData = DataLoader(trainsrc, traintrg, trainmta,
args.batch)
trainData.load()
print("Loaded data,training data size ", trainData.size)
valsrc = os.path.join(args.data, "val.src")
valtrg = os.path.join(args.data, "val.trg")
valmta = os.path.join(args.data, "val.mta")
validation = True
if os.path.isfile(valsrc) and os.path.isfile(valtrg):
valData = DataLoader(valsrc, valtrg, valmta,
args.batch, True)
valData.load()
assert valData.size > 0, "Validation data size must be greater than 0"
print("=> Loaded validation data size {}".format(valData.size))
else:
print("No validation data found, training without validating...")
validation = False
num_iteration = int(trainData.size / args.batch * args.pretrain_epoch)
best_prec_loss = float('inf')
print("=> Iteration starts at {} "
"and will end at {}".format(start_iteration, num_iteration-1))
# training
for iteration in range(start_iteration, num_iteration):
ae_optimizer.zero_grad()
rc_optimizer.zero_grad()
# reconstruction loss
gendata = trainData.getbatch_generative()
loss, _ = losses.reconstructionLoss(
gendata, autoencoder, rclayer, rclossF, args, cuda0, cuda1, loss_cuda)
# compute the gradients
loss.backward()
# clip the gradients
clip_grad_norm_(autoencoder.parameters(), args.max_grad_norm)
clip_grad_norm_(rclayer.parameters(),
args.max_grad_norm)
# one step optimization
ae_optimizer.step()
rc_optimizer.step()
if iteration % args.print_freq == 0:
print("Iteration: {0:}\tReconstruction genLoss: {1:.3f}\t".format(
iteration, loss))
if (iteration % args.save_freq == 0 or iteration == num_iteration - 1) and validation:
prec_loss = validate(
valData, (autoencoder, rclayer), rclossF, args, cuda0, cuda1, loss_cuda)
if prec_loss < best_prec_loss:
best_prec_loss = prec_loss
print("Saving the model at iteration {} validation loss {}"
.format(iteration, prec_loss))
save_pretrain_checkpoint({
"iteration": iteration,
"autoencoder": autoencoder.state_dict(),
"rclayer": rclayer.state_dict(),
"ae_optimizer": ae_optimizer.state_dict(),
"rc_optimizer": rc_optimizer.state_dict(),
"pretrain": False
}, args)
print("-"*7 + " Pretrain model finished " + "-"*7)