fix subtraction broadcasted access memory wrong values

This commit is contained in:
lucasdelimanogueira 2024-05-18 08:54:43 -03:00
parent 13da54ca39
commit 1094a9c1cd
2 changed files with 3 additions and 2 deletions

View file

@ -316,6 +316,8 @@ class Tensor:
if isinstance(other, (int, float)):
other = other * self.ones_like()
broadcasted_shape = []
# Function to determine if broadcasting is needed and get the broadcasted shape
def broadcast_shape(shape1, shape2):
if shape1 == shape2:
@ -325,7 +327,6 @@ class Tensor:
shape1 = [1] * (max_len - len(shape1)) + shape1
shape2 = [1] * (max_len - len(shape2)) + shape2
broadcasted_shape = []
for dim1, dim2 in zip(shape1, shape2):
if dim1 != dim2 and dim1 != 1 and dim2 != 1:
raise ValueError("Shapes are not compatible for broadcasting")

View file

@ -9,7 +9,7 @@ W2 = norch.Tensor([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]], requires_
B2 = norch.Tensor([[1, 2, 3, 4, 5]], requires_grad=True)
Z1 = W1 @ X + B1
Z1 = W1 @ X - B1
# Perform matrix multiplication