diff --git a/micrograd/engine.py b/micrograd/engine.py index afd82cc5..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 """ @@ -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