rohitg00/ai-engineering-from-scratch · error · ValueError
Cannot add shapes {self.shape} and {other.shape}
Error message
Cannot add shapes {self.shape} and {other.shape} What it means
Error "Cannot add shapes {self.shape} and {other.shape}" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py:72
def __add__(self, other):
if isinstance(other, Matrix):
if other.shape == self.shape:
return Matrix([
[self.data[i][j] + other.data[i][j] for j in range(self.cols)]
for i in range(self.rows)
])
if other.rows == 1 and other.cols == self.cols:
return Matrix([
[self.data[i][j] + other.data[0][j] for j in range(self.cols)]
for i in range(self.rows)
])
if other.cols == 1 and other.rows == self.rows:
return Matrix([
[self.data[i][j] + other.data[i][0] for j in range(self.cols)]
for i in range(self.rows)
])
raise ValueError(f"Cannot add shapes {self.shape} and {other.shape}")
def __sub__(self, other):
return Matrix([
[self.data[i][j] - other.data[i][j] for j in range(self.cols)]
for i in range(self.rows)
])
def scalar_multiply(self, scalar):
return Matrix([
[self.data[i][j] * scalar for j in range(self.cols)]
for i in range(self.rows)
])
def element_wise_multiply(self, other):
return Matrix([
[self.data[i][j] * other.data[i][j] for j in range(self.cols)]
for i in range(self.rows)
])View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/01-math-foundations/02-vectors-matrices-operations/code/matrices.py:72 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/aa1573a450a56961.
Report an issue: GitHub.