def test_transpose_backward(): shape = (2, 3, 4) p = (2, 0, 1) x0 = np.arange(np.prod(shape), dtype=np.float32).reshape(*shape) shapet = tuple(shape[i] for i in p) x = C.input_variable(shape, needs_gradient=True) y = C.reduce_sum(C.cos(C.transpose(x, p))) xt = C.input_variable(shapet, needs_gradient=True) yt = C.reduce_sum(C.cos(xt)) g = np.squeeze(y.grad({x: x0})) gt = np.squeeze(yt.grad({xt: np.transpose(x0, p)})) assert np.allclose(np.transpose(g, p), gt)
def test_transpose_backward(): shape = (2, 3, 4) p = (2, 0, 1) x0 = np.arange(np.prod(shape), dtype=np.float32).reshape(*shape) shapet = tuple(shape[i] for i in p) x = C.input_variable(shape, needs_gradient=True) y = C.reduce_sum(C.cos(C.transpose(x, p))) xt = C.input_variable(shapet, needs_gradient=True) yt = C.reduce_sum(C.cos(xt)) g = np.squeeze(y.grad({x:x0})) gt = np.squeeze(yt.grad({xt:np.transpose(x0, p)})) assert np.allclose(np.transpose(g, p), gt)
def test_Cos(tmpdir, dtype): data = np.asarray([0.0, -0.5, 0.5, 10, 20], dtype) model = C.cos(data) verify_no_input(model, tmpdir, 'Cos_0')