-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fc.py
50 lines (37 loc) · 1.41 KB
/
test_fc.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
50
import sys; import os; sys.path.insert(1, os.path.join(os.getcwd(), "numpy_nn"))
import unittest
import torch
import numpy as np
from test_layer import TestLayer
from numpy_nn.modules.np_nn import FullyConnectedLayer
# np_seed = 0
# torch_seed = 0
# np.random.seed(np_seed)
# torch.manual_seed(torch_seed)
class TestFullyConnectedLayer(TestLayer):
def setUp(self) -> None:
pass
def test_fc(self):
"""
FullyConnectedLayer test
"""
n_input_neurons = 6
n_output_neurons = 3
n_samples = 5
n_iters = 3
my_fc_params = torch_fc_params = (n_input_neurons, n_output_neurons)
input_shape = (n_samples, n_input_neurons)
output_shape = (n_samples, n_output_neurons)
for sampler in (np.random.rand, np.random.randn):
for _ in range(n_iters):
input_np = sampler(*input_shape).astype(np.float32)
dJ_dout = sampler(*output_shape)
with self.subTest(input_np = input_np, dJ_dout = dJ_dout, sampler = sampler):
self._test_module(
my_module = FullyConnectedLayer(*my_fc_params),
torch_module = torch.nn.Linear(*torch_fc_params),
input_np = input_np,
dJ_dout = dJ_dout,
atol=1e-6)
if __name__ == "__main__":
unittest.main()