-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·76 lines (61 loc) · 2.8 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
import torch
import torch.optim as optim
import math
from data_loader import img2para_dataset
from utils import cal_loss
def forward(args, model, train):
if train:
model.train()
else:
model.eval()
dataset = img2para_dataset(args, train)
data_loader = torch.utils.data.DataLoader(
dataset=dataset,
num_workers=args.num_workers,
batch_size=args.batch_size,
shuffle=train,
)
# rnn_params = {'p':[], 's':[], 'w':[]}
# for name, param in model.named_parameters():
# if 'pRNN' in name:
# rnn_params['p'].append(param)
# elif 'sRNN' in name:
# rnn_params['s'].append(param)
# elif 'wRNN' in name:
# rnn_params['w'].append(param)
if args.optim == 'adam':
optimizer = optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
elif args.optim == 'sgd':
optimizer = optim.SGD(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
elif args.optim == 'rmsprop':
optimizer = optim.RMSprop(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
else:
optimizer = optim.Adagrad(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
total_cost = 0
total_word_cost = 0
total_sent_cost = 0
total_word_count = 0
# total_sent_count = 0
for batch_idx, batch_data in enumerate(data_loader):
real_batch_size = len(batch_data[0])
batch_data = [_.to(args.device) for _ in batch_data[1:]]
img_feats, densecap, para_words_labels, stop_labels, words_mask, densecap_mask, fake_words, fake_words_mask = batch_data
predict_words, predict_stop = model(img_feats, para_words_labels, words_mask, fake_words, fake_words_mask)
para_words_count = torch.sum(words_mask)
word_cost, sent_cost = cal_loss(para_words_labels, predict_words, words_mask, stop_labels, predict_stop)
# para_sents_count = torch.sum(stop_mask)
cost = (args.sent_cost_lambda * sent_cost + word_cost) / real_batch_size
if train:
optimizer.zero_grad()
cost.backward()
optimizer.step()
total_cost += cost.item()
total_word_cost += word_cost.item()
total_sent_cost += sent_cost.item() / real_batch_size
total_word_count += para_words_count
# total_sent_count += para_sents_count
if train:
print ("batch: {0} loss: {1:.2f}, perp: {2:.2f}, sent loss: {3:.2f}"
.format(batch_idx, word_cost.item() / real_batch_size, math.exp(word_cost.item() / para_words_count),
sent_cost.item() / real_batch_size))
return total_cost / batch_idx, math.exp(total_word_cost / total_word_count), total_sent_cost / batch_idx