From 11682c11eddee591b76a4bdd4756c7e70e91b586 Mon Sep 17 00:00:00 2001 From: ProfiDE Date: Tue, 16 Jun 2026 17:02:35 +0330 Subject: [PATCH] fix: correct gradient calculation in __truediv__ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The derivative of x/y with respect to y is -x/y², but the implementation was missing the negative sign, causing incorrect gradient propagation during backpropagation. Changes: - Added negative sign to other.grad calculation - Simplified expression using direct division instead of exponent - Improved readability with explicit derivative formulas Fixes incorrect gradient flow in division operations. --- bitgrad/grad.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bitgrad/grad.py b/bitgrad/grad.py index 8f2b258..d566dd0 100644 --- a/bitgrad/grad.py +++ b/bitgrad/grad.py @@ -48,13 +48,13 @@ def _backward(): return out - def __truediv__(self,other): + def __truediv__(self, other): other = other if isinstance(other, Value) else Value(other) - out = Value(self.data * other.data**(-1.0), (self, other), 'truediv') + out = Value(self.data / other.data, (self, other), 'truediv') def _backward(): - self.grad += other.data**(-1.0) * out.grad - other.grad += self.data * out.grad + self.grad += (1.0 / other.data) * out.grad + other.grad += (-self.data / (other.data ** 2)) * out.grad out._backward = _backward return out