From ee40e4bd81fa101b9f44d427133147ecdf9ba830 Mon Sep 17 00:00:00 2001 From: Max Masterton Date: Tue, 29 Jul 2025 13:39:31 +0100 Subject: [PATCH 1/2] Exp, Log and Softmax operations Maxes it possible to use softmax with micrograd --- micrograd/engine.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/micrograd/engine.py b/micrograd/engine.py index afd82cc5..936eeae1 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 / x + 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 From 9be1bc54c50e0ed2ccae7d81cc8fa1719bff9504 Mon Sep 17 00:00:00 2001 From: Max Masterton Date: Thu, 25 Sep 2025 18:37:58 +0100 Subject: [PATCH 2/2] Fixed bug with _backward in log --- micrograd/engine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micrograd/engine.py b/micrograd/engine.py index 936eeae1..9827a4cb 100644 --- a/micrograd/engine.py +++ b/micrograd/engine.py @@ -67,13 +67,13 @@ def log(self): out = Value(x, (self,)) def _backward(): - self.grad += out.grad / x + self.grad += out.grad / self.data out._backward = _backward return out @staticmethod - def softmax(logits): + def softmax(*logits): counts = tuple(logit.exp() for logit in logits) total = sum(counts) return tuple(count / total for count in counts)