예제 #1
0
    def test_composite(self):
        A = Square()
        B = Exp()
        C = Square()

        x = Variable(np.array(0.5))
        a = A(x)
        b = B(a)
        y = C(b)
        print(y.data)
예제 #2
0
    def test_function_chain(self):
        A = Square()
        B = Exp()
        C = Square()

        x = Variable(np.array(0.5))
        a = A(x)
        b = B(a)
        y = C(b)
        self.assertTrue(y.creator == C)
        self.assertTrue(y.creator._input == b)
        self.assertTrue(y.creator._input.creator == B)
        self.assertTrue(y.creator._input.creator._input == a)
        self.assertTrue(y.creator._input.creator._input.creator == A)
        self.assertTrue(y.creator._input.creator._input.creator._input == x)
예제 #3
0
 def test_backward(self):
     A = Square()
     B = Exp()
     C = Square()
     x = Variable(np.array(0.5))
     a = A(x)
     b = B(a)
     y = C(b)
     y.grad = np.array(1.0)
     b.grad = C.backward(y.grad)
     a.grad = B.backward(b.grad)
     x.grad = A.backward(a.grad)
     print(x.grad)
예제 #4
0
    def test_function_chain_backward(self):
        A = Square()
        B = Exp()
        C = Square()

        x = Variable(np.array(0.5))
        a = A(x)
        b = B(a)
        y = C(b)
        y.grad = np.array(1.0)
        C = y.creator
        b = C._input
        b.grad = C.backward(y.grad)
        B = b.creator
        a = B._input
        a.grad = B.backward(b.grad)

        A = a.creator
        x = A._input
        x.grad = A.backward(a.grad)
        print(x.grad)
예제 #5
0
 def f(x):
     A = Square()
     B = Exp()
     C = Square()
     return C(B(A(x)))
예제 #6
0
 def test_differential(self):
     f = Square()
     x = Variable(np.array(2.0))
     dy = numerical_diff(f, x)
     print(dy)
예제 #7
0
 def test_function(self):
     x = Variable(np.array(10))
     f = Square()
     y = f(x)
     self.assertTrue(isinstance(y, Variable))
     print(y.data)