forked from oreilly-japan/deep-learning-from-scratch-3
-
Notifications
You must be signed in to change notification settings - Fork 94
/
test_loss.py
49 lines (39 loc) · 1.52 KB
/
test_loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import unittest
import numpy as np
from dezero import Variable
import dezero.functions as F
from dezero.utils import gradient_check, array_allclose
class TestMSE_simple(unittest.TestCase):
def test_forward1(self):
x0 = np.array([0.0, 1.0, 2.0])
x1 = np.array([0.0, 1.0, 2.0])
expected = ((x0 - x1) ** 2).sum() / x0.size
y = F.mean_squared_error_simple(x0, x1)
self.assertTrue(array_allclose(y.data, expected))
def test_backward1(self):
x0 = np.random.rand(10)
x1 = np.random.rand(10)
f = lambda x0: F.mean_squared_error_simple(x0, x1)
self.assertTrue(gradient_check(f, x0))
def test_backward2(self):
x0 = np.random.rand(100)
x1 = np.random.rand(100)
f = lambda x0: F.mean_squared_error_simple(x0, x1)
self.assertTrue(gradient_check(f, x0))
class TestMSE_simple(unittest.TestCase):
def test_forward1(self):
x0 = np.array([0.0, 1.0, 2.0])
x1 = np.array([0.0, 1.0, 2.0])
expected = ((x0 - x1) ** 2).sum() / x0.size
y = F.mean_squared_error(x0, x1)
self.assertTrue(array_allclose(y.data, expected))
def test_backward1(self):
x0 = np.random.rand(10)
x1 = np.random.rand(10)
f = lambda x0: F.mean_squared_error(x0, x1)
self.assertTrue(gradient_check(f, x0))
def test_backward2(self):
x0 = np.random.rand(100)
x1 = np.random.rand(100)
f = lambda x0: F.mean_squared_error(x0, x1)
self.assertTrue(gradient_check(f, x0))