-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
54 lines (40 loc) · 824 Bytes
/
test.py
File metadata and controls
54 lines (40 loc) · 824 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
42
43
44
45
46
47
48
49
50
51
52
53
54
from torch import tensor, ones
from torch import sigmoid
from torch import pow, sum
inp = [
[.2, .3 ,.1],
[.1, .4, .7],
[.1, .6, .4],
]
w1 = [
[.1, .6 ,.2],
[.7, -.2, -.1],
[.2, .7, .1],
]
w2 = [
[.3, .1 ,.01],
[-.1, -.1, -.2],
[-.3, .1, .2],
]
target = ones(3,3) * 3
inp = tensor(inp,requires_grad=True)
w1 = tensor(w1,requires_grad=True)
w2 = tensor(w2,requires_grad=True)
target = tensor(target)
out1 = inp @ w1
# sum(out1).backward(retain_graph=True)
# print(w1.grad)
# w1.grad = None
out1 = sigmoid(out1)
# sum(out1).backward(retain_graph=True)
# print(w1.grad)
# w1.grad = None
out2 = out1 @ w2
# sum(out2).backward(retain_graph=True)
# print(w2.grad)
# w1.grad = None
# w2.grad = None
loss = pow(target-out2,2)
sum(loss).backward()
print(inp.grad)
#print(loss)