diff --git a/micrograd/engine.py b/micrograd/engine.py index afd82cc5..9827a4cb 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 """ @@ -51,6 +52,32 @@ def _backward(): return out + def exp(self): + x = math.exp(self.data) + out = Value(x, (self,)) + + def _backward(): + self.grad += x * out.grad + out._backward = _backward + + return out + + def log(self): + x = math.log(self.data) + out = Value(x, (self,)) + + def _backward(): + self.grad += out.grad / self.data + out._backward = _backward + + return out + + @staticmethod + def softmax(*logits): + counts = tuple(logit.exp() for logit in logits) + total = sum(counts) + return tuple(count / total for count in counts) + def backward(self): # topological order all of the children in the graph