Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit __pow()__ to calculate the derivation of both Exponent and base. #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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