-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.py
145 lines (108 loc) · 4.21 KB
/
nn.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import torch
import copy
class Basic:
def __init__(self):
self.w = None
self.b = None
self.forward_value = None
self.w_history_grad = None
self.b_history_grad = None
def forward(self, input):
pass
def backward(self, input):
pass
def zero_grad(self):
pass
def step(self, lr=0.001):
pass
class Sigmoid(Basic):
def __init__(self):
super(Sigmoid, self).__init__()
def forward(self, input):
out = 1 / (1 + torch.exp(-input))
self.forward_value = copy.deepcopy(out.detach().clone())
return out
def backward(self, input):
return input * self.forward_value.transpose(1, 2) * (1 - self.forward_value.transpose(1, 2))
class Tanh(Basic):
def __init__(self):
super(Tanh, self).__init__()
def forward(self, input):
out = (1 - torch.exp(-2 * input)) / (1 + torch.exp(-2 * input))
self.forward_value = copy.deepcopy(out.detach().clone())
return out
def backward(self, input):
return input * (1 - self.forward_value.transpose(1, 2) * self.forward_value.transpose(1, 2))
class Linear(Basic):
def __init__(self, input, output, bias=True):
super(Linear, self).__init__()
assert type(input) is int
assert type(output) is int
self.w = torch.rand([output, input], dtype=torch.float32, requires_grad=True)
self.w_history_grad = torch.zeros_like(self.w)
if bias:
self.b = torch.rand([output, 1], dtype=torch.float32, requires_grad=True)
self.b_history_grad = torch.zeros_like(self.b)
def zero_grad(self):
self.w.grad = torch.zeros_like(self.w)
if self.b is not None:
self.b.grad = torch.zeros_like(self.b)
def forward(self, input):
self.forward_value = copy.deepcopy(input.detach().clone())
self.batchsize, self.input_channel = input.shape[0], input.shape[1]
w = self.w.repeat(self.batchsize, 1, 1)
if self.b is not None:
b = self.b.repeat(self.batchsize, 1, 1)
out = torch.bmm(w, input)
if self.b is not None:
return out + b
else:
return out
def backward(self, input=None):
self.w.grad = torch.bmm(input.transpose(1, 2), self.forward_value.transpose(1, 2)).mean(0) + self.w.grad
if self.b is not None:
self.b.grad = input.mean(0).T
return torch.bmm(input, self.w.repeat(self.batchsize, 1, 1))
def step(self, lr=0.001):
self.w.data -= lr * self.w.grad
self.w_history_grad = self.w.grad
if self.b is not None:
self.b.data -= lr * self.b.grad
self.b_history_grad = self.b.grad
class Sequential:
def __init__(self, input=[]):
self.model_list = input
def forward(self, input):
x = input
for i in range(len(self.model_list)):
x = self.model_list[i].forward(x)
return x
def step(self, lr=0.001):
for i in range(len(self.model_list)):
self.model_list[i].step(lr)
def zero_grad(self):
for i in range(len(self.model_list)):
self.model_list[i].zero_grad()
def backward(self, input=None):
x = input
for i in reversed(range(len(self.model_list))):
x = self.model_list[i].backward(x)
class L2loss(Basic):
def __init__(self, net: Basic):
super(L2loss, self).__init__()
self.net = net
def forward(self, input, label):
self.forward_value = (input - label.reshape(input.shape)).transpose(1, 2)
return ((input - label) ** 2).mean()
def backward(self, input=None):
return self.net.backward(self.forward_value)
class CrossEntropy(L2loss):
def __init__(self, net: Basic):
super(CrossEntropy, self).__init__(net)
def forward(self, input, label):
self.forward_value = (-label.reshape(input.shape) * 1 / input + (1 - label.reshape(input.shape)) * 1 / (
1 - input)).transpose(1, 2)
return (-label.reshape(input.shape) * torch.log(input) - (1 - label.reshape(input.shape)) * torch.log(
1 - input)).mean()
def backward(self, input=None):
super(CrossEntropy, self).backward()