-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ff.py
More file actions
33 lines (28 loc) · 1.15 KB
/
Copy pathtest_ff.py
File metadata and controls
33 lines (28 loc) · 1.15 KB
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
import feedforward as ff
import numpy as np
def test_sigmoid():
'''unit test of function sigmoid()'''
a = np.array([-10.0, -1.0, 0.0, 1.0, 10.0])
expected = np.array([0.0, 0.27, 0.5, 0.73, 1.0])
assert np.all(ff.sigmoid(a).round(2) == expected)
def test_feed_forward(dataset):
X,_ = dataset
X_bias = np.hstack((X,np.ones((50,1))))
hidden_weights = np.random.uniform(size=(3,2))
outer_weights = np.random.uniform(size=(3,1))
out = ff.feed_forward(X_bias, [hidden_weights, outer_weights])
assert out[0].shape == (50, 2)
assert out[1].shape == (50, 1)
Xref = np.array([[1.0, 2.0, 1.0]])
whidden = np.array([[1.0, 2.0, 0.0],
[-1.0, -2.0, 0.0]
]).T
wout = np.array([1.0, -1.0, 0.5]).T
out = ff.feed_forward(Xref, [whidden, wout])
assert np.all(out[0].round(2) == np.array([[0.99, 0.01]]))
assert np.all(out[1].round(2) == np.array([[0.82]]))
def test_logloss():
ytrue = np.array([0.0, 0.0, 1.0, 1.0])
ypred = np.array([0.01, 0.99, 0.01, 0.99])
expected = np.array([0.01, 4.61, 4.61, 0.01])
assert np.all(ff.log_loss(ytrue, ypred).round(2) == expected)