Skip to content

test_transpose_forward - ValueError: axes don't match array #24

@NaifAlqahtani

Description

@NaifAlqahtani

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.

tests/hw1/test_autograd_hw.py::test_power_scalar_forward PASSED          [  8%]
tests/hw1/test_autograd_hw.py::test_divide_forward PASSED                [ 16%]
tests/hw1/test_autograd_hw.py::test_divide_scalar_forward PASSED         [ 25%]
tests/hw1/test_autograd_hw.py::test_matmul_forward PASSED                [ 33%]
tests/hw1/test_autograd_hw.py::test_summation_forward PASSED             [ 41%]
tests/hw1/test_autograd_hw.py::test_broadcast_to_forward PASSED          [ 50%]
tests/hw1/test_autograd_hw.py::test_reshape_forward PASSED               [ 58%]
tests/hw1/test_autograd_hw.py::test_negate_forward PASSED                [ 66%]
tests/hw1/test_autograd_hw.py::test_transpose_forward FAILED             [ 75%]
tests/hw1/test_autograd_hw.py::test_log_forward PASSED                   [ 83%]
tests/hw1/test_autograd_hw.py::test_exp_forward PASSED                   [ 91%]
tests/hw1/test_autograd_hw.py::test_ewisepow_forward PASSED              [100%]

error:

    def test_transpose_forward():
        x = ndl.Tensor([[[1.95]], [[2.7]], [[3.75]]])
>       x_T = ndl.transpose(x, axes=(1, 2))
ValueError: axes don't match array

here is the modified test case so each action is done in its own line:

def test_transpose_forward():
    x = ndl.Tensor([[[1.95]], [[2.7]], [[3.75]]])
    x_T = ndl.transpose(x, axes=(1, 2))
    x_T = x_T.numpy()
    res = np.array([[[1.95]], [[2.7]], [[3.75]]])
    np.testing.assert_allclose(x_T, res)

I have tried many solutions, a few of which:

    def compute(self, a):
        ### BEGIN YOUR SOLUTION
        return array_api.transpose(a, axes=self.axes)
        ### END YOUR SOLUTION
    def compute(self, a):
        ### BEGIN YOUR SOLUTION
        if self.axes is None:
            self.axes = (len(a.shape) - 2, len(a.shape) - 1)

        # If the tensor has fewer dimensions than the max axis in self.axes, raise an error
        if max(self.axes) >= len(a.shape):
            raise ValueError(
                "Axes out of bounds for array with shape {}".format(a.shape)
            )

        # If the specified axes are identical or the dimensions are already (1,1), return the input tensor directly
        if a.shape[self.axes[0]] == 1 and a.shape[self.axes[1]] == 1:
            return a

        # Perform the transpose
        return array_api.transpose(a, axes=self.axes)
        ### END YOUR SOLUTION

Any help please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions