1
|
|
|
from mcfly import find_architecture |
2
|
|
|
import numpy as np |
3
|
|
|
from nose.tools import assert_equal, assert_equals, assert_almost_equal |
4
|
|
|
from keras.utils.np_utils import to_categorical |
5
|
|
|
|
6
|
|
|
import unittest |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class FindArchitectureSuite(unittest.TestCase): |
10
|
|
|
|
11
|
|
|
"""Basic test cases.""" |
12
|
|
|
|
13
|
|
|
def test_kNN_accuracy_1(self): |
14
|
|
|
""" |
15
|
|
|
The accuracy for this single-point dataset should be 1. |
16
|
|
|
""" |
17
|
|
|
X_train = np.array([[[1]], [[0]]]) |
18
|
|
|
y_train = np.array([[1, 0], [0, 1]]) |
19
|
|
|
X_val = np.array([[[0.9]]]) |
20
|
|
|
y_val = np.array([[1, 0]]) |
21
|
|
|
|
22
|
|
|
acc = find_architecture.kNN_accuracy( |
23
|
|
|
X_train, y_train, X_val, y_val, k=1) |
24
|
|
|
assert_almost_equal(acc, 1.0) |
25
|
|
|
|
26
|
|
|
def test_kNN_accuracy_0(self): |
27
|
|
|
""" |
28
|
|
|
The accuracy for this single-point dataset should be 0. |
29
|
|
|
""" |
30
|
|
|
X_train = np.array([[[1]], [[0]]]) |
31
|
|
|
y_train = np.array([[1, 0], [0, 1]]) |
32
|
|
|
X_val = np.array([[[0.9]]]) |
33
|
|
|
y_val = np.array([[0, 1]]) |
34
|
|
|
|
35
|
|
|
acc = find_architecture.kNN_accuracy( |
36
|
|
|
X_train, y_train, X_val, y_val, k=1) |
37
|
|
|
assert_almost_equal(acc, 0) |
38
|
|
|
|
39
|
|
|
def test_find_best_architecture(self): |
40
|
|
|
num_timesteps = 100 |
41
|
|
|
num_channels = 2 |
42
|
|
|
num_samples_train = 5 |
43
|
|
|
num_samples_val = 3 |
44
|
|
|
X_train = np.random.rand( |
45
|
|
|
num_samples_train, |
46
|
|
|
num_timesteps, |
47
|
|
|
num_channels) |
48
|
|
|
y_train = to_categorical(np.array([0, 0, 1, 1, 1])) |
49
|
|
|
X_val = np.random.rand(num_samples_val, num_timesteps, num_channels) |
50
|
|
|
y_val = to_categorical(np.array([0, 1, 1])) |
51
|
|
|
best_model, best_params, best_model_type, knn_acc = find_architecture.find_best_architecture( |
52
|
|
|
X_train, y_train, X_val, y_val, verbose=False, |
53
|
|
|
number_of_models=1, nr_epochs=1) |
54
|
|
|
assert hasattr(best_model, 'fit') |
55
|
|
|
self.assertIsNotNone(best_params) |
56
|
|
|
self.assertIsNotNone(best_model_type) |
57
|
|
|
assert knn_acc >= 0 and knn_acc <= 1 |
58
|
|
|
|
59
|
|
|
def setUp(self): |
60
|
|
|
np.random.seed(1234) |
61
|
|
|
|
62
|
|
|
if __name__ == '__main__': |
63
|
|
|
unittest.main() |
64
|
|
|
|