forked from clementfarabet/lua---nng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
369 lines (329 loc) · 10.2 KB
/
test.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
-----------------------------------------------------------------
-- A few basic tests cases (TODO: expand)
-----------------------------------------------------------------
-- dependencies
require('g')
-- Tests to run
tests = {}
-- Check how are asynchronous updates and queries handled.
function tests.validityPropagation()
local input1 = g.DataNode()
local input2 = g.DataNode()
local mod1 = nn.Linear(10,10){input1}
local mod2 = nn.Linear(10,10){input2}
local mod3 = nn.JoinTable(1){mod1.output, mod2.output}
local mod4 = nn.Tanh(){mod3.output}
local output2 = mod2.output
local output4 = mod4.output
local function g1()
local l = {'i',input1.valid,input2.valid,
'-m', mod1.valid,mod2.valid,mod3.valid,mod4.valid,
'-o',output2.valid,output4.valid}
local s = ''
for _,v in ipairs(l) do
if v ==true then
s = s.."1"
elseif v==false then
s = s.."0"
else
s = s..v
end
end
return s
end
print(g1(), "invalid inputs, invalid outputs") --OK
input1.write(lab.randn(10))
print(g1(), "valid input1, invalid input2, invalid outputs") --OK
input2.write(lab.randn(10))
print(g1(), "valid inputs, invalid outputs") --OK
output2.read()
print(g1(), "valid output2, invalid output3") --OK
output4.read()
print(g1(), "valid outputs") --OK
input1.write(lab.randn(10))
print(g1(), "valid output 2, invalid output3") --OK
input2.write(lab.randn(10))
print(g1(), "invalid outputs") --OK
output4.read()
print(g1(), "valid outputs") --OK
end
-- Nesting can of groups of nodes can be arbitraily deep
-- This example recursively builds a deep MLP like that
-- TODO: also test nested flattening
function tests.nesting()
local depth = 100
local size = 5
local base = g.DataNode()
local top = g.groupNodes({}, base)
for _=1,depth do
local affine = nn.Linear(size, size){top.output}
local squash = nn.Tanh(){affine.output}
top = g.groupNodes({top, affine, squash}, squash.output)
end
base.write(lab.ones(size))
print(top.output.read())
end
function tests.counter()
local cnode = g.CounterNode()
-- without ticks nothing budges
for i=1,3 do
print(cnode.output.read()[1].."="..1)
end
-- with ticks it counts
for i=2,6 do
cnode.tick()
print(cnode.output.read()[1].."="..i, "ticked!")
end
end
-- Basic testing of the network flow in an Elman network
-- note that once set, the input to the net does not change here
function tests.elman()
local input1 = g.DataNode()
local en = g.ElmanNode({3,9,2}, input1)
input1.write(lab.zeros(3))
print(en.output.read()[1], "first pass")
print(en.output.read()[1], "first pass (still)")
en.tick()
print(en.output.read()[1], "second pass")
en.tick()
print(en.output.read()[1], "third pass")
end
-- A simple neural network that produces the square roots of the Fibonacci numbers
function tests.fibonacci()
-- the flipflop is initialized with (0,1)
local init = torch.Tensor(2):zero()
init[2] = 1
local ff = g.TimeDelayNode(2, init)
-- the linear transformation does: x,y <- y, x+y
local mod = nn.Linear(2,2){ff.output}
mod.module.weight:fill(1)
mod.module.weight[1][1]=0
mod.module.bias:fill(0)
ff.connectInput(mod.output)
local omod = nn.Sqrt(){mod.output}
local fibnode = g.groupNodes({mod, omod, ff}, omod.output)
-- let's see if the 12th member of the sequence is indeed 12^2...
for i=1,12 do
print(i, fibnode.output.read()[1])
fibnode.tick()
end
end
-- Illustrate the LSTM gating
function tests.LSTM()
local size=2
local datain = g.DataNode()
local ingatein = g.DataNode()
local forgetgatein = g.DataNode()
local outgatein = g.DataNode()
local lstmnode = g.LstmUnit(size, datain, ingatein, forgetgatein, outgatein)
local outvar = lstmnode.output
-- input data: [0.01, 0.1]
local incs = lab.ones(size)*0.1
incs[1]= 0.02
datain.write(incs)
-- gates completely open
local open = lab.ones(size)*1000
ingatein.write(open)
forgetgatein.write(open)
outgatein.write(open)
for i=1,10 do
print(i, outvar.read()[1], outvar.read()[2])
lstmnode.tick()
end
print()
-- close input gate on one unit
local halfopen = lab.ones(size)*1000
halfopen[1] = -1000
ingatein.write(halfopen)
for i=1,5 do
print(i, outvar.read()[1], outvar.read()[2])
lstmnode.tick()
end
print()
-- forget immediately on the other one now
local closed = lab.ones(size)*-1000
forgetgatein.write(closed)
for i=1,5 do
print(i, outvar.read()[1], outvar.read()[2])
lstmnode.tick()
end
end
-- Testing a backward pass
function tests.backward()
local input = g.DataNode()
local sizes = {4, 3, 9, 5, 2}
local mlp = g.MultiLayerPerceptron(sizes, input)
input.write(lab.ones(sizes[1]))
-- let's do a forward to check the network works
print("forward", mlp.output.read())
-- now construct the backward layer
local outerr = g.DataNode()
local lasttanh = mlp.nodes[2*(#sizes-1)]
local firstlinear = mlp.nodes[1]
local r = g.groupNodes(g.backwardTwin(lasttanh, outerr))
print(r)
-- see if the backward works
outerr.write(lab.ones(sizes[#sizes]))
print("lastback", lasttanh.twin.output.read())
print("firstback", firstlinear.twin.output.read()[1])
print("gradients", firstlinear.twin.gradParameters.read()[1])
end
-- Testing a classical ConvNet
function tests.convnet()
-- define convnet
input = g.DataNode()
features = {3, 8, 16, 32}
fanins = {1, 4, 16}
filters = {7, 7, 7}
poolings = {2, 2}
convnet = g.ConvNet(features, fanins, filters, poolings, input)
-- and a linear classifier for a 4-class problem
reshaper = nn.Reshape(32){convnet.output}
classifier = nn.Linear(32, 4){reshaper.output}
-- loss
target = g.DataNode()
logsoftmax = nn.LogSoftMax(){classifier.output}
loss = nn.ClassNLLCriterion(){logsoftmax.output, target}
-- random input: a 3-channel 46x46 image
input.write(lab.randn(3, 46, 46))
-- let's do a forward to check that the network works
print("forward", logsoftmax.output.read())
-- evaluate the loss
target.write(3)
print("target:", target.read())
print("loss:", loss.output.read())
-- verify the backward construction works too
g.backwardTwin(convnet, g.DataNode())
end
-- Testing criterion
function tests.criterion()
input = g.DataNode()
target = g.DataNode()
mlp = g.MultiLayerPerceptron({10,2}, input)
loss = nn.MSECriterion(){input,target}
t = lab.zeros(10); t[4] = 1; -- desired target: 4th class
input.write(lab.randn(10))
target.write(t)
print("output", mlp.output.read())
print("cost", loss.output.read())
end
-- Test flatten function
function tests.flatten()
input = g.DataNode()
sizes = {2, 3, 2}
mlp = g.MultiLayerPerceptron(sizes, input)
params = g.flattenNodes{mlp}
nparams = sizes[1]*sizes[2] + sizes[2] + sizes[2]*sizes[3] + sizes[3]
print('nb of parameters = ' .. nparams)
mlp.nodes[1].parameters.guts[1]:fill(1)
mlp.nodes[1].parameters.guts[2]:fill(2)
mlp.nodes[3].parameters.guts[1]:fill(3)
mlp.nodes[3].parameters.guts[2]:fill(4)
print(params)
end
-- Test weight sharing
function tests.share()
input = g.DataNode()
sizes = {2, 3, 2}
mlp1 = g.MultiLayerPerceptron(sizes, input)
mlp2 = g.MultiLayerPerceptron(sizes, input)
-- share all params btwn two mlps
g.shareParameters{mlp1, mlp2}
-- flatten them
flat = g.flattenNodes{mlp1,mlp2}
-- set all params of mlp1:
mlp1.nodes[1].parameters.guts[1]:fill(1)
mlp1.nodes[1].parameters.guts[2]:fill(2)
mlp1.nodes[3].parameters.guts[1]:fill(3)
mlp1.nodes[3].parameters.guts[2]:fill(4)
-- verify that mlp2's params are good:
params = g.getParameters{mlp2}
for _,p in ipairs(params) do
print(p)
end
-- print flattened vector
print(flat)
end
-- Test cloning
function tests.clone()
input = g.DataNode()
sizes = {2, 3, 2}
mlp1 = g.MultiLayerPerceptron(sizes, input)
mlp2 = g.cloneNode(mlp1)
input2 = mlp2.nodes[1].inputs[1]
-- let's do a forward with different inputs
input.write(lab.randn(sizes[1]))
input2.write(lab.randn(sizes[1]))
print("forward module1", mlp1.output.read())
print("forward module2", mlp2.output.read())
-- and then a forward with the same input
input2.write(input.read())
print("forward module1", mlp1.output.read())
print("forward module2", mlp2.output.read())
-- verify the backward construction is not affected
g.backwardTwin(mlp1, g.DataNode())
g.backwardTwin(mlp2, g.DataNode())
end
-- Test backward with nesting
function tests.backwardNesting()
local input = g.DataNode()
local mlp1 = g.MultiLayerPerceptron({3,7,5}, input)
local mlp2 = g.MultiLayerPerceptron({5,6,2}, mlp1.output)
local both = g.groupNodes({mlp1, mlp2}, mlp2.output)
print(both)
input.write(lab.ones(3))
print("forward", both.output.read())
local outerr = g.DataNode()
g.backwardTwin(both, outerr)
outerr.write(lab.ones(2))
print("backward", both.twin.output.read())
end
-- Test non-sequential network structure
function tests.blockConnected()
local sizes = {{3,4}, {5,7}, {19}, {2,3,2}, {2}}
local inputs = {}
for i, s in ipairs(sizes[1]) do
inputs[i] = g.DataNode()
end
local net = g.BlockConnectedPerceptron(sizes, inputs)
print(net)
for i, input in ipairs(inputs) do
input.write(lab.ones(input.outputsize))
end
print("forward", net.output.read())
end
-- Test backward with non-sequential graph
function tests.backwardNonseq()
local sizes = {{5,3}, {2}, {4,3},{2, 3,2}, {1}}
local inputs = {}
for i, s in ipairs(sizes[1]) do
inputs[i] = g.DataNode()
end
local net = g.BlockConnectedPerceptron(sizes, inputs)
for i, input in ipairs(inputs) do
input.write(lab.ones(input.outputsize))
end
print("forward", net.output.read())
-- backward
local outerr = g.DataNode()
g.backwardTwin(net, outerr)
outerr.write(lab.ones(net.output.outputsize))
print("backward-last", net.nodes[#net.nodes].twin, net.nodes[#net.nodes].twin.output.read())
print("backward", net.nodes[1].twin, net.nodes[1].twin.output.read()[1])
end
-- TODO: Test combination of flattening and nesting, and re-flattening, and re-nesting
-- TODO: Test flattening and weight-sharing
-- what if the shared weights are part of different graphs that are flattened?
-- TODO: Test backward with time-delays
-- TODO: Flattened gradient vector
-- TODO: Backward building invoked by adding a criterion
-- run all the tests
for k,t in pairs(tests) do
print('==================================================')
print('testing: ' .. k)
t()
print()
end
print('==================================================')
print('All tests done.')
print('==================================================')