Fix autograd issue not backpropagating

This commit is contained in:
lucasdelimanogueira 2024-05-01 00:20:11 -03:00
parent f50ce6adf6
commit cec30cb99e
5 changed files with 20 additions and 24 deletions

View file

@ -1,20 +1,20 @@
class AddBackward:
def __init__(self, x, y):
self.tensors = [x, y]
self.input = [x, y]
def backward(self, gradient):
return [gradient, gradient]
class SubBackward:
def __init__(self, x, y):
self.tensors = [x, y]
self.input = [x, y]
def backward(self, gradient):
return [gradient, -gradient]
class ScalarMulBackward:
def __init__(self, x, scalar):
self.tensors = [x]
self.input = [x]
self.scalar = scalar
def backward(self, gradient):
@ -23,17 +23,17 @@ class ScalarMulBackward:
class ElementwiseMulBackward:
def __init__(self, x, y):
self.tensors = [x, y]
self.input = [x, y]
def backward(self, gradient):
return [gradient * self.tensors[1], gradient * self.tensors[0]]
return [gradient * self.input[1], gradient * self.input[0]]
class SumBackward:
def __init__(self, x):
self.tensor = x
self.input = [x]
def backward(self, gradient):
# Since sum reduces a tensor to a scalar, gradient is broadcasted to match the original shape.
return self.tensor.ones_like() * float(gradient.tensor.contents.data.value)
return [float(gradient.tensor.contents.data[0]) * self.input[0].ones_like()]

View file

@ -133,27 +133,22 @@ class Tensor:
return
if gradient is None:
if self.shape != [1]:
if self.shape == [1]:
gradient = Tensor([1])
else:
raise RuntimeError("Gradient argument must be specified for non-scalar tensors.")
gradient = self.ones_like()
if self.shape != [1]:
raise RuntimeError("Only scalar tensors can be used to trigger backward propagation.")
if self.grad is None:
self.grad = gradient
else:
self.grad += gradient
if self.grad_fn is not None:
if self.grad_fn is not None: # not a leaf
grads = self.grad_fn.backward(gradient)
if len(grads) == 1:
self.grad = grads[0]
else:
for tensor, grad in zip(self.grad_fn.tensors, grads):
tensor.backward(grad)
for tensor, grad in zip(self.grad_fn.input, grads):
tensor.backward(grad)
def zero_grad(self):
self.grad = None

View file

@ -29,10 +29,11 @@ if __name__ == "__main__":
#d = b-c
c = a.sum()
c.backward()
print(a.grad)
print(b.grad)
c = a*b
d = c.sum()
d.backward()
print(c.grad)
#print(a ** 2)
"""#print(a)