-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.py
More file actions
170 lines (129 loc) · 5.29 KB
/
Copy pathkernel.py
File metadata and controls
170 lines (129 loc) · 5.29 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import math
class Value:
def __init__(self,data,label="",pre=[]):
self.data = float(data)
self.label = label
self.pre = pre
self.grad = 0
self._backward = lambda: None
def show(self):
if self.label != "":
if len(self.label) > 2 and self.label[0] == "(" and self.label[-1] == ")":
return f"Value({self.label[1:-1]}: {self.data} | grad: {self.grad})"
else:
return f"Value({self.label}: {self.data} | grad: {self.grad})"
else:
return f"Value({self.data} | grad: {self.grad})"
def __repr__(self):
return f"Value({self.data} | grad: {self.grad})"
#反向传播
def backward(self):
visited = set()
topo = []
#拓扑排序收集各个节点
def build_topo(node):
if node not in visited:
visited.add(node)
for pre in node.pre:
build_topo(pre)
topo.append(node)
build_topo(self)
#开始反向传播
self.grad = 1
for node in reversed(topo):
node._backward()
#实现-a
def __neg__(self):
out = Value(-self.data,label=f"-{self.label}",pre=[self])
def _backward():
self.grad += -out.grad
out._backward = _backward
return out
#实现a + b/a + 1
def __add__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(self.data + other.data,label=f"({self.label}+{other.label})",pre=[self,other])
def _backward():
self.grad += out.grad
other.grad += out.grad
out._backward = _backward
return out
#实现1 + a
def __radd__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(self.data + other.data,label=f"({other.label}+{self.label})",pre=[self,other])
def _backward():
self.grad += out.grad
other.grad += out.grad
out._backward = _backward
return out
#实现a - b/a - 1
def __sub__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(self.data - other.data,label=f"({self.label}-{other.label})",pre=[self,other])
def _backward():
self.grad += out.grad
other.grad += -out.grad
out._backward = _backward
return out
#实现1 - a
def __rsub__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(other.data - self.data,label=f"({other.label}-{self.label})",pre=[self,other])
def _backward():
self.grad += -out.grad
other.grad += out.grad
out._backward = _backward
return out
#实现a * b/a * 2
def __mul__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(self.data * other.data,label=f"({self.label}*{other.label})",pre=[self,other])
def _backward():
self.grad += other.data * out.grad
other.grad += self.data * out.grad
out._backward = _backward
return out
#实现2 * a
def __rmul__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(other.data * self.data,label=f"({other.label}*{self.label})",pre=[self,other])
def _backward():
self.grad += other.data * out.grad
other.grad += self.data * out.grad
out._backward = _backward
return out
#实现a / b/a / 2
def __truediv__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(self.data / other.data,label=f"({self.label}/{other.label})",pre=[self,other])
def _backward():
self.grad += (1 / other.data) * out.grad
other.grad += -self.data * ((1 / other.data)**2) * out.grad
out._backward = _backward
return out
#实现2 / a
def __rtruediv__(self,other):
other = other if isinstance(other,Value) else Value(other,label=str(other))
out = Value(other.data / self.data,label=f"({other.label}/{self.label})",pre=[self,other])
def _backward():
other.grad += (1 / self.data) * out.grad
self.grad += -other.data * ((1 / self.data)**2) * out.grad
out._backward = _backward
return out
#实现a ** b/a ** 2
def __pow__(self,other):
assert isinstance(other, (int, float)), "仅支持int/float作为幂次"
out = Value(self.data ** other,label=f"({self.label}**{other})",pre=[self])
def _backward():
self.grad += other * (self.data ** (other - 1)) * out.grad
out._backward = _backward
return out
#实现ReLU激活函数
def ReLU(self):
out = Value(0 if self.data < 0 else self.data,label=f"ReLU({self.label})",pre=[self])
def _backward():
if self.data > 0:
self.grad += out.grad
out._backward = _backward
return out