forked from WegraLee/deep-learning-from-scratch-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_matmul.py
28 lines (23 loc) · 860 Bytes
/
test_matmul.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
import unittest
import numpy as np
from dezero import Variable
import dezero.functions as F
from dezero.utils import gradient_check, array_allclose
class TestMatmul(unittest.TestCase):
def test_forward1(self):
x = Variable(np.array([[1, 2, 3], [4, 5, 6]]))
w = Variable(x.data.T)
y = F.matmul(x, w)
res = y.data
expected = np.array([[14, 32], [32, 77]])
self.assertTrue(array_allclose(res, expected))
def test_backward1(self):
x = np.random.randn(3, 2)
w = np.random.randn(2, 3)
f = lambda x: F.matmul(x, Variable(w))
self.assertTrue(gradient_check(f, x))
def test_backward2(self):
x_data = np.random.randn(10, 1)
w_data = np.random.randn(1, 5)
f = lambda w: F.matmul(Variable(x_data), w)
self.assertTrue(gradient_check(f, w_data))