-
Notifications
You must be signed in to change notification settings - Fork 27
/
train_ctc.py
151 lines (138 loc) · 6.15 KB
/
train_ctc.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
import os
import time
import random
import argparse
import logging
import numpy as np
import torch
from torch import nn, autograd
from torch.autograd import Variable
import torch.nn.functional as F
from warpctc_pytorch import CTCLoss
import kaldi_io
from model import RNNModel
import tensorboard_logger as tb
from DataLoader import SequentialLoader, TokenAcc
parser = argparse.ArgumentParser(description='PyTorch LSTM CTC Acoustic Model on TIMIT.')
parser.add_argument('--lr', type=float, default=1e-3,
help='initial learning rate')
parser.add_argument('--epochs', type=int, default=200,
help='upper epoch limit')
parser.add_argument('--batch_size', type=int, default=1, metavar='N',
help='batch size')
parser.add_argument('--dropout', type=float, default=0,
help='dropout applied to layers (0 = no dropout)')
parser.add_argument('--bi', default=False, action='store_true',
help='whether use bidirectional lstm')
parser.add_argument('--noise', default=False, action='store_true',
help='add Gaussian weigth noise')
parser.add_argument('--log-interval', type=int, default=50, metavar='N',
help='report interval')
parser.add_argument('--stdout', default=False, action='store_true', help='log in terminal')
parser.add_argument('--out', type=str, default='exp/ctc_lr1e-3',
help='path to save the final model')
parser.add_argument('--cuda', default=True, action='store_false')
parser.add_argument('--init', type=str, default='',
help='Initial am parameters')
parser.add_argument('--gradclip', default=False, action='store_true')
parser.add_argument('--schedule', default=False, action='store_true')
args = parser.parse_args()
os.makedirs(args.out, exist_ok=True)
with open(os.path.join(args.out, 'args'), 'w') as f:
f.write(str(args))
if args.stdout: logging.basicConfig(format='%(asctime)s: %(message)s', datefmt='%H:%M:%S', level=logging.INFO)
else: logging.basicConfig(format='%(asctime)s: %(message)s', datefmt='%H:%M:%S', filename=os.path.join(args.out, 'train.log'), level=logging.INFO)
tb.configure(args.out)
random.seed(1024)
torch.manual_seed(1024)
torch.cuda.manual_seed_all(1024)
model = RNNModel(123, 62, 250, 3, args.dropout, bidirectional=args.bi)
if args.init: model.load_state_dict(torch.load(args.init))
else:
for param in model.parameters(): torch.nn.init.uniform(param, -0.1, 0.1)
if args.cuda: model.cuda()
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=.9)
criterion = CTCLoss()
# data set
trainset = SequentialLoader('train', args.batch_size)
devset = SequentialLoader('dev', args.batch_size)
tri = cvi = 0
def eval():
global cvi
losses = []
tacc = TokenAcc()
for xs, ys, xlen, ylen in devset:
x = Variable(torch.FloatTensor(xs), volatile=True).cuda()
ys = np.hstack([ys[i, :j] for i, j in enumerate(ylen)])
y = Variable(torch.IntTensor(ys))
xl = Variable(torch.IntTensor(xlen)); yl = Variable(torch.IntTensor(ylen))
model.eval()
out = model(x)[0]
loss = criterion(out.transpose(0,1).contiguous(), y, xl, yl)
loss = float(loss.data) * len(xlen) # batch size
losses.append(loss)
tacc.update(out.data.cpu().numpy(), xlen, ys)
tb.log_value('cv_loss', loss/len(xlen), cvi)
cvi += 1
return sum(losses) / len(devset), tacc.getAll()
def train():
def adjust_learning_rate(optimizer, lr):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
# lr = args.lr * (0.1 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def add_noise(x):
dim = x.shape[-1]
noise = torch.normal(torch.zeros(dim), 0.075)
if x.is_cuda: noise = noise.cuda()
x.data += noise
global tri
prev_loss = 1000
best_model = None
lr = args.lr
for epoch in range(1, args.epochs):
totloss = 0; losses = []
start_time = time.time()
tacc = TokenAcc()
for i, (xs, ys, xlen, ylen) in enumerate(trainset):
x = Variable(torch.FloatTensor(xs))
if args.cuda: x = x.cuda()
if args.noise: add_noise(x)
ys = np.hstack([ys[i, :j] for i, j in enumerate(ylen)])
y = Variable(torch.IntTensor(ys))
xl = Variable(torch.IntTensor(xlen)); yl = Variable(torch.IntTensor(ylen))
model.train()
optimizer.zero_grad()
out = model(x)[0]
loss = criterion(out.transpose(0,1).contiguous(), y, xl, yl)
loss.backward()
loss = float(loss.data) * len(xlen) # batch size
totloss += loss; losses.append(loss)
tacc.update(out.data.cpu().numpy(), xlen, ys)
if args.gradclip: grad_norm = nn.utils.clip_grad_norm(model.parameters(), 200)
optimizer.step()
tb.log_value('train_loss', loss/len(xlen), tri)
if args.gradclip: tb.log_value('train_grad_norm', grad_norm, tri)
tri += 1
if i % args.log_interval == 0 and i > 0:
loss = totloss / args.batch_size / args.log_interval
logging.info('[Epoch %d Batch %d] loss %.2f, PER %.2f'%(epoch, i, loss, tacc.get()))
totloss = 0
losses = sum(losses) / len(trainset)
val_l, per = eval()
logging.info('[Epoch %d] time cost %.2fs, train loss %.2f, PER %.2f; cv loss %.2f, PER %.2f; lr %.3e'%(
epoch, time.time()-start_time, losses, tacc.getAll(), val_l, per, lr
))
if val_l < prev_loss:
prev_loss = val_l
best_model = '{}/params_epoch{:02d}_tr{:.2f}_cv{:.2f}'.format(args.out, epoch, losses, val_l)
torch.save(model.state_dict(), best_model)
else:
torch.save(model.state_dict(), '{}/params_epoch{:02d}_tr{:.2f}_cv{:.2f}_rejected'.format(args.out, epoch, losses, val_l))
model.load_state_dict(torch.load(best_model))
if args.cuda: model.cuda()
if args.schedule:
lr /= 2
adjust_learning_rate(optimizer, lr)
if __name__ == '__main__':
train()