-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ce.py
113 lines (94 loc) · 3.44 KB
/
test_ce.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
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
import torch
import NeuralNetwork
#With this block, we don't need to set device=DEVICE for every tensor.
torch.set_default_dtype(torch.float32)
if torch.cuda.is_available():
torch.cuda.set_device(0)
torch.set_default_tensor_type(torch.cuda.FloatTensor)
print("Running on the GPU")
else:
print("Running on the CPU")
DTYPE = torch.float32
def test_ce_1(verbose=False):
x = torch.tensor([[0.998], [0.001], [0.001]], dtype=DTYPE)
nntest = NeuralNetwork.Network(3, 3, loss="ce")
sm = nntest.add_softmax()
actual = torch.tensor([[1], [0],[0]], dtype=DTYPE)
l = nntest.forward(x, actual)
# print(sm.output)
#print(nntest._loss.output)
nntest.backward()
#print("Softmax...", sm.output)
x_b = torch.tensor([[0.998], [0.001], [0.001]], dtype=DTYPE, requires_grad=True)
actual_b = torch.tensor([[1], [0],[0]], dtype=DTYPE)
softmax = torch.div(torch.exp(x_b), torch.exp(x_b).sum(axis=0))
#print(softmax)
ce = (actual_b * torch.log(softmax + 1E-7))
ce = (ce.sum(axis=0) * -1).mean()
softmax.retain_grad()
x_b.retain_grad()
ce.backward()
if verbose:
print("softmax grad", sm.gradient)
print("loss grad", nntest._loss.gradient)
print("Backward cald by torch")
print("softmax grad", x_b.grad)
print("loss grad", softmax.grad)
def test_ce_2(verbose=False):
x = torch.tensor([[1], [0.6], [0.3]], dtype=DTYPE)
nntest = NeuralNetwork.Network(3, 3, loss="ce")
sm = nntest.add_softmax()
actual = torch.tensor([[1], [0],[0]], dtype=DTYPE)
l = nntest.forward(x, actual)
# print(sm.output)
#print(nntest._loss.output)
nntest.backward()
#print("Softmax...", sm.output)
x_b = torch.tensor([[1], [0.6], [0.3]], dtype=DTYPE, requires_grad=True)
actual_b = torch.tensor([[1], [0],[0]], dtype=DTYPE)
softmax = torch.div(torch.exp(x_b), torch.exp(x_b).sum(axis=0))
#print(softmax)
ce = (actual_b * torch.log(softmax + 1E-7))
ce = (ce.sum(axis=0) * -1).mean()
softmax.retain_grad()
x_b.retain_grad()
ce.backward()
if verbose:
print("softmax grad", sm.gradient)
print("loss grad", nntest._loss.gradient)
print("Backward cald by torch")
print("softmax grad", x_b.grad)
print("loss grad", softmax.grad)
def test_ce_3():
"""
THIS WILL NOT WORK, STOP IT
:return:
"""
x = torch.tensor([[1,5], [0.6,2], [0.3,1]], dtype=DTYPE)
nntest = NeuralNetwork.Network(3, 3, loss="ce")
sm = nntest.add_softmax()
actual = torch.tensor([[1], [0],[0]], dtype=DTYPE)
l = nntest.forward(x, actual)
# print(sm.output)
#print(nntest._loss.output)
nntest.backward()
#print("Softmax...", sm.output)
print("softmax grad", sm.gradient)
print("loss grad", nntest._loss.gradient)
x_b = torch.tensor([[1,5], [0.6,2], [0.3,1]], dtype=DTYPE, requires_grad=True)
actual_b = torch.tensor([[1], [0],[0]], dtype=DTYPE)
softmax = torch.div(torch.exp(x_b), torch.exp(x_b).sum(axis=0))
#print(softmax)
ce = (actual_b * torch.log(softmax + 1E-7))
ce = (ce.sum(axis=0) * -1).mean()
softmax.retain_grad()
x_b.retain_grad()
ce.backward()
print("Backward cald by torch")
print("softmax grad", x_b.grad)
print("loss grad", softmax.grad)
if __name__ == '__main__':
# Please VISUALLY check gradients!
test_ce_1(verbose=True)
test_ce_2(verbose=True)
# test_ce_3() THIS TEST DOES NOT WORK - SOFTMAX WILL NOT WORK IN 3D