-
Notifications
You must be signed in to change notification settings - Fork 36
/
train.py
executable file
·137 lines (109 loc) · 3.97 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
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from simple_ntc.trainer import Trainer
from simple_ntc.data_loader import DataLoader
from simple_ntc.models.rnn import RNNClassifier
from simple_ntc.models.cnn import CNNClassifier
def define_argparser():
'''
Define argument parser to set hyper-parameters.
'''
p = argparse.ArgumentParser()
p.add_argument('--model_fn', required=True)
p.add_argument('--train_fn', required=True)
p.add_argument('--gpu_id', type=int, default=-1)
p.add_argument('--verbose', type=int, default=2)
p.add_argument('--min_vocab_freq', type=int, default=5)
p.add_argument('--max_vocab_size', type=int, default=999999)
p.add_argument('--batch_size', type=int, default=256)
p.add_argument('--n_epochs', type=int, default=10)
p.add_argument('--word_vec_size', type=int, default= 256)
p.add_argument('--dropout', type=float, default=.3)
p.add_argument('--max_length', type=int, default=256)
p.add_argument('--rnn', action='store_true')
p.add_argument('--hidden_size', type=int, default=512)
p.add_argument('--n_layers', type=int, default=4)
p.add_argument('--cnn', action='store_true')
p.add_argument('--use_batch_norm', action='store_true')
p.add_argument('--window_sizes', type=int, nargs='*', default=[3, 4, 5])
p.add_argument('--n_filters', type=int, nargs='*', default=[100, 100, 100])
config = p.parse_args()
return config
def main(config):
loaders = DataLoader(
train_fn=config.train_fn,
batch_size=config.batch_size,
min_freq=config.min_vocab_freq,
max_vocab=config.max_vocab_size,
device=config.gpu_id
)
print(
'|train| =', len(loaders.train_loader.dataset),
'|valid| =', len(loaders.valid_loader.dataset),
)
vocab_size = len(loaders.text.vocab)
n_classes = len(loaders.label.vocab)
print('|vocab| =', vocab_size, '|classes| =', n_classes)
if config.rnn is False and config.cnn is False:
raise Exception('You need to specify an architecture to train. (--rnn or --cnn)')
if config.rnn:
# Declare model and loss.
model = RNNClassifier(
input_size=vocab_size,
word_vec_size=config.word_vec_size,
hidden_size=config.hidden_size,
n_classes=n_classes,
n_layers=config.n_layers,
dropout_p=config.dropout,
)
optimizer = optim.Adam(model.parameters())
crit = nn.NLLLoss()
print(model)
if config.gpu_id >= 0:
model.cuda(config.gpu_id)
crit.cuda(config.gpu_id)
rnn_trainer = Trainer(config)
rnn_model = rnn_trainer.train(
model,
crit,
optimizer,
loaders.train_loader,
loaders.valid_loader
)
if config.cnn:
# Declare model and loss.
model = CNNClassifier(
input_size=vocab_size,
word_vec_size=config.word_vec_size,
n_classes=n_classes,
use_batch_norm=config.use_batch_norm,
dropout_p=config.dropout,
window_sizes=config.window_sizes,
n_filters=config.n_filters,
)
optimizer = optim.Adam(model.parameters())
crit = nn.NLLLoss()
print(model)
if config.gpu_id >= 0:
model.cuda(config.gpu_id)
crit.cuda(config.gpu_id)
cnn_trainer = Trainer(config)
cnn_model = cnn_trainer.train(
model,
crit,
optimizer,
loaders.train_loader,
loaders.valid_loader
)
torch.save({
'rnn': rnn_model.state_dict() if config.rnn else None,
'cnn': cnn_model.state_dict() if config.cnn else None,
'config': config,
'vocab': loaders.text.vocab,
'classes': loaders.label.vocab,
}, config.model_fn)
if __name__ == '__main__':
config = define_argparser()
main(config)