-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_zero.py
44 lines (37 loc) · 1.48 KB
/
test_zero.py
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
import torch
import NeuralNetwork
def try_gpu(i=0): #@save
"""Return gpu(i) if exists, otherwise return cpu().
https://d2l.ai/chapter_deep-learning-computation/use-gpu.html
"""
if torch.cuda.device_count() >= i + 1:
return torch.device(f'cuda:{i}')
return torch.device('cpu')
DEVICE=try_gpu()
DTYPE = torch.float32
def test_gradients_doesnt_crash():
x = torch.tensor([[1], [3], [2]], device=DEVICE, dtype=DTYPE)
w1 = torch.tensor([[-1, 1, .5], [2, 1, -1], [-2, 2, 1]], device=DEVICE, dtype=DTYPE)
b1 = torch.tensor([[1], [-5], [3]], device=DEVICE, dtype=DTYPE)
w2 = torch.tensor([[1, -1, 1], [0, -1, 0]], device=DEVICE, dtype=DTYPE)
b2 = torch.tensor([[-5], [2]], device=DEVICE, dtype=DTYPE)
nntest = NeuralNetwork.Network(3, 2, DEVICE)
actual = torch.tensor([[9], [3]], device=DEVICE, dtype=DTYPE)
lin1 = nntest.add_linear(w1, b1)
lin2 = nntest.add_linear(w2, b2)
l = nntest.forward(x, actual)
nntest.backward()
nntest.step()
nntest.zero_grad()
w1_zero = torch.zeros(w1.shape)
w2_zero = torch.zeros(w2.shape)
b1_zero = torch.zeros(b1.shape)
b2_zero = torch.zeros(b2.shape)
x_zero = torch.zeros(x.shape)
assert torch.all(lin1.W.gradient == w1_zero)
assert torch.all(lin2.W.gradient == w2_zero)
assert torch.all(lin1.b.gradient == b1_zero)
assert torch.all(lin2.b.gradient == b2_zero)
assert torch.all(lin2.gradient == x_zero)
if __name__ == '__main__':
test_gradients_doesnt_crash()