def test_shape(self): x = Variable(np.random.randn(1, 2, 3)) y = x.reshape((6, )) y.backward(retain_grad=True) self.assertEqual(x.shape, x.grad.shape) self.assertEqual(y.shape, (6, )) a = Variable(np.array([[1, 2, 3], [4, 5, 6]])) b = F.transpose(a) c = a.T b.backward() self.assertEqual(a.grad.shape, a.shape) self.assertEqual(b.shape, c.shape)
import os, sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np from dezero import Variable import dezero.functions as F x = Variable(np.array([[0, 1, 2], [3, 4, 5]])) y = F.reshape(x, (6, )) # or y = x.reshape(6) y.backward(retain_grad=True) print(y.grad) x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.transpose(x) # or y = x.T y.backward() print(x.grad)
import os, sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np from dezero import Variable import dezero.functions as F x = Variable(np.array([[0, 1, 2], [3, 4, 5]])) y = F.reshape(x, (6, )) # y = x.reshape(6) y.backward(retain_grad=True) print(x.grad) x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.transpose(x) # y = x.T y.backward() print(x.grad)
def test_backward(self): x = Variable(np.arange(6).reshape(2, 3)) y = transpose(x) y.backward() self.assertEqual(x.grad.shape, x.shape)
def test_forward(self): x = Variable(np.arange(6).reshape(2, 3)) y = transpose(x) self.assertEqual(y.shape, (3, 2))
import dezero.functions as F x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.reshape(x, (6,)) y.backward(retain_grad=True) print(x.grad) #args feature test #def res(*shape): # if len(shape)==1 :print(shape, shape[0]) #res((1, 0)) #res([1, 0]) #res(1, 0) x1 = Variable(np.array([[1, 2, 3], [4, 5, 6]])) print(x1.grad) y1 = F.transpose(x1) y1.backward(retain_grad=True) print(y1.grad) print(x1.grad) #x = Variable(np.random.rand(2, 3)) #print(x) #x = x.transpose() #print(x) #x = x.T #print(x)
def test_forward1(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.transpose(x) self.assertEqual(y.shape, (3, 2))
def test_transpose_backward(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.transpose(x) y.backward(create_graph=True) assert x.grad.shape == (2, 3)
def test_transpose_forward(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.transpose(x) assert y.shape == (3, 2)
def test_transpose(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = transpose(x) y.backward() assert_array_equal(y.data, np.transpose(x.data)) assert_array_equal(x.grad.data, np.array([[1, 1, 1], [1, 1, 1]]))