-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtt2.lua
55 lines (43 loc) · 1.12 KB
/
tt2.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
require 'kex'
require 'cunn'
kex.cudahacks()
collectgarbage()
torch.setdefaulttensortype('torch.CudaTensor')
--torch.setdefaulttensortype('torch.FloatTensor')
n1=784
n2=784
no=200
m1 = nn.TensorLinear(n1,n2,no)
m2 = nn.TensorLinear(n1,n2,no)
m1.weight:copy(m2.weight)
m1.bias:copy(m2.bias)
in1 = torch.rand(n1)
in2 = torch.rand(n2)
go = torch.rand(no)
m1:updateOutput1({in1,in2})
m2:updateOutput2({in1,in2})
print('fdist ', torch.dist(m1.output,m2.output))
m1:zeroGradParameters()
m2:zeroGradParameters()
m1:accGradParameters1({in1,in2},go)
m2:accGradParameters2({in1,in2},go)
print('bdist ', torch.dist(m1.gradBias,m2.gradBias))
print('wdist ', torch.dist(m1.gradWeight,m2.gradWeight))
t=torch.Timer()
for i=1,100 do
m1:updateOutput1({in1,in2})
m1:zeroGradParameters()
m1:updateGradInput({in1,in2},go)
m1:accGradParameters1({in1,in2},go)
--collectgarbage()
end
print('m1 time ', t:time().real)
t=torch.Timer()
for i=1,100 do
m2:updateOutput2({in1,in2})
m2:zeroGradParameters()
m2:updateGradInput({in1,in2},go)
m2:accGradParameters2({in1,in2},go)
--collectgarbage()
end
print('m2 time ', t:time().real)