-
Notifications
You must be signed in to change notification settings - Fork 232
/
test.lua
135 lines (110 loc) · 3.48 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
#! /usr/bin/env luajit
require 'Test'
require 'cutorch'
require 'cunn'
require 'cudnn'
require 'libadcensus'
include('Margin2.lua')
include('StereoJoin1.lua')
include('StereoJoin.lua')
include('Normalize.lua')
function test_StereoJoin1()
print('test_StereoJoin1')
input_ = torch.Tensor(6, 4, 5, 6):normal()
-- forward
output_ = torch.Tensor(input_:size(1) / 2, 1, input_:size(3), input_:size(4))
for dim1 = 1, output_:size(1) do
for dim3 = 1, output_:size(3) do
for dim4 = 1, output_:size(4) do
local sum = 0
for dim2_ = 1, input_:size(2) do
left = input_[{dim1 * 2 - 1,dim2_,dim3,dim4}]
right = input_[{dim1 * 2,dim2_,dim3,dim4}]
sum = sum + left * right
end
output_[{dim1,1,dim3,dim4}] = sum
end
end
end
module = nn.Sequential()
module:add(nn.StereoJoin1(op))
module:cuda()
module:forward(input_:cuda())
print(output_:add(-1, module.output:double()):abs():max())
-- backward
print(testJacobian(module, input_:cuda()))
end
function test_StereoJoin()
print('test_StereoJoin')
input_ = torch.Tensor(2, 32, 10, 20):normal()
output_ = torch.Tensor(1, 16, 10, 20):zero()
for dim2 = 1, output_:size(2) do
for dim3 = 1, output_:size(3) do
for dim4 = 1, output_:size(4) do
if dim4 - dim2 + 1 <= 0 then
output_[{1,dim2,dim3,dim4}] = 0 / 0
else
sum = 0.0
for dim2_ = 1, input_:size(2) do
left = input_[{1,dim2_,dim3,dim4}]
right = input_[{2,dim2_,dim3,dim4 - dim2 + 1}]
sum = sum + left * right
end
output_[{1,dim2,dim3,dim4}] = sum
end
end
end
end
module = nn.StereoJoin(output_:size(2)):cuda()
module:forward(input_:cuda())
print(output_:add(-1, module.output:double()):abs():max())
end
test_StereoJoin()
function test_Normalize()
print('test_Normalize')
input_ = torch.Tensor(2, 3, 4, 5):normal()
output_ = torch.Tensor(2, 3, 4, 5):normal()
norm_ = torch.Tensor(2, 1, 4, 5):zero()
-- forward
for dim1 = 1, output_:size(1) do
for dim3 = 1, output_:size(3) do
for dim4 = 1, output_:size(4) do
sum = 0.0
for dim2 = 1, output_:size(2) do
x = input_[{dim1,dim2,dim3,dim4}]
sum = sum + x * x
end
norm_[{dim1,1,dim3,dim4}] = sum
for dim2 = 1, output_:size(2) do
output_[{dim1,dim2,dim3,dim4}] = input_[{dim1,dim2,dim3,dim4}] / math.sqrt(sum)
end
end
end
end
module = nn.Normalize():cuda()
module:forward(input_:cuda())
print(norm_:add(-1, module.norm:double()):abs():max())
print(output_:add(-1, module.output:double()):abs():max())
-- backward
print(testJacobian(module, input_:cuda()))
end
function test_Margin2()
print('test_Margin2')
margin = 0.1
pow = 2
input_ = torch.Tensor(64, 1, 1, 1):uniform()
tmp_ = torch.Tensor(32, 1, 1, 1):zero()
-- forward
for dim1 = 1, tmp_:size(1) do
d = math.max(0, input_[{dim1 * 2,1,1,1}] - input_[{dim1 * 2 - 1,1,1,1}] + margin)
if pow == 2 then
d = d * d * 0.5
end
tmp_[dim1] = d
end
module = nn.Margin2(margin, pow):cuda()
module:forward(input_:cuda(), nil)
print(tmp_:add(-1, module.tmp:double()):abs():max())
-- backward
print(testCriterion(module, input_:cuda(), nil))
end