-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fnn.py
41 lines (31 loc) · 1.14 KB
/
test_fnn.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
import json
import unittest
import numpy as np
import fnn
import mnist_experiment as me
import check_grad as cg
with open('toy_dataset.json', 'r') as f:
toy_dataset = json.load(f)
toy_data = np.array(toy_dataset['data'][:10])
toy_labels = np.array(toy_dataset['labels'][:10])
class TestFNN(unittest.TestCase):
def setUp(self):
np.random.seed(seed=349)
self.model = fnn.FNN(784, 10, [16, 8], [fnn.relu, fnn.relu])
def test_forwardprop(self):
# Used to check the whether bias is dealt correctly
# in forwardprop.
self.model.layers[-1].b[0][0] = 1.0
probs, loss = self.model.forwardprop(toy_data, toy_labels)
self.assertTrue(abs(loss - 2.3677889) < 0.0000001)
print '\n' + '=' * 50 + '\n'
print "Your forward propagation is correct!"
print '\n' + '=' * 50 + '\n'
def test_backprop(self):
self.assertTrue(
cg.check_backprop(self.model, toy_data, toy_labels) < 1e-4)
print '\n' + '=' * 50 + '\n'
print "Your backpropagation is correct!"
print '\n' + '=' * 50 + '\n'
if __name__ == '__main__':
unittest.main()