1
- -- you can easily test specific units like this:
1
+ -- you can easily test specific units like this:
2
2
-- th -lnn -e "nn.test{'LookupTable'}"
3
3
-- th -lnn -e "nn.test{'LookupTable', 'Add'}"
4
4
@@ -66,7 +66,7 @@ function nntest.Add()
66
66
local ferr ,berr = jac .testIO (module ,input )
67
67
mytester :asserteq (ferr , 0 , torch .typename (module ) .. ' - i/o forward err ' )
68
68
mytester :asserteq (berr , 0 , torch .typename (module ) .. ' - i/o backward err ' )
69
- end
69
+ end
70
70
end
71
71
72
72
function nntest .CMul ()
@@ -160,12 +160,12 @@ function nntest.HardTanh()
160
160
local inj = math.random (3 ,5 )
161
161
local ink = math.random (3 ,5 )
162
162
local input = torch .Tensor (ink , inj , ini ):zero ()
163
-
163
+
164
164
local module = nn .HardTanh ()
165
-
165
+
166
166
local err = jac .testJacobian (module , input )
167
167
mytester :assertlt (err , precision , ' error on state ' )
168
-
168
+
169
169
local ferr , berr = jac .testIO (module , input )
170
170
mytester :asserteq (ferr , 0 , torch .typename (module ) .. ' - i/o forward err ' )
171
171
mytester :asserteq (berr , 0 , torch .typename (module ) .. ' - i/o backward err ' )
@@ -176,12 +176,12 @@ function nntest.Abs()
176
176
local inj = math.random (3 ,5 )
177
177
local ink = math.random (3 ,5 )
178
178
local input = torch .Tensor (ink , inj , ini ):zero ()
179
-
179
+
180
180
local module = nn .Abs ()
181
-
181
+
182
182
local err = jac .testJacobian (module , input )
183
183
mytester :assertlt (err , precision , ' error on state ' )
184
-
184
+
185
185
local ferr , berr = jac .testIO (module , input )
186
186
mytester :asserteq (ferr , 0 , torch .typename (module ) .. ' - i/o forward err ' )
187
187
mytester :asserteq (berr , 0 , torch .typename (module ) .. ' - i/o backward err ' )
@@ -376,25 +376,25 @@ function nntest.SparseLinear()
376
376
local ini = math.random (50 ,100 )
377
377
local inj = math.random (5 ,10 )
378
378
local numNonzero = math.random (3 ,5 )
379
-
379
+
380
380
local module = nn .SparseLinear (ini ,inj )
381
381
382
382
-- Create a random sparse vector
383
- N = {}
383
+ local N = {}
384
384
for i = 1 , ini do N [i ] = i end
385
- for i = 1 , numNonzero do
385
+ for i = 1 , numNonzero do
386
386
local j = math.random (i ,ini )
387
387
N [i ], N [j ] = N [j ], N [i ]
388
- end
388
+ end
389
389
local input = torch .Tensor (numNonzero , 2 ):zero ()
390
390
for i = 1 , numNonzero do input [{i ,1 }] = N [i ] end
391
391
local values = input :select (2 ,2 )
392
392
values :copy (torch .rand (values :nElement ())):mul (2 ):add (- 1 )
393
-
393
+
394
394
-- Check output
395
395
local actual = module :forward (input )
396
396
local expected = torch .Tensor (inj )
397
- for j = 1 , inj do
397
+ for j = 1 , inj do
398
398
expected [j ] = 0
399
399
for i = 1 ,numNonzero do
400
400
expected [j ] = expected [j ] + values [i ] * module .weight [{j , N [i ]}]
@@ -412,13 +412,13 @@ function nntest.SparseLinear()
412
412
413
413
local err = sjac .testJacobianParameters (module , input , module .bias , module .gradBias )
414
414
mytester :assertlt (err ,precision , ' error on bias ' )
415
-
415
+
416
416
local err = sjac .testJacobianUpdateParameters (module , input , module .weight )
417
417
mytester :assertlt (err ,precision , ' error on weight [direct update] ' )
418
418
419
419
local err = sjac .testJacobianUpdateParameters (module , input , module .bias )
420
420
mytester :assertlt (err ,precision , ' error on bias [direct update] ' )
421
-
421
+
422
422
for t ,err in pairs (sjac .testAllUpdate (module , input , ' weight' , ' gradWeight' )) do
423
423
mytester :assertlt (err , precision , string.format (
424
424
' error on weight [%s]' , t ))
@@ -483,7 +483,7 @@ local function criterionJacobianTest1D(cri, input, target)
483
483
local fx1 = cri :forward (input , target )
484
484
-- f(xi - h)
485
485
input [i ] = input [i ] - 2 * eps
486
- local fx2 = cri :forward (input , target )
486
+ local fx2 = cri :forward (input , target )
487
487
-- f'(xi) = (f(xi + h) - f(xi - h)) / 2h
488
488
local cdfx = (fx1 - fx2 ) / (2 * eps )
489
489
-- store f' in appropriate place
@@ -501,14 +501,14 @@ function nntest.MSECriterion()
501
501
local input = torch .rand (10 )
502
502
local target = input :clone ():add (torch .rand (10 ))
503
503
local cri = nn .MSECriterion ()
504
- criterionJacobianTest1D (cri , input , target )
504
+ criterionJacobianTest1D (cri , input , target )
505
505
end
506
506
507
507
function nntest .MarginCriterion ()
508
508
local input = torch .rand (100 )
509
509
local target = input :clone ():add (torch .rand (100 ))
510
510
local cri = nn .MarginCriterion ()
511
- criterionJacobianTest1D (cri , input , target )
511
+ criterionJacobianTest1D (cri , input , target )
512
512
end
513
513
514
514
function nntest .WeightedMSECriterion ()
@@ -536,9 +536,9 @@ function nntest.DistKLDivCriterion()
536
536
end
537
537
538
538
function nntest .ClassNLLCriterion ()
539
- local numLabels = math.random (5 ,10 )
539
+ local numLabels = math.random (5 ,10 )
540
540
local input = torch .rand (numLabels )
541
- local target = math.random (1 ,numLabels )
541
+ local target = math.random (1 ,numLabels )
542
542
543
543
-- default ClassNLLCriterion
544
544
local cri = nn .ClassNLLCriterion ()
@@ -814,19 +814,19 @@ function nntest.SpatialConvolution()
814
814
local input = torch .Tensor (from , inj , ini ):zero ()
815
815
816
816
-- stochastic
817
-
817
+
818
818
local err = jac .testJacobian (module , input )
819
819
mytester :assertlt (err , precision , ' error on state ' )
820
-
820
+
821
821
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
822
822
mytester :assertlt (err , precision , ' error on weight ' )
823
-
823
+
824
824
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
825
825
mytester :assertlt (err , precision , ' error on bias ' )
826
826
827
827
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
828
828
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
829
-
829
+
830
830
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
831
831
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
832
832
@@ -841,7 +841,7 @@ function nntest.SpatialConvolution()
841
841
end
842
842
843
843
-- batch
844
-
844
+
845
845
-- verbose = true
846
846
local batch = math.random (2 ,5 )
847
847
outi = math.random (4 ,8 )
@@ -857,16 +857,16 @@ function nntest.SpatialConvolution()
857
857
858
858
local err = jac .testJacobian (module , input )
859
859
mytester :assertlt (err , precision , ' batch error on state ' )
860
-
860
+
861
861
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
862
862
mytester :assertlt (err , precision , ' batch error on weight ' )
863
-
863
+
864
864
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
865
865
mytester :assertlt (err , precision , ' batch error on bias ' )
866
866
867
867
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
868
868
mytester :assertlt (err , precision , ' batch error on weight [direct update] ' )
869
-
869
+
870
870
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
871
871
mytester :assertlt (err , precision , ' batch error on bias [direct update] ' )
872
872
@@ -879,7 +879,7 @@ function nntest.SpatialConvolution()
879
879
mytester :assertlt (err , precision , string.format (
880
880
' batch error on bias [%s]' , t ))
881
881
end
882
-
882
+
883
883
local ferr , berr = jac .testIO (module , input )
884
884
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
885
885
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -898,19 +898,19 @@ function nntest.SpatialConvolutionMM()
898
898
local input = torch .Tensor (from , inj , ini ):zero ()
899
899
900
900
-- stochastic
901
-
901
+
902
902
local err = jac .testJacobian (module , input )
903
903
mytester :assertlt (err , precision , ' error on state ' )
904
-
904
+
905
905
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
906
906
mytester :assertlt (err , precision , ' error on weight ' )
907
-
907
+
908
908
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
909
909
mytester :assertlt (err , precision , ' error on bias ' )
910
910
911
911
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
912
912
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
913
-
913
+
914
914
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
915
915
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
916
916
@@ -925,7 +925,7 @@ function nntest.SpatialConvolutionMM()
925
925
end
926
926
927
927
-- batch
928
-
928
+
929
929
-- verbose = true
930
930
local batch = math.random (2 ,5 )
931
931
outi = math.random (4 ,8 )
@@ -937,16 +937,16 @@ function nntest.SpatialConvolutionMM()
937
937
938
938
local err = jac .testJacobian (module , input )
939
939
mytester :assertlt (err , precision , ' batch error on state ' )
940
-
940
+
941
941
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
942
942
mytester :assertlt (err , precision , ' batch error on weight ' )
943
-
943
+
944
944
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
945
945
mytester :assertlt (err , precision , ' batch error on bias ' )
946
946
947
947
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
948
948
mytester :assertlt (err , precision , ' batch error on weight [direct update] ' )
949
-
949
+
950
950
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
951
951
mytester :assertlt (err , precision , ' batch error on bias [direct update] ' )
952
952
@@ -959,7 +959,7 @@ function nntest.SpatialConvolutionMM()
959
959
mytester :assertlt (err , precision , string.format (
960
960
' batch error on bias [%s]' , t ))
961
961
end
962
-
962
+
963
963
local ferr , berr = jac .testIO (module , input )
964
964
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
965
965
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -980,13 +980,13 @@ function nntest.SpatialConvolutionMap()
980
980
981
981
local module = nn .SpatialConvolutionMap (nn .tables .random (from , to , fanin ), ki , kj , si , sj )
982
982
local input = torch .Tensor (from , inj , ini ):zero ()
983
-
983
+
984
984
local err = jac .testJacobian (module , input )
985
985
mytester :assertlt (err , precision , ' error on state ' )
986
-
986
+
987
987
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
988
988
mytester :assertlt (err , precision , ' error on weight ' )
989
-
989
+
990
990
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
991
991
mytester :assertlt (err , precision , ' error on bias ' )
992
992
@@ -999,7 +999,7 @@ function nntest.SpatialConvolutionMap()
999
999
mytester :assertlt (err , precision , string.format (
1000
1000
' error on bias [%s]' , t ))
1001
1001
end
1002
-
1002
+
1003
1003
local ferr , berr = jac .testIO (module , input )
1004
1004
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1005
1005
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1055,20 +1055,20 @@ function nntest.SpatialFullConvolution()
1055
1055
local inj = math.random (5 ,8 )
1056
1056
local module = nn .SpatialFullConvolution (from , to , ki , kj , si , sj )
1057
1057
local input = torch .Tensor (from , inj , ini ):zero ()
1058
-
1058
+
1059
1059
-- stochastic
1060
1060
local err = jac .testJacobian (module , input )
1061
1061
mytester :assertlt (err , precision , ' error on state ' )
1062
-
1062
+
1063
1063
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1064
1064
mytester :assertlt (err , precision , ' error on weight ' )
1065
-
1065
+
1066
1066
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1067
1067
mytester :assertlt (err , precision , ' error on bias ' )
1068
1068
1069
1069
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1070
1070
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
1071
-
1071
+
1072
1072
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1073
1073
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
1074
1074
@@ -1091,16 +1091,16 @@ function nntest.SpatialFullConvolution()
1091
1091
1092
1092
local err = jac .testJacobian (module , input )
1093
1093
mytester :assertlt (err , precision , ' batch error on state ' )
1094
-
1094
+
1095
1095
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1096
1096
mytester :assertlt (err , precision , ' batch error on weight ' )
1097
-
1097
+
1098
1098
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1099
1099
mytester :assertlt (err , precision , ' batch error on bias ' )
1100
1100
1101
1101
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1102
1102
mytester :assertlt (err , precision , ' batch error on weight [direct update] ' )
1103
-
1103
+
1104
1104
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1105
1105
mytester :assertlt (err , precision , ' batch error on bias [direct update] ' )
1106
1106
@@ -1113,7 +1113,7 @@ function nntest.SpatialFullConvolution()
1113
1113
mytester :assertlt (err , precision , string.format (
1114
1114
' batch error on bias [%s]' , t ))
1115
1115
end
1116
-
1116
+
1117
1117
local ferr , berr = jac .testIO (module , input )
1118
1118
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1119
1119
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1132,20 +1132,20 @@ function nntest.SpatialFullConvolutionMap()
1132
1132
local inj = math.random (5 ,7 )
1133
1133
local module = nn .SpatialFullConvolutionMap (tt , ki , kj , si , sj )
1134
1134
local input = torch .Tensor (from , inj , ini ):zero ()
1135
-
1135
+
1136
1136
-- stochastic
1137
1137
local err = jac .testJacobian (module , input )
1138
1138
mytester :assertlt (err , precision , ' error on state ' )
1139
-
1139
+
1140
1140
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1141
1141
mytester :assertlt (err , precision , ' error on weight ' )
1142
-
1142
+
1143
1143
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1144
1144
mytester :assertlt (err , precision , ' error on bias ' )
1145
1145
1146
1146
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1147
1147
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
1148
-
1148
+
1149
1149
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1150
1150
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
1151
1151
@@ -1158,7 +1158,7 @@ function nntest.SpatialFullConvolutionMap()
1158
1158
mytester :assertlt (err , precision , string.format (
1159
1159
' error on bias [%s]' , t ))
1160
1160
end
1161
-
1161
+
1162
1162
local ferr , berr = jac .testIO (module , input )
1163
1163
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1164
1164
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1223,7 +1223,7 @@ local function batchcompare(smod, sin, plist)
1223
1223
1224
1224
smod :accGradParameters (sin , sgout , 1 )
1225
1225
bmod :accGradParameters (bin , bgout , 1 )
1226
-
1226
+
1227
1227
mytester :assertTensorEq (sout ,bout :select (1 ,1 ), 1e-8 , ' batchcompare error on output' )
1228
1228
mytester :assertTensorEq (sgin ,bgin :select (1 ,1 ), 1e-8 , ' batchcompare error on gradInput' )
1229
1229
@@ -1265,7 +1265,7 @@ function nntest.SpatialFullConvolutionBatchCompare()
1265
1265
1266
1266
batchcompare (module ,input , {' weight' ,' bias' ,' gradWeight' ,' gradBias' })
1267
1267
end
1268
-
1268
+
1269
1269
1270
1270
1271
1271
function nntest .SpatialSubSamplingBatchCompare ()
@@ -1296,19 +1296,19 @@ function nntest.SpatialSubSampling()
1296
1296
local inj = (outj - 1 )* sj + kj
1297
1297
local module = nn .SpatialSubSampling (from , ki , kj , si , sj )
1298
1298
local input = torch .Tensor (from , inj , ini ):zero ()
1299
-
1299
+
1300
1300
local err = jac .testJacobian (module , input )
1301
1301
mytester :assertlt (err , precision , ' error on state ' )
1302
-
1302
+
1303
1303
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1304
1304
mytester :assertlt (err , precision , ' error on weight ' )
1305
-
1305
+
1306
1306
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1307
1307
mytester :assertlt (err , precision , ' error on bias ' )
1308
1308
1309
1309
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1310
1310
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
1311
-
1311
+
1312
1312
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1313
1313
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
1314
1314
@@ -1337,16 +1337,16 @@ function nntest.SpatialSubSampling()
1337
1337
1338
1338
local err = jac .testJacobian (module , input )
1339
1339
mytester :assertlt (err , precision , ' batch error on state ' )
1340
-
1340
+
1341
1341
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1342
1342
mytester :assertlt (err , precision , ' batch error on weight ' )
1343
-
1343
+
1344
1344
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1345
1345
mytester :assertlt (err , precision , ' batch error on bias ' )
1346
1346
1347
1347
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1348
1348
mytester :assertlt (err , precision , ' batch error on weight [direct update] ' )
1349
-
1349
+
1350
1350
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1351
1351
mytester :assertlt (err , precision , ' batch error on bias [direct update] ' )
1352
1352
@@ -1359,7 +1359,7 @@ function nntest.SpatialSubSampling()
1359
1359
mytester :assertlt (err , precision , string.format (
1360
1360
' batch error on bias [%s]' , t ))
1361
1361
end
1362
-
1362
+
1363
1363
local ferr , berr = jac .testIO (module , input )
1364
1364
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1365
1365
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1442,12 +1442,12 @@ function nntest.Tanh()
1442
1442
local inj = math.random (3 ,5 )
1443
1443
local ink = math.random (3 ,5 )
1444
1444
local input = torch .Tensor (ink , inj , ini ):zero ()
1445
-
1445
+
1446
1446
local module = nn .Tanh ()
1447
-
1447
+
1448
1448
local err = jac .testJacobian (module , input )
1449
1449
mytester :assertlt (err , precision , ' error on state ' )
1450
-
1450
+
1451
1451
local ferr , berr = jac .testIO (module , input )
1452
1452
mytester :asserteq (ferr , 0 , torch .typename (module ) .. ' - i/o forward err ' )
1453
1453
mytester :asserteq (berr , 0 , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1463,13 +1463,13 @@ function nntest.TemporalConvolution()
1463
1463
local ini = (outi - 1 )* si + ki
1464
1464
local module = nn .TemporalConvolution (from , to , ki ,si )
1465
1465
local input = torch .Tensor (ini , from ):zero ()
1466
-
1466
+
1467
1467
local err = jac .testJacobian (module , input )
1468
1468
mytester :assertlt (err , precision , ' error on state ' )
1469
-
1469
+
1470
1470
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1471
1471
mytester :assertlt (err , precision , ' error on weight ' )
1472
-
1472
+
1473
1473
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1474
1474
mytester :assertlt (err , precision , ' error on bias ' )
1475
1475
@@ -1488,17 +1488,17 @@ function nntest.TemporalConvolution()
1488
1488
mytester :assertlt (err , precision , string.format (
1489
1489
' error on bias [%s]' , t ))
1490
1490
end
1491
-
1491
+
1492
1492
-- 2D
1493
1493
local nBatchFrame = 4
1494
1494
local input = torch .Tensor (nBatchFrame , ini , from ):zero ()
1495
-
1495
+
1496
1496
local err = jac .testJacobian (module , input )
1497
1497
mytester :assertlt (err , precision , ' error on state ' )
1498
-
1498
+
1499
1499
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1500
1500
mytester :assertlt (err , precision , ' error on weight ' )
1501
-
1501
+
1502
1502
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1503
1503
mytester :assertlt (err , precision , ' error on bias ' )
1504
1504
@@ -1517,21 +1517,21 @@ function nntest.TemporalConvolution()
1517
1517
mytester :assertlt (err , precision , string.format (
1518
1518
' error on bias [%s]' , t ))
1519
1519
end
1520
-
1520
+
1521
1521
local ferr , berr = jac .testIO (module , input )
1522
1522
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1523
1523
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
1524
-
1524
+
1525
1525
-- 2D matches 1D
1526
1526
local output = module :forward (input ):clone ()
1527
1527
local outputGrad = torch .randn (output :size ())
1528
1528
local inputGrad = module :backward (input , outputGrad ):clone ()
1529
-
1529
+
1530
1530
local input1D = input :select (1 , 2 )
1531
1531
local output1D = module :forward (input1D )
1532
1532
local outputGrad1D = outputGrad :select (1 , 2 )
1533
1533
local inputGrad1D = module :backward (input1D , outputGrad1D )
1534
-
1534
+
1535
1535
mytester :assertTensorEq (output :select (1 ,2 ), output1D , 0.000001 , ' error on 2D vs 1D forward)' )
1536
1536
mytester :assertTensorEq (inputGrad :select (1 ,2 ), inputGrad1D , 0.000001 , ' error on 2D vs 1D backward)' )
1537
1537
end
@@ -1544,19 +1544,19 @@ function nntest.TemporalSubSampling()
1544
1544
local ini = (outi - 1 )* si + ki
1545
1545
local module = nn .TemporalSubSampling (from , ki , si )
1546
1546
local input = torch .Tensor (ini , from ):zero ()
1547
-
1547
+
1548
1548
local err = jac .testJacobian (module , input )
1549
1549
mytester :assertlt (err , precision , ' error on state ' )
1550
-
1550
+
1551
1551
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1552
1552
mytester :assertlt (err , precision , ' error on weight ' )
1553
-
1553
+
1554
1554
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1555
1555
mytester :assertlt (err , precision , ' error on bias ' )
1556
-
1556
+
1557
1557
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1558
1558
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
1559
-
1559
+
1560
1560
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1561
1561
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
1562
1562
@@ -1601,17 +1601,17 @@ function nntest.TemporalMaxPooling()
1601
1601
local ferr , berr = jac .testIO (module , input )
1602
1602
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1603
1603
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
1604
-
1604
+
1605
1605
-- 2D matches 1D
1606
1606
local output = module :forward (input ):clone ()
1607
1607
local outputGrad = torch .randn (output :size ())
1608
1608
local inputGrad = module :backward (input , outputGrad ):clone ()
1609
-
1609
+
1610
1610
local input1D = input :select (1 , 2 )
1611
1611
local output1D = module :forward (input1D )
1612
1612
local outputGrad1D = outputGrad :select (1 , 2 )
1613
1613
local inputGrad1D = module :backward (input1D , outputGrad1D )
1614
-
1614
+
1615
1615
mytester :assertTensorEq (output :select (1 ,2 ), output1D , 0.000001 , ' error on 2D vs 1D forward)' )
1616
1616
mytester :assertTensorEq (inputGrad :select (1 ,2 ), inputGrad1D , 0.000001 , ' error on 2D vs 1D backward)' )
1617
1617
end
@@ -1633,19 +1633,19 @@ function nntest.VolumetricConvolution()
1633
1633
local inj = (outj - 1 )* sj + kj
1634
1634
local module = nn .VolumetricConvolution (from , to , kt , ki , kj , st , si , sj )
1635
1635
local input = torch .Tensor (from , int , inj , ini ):zero ()
1636
-
1636
+
1637
1637
local err = jac .testJacobian (module , input )
1638
1638
mytester :assertlt (err , precision , ' error on state ' )
1639
-
1639
+
1640
1640
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight )
1641
1641
mytester :assertlt (err , precision , ' error on weight ' )
1642
-
1642
+
1643
1643
local err = jac .testJacobianParameters (module , input , module .bias , module .gradBias )
1644
1644
mytester :assertlt (err , precision , ' error on bias ' )
1645
1645
1646
1646
local err = jac .testJacobianUpdateParameters (module , input , module .weight )
1647
1647
mytester :assertlt (err , precision , ' error on weight [direct update] ' )
1648
-
1648
+
1649
1649
local err = jac .testJacobianUpdateParameters (module , input , module .bias )
1650
1650
mytester :assertlt (err , precision , ' error on bias [direct update] ' )
1651
1651
@@ -1658,7 +1658,7 @@ function nntest.VolumetricConvolution()
1658
1658
mytester :assertlt (err , precision , string.format (
1659
1659
' error on bias [%s]' , t ))
1660
1660
end
1661
-
1661
+
1662
1662
local ferr , berr = jac .testIO (module , input )
1663
1663
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1664
1664
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1681,10 +1681,10 @@ function nntest.VolumetricMaxPooling()
1681
1681
local inj = (outj - 1 )* sj + kj
1682
1682
local module = nn .VolumetricMaxPooling (kt , ki , kj , st , si , sj )
1683
1683
local input = torch .Tensor (from , int , inj , ini ):zero ()
1684
-
1684
+
1685
1685
local err = jac .testJacobian (module , input )
1686
1686
mytester :assertlt (err , precision , ' error on state ' )
1687
-
1687
+
1688
1688
local ferr , berr = jac .testIO (module , input )
1689
1689
mytester :asserteq (0 , ferr , torch .typename (module ) .. ' - i/o forward err ' )
1690
1690
mytester :asserteq (0 , berr , torch .typename (module ) .. ' - i/o backward err ' )
@@ -1833,12 +1833,12 @@ end
1833
1833
function nntest .Module_getParameters_8 ()
1834
1834
local function makeMLP (nin , ns )
1835
1835
local net = nn .Sequential ()
1836
-
1837
- for k ,v in ipairs (ns ) do
1836
+
1837
+ for k ,v in ipairs (ns ) do
1838
1838
net :add (nn .Linear (nin , v ))
1839
1839
nin = v
1840
1840
end
1841
- _ ,_ = net :getParameters ()
1841
+ local _ ,_ = net :getParameters ()
1842
1842
return net
1843
1843
end
1844
1844
@@ -1847,9 +1847,9 @@ function nntest.Module_getParameters_8()
1847
1847
1848
1848
local net = nn .Sequential ():add (mlp1 :get (1 ))
1849
1849
:add (mlp2 :get (1 ))
1850
-
1850
+
1851
1851
-- clone the second MLP to ensure that the weights before calling getParameters are preserved
1852
- mlp2 = mlp2 :clone ()
1852
+ mlp2 = mlp2 :clone ()
1853
1853
1854
1854
local p , gp = net :getParameters ()
1855
1855
@@ -1858,7 +1858,7 @@ function nntest.Module_getParameters_8()
1858
1858
-- check that the weights have the same values as before get Parameters was called
1859
1859
mytester :asserteq ((net .modules [1 ].weight - mlp1 .modules [1 ].weight ):norm (), 0 , ' error when using partial realloc' )
1860
1860
mytester :asserteq ((net .modules [2 ].weight - mlp2 .modules [1 ].weight ):norm (), 0 , ' error when using partial realloc' )
1861
-
1861
+
1862
1862
end
1863
1863
1864
1864
function nntest .PairwiseDistance ()
@@ -1876,17 +1876,17 @@ function nntest.PairwiseDistance()
1876
1876
1877
1877
local err = jac .testJacobian (module ,input )
1878
1878
mytester :assertlt (err ,precision , ' error on state ' )
1879
-
1879
+
1880
1880
local ferr ,berr = jac .testIO (module ,input )
1881
1881
mytester :asserteq (ferr , 0 , torch .typename (module ).. ' - i/o forward err ' )
1882
1882
mytester :asserteq (berr , 0 , torch .typename (module ).. ' - i/o backward err ' )
1883
1883
1884
1884
-- Also check that the forward prop result is correct.
1885
1885
input = torch .rand (2 , ini )
1886
- err = torch .dist (input :select (1 ,1 ), input :select (1 ,2 ), p ) -
1886
+ err = torch .dist (input :select (1 ,1 ), input :select (1 ,2 ), p ) -
1887
1887
module :forward (input )[1 ]
1888
- mytester :assertlt (err ,precision , ' error on non-batch fprop ' )
1889
-
1888
+ mytester :assertlt (err ,precision , ' error on non-batch fprop ' )
1889
+
1890
1890
-- TEST CASE 2: batch input
1891
1891
local inj = math.random (3 ,5 )
1892
1892
input = torch .Tensor (2 , inj , ini ):zero ()
@@ -1905,12 +1905,12 @@ function nntest.PairwiseDistance()
1905
1905
local inputb = torch .rand (inj ,ini )
1906
1906
local dist_manual = torch .Tensor (inj )
1907
1907
for i = 1 , inputa :size (1 ) do
1908
- dist_manual [i ] = torch .dist (inputa :select (1 ,i ), inputb :select (1 ,i ),p )
1908
+ dist_manual [i ] = torch .dist (inputa :select (1 ,i ), inputb :select (1 ,i ),p )
1909
1909
end
1910
1910
-- compare the distances to the module's fprop
1911
1911
local dist = module :forward (torch .cat (inputa ,inputb ,1 ):resize (2 ,inj ,ini ))
1912
- err = dist - dist_manual
1913
- mytester :assertlt (err :norm (), precision , torch .typename (module ) ..
1912
+ err = dist - dist_manual
1913
+ mytester :assertlt (err :norm (), precision , torch .typename (module ) ..
1914
1914
' error on batch fprop ' )
1915
1915
end
1916
1916
end
@@ -1923,7 +1923,7 @@ function nntest.LookupTable()
1923
1923
local module = nn .LookupTable (totalIndex , entry_size )
1924
1924
local minval = 1
1925
1925
local maxval = totalIndex
1926
-
1926
+
1927
1927
local output = module :forward (input )
1928
1928
module :backwardUpdate (input , output , 0.1 )
1929
1929
input :zero ()
@@ -1934,7 +1934,7 @@ function nntest.LookupTable()
1934
1934
1935
1935
local err = jac .testJacobianUpdateParameters (module , input , module .weight , minval , maxval )
1936
1936
mytester :assertlt (err ,precision , ' 1D error on weight [direct update] ' )
1937
-
1937
+
1938
1938
module .gradWeight :zero ()
1939
1939
for t ,err in pairs (jac .testAllUpdate (module , input , ' weight' , ' gradWeight' )) do
1940
1940
mytester :assertlt (err , precision , string.format (
@@ -1947,7 +1947,7 @@ function nntest.LookupTable()
1947
1947
1948
1948
local err = jac .testJacobianParameters (module , input , module .weight , module .gradWeight , minval , maxval )
1949
1949
mytester :assertlt (err ,precision , ' 2D error on weight ' )
1950
-
1950
+
1951
1951
local err = jac .testJacobianUpdateParameters (module , input , module .weight , minval , maxval )
1952
1952
mytester :assertlt (err ,precision , ' 2D error on weight [direct update] ' )
1953
1953
@@ -1962,15 +1962,15 @@ function nntest.LookupTable()
1962
1962
local ferr ,berr = jac .testIO (module ,input ,minval ,maxval )
1963
1963
mytester :asserteq (ferr , 0 , torch .typename (module ) .. ' - i/o forward err ' )
1964
1964
mytester :asserteq (berr , 0 , torch .typename (module ) .. ' - i/o backward err ' )
1965
-
1965
+
1966
1966
-- accUpdate
1967
1967
module :accUpdateOnly ()
1968
1968
mytester :assert (not module .gradWeight , ' gradWeight is nil' )
1969
1969
module :float ()
1970
1970
local output = module :forward (input )
1971
1971
module :backwardUpdate (input , output , 0.1 )
1972
1972
end
1973
-
1973
+
1974
1974
function nntest .AddConstant ()
1975
1975
local nbatch = torch .random (3 , 5 )
1976
1976
local f = torch .random (3 , 5 )
@@ -2049,18 +2049,18 @@ end
2049
2049
2050
2050
function nntest .SelectTable ()
2051
2051
local input = {
2052
- torch .rand (3 ,4 ,5 ), torch .rand (3 ,4 ,5 ),
2053
- {torch .rand (3 ,4 ,5 )},
2052
+ torch .rand (3 ,4 ,5 ), torch .rand (3 ,4 ,5 ),
2053
+ {torch .rand (3 ,4 ,5 )},
2054
2054
{torch .rand (3 ,4 ,5 ), {torch .rand (3 ,4 ,5 )}}
2055
2055
}
2056
2056
local gradOutputs = {
2057
- torch .rand (3 ,4 ,5 ), torch .rand (3 ,4 ,5 ),
2058
- {torch .rand (3 ,4 ,5 )},
2057
+ torch .rand (3 ,4 ,5 ), torch .rand (3 ,4 ,5 ),
2058
+ {torch .rand (3 ,4 ,5 )},
2059
2059
{torch .rand (3 ,4 ,5 ), {torch .rand (3 ,4 ,5 )}}
2060
2060
}
2061
2061
local zeros = {
2062
- torch .Tensor (3 ,4 ,5 ):zero (), torch .Tensor (3 ,4 ,5 ):zero (),
2063
- {torch .Tensor (3 ,4 ,5 ):zero ()},
2062
+ torch .Tensor (3 ,4 ,5 ):zero (), torch .Tensor (3 ,4 ,5 ):zero (),
2063
+ {torch .Tensor (3 ,4 ,5 ):zero ()},
2064
2064
{torch .Tensor (3 ,4 ,5 ):zero (), {torch .Tensor (3 ,4 ,5 ):zero ()}}
2065
2065
}
2066
2066
local nonIdx = {2 ,3 ,4 ,1 }
@@ -2088,7 +2088,7 @@ function nntest.MixtureTable()
2088
2088
local expertInput = torch .randn (5 ,3 ,6 )
2089
2089
local gradOutput = torch .randn (5 ,6 )
2090
2090
local input = {
2091
- torch .rand (5 ,3 ),
2091
+ torch .rand (5 ,3 ),
2092
2092
{expertInput :select (2 ,1 ), expertInput :select (2 ,2 ), expertInput :select (2 ,3 )}
2093
2093
}
2094
2094
local module = nn .MixtureTable ()
@@ -2111,13 +2111,13 @@ function nntest.MixtureTable()
2111
2111
local gradInput = module :backward (input , gradOutput )
2112
2112
mytester :assertTensorEq (gradInput [1 ], gaterGradInput2 , 0.000001 , " mixture2 gater gradInput" )
2113
2113
mytester :assertTensorEq (gradInput [2 ], expertGradInput2 , 0.000001 , " mixture2 expert gradInput" )
2114
-
2114
+
2115
2115
--[[ 3D ]] --
2116
2116
local expertInput = torch .randn (5 ,6 ,3 ,2 )
2117
2117
local gradOutput = torch .randn (5 ,6 ,2 )
2118
2118
-- expertInput is a Table:
2119
2119
local input = {
2120
- torch .rand (5 ,3 ),
2120
+ torch .rand (5 ,3 ),
2121
2121
{expertInput :select (3 ,1 ), expertInput :select (3 ,2 ), expertInput :select (3 ,3 )}
2122
2122
}
2123
2123
local module = nn .MixtureTable ()
@@ -2140,13 +2140,13 @@ function nntest.MixtureTable()
2140
2140
local gradInput = module :backward (input , gradOutput )
2141
2141
mytester :assertTensorEq (gradInput [1 ], gaterGradInput2 , 0.000001 , " mixture4 gater gradInput" )
2142
2142
mytester :assertTensorEq (gradInput [2 ], expertGradInput2 , 0.000001 , " mixture4 expert gradInput" )
2143
-
2143
+
2144
2144
--[[ 1D ]] --
2145
2145
-- expertInput is a Table:
2146
2146
local expertInput = torch .randn (3 ,6 )
2147
2147
local gradOutput = torch .randn (6 )
2148
2148
local input = {
2149
- torch .rand (3 ),
2149
+ torch .rand (3 ),
2150
2150
{expertInput :select (1 ,1 ), expertInput :select (1 ,2 ), expertInput :select (1 ,3 )}
2151
2151
}
2152
2152
local module = nn .MixtureTable ()
@@ -2164,7 +2164,7 @@ function nntest.MixtureTable()
2164
2164
-- test type-cast
2165
2165
module :float ()
2166
2166
local input2 = {
2167
- input [1 ]:float (),
2167
+ input [1 ]:float (),
2168
2168
{input [2 ][1 ]:float (), input [2 ][2 ]:float (), input [2 ][3 ]:float ()}
2169
2169
}
2170
2170
local output = module :forward (input2 )
@@ -2190,13 +2190,13 @@ function nntest.MixtureTable()
2190
2190
local gradInput = module :backward (input2 , gradOutput :float ())
2191
2191
mytester :assertTensorEq (gradInput [1 ], gaterGradInput2 :float (), 0.000001 , " mixture6B gater gradInput" )
2192
2192
mytester :assertTensorEq (gradInput [2 ], expertGradInput2 :float (), 0.000001 , " mixture6B expert gradInput" )
2193
-
2193
+
2194
2194
--[[ 2D gater, 1D expert]] --
2195
2195
-- expertInput is a Table:
2196
2196
local expertInput = torch .randn (5 ,3 )
2197
2197
local gradOutput = torch .randn (5 )
2198
2198
local input = {
2199
- torch .rand (5 ,3 ),
2199
+ torch .rand (5 ,3 ),
2200
2200
{expertInput :select (2 ,1 ), expertInput :select (2 ,2 ), expertInput :select (2 ,3 )}
2201
2201
}
2202
2202
local module = nn .MixtureTable ()
@@ -2270,7 +2270,7 @@ function nntest.SpatialUpSamplingNearest()
2270
2270
table.insert (shape , torch .random (2 , 2 + dim - 1 ))
2271
2271
end
2272
2272
2273
- -- Check that the gradient is correct by using finite elements
2273
+ -- Check that the gradient is correct by using finite elements
2274
2274
local input = torch .Tensor (unpack (shape )):zero ()
2275
2275
2276
2276
local err = jac .testJacobian (m , input )
@@ -2286,10 +2286,10 @@ function nntest.ConcatTable()
2286
2286
-- Test tensor input
2287
2287
local input = torch .rand (5 , 5 , 5 )
2288
2288
local m = nn .Sequential ()
2289
-
2289
+
2290
2290
local concat = nn .ConcatTable ()
2291
2291
concat :add (nn .Identity ())
2292
-
2292
+
2293
2293
m :add (concat ) -- Output of concat is a table of length 1
2294
2294
m :add (nn .JoinTable (1 )) -- jac needs a tensor tensor output
2295
2295
@@ -2308,7 +2308,7 @@ function nntest.ConcatTable()
2308
2308
torch .randn (3 ,3 ,4 ):float (), torch .randn (3 ,3 ,4 ):float (), torch .randn (3 ,3 ,4 ):float ()
2309
2309
}
2310
2310
local gradOutput = {
2311
- {_gradOutput [1 ][1 ], _gradOutput [2 ][1 ], {_gradOutput [3 ][1 ]}},
2311
+ {_gradOutput [1 ][1 ], _gradOutput [2 ][1 ], {_gradOutput [3 ][1 ]}},
2312
2312
{_gradOutput [1 ][2 ], _gradOutput [2 ][2 ], {_gradOutput [3 ][2 ]}},
2313
2313
{_gradOutput [1 ][3 ], _gradOutput [2 ][3 ], {_gradOutput [3 ][3 ]}}
2314
2314
}
@@ -2317,7 +2317,7 @@ function nntest.ConcatTable()
2317
2317
module :add (nn .Identity ())
2318
2318
module :add (nn .Identity ())
2319
2319
module :float ()
2320
-
2320
+
2321
2321
local output = module :forward (input )
2322
2322
local output2 = {input , input , input }
2323
2323
equal (output2 , output , " ConcatTable table output" )
@@ -2328,7 +2328,7 @@ end
2328
2328
2329
2329
function nntest .FlattenTable ()
2330
2330
-- Create a nested table. Obviously we can't even stochastically test
2331
- -- the space of all possible nested tables (it's infinite), but here is a
2331
+ -- the space of all possible nested tables (it's infinite), but here is a
2332
2332
-- hand-coded one that covers all the cases we need:
2333
2333
local input = {
2334
2334
torch .rand (1 ),
@@ -2380,7 +2380,7 @@ function nntest.FlattenTable()
2380
2380
input [2 ][# (input [2 ])+ 1 ] = torch .rand (5 )
2381
2381
m :forward (input )
2382
2382
mytester :assert (old_input_map ~= m .input_map and old_output ~= m .output )
2383
-
2383
+
2384
2384
-- CASE 3: An element is removed from the input table
2385
2385
old_input_map = m .input_map
2386
2386
old_output = m .output
0 commit comments