From 15e3eceb13bbf056db3ae4760493985d04cf1078 Mon Sep 17 00:00:00 2001 From: Yara97Mansour <165589291+Yara97Mansour@users.noreply.github.com> Date: Thu, 9 May 2024 19:27:49 +0300 Subject: [PATCH 1/2] Update engine.py # __pow()__ changed to calculate the derivation of both Exponent and base --- micrograd/engine.py | 1 + 1 file changed, 1 insertion(+) diff --git a/micrograd/engine.py b/micrograd/engine.py index afd82cc5..bcf39848 100644 --- a/micrograd/engine.py +++ b/micrograd/engine.py @@ -32,6 +32,7 @@ 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}') From 8635ccb0f46b59fef2d9c02fb0cb7208f4092cf8 Mon Sep 17 00:00:00 2001 From: Yara97Mansour <165589291+Yara97Mansour@users.noreply.github.com> Date: Thu, 9 May 2024 22:01:11 +0300 Subject: [PATCH 2/2] Update engine.py __pow()__ has been changed --- micrograd/engine.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/micrograd/engine.py b/micrograd/engine.py index bcf39848..8de7c792 100644 --- a/micrograd/engine.py +++ b/micrograd/engine.py @@ -1,3 +1,4 @@ +import math class Value: """ stores a single scalar value and its gradient """ @@ -34,11 +35,12 @@ def _backward(): # __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