3
""" stores a single scalar value and its gradient """
5
def __init__(self, data, _children=(), _op=''):
8
# internal variables used for autograd graph construction
9
self._backward = lambda: None
10
self._prev = set(_children)
11
self._op = _op # the op that produced this node, for graphviz / debugging / etc
13
def __add__(self, other):
14
other = other if isinstance(other, Value) else Value(other)
15
out = Value(self.data + other.data, (self, other), '+')
19
other.grad += out.grad
20
out._backward = _backward
24
def __mul__(self, other):
25
other = other if isinstance(other, Value) else Value(other)
26
out = Value(self.data * other.data, (self, other), '*')
29
self.grad += other.data * out.grad
30
other.grad += self.data * out.grad
31
out._backward = _backward
35
def __pow__(self, other):
36
assert isinstance(other, (int, float)), "only supporting int/float powers for now"
37
out = Value(self.data**other, (self,), f'**{other}')
40
self.grad += (other * self.data**(other-1)) * out.grad
41
out._backward = _backward
46
out = Value(0 if self.data < 0 else self.data, (self,), 'ReLU')
46
out = Value(0 if self.data < 0 else self.data, (self,), 'ReLU')
49
self.grad += (out.data > 0) * out.grad
50
out._backward = _backward
56
# topological order all of the children in the graph
67
# go one variable at a time and apply the chain rule to get its gradient
69
for v in reversed(topo):
72
def __neg__(self): # -self
75
def __radd__(self, other): # other + self
78
def __sub__(self, other): # self - other
79
return self + (-other)
81
def __rsub__(self, other): # other - self
82
return other + (-self)
84
def __rmul__(self, other): # other * self
87
def __truediv__(self, other): # self / other
88
return self * other**-1
90
def __rtruediv__(self, other): # other / self
91
return other * self**-1
94
return f"Value(data={self.data}, grad={self.grad})"