Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions micrograd/engine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math

class Value:
""" stores a single scalar value and its gradient """
Expand Down Expand Up @@ -32,12 +33,14 @@ def _backward():

return out

# __pow()__ changed to calculate the derivation of both Exponent and base
def __pow__(self, other):
assert isinstance(other, (int, float)), "only supporting int/float powers for now"
out = Value(self.data**other, (self,), f'**{other}')
other = other if isinstance(other, Value) else Value(other)
out = Value(self.data**other.data, (self,other), '**')

def _backward():
self.grad += (other * self.data**(other-1)) * out.grad
self.grad += (other.data * self.data**((other.data)-1)) * out.grad
other.grad += (math.log(self.data)) * (self.data**other.data) * out.grad
out._backward = _backward

return out
Expand Down