-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test_transpose_forward - ValueError: axes don't match array #24
Comments
Found a fix, for those running into this problem in the future. Basically, all chatGPT's attempts failed 😂... I copied the exact same prompt to claude 3.5 sonnet and it produced the correct solutions on the second try. Claude is GOAT of programming LLMS.
x = ndl.Tensor([[[1.95]], [[2.7]], [[3.75]]]) # Shape is (3, 1, 1)
x_T = ndl.transpose(x, axes=(1, 2)) # Trying to transpose axes 1 and 2
class Transpose(TensorOp):
"""Reverses the order of two axes (axis1, axis2), defaults to the last two axes
Args:
axes: Optional tuple specifying the axes to transpose. If None, defaults to
transposing the last two axes.
"""
def __init__(self, axes: Optional[tuple] = None):
self.axes = axes
def compute(self, a):
### BEGIN YOUR SOLUTION
# If axes not specified, transpose last two dimensions
ndim = len(a.shape)
if self.axes is None:
if ndim <= 1:
return a
self.axes = (ndim-2, ndim-1)
# Create the full permutation
# Start with identity permutation: [0, 1, 2, ..., n-1]
perm = list(range(ndim))
# Swap the specified axes
axis1, axis2 = self.axes
# Handle negative indices
if axis1 < 0:
axis1 += ndim
if axis2 < 0:
axis2 += ndim
# Validate axes
if not (0 <= axis1 < ndim and 0 <= axis2 < ndim):
raise ValueError("axes out of bounds")
# Swap the positions in permutation
perm[axis1], perm[axis2] = perm[axis2], perm[axis1]
return array_api.transpose(a, axes=tuple(perm))
### END YOUR SOLUTION
|
Hi, Naif. I'm also taking this course. I found that in numpy's manual, class Transpose(TensorOp):
def __init__(self, axes: Optional[tuple] = None):
self.axes = axes
def compute(self, a):
if self.axes:
swap_axes = {self.axes[0]: self.axes[1], self.axes[1]: self.axes[0]}
else:
swap_axes = {a.ndim - 1: a.ndim - 2, a.ndim - 2: a.ndim - 1}
permutation = [swap_axes[i] if i in swap_axes else i for i in range(a.ndim)]
return array_api.transpose(a, axes=permutation) |
Hi all,
Thanks for making the course public.
I struggled to find any forums for this course online so I'm resorting to creating an issue here as a last ditch effort instead of getting in touch with the course instructors/TAs directly.
I can't for the life of me figure out why the test fails.
error:
here is the modified test case so each action is done in its own line:
I have tried many solutions, a few of which:
Any help please?
The text was updated successfully, but these errors were encountered: