Beispiel #1
0
def testbackward():
    data = np.array([1, 2])
    x = Variable(data)
    A = Square()
    B = Exp()
    a = A(x)
    y = B(a)
    y.grad = np.array([1, 1])
    a.grad = B.backward(y.grad)
    x.grad = A.backward(a.grad)

    return x.grad
if '__file__' in globals():
    import os, sys

    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np

from dezero import Variable
from dezero.functions import Square, Exp

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)