-
Notifications
You must be signed in to change notification settings - Fork 87
/
train.py
105 lines (82 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
import argparse
import os
import numpy as np
import torch as t
from torch.optim import Adam
from utils.batch_loader import BatchLoader
from utils.parameters import Parameters
from model.rvae import RVAE
if __name__ == "__main__":
if not os.path.exists('data/word_embeddings.npy'):
raise FileNotFoundError("word embeddings file was't found")
parser = argparse.ArgumentParser(description='RVAE')
parser.add_argument('--num-iterations', type=int, default=120000, metavar='NI',
help='num iterations (default: 120000)')
parser.add_argument('--batch-size', type=int, default=32, metavar='BS',
help='batch size (default: 32)')
parser.add_argument('--use-cuda', type=bool, default=True, metavar='CUDA',
help='use cuda (default: True)')
parser.add_argument('--learning-rate', type=float, default=0.00005, metavar='LR',
help='learning rate (default: 0.00005)')
parser.add_argument('--dropout', type=float, default=0.3, metavar='DR',
help='dropout (default: 0.3)')
parser.add_argument('--use-trained', type=bool, default=False, metavar='UT',
help='load pretrained model (default: False)')
parser.add_argument('--ce-result', default='', metavar='CE',
help='ce result path (default: '')')
parser.add_argument('--kld-result', default='', metavar='KLD',
help='ce result path (default: '')')
args = parser.parse_args()
batch_loader = BatchLoader('')
parameters = Parameters(batch_loader.max_word_len,
batch_loader.max_seq_len,
batch_loader.words_vocab_size,
batch_loader.chars_vocab_size)
rvae = RVAE(parameters)
if args.use_trained:
rvae.load_state_dict(t.load('trained_RVAE'))
if args.use_cuda:
rvae = rvae.cuda()
optimizer = Adam(rvae.learnable_parameters(), args.learning_rate)
train_step = rvae.trainer(optimizer, batch_loader)
validate = rvae.validater(batch_loader)
ce_result = []
kld_result = []
for iteration in range(args.num_iterations):
cross_entropy, kld, coef = train_step(iteration, args.batch_size, args.use_cuda, args.dropout)
if iteration % 5 == 0:
print('\n')
print('------------TRAIN-------------')
print('----------ITERATION-----------')
print(iteration)
print('--------CROSS-ENTROPY---------')
print(cross_entropy.data.cpu().numpy()[0])
print('-------------KLD--------------')
print(kld.data.cpu().numpy()[0])
print('-----------KLD-coef-----------')
print(coef)
print('------------------------------')
if iteration % 10 == 0:
cross_entropy, kld = validate(args.batch_size, args.use_cuda)
cross_entropy = cross_entropy.data.cpu().numpy()[0]
kld = kld.data.cpu().numpy()[0]
print('\n')
print('------------VALID-------------')
print('--------CROSS-ENTROPY---------')
print(cross_entropy)
print('-------------KLD--------------')
print(kld)
print('------------------------------')
ce_result += [cross_entropy]
kld_result += [kld]
if iteration % 20 == 0:
seed = np.random.normal(size=[1, parameters.latent_variable_size])
sample = rvae.sample(batch_loader, 50, seed, args.use_cuda)
print('\n')
print('------------SAMPLE------------')
print('------------------------------')
print(sample)
print('------------------------------')
t.save(rvae.state_dict(), 'trained_RVAE')
np.save('ce_result_{}.npy'.format(args.ce_result), np.array(ce_result))
np.save('kld_result_npy_{}'.format(args.kld_result), np.array(kld_result))