-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmonovariant_main.lua
291 lines (228 loc) · 9.11 KB
/
monovariant_main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-- Basic Usage (from scratch): th main.lua --no_load
-- Ideally preload model trained using main.lua
require 'sys'
require 'xlua'
require 'torch'
require 'nn'
require 'rmsprop'
require 'modules/KLDCriterion'
require 'modules/LinearCR'
require 'modules/Reparametrize'
require 'modules/SelectiveOutputClamp'
require 'modules/SelectiveGradientFilter'
require 'optim'
require 'testf'
require 'utils'
require 'config'
torch.setdefaulttensortype('torch.FloatTensor')
cmd = torch.CmdLine()
cmd:text()
cmd:text()
cmd:text('Train a network to store particular information in particular nodes.')
cmd:text()
cmd:text('Options')
cmd:text('Change these options:')
cmd:option('--import', 'default', 'the containing folder of the network to load in. does nothing with `no_load`')
cmd:option('--networks_dir', 'networks', 'the directory to save the resulting networks in')
cmd:option('--name', 'default', 'the name for this network. used for saving the network and results')
cmd:option('--datasetdir', 'DATASET', 'dataset source directory')
cmd:option('--dim_hidden', 200, 'dimension of the representation layer')
cmd:option('--feature_maps', 96, 'number of feature maps')
cmd:option('--force_invariance', false, 'propagate error equal to change in outputs corresponding to fixed variables')
cmd:option('--invariance_strength',0, 'multiplier for the invariance error signal')
cmd:option('--no_load', false, 'do not load in an existing network')
cmd:option('--shape_bias', false, 'use more training samples from the shape set')
cmd:option('--shape_bias_amount', 15, 'the ratio of extra samples from shape set. does nothing without `shape_bias`')
cmd:option('--learning_rate', -0.0005, 'learning rate for the network')
cmd:option('--momentum_decay', 0.1, 'decay rate for momentum in rmsprop')
cmd:text()
cmd:text()
cmd:text("Probably don't change these:")
cmd:option('--threads',2,'how many threads to use in torch')
cmd:option('--num_train_batches',5000,'number of batches to train with per epoch')
cmd:option('--num_train_batches_per_type',3000,'number of available train batches of each data type')
cmd:option('--num_test_batches',1400,'number of batches to test with')
cmd:option('--num_test_batches_per_type',350,'number of available test batches of each type')
cmd:option('--bsize',20,'number of samples per batch')
-- cuda options
cmd:option('--useCuda', false,'Use cuda')
cmd:option('--useCudnn', false,'Use cudnn')
cmd:option('--deviceId', 1, 'which cuda device to use.')
cmd:text()
opt = cmd:parse(arg)
if opt.useCuda then
require 'cutorch'
require 'cunn'
if opt.useCudnn then require 'cudnn' end
end
if opt.useCudnn then assert(opt.useCuda, 'Please enable useCuda as well') end
opt.save = paths.concat(opt.networks_dir, opt.name)
os.execute('mkdir -p ' .. opt.save)
config = {
learningRate = opt.learning_rate,
momentumDecay = opt.momentum_decay,
updateDecay = 0.01
}
torch.setnumthreads(opt.threads)
-- log out the options used for creating this network to a file in the save directory.
-- super useful when you're moving folders around so you don't lose track of things.
local f = assert(io.open(opt.save .. '/cmd_options.txt', 'w'))
for key, val in pairs(opt) do
f:write(tostring(key) .. ": " .. tostring(val) .. "\n")
end
f:flush()
f:close()
MODE_TRAINING = "FT_training"
MODE_TEST = "FT_test"
model = init_network2_150_mv(opt.dim_hidden, opt.feature_maps)
criterion = nn.BCECriterion()
criterion.sizeAverage = false
KLD = nn.KLDCriterion()
KLD.sizeAverage = false
if opt.useCuda then
if opt.useCudnn then cudnn.convert(model, cudnn) end
cutorch.setDevice(opt.deviceId)
criterion:cuda()
KLD:cuda()
model:cuda()
cutorch.synchronize()
end
print(model)
parameters, gradients = model:getParameters()
print('Num before', #parameters)
if not opt.no_load then
-- load all the values from the network stored in opt.import
lowerboundlist = torch.load(opt.import .. '/lowerbound.t7')
lowerbound_test_list = torch.load(opt.import .. '/lowerbound_test.t7')
state = torch.load(opt.import .. '/state.t7')
p = torch.load(opt.import .. '/parameters.t7')
print('Loaded p size:', #p)
parameters:copy(p)
epoch = lowerboundlist:size(1)
config = torch.load(opt.import .. '/config.t7')
else
epoch = 0
end
clamps = model:findModules('nn.SelectiveOutputClamp')
gradFilters = model:findModules('nn.SelectiveGradientFilter')
for clampIndex = 1, #clamps do
gradFilters[clampIndex].force_invariance = opt.force_invariance
gradFilters[clampIndex].invariance_strength = opt.invariance_strength
end
testLogger = optim.Logger(paths.concat(opt.save, 'test.log'))
reconstruction = 0
batch = opt.useCuda and torch.CudaTensor() or torch.FloatTensor()
target = opt.useCuda and torch.CudaTensor() or torch.FloatTensor()
while true do
epoch = epoch + 1
local lowerbound = 0
local time = sys.clock()
print("about to start a batch")
for i = 1, opt.num_train_batches do
xlua.progress(i, opt.num_train_batches)
--Prepare Batch
-- local batch = load_mv_batch(i, opt.dataset_name, MODE_TRAINING)
-- load_random_mv_batch returns two things:
-- 1. the batch itself
-- 2. the type of batch it has selected (AZ, EL, LIGHT_AZ)
if opt.shape_bias then
batchCPU, dataset_type = load_random_mv_shape_bias_batch(MODE_TRAINING)
else
batchCPU, dataset_type = load_random_mv_batch(MODE_TRAINING)
end
-- set the clamp and gradient passthroughs
for clampIndex = 1, #clamps do
if dataset_type == 1 then
clamps[clampIndex]:setPassthroughIndices(1)
gradFilters[clampIndex]:setPassthroughIndices(1)
elseif dataset_type == 2 then
clamps[clampIndex]:setPassthroughIndices(2)
gradFilters[clampIndex]:setPassthroughIndices(2)
elseif dataset_type == 3 then
clamps[clampIndex]:setPassthroughIndices(3)
gradFilters[clampIndex]:setPassthroughIndices(3)
elseif dataset_type == 4 then
clamps[clampIndex]:setPassthroughIndices({4,opt.dim_hidden})
gradFilters[clampIndex]:setPassthroughIndices({4,opt.dim_hidden})
end
clamps[clampIndex].active = true
gradFilters[clampIndex].active = true
end
if opt.useCuda then
batch:resize(batchCPU:size()):copy(batchCPU)
else
batch = batchCPU
end
--Optimization function
local opfunc = function(x)
collectgarbage()
if x ~= parameters then
parameters:copy(x)
end
model:zeroGradParameters()
local f = model:forward(batch)
--local target = target or batch.new()
target:resizeAs(f):copy(batch)
local err = - criterion:forward(f, target)
local df_dw = criterion:backward(f, target):mul(-1)
model:backward(batch,df_dw)
local encoder_output = model:get(1).output
local KLDerr = KLD:forward(encoder_output, target)
local dKLD_dw = KLD:backward(encoder_output, target)
encoder:backward(batch,dKLD_dw)
local lowerbound = err + KLDerr
if opt.verbose then
print("BCE",err/batch:size(1))
print("KLD", KLDerr/batch:size(1))
print("lowerbound", lowerbound/batch:size(1))
end
return lowerbound, gradients
end -- /opfunc
x, batchlowerbound = rmsprop(opfunc, parameters, config, state)
lowerbound = lowerbound + batchlowerbound[1]
end
print("\nEpoch: " .. epoch ..
" Lowerbound: " .. lowerbound/opt.num_train_batches ..
" time: " .. sys.clock() - time)
--Keep track of the lowerbound over time
if lowerboundlist then
lowerboundlist = torch.cat(lowerboundlist
,torch.Tensor(1,1):fill(lowerbound/opt.num_train_batches)
,1)
else
lowerboundlist = torch.Tensor(1,1):fill(lowerbound/opt.num_train_batches)
end
-- save the current net
if true then
local filename = paths.concat(opt.save, 'vxnet.net')
os.execute('mkdir -p ' .. sys.dirname(filename))
if paths.filep(filename) then
os.execute('mv ' .. filename .. ' ' .. filename .. '.old')
end
print('<trainer> saving network to '..filename)
torch.save(filename, model)
end
-- Compute the lowerbound of the test set and save it
lowerbound_test = testf_MV(false)
if true then
if lowerbound_test_list then
lowerbound_test_list = torch.cat(lowerbound_test_list
,torch.Tensor(1,1):fill(lowerbound_test/opt.num_test_batches)
,1)
else
lowerbound_test_list = torch.Tensor(1,1):fill(lowerbound_test/opt.num_test_batches)
end
print('testlowerbound = ' .. lowerbound_test/opt.num_test_batches)
--Save everything to be able to restart later
torch.save(opt.save .. '/parameters.t7', parameters)
torch.save(opt.save .. '/state.t7', state)
torch.save(opt.save .. '/lowerbound.t7', torch.Tensor(lowerboundlist))
torch.save(opt.save .. '/lowerbound_test.t7', torch.Tensor(lowerbound_test_list))
torch.save(opt.save .. '/config.t7', config)
end
-- plot errors
if false then
testLogger:style{['% mean class accuracy (test set)'] = '-'}
testLogger:plot()
end
end