-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnngraph_handin.lua
More file actions
41 lines (33 loc) · 944 Bytes
/
Copy pathnngraph_handin.lua
File metadata and controls
41 lines (33 loc) · 944 Bytes
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
require 'nngraph'
-- Seed for nn.Linear weights and random input generators
torch.manualSeed(123)
-- Sizes
size1 = 4
size2 = size1
size3 = 6
-- Nodes for gModule
x1 = nn.Identity()()
x2 = nn.Identity()()
lin_node = nn.Linear(size3, size2)()
mul_node = nn.CMulTable()({x2,lin_node})
add_node = nn.CAddTable()({mul_node, x1})
-- gModule for exercise answer
answer_gmod = nn.gModule({x1, x2, lin_node}, {add_node})
-- Generate inputs
a = torch.rand(size1)
b = torch.rand(size2)
c = torch.rand(size3)
-- Forward propagate the inputs and print answer
print("Testing on some random tensors")
print("Answer using gModule:")
print(answer_gmod:forward({a,b,c}))
-- Calculate the same answer step by step
torch.manualSeed(123)
lin = nn.Linear(size3, size2)
lin_c = lin:forward(c)
cmul = nn.CMulTable()
cadd = nn.CAddTable()
lin_c_cmul_b = cmul:forward({lin_c, b})
ans = cadd:forward({lin_c_cmul_b, a})
print("Answer without gModule:")
print(ans)