repositori / bagas / micrograd

micrograd public ready

https://github.com/karpathy/micrograd commit 7bc720e 5 berkas · 10 potongan 2026-11-21T15:02:58Z
5berkas
4berkas python
10potongan
python 4 markdown 1
micrograd/engine.pypython 99 baris ditampilkan
1
2
class Value:
3
""" stores a single scalar value and its gradient """
4
5
def __init__(self, data, _children=(), _op=''):
6
self.data = data
7
self.grad = 0
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
12
13
def __add__(self, other):
14
other = other if isinstance(other, Value) else Value(other)
15
out = Value(self.data + other.data, (self, other), '+')
16
17
def _backward():
18
self.grad += out.grad
19
other.grad += out.grad
20
out._backward = _backward
21
22
return out
23
24
def __mul__(self, other):
25
other = other if isinstance(other, Value) else Value(other)
26
out = Value(self.data * other.data, (self, other), '*')
27
28
def _backward():
29
self.grad += other.data * out.grad
30
other.grad += self.data * out.grad
31
out._backward = _backward
32
33
return out
34
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}')
38
39
def _backward():
40
self.grad += (other * self.data**(other-1)) * out.grad
41
out._backward = _backward
42
43
return out
44
45
def relu(self):
46
out = Value(0 if self.data < 0 else self.data, (self,), 'ReLU')
42
43
return out
44
45
def relu(self):
46
out = Value(0 if self.data < 0 else self.data, (self,), 'ReLU')
47
48
def _backward():
49
self.grad += (out.data > 0) * out.grad
50
out._backward = _backward
51
52
return out
53
54
def backward(self):
55
56
# topological order all of the children in the graph
57
topo = []
58
visited = set()
59
def build_topo(v):
60
if v not in visited:
61
visited.add(v)
62
for child in v._prev:
63
build_topo(child)
64
topo.append(v)
65
build_topo(self)
66
67
# go one variable at a time and apply the chain rule to get its gradient
68
self.grad = 1
69
for v in reversed(topo):
70
v._backward()
71
72
def __neg__(self): # -self
73
return self * -1
74
75
def __radd__(self, other): # other + self
76
return self + other
77
78
def __sub__(self, other): # self - other
79
return self + (-other)
80
81
def __rsub__(self, other): # other - self
82
return other + (-self)
83
84
def __rmul__(self, other): # other * self
85
return self * other
86
87
def __truediv__(self, other): # self / other
88
return self * other**-1
89
90
def __rtruediv__(self, other): # other / self
91
return other * self**-1
92
93
def __repr__(self):
94
return f"Value(data={self.data}, grad={self.grad})"