def test_forward(self): x = Variable(np.arange(12).reshape(3, 4)) y = sum_to(x, (1, 4)) assert_equal(y.data, np.array([[12, 15, 18, 21]]))
def test_backward(self): x = Variable(np.arange(12).reshape(3, 4)) y = sum_to(x, (1, 4)) y.backward() assert_equal(x.grad.data, np.ones((3, 4)))
def test_backward3(self): x_data = np.random.rand(10, 20, 20) * 100 f = lambda x: F.sum_to(x, (10, )) self.assertTrue(gradient_check(f, x_data))
def test_backward4(self): x_data = np.random.rand(10) f = lambda x: F.sum_to(x, (10, )) + 1 self.assertTrue(gradient_check(f, x_data))
def test_forward3(self): x = Variable(np.random.rand(10)) y = F.sum_to(x, (10, )) expected = x.data # 同じ形状なので何もしない self.assertTrue(array_allclose(y.data, expected))
def test_forward2(self): x = Variable(np.array([[1., 2., 3.], [4., 5., 6.]])) y = F.sum_to(x, (1, 3)) expected = np.sum(x.data, axis=0, keepdims=True) self.assertTrue(array_allclose(y.data, expected))
def test_forward1(self): x = Variable(np.random.rand(10)) y = F.sum_to(x, (1, )) expected = np.sum(x.data) self.assertTrue(array_allclose(y.data, expected))
def test_backward2(self): x_data = np.random.rand(10, 10) * 10 f = lambda x: F.sum_to(x, (10, )) self.assertTrue(check_backward(f, x_data))
if '__file__' in globals(): import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np import matplotlib.pyplot as plt from dezero import Variable from dezero import setup_variable from dezero.utils import plot_dot_graph import dezero.functions as F setup_variable() if __name__ == '__main__': x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = F.sum_to(x, (1, 1)) print(y)