This repository has been archived by the owner on May 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
trainMSRVID.lua
146 lines (120 loc) · 3.77 KB
/
trainMSRVID.lua
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
--[[
Author: Hua He
Usage: th trainMSRVID.lua
Training script for semantic relatedness prediction on the MSRVID dataset.
--]]
require('torch')
require('nn')
require('nngraph')
require('optim')
require('xlua')
require('sys')
require('lfs')
similarityMeasure = {}
include('util/read_data.lua')
include('util/Vocab.lua')
include('Conv.lua')
include('CsDis.lua')
--include('PaddingReshape.lua')
printf = utils.printf
-- global paths (modify if desired)
similarityMeasure.data_dir = 'data'
similarityMeasure.models_dir = 'trained_models'
similarityMeasure.predictions_dir = 'predictions'
function header(s)
print(string.rep('-', 80))
print(s)
print(string.rep('-', 80))
end
-- Pearson correlation
function pearson(x, y)
x = x - x:mean()
y = y - y:mean()
return x:dot(y) / (x:norm() * y:norm())
end
-- read command line arguments
local args = lapp [[
Training script for semantic relatedness prediction on the SICK dataset.
-m,--model (default dependency) Model architecture: [dependency, lstm, bilstm]
-l,--layers (default 1) Number of layers (ignored for Tree-LSTM)
-d,--dim (default 150) LSTM memory dimension
]]
local model_name, model_class, model_structure
model_name = 'convOnly'
model_class = similarityMeasure.Conv
model_structure = model_name
torch.seed()
--torch.manualSeed(123)
print('<torch> using the specified seed: ' .. torch.initialSeed())
-- directory containing dataset files
local data_dir = 'data/msrvid/'
-- load vocab
local vocab = similarityMeasure.Vocab(data_dir .. 'vocab-cased.txt')
-- load embeddings
print('loading word embeddings')
local emb_dir = 'data/glove/'
local emb_prefix = emb_dir .. 'glove.840B'
local emb_vocab, emb_vecs = similarityMeasure.read_embedding(emb_prefix .. '.vocab', emb_prefix .. '.300d.th')
local emb_dim = emb_vecs:size(2)
-- use only vectors in vocabulary (not necessary, but gives faster training)
local num_unk = 0
local vecs = torch.Tensor(vocab.size, emb_dim)
for i = 1, vocab.size do
local w = vocab:token(i)
if emb_vocab:contains(w) then
vecs[i] = emb_vecs[emb_vocab:index(w)]
else
num_unk = num_unk + 1
vecs[i]:uniform(-0.05, 0.05)
end
end
print('unk count = ' .. num_unk)
emb_vocab = nil
emb_vecs = nil
collectgarbage()
local taskD = 'vid'
-- load datasets
print('loading datasets')
local train_dir = data_dir .. 'train/'
local dev_dir = data_dir .. 'dev/'
local test_dir = data_dir .. 'test/'
local train_dataset = similarityMeasure.read_relatedness_dataset(train_dir, vocab, taskD)
local dev_dataset = similarityMeasure.read_relatedness_dataset(dev_dir, vocab, taskD)
printf('num train = %d\n', train_dataset.size)
printf('num dev = %d\n', dev_dataset.size)
-- initialize model
local model = model_class{
emb_vecs = vecs,
structure = model_structure,
mem_dim = 150,
task = taskD,
}
-- number of epochs to train
local num_epochs = 35
-- print information
header('model configuration')
printf('max epochs = %d\n', num_epochs)
model:print_config()
if lfs.attributes(similarityMeasure.predictions_dir) == nil then
lfs.mkdir(similarityMeasure.predictions_dir)
end
-- train
local train_start = sys.clock()
local best_dev_score = -1.0
local best_dev_model = model
-- threads
--torch.setnumthreads(4)
--print('<torch> number of threads in used: ' .. torch.getnumthreads())
header('Training model')
local id = 2007
print("Id: " .. id)
for i = 1, num_epochs do
local start = sys.clock()
print('--------------- EPOCH ' .. i .. '--- -------------')
model:trainCombineOnly(train_dataset)
print('Finished epoch in ' .. ( sys.clock() - start) )
local dev_predictions = model:predict_dataset(dev_dataset)
local dev_score = pearson(dev_predictions, dev_dataset.labels)
printf('-- score: %.5f\n', dev_score)
end
print('finished training in ' .. (sys.clock() - train_start))