-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtrain_word_prediction_on_nyt.py
147 lines (129 loc) · 6.63 KB
/
train_word_prediction_on_nyt.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
import torch
import torch.nn as nn
import torch.utils.data
import logging
import os
import sys
import random
import argparse
from model import NeuralTensorNetwork, RoleFactoredTensorModel
from dataset import EmbeddingWithBias, WordPredictionDataset, WordPredictionDataset_collate_fn
from event_tensors.glove_utils import Glove
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--use_gpu', type=int, default=1)
parser.add_argument('--random_seed', type=int, default=19950125)
parser.add_argument('--vocab_size', type=int, default=400000)
parser.add_argument('--emb_dim', type=int, default=100)
parser.add_argument('--update_embeddings', type=int, default=1)
parser.add_argument('--epochs', type=int, default=1)
parser.add_argument('--emb_file', type=str, default='data/glove.6B.100d.ext.txt')
parser.add_argument('--dataset_file', type=str, default='data/word_prediction_small.txt')
parser.add_argument('--model', type=str, default='RoleFactor')
parser.add_argument('--em_k', type=int, default=100)
parser.add_argument('--em_r', type=int, default=10)
parser.add_argument('--neg_samples', type=int, default=4)
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--lr', type=float, default=1e-2)
parser.add_argument('--weight_decay', type=float, default=1e-5)
parser.add_argument('--initial_accumulator_value', type=float, default=0.1)
parser.add_argument('--report_every', type=int, default=200)
parser.add_argument('--save_checkpoint', type=str, default='')
parser.add_argument('--load_checkpoint', type=str, default='')
option = parser.parse_args()
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
torch.manual_seed(option.random_seed)
random.seed(option.random_seed)
glove = Glove(option.emb_file)
logging.info('Embeddings loaded')
embeddings = nn.Embedding(option.vocab_size, option.emb_dim, padding_idx=1)
neg_embeddings = EmbeddingWithBias(option.vocab_size, option.emb_dim)
if option.model == 'NTN':
event_model = NeuralTensorNetwork(embeddings, option.em_k)
elif option.model == 'RoleFactor':
event_model = RoleFactoredTensorModel(embeddings, option.em_k)
else:
logging.info('Unknwon model: ' + option.model)
exit(1)
criterion = nn.CrossEntropyLoss()
# load pretrained embeddings
embeddings.weight.data.copy_(torch.from_numpy(glove.embd).float())
if not option.update_embeddings:
event_model.embeddings.weight.requires_grad = False
if option.use_gpu:
event_model.cuda()
neg_embeddings.cuda()
criterion.cuda()
params = [
{ 'params': event_model.embeddings.parameters() },
{ 'params': neg_embeddings.parameters() }
]
if option.model == 'NTN':
params += [
{ 'params': event_model.subj_verb_comp.parameters(), 'weight_decay': option.weight_decay },
{ 'params': event_model.verb_obj_comp.parameters(), 'weight_decay': option.weight_decay },
{ 'params': event_model.final_comp.parameters(), 'weight_decay': option.weight_decay },
{ 'params': event_model.linear1.parameters(), 'weight_decay': option.weight_decay },
{ 'params': event_model.linear2.parameters(), 'weight_decay': option.weight_decay },
{ 'params': event_model.linear3.parameters(), 'weight_decay': option.weight_decay }
]
elif option.model == 'RoleFactor':
params += [
{ 'params': event_model.tensor_comp.parameters(), 'weight_decay': option.weight_decay },
{ 'params': event_model.w.parameters(), 'weight_decay': option.weight_decay }
]
else:
params = None
optimizer = torch.optim.Adagrad(params, lr=option.lr, initial_accumulator_value=option.initial_accumulator_value)
# load checkpoint if provided
if option.load_checkpoint != '':
checkpoint = torch.load(option.load_checkpoint)
event_model.load_state_dict(checkpoint['model_state_dict'])
neg_embeddings.load_state_dict(checkpoint['neg_embeddings_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
logging.info('Loaded checkpoint: ' + option.load_checkpoint)
dataset = WordPredictionDataset()
logging.info('Loading dataset: ' + option.dataset_file)
dataset.load(option.dataset_file, glove)
logging.info('Loaded dataset: ' + option.dataset_file)
data_loader = torch.utils.data.DataLoader(dataset, collate_fn=WordPredictionDataset_collate_fn, batch_size=option.batch_size, shuffle=False)
for epoch in range(option.epochs):
epoch += 1
logging.info('Epoch ' + str(epoch))
avg_loss = 0
for i, batch in enumerate(data_loader):
i += 1
optimizer.zero_grad()
subj_id, subj_w, verb_id, verb_w, obj_id, obj_w, word_id = batch
batch_size = word_id.size(0)
neg_samples = torch.LongTensor(random.sample(range(option.vocab_size), batch_size * option.neg_samples)).view(batch_size, -1)
word_id = torch.cat([word_id.unsqueeze(1), neg_samples], dim=1)
labels = torch.zeros(batch_size).long()
if option.use_gpu:
subj_id = subj_id.cuda()
subj_w = subj_w.cuda()
verb_id = verb_id.cuda()
verb_w = verb_w.cuda()
obj_id = obj_id.cuda()
obj_w = obj_w.cuda()
word_id = word_id.cuda()
labels = labels.cuda()
event_emb = event_model(subj_id, subj_w, verb_id, verb_w, obj_id, obj_w) # (batch, emb_dim)
nce_weights, nce_biases = neg_embeddings(word_id) # (batch, 1+neg, emb_dim), (batch, 1+neg)
scores = torch.bmm(
event_emb.unsqueeze(1), # (batch, 1, emb_dim)
nce_weights.transpose(1, 2) # (batch, emb_dim, 1+neg)
).squeeze() + nce_biases # (batch, 1+neg)
loss = criterion(scores, labels)
avg_loss += loss.item() / option.report_every
if i % option.report_every == 0:
logging.info('Batch %d, loss=%.4f' % (i, avg_loss))
avg_loss = 0
if option.save_checkpoint != '':
checkpoint = {
'model_state_dict': event_model.state_dict(),
'neg_embeddings_state_dict': neg_embeddings.state_dict(),
'optimizer_staet_dict': optimizer.state_dict()
}
torch.save(checkpoint, option.save_checkpoint)
logging.info('Saved checkpoint: ' + option.save_checkpoint)