Beispiel #1
0
 def test_forward(self):
     x = Variable(np.array(2.0))
     y = fn.square(x)
     expected = np.array(4.0)
     self.assertEqual(
         y.data, expected
     )  # 두 개체가 동일한지 여부를 판정 이외에도(assertGreater, self.assertTrue등)
Beispiel #2
0
 def test_gradient_check(self):
     x = Variable(np.random.rand(1))
     y = fn.square(x)
     y.backward()
     num_grad = fn.numerical_diff(fn.square, x)  # 수치미분
     flg = np.allclose(x.grad, num_grad)
     #np.allclose(a,b) : 기본값 |a-b|<=(atol + rtol * |b|) 만족하면 True (값이 얼마나 가까운지 판단)
     self.assertTrue(flg)
    def forward(self, x):
        y = np.sin(x)
        return y

    def backward(self, gy):
        x = self.inputs[0].data
        gx = gy * np.cos(x)
        return gx


def sin(x):
    return Sin()(x)


def my_sin(x, threshold=0.0001):
    y = 0
    for i in range(100000):
        c = (-1)**i / math.factorial(2 * i + 1)
        t = c * x**(2 * i + 1)
        y = y + t
        if abs(t.data) < threshold:
            break
    return y


if __name__ == '__main__':
    x = Variable(np.array(np.pi / 4))
    y = my_sin(x)
    y.backward()
    plot_dot_graph(y, verbose=False, to_file='my_sin.png')
import os, sys; sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
# Import core_simple explicitly
from dezero.core_simple import Variable
from dezero.core_simple import setup_variable
setup_variable()


def f(x):
    y = x ** 4 - 2 * x ** 2
    return y


def gx2(x):
    return 12 * x ** 2 - 4


logs = []
x = Variable(np.array(2.0))
iters = 200

for i in range(iters):
    print(i, x)

    y = f(x)
    x.cleargrad()
    y.backward()

    x.data -= 0.01 * x.grad
Beispiel #5
0
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    import numpy as np
    from dezero.core_simple import Variable
    from dezero.core_simple import setup_variable
setup_variable()


def sphere(x, y):
    z = x**2 + y**2
    return z


def metyas(x, y):
    z = 0.26 * (x**2 + y**2) - 0.48 * x * y
    return z


def goldstein(x, y):
    z = (1 + (x + y + 1)**2 * (19 - 14 * x + 3 * x ** 2 - 14 * y + 6 * x * y + 3 * y ** 2)) * \
        (30 + (2 * x - 3 * y)**2 * (18 - 32 * x + 12 *
                                    x ** 2 + 48 * y - 36 * x * y + 27 * y ** 2))
    return z


if __name__ == '__main__':
    x = Variable(np.array(1.0))
    y = Variable(np.array(1.0))
    z = goldstein(x, y)
    z.backward()
    print(x.grad, y.grad)
 def test_neg_overload(self):
     x = Variable(np.array(2.0))
     assert (-x).data == -2.0
 def test_neg_backward(self):
     x = Variable(np.array(2.0))
     y = -x
     y.backward()
     assert x.grad == -1
 def test_calc_multi_type_ndarray_right(self):
     x = Variable(np.array(2.0))
     y = np.array(3.0) + x
     assert y.data == Variable(np.array(5.0)).data
 def test_name(self):
     x = Variable(np.array(1), "test")
     assert x.name == "test"
class TestVariable:
    def test_name(self):
        x = Variable(np.array(1), "test")
        assert x.name == "test"

    @pytest.mark.parametrize("input, expected", [(Variable(np.array(1)), 'int32'), (Variable(np.array([1.0, 2.0, 3.0])),  'float64'), (Variable(np.array([[1.0, 2.0], [2.0, 3.0]])),  'float64')])
    def test_dtype(self, input, expected):
        assert input.dtype == expected

    @pytest.mark.parametrize("input, expected", [(Variable(np.array(1.0)), 0), (Variable(np.array([1.0, 2.0, 3.0])), 1), (Variable(np.array([[1.0, 2.0], [2.0, 3.0]])), 2)])
    def test_ndim(self, input, expected):
        assert input.ndim == expected

    @pytest.mark.parametrize("input, expected", [(Variable(np.array(1.0)), 1), (Variable(np.array([1.0, 2.0, 3.0])), 3), (Variable(np.array([[1.0, 2.0], [2.0, 3.0]])), 4)])
    def test_size(self, input, expected):
        assert input.size == expected

    @pytest.mark.parametrize("input, expected", [(Variable(np.array(1.0)), 1), (Variable(np.array([1.0, 2.0, 3.0])), 3), (Variable(np.array([[1.0, 2.0], [2.0, 3.0]])), 2)])
    def test_len(self, input, expected):
        assert len(input) == expected

    @pytest.mark.parametrize("input, expected", [(Variable(np.array(1.0)), ()), (Variable(np.array([1.0, 2.0, 3.0])), (3,)), (Variable(np.array([[1.0, 2.0], [2.0, 3.0]])), (2, 2))])
    def test_shape(self, input, expected):
        assert input.shape == expected

    @pytest.mark.parametrize("input, expected", [(Variable(np.array(1.0)), "variable(1.0)\n"), (Variable(np.array([1.0, 2.0, 3.0])),  "variable([1. 2. 3.])\n"), (Variable(np.array([[1.0, 2.0], [2.0, 3.0]])),  "variable([[1. 2.]\n          [2. 3.]])\n")])
    def test_shape(self, input, expected, capsys):
        print(input)
        captured = capsys.readouterr()
        assert captured.out == expected

    @pytest.mark.parametrize("input, expected", [(np.array(1.0), Variable(np.array(1.0))), (Variable(np.array(1.0)), Variable(np.array(1.0)))])
    def test_as_variable(self, input, expected):
        assert type(as_variable(input)) == type(expected)
 def test_pow_backward_overload(self):
     x = Variable(np.array(2.0))
     y = x ** 3
     y.backward()
     assert x.grad == 12
 def test_pow_overload(self):
     x = Variable(np.array(2.0))
     assert (x ** 3).data == 8
 def test_pow_backward(self):
     x = Variable(np.array(2.0))
     y = pow(x, 3)
     y.backward()
     assert x.grad == 12
 def test_pow(self):
     x = Variable(np.array(2.0))
     assert pow(x, 3).data == 8
def culc_object():
    yield Variable(np.array(2)), Variable(np.array(3))
Beispiel #16
0
 def test_backward(self):
     x = Variable(np.array(3.0))
     y = fn.square(x)
     y.backward()
     expected = np.array(6.0)
     self.assertEqual(x.grad, expected)
 def test_neg(self):
     x = Variable(np.array(2.0))
     assert neg(x).data == -2.0
import numpy as np
# core_simple を明示的にインポート
# (dezero/__init__.py の is_simple_core = False でも動作させるため)
from dezero.core_simple import Variable
from dezero.core_simple import setup_variable

setup_variable()


def rosenbrock(x0, x1):
    y = 100 * (x1 - x0**2)**2 + (x0 - 1)**2
    return y


logs = []
x0 = Variable(np.array(0.0))
x1 = Variable(np.array(2.0))
iters = 1000
lr = 0.001

for i in range(iters):
    print(x0, x1)

    y = rosenbrock(x0, x1)

    x0.cleargrad()
    x1.cleargrad()
    y.backward()

    logs.append([float(x0.data), float(x1.data), float(y.data)])
 def test_calc_multi_type_left(self):
     x = Variable(np.array(2.0))
     y = x + 3
     assert y.data == Variable(np.array(5.0)).data