def test_transpose(x, data): axes = data.draw( valid_axes(x.ndim, min_dim=x.ndim, max_dim=x.ndim).map( lambda out: (out, ) if isinstance(out, int) else out), label="axes", ) x_arr = Tensor(np.copy(x)) o = transpose(x_arr, axes, constant=False) grad = data.draw( hnp.arrays(shape=o.shape, dtype=float, elements=st.floats(1, 10), unique=True), label="grad", ) o.backward(grad) def f(x): return np.transpose(x, axes) assert_allclose(o.data, f(x)) dx, = numerical_gradient_full(f, x, back_grad=grad) assert_allclose(x_arr.grad, dx) out = transpose(x, constant=True) assert out.constant and not x_arr.constant
def test_transpose(x, data): axes = data.draw(valid_axes(x.ndim), label="axes") if axes is not None: assume(len(axes) == x.ndim) x_arr = Tensor(np.copy(x)) o = transpose(x_arr, axes, constant=False) grad = data.draw( hnp.arrays(shape=o.shape, dtype=float, elements=st.floats(1, 10), unique=True), label="grad", ) o.backward(grad) def f(x): return np.transpose(x, axes) assert_allclose(o.data, f(x)) dx, = numerical_gradient_full(f, x, back_grad=grad) assert_allclose(x_arr.grad, dx) out = transpose(x, constant=True) assert out.constant and not x_arr.constant
def test_input_validation(): x = Tensor([[1, 2]]) with raises(TypeError): transpose(x, (0, ), 1)