def test_find_best_architecture(self): """ Find_best_architecture should return a single model, parameters, type and valid knn accuracy.""" np.random.seed(123) num_timesteps = 100 num_channels = 2 num_samples_train = 5 num_samples_val = 3 X_train = np.random.rand(num_samples_train, num_timesteps, num_channels) y_train = to_categorical(np.array([0, 0, 1, 1, 1])) X_val = np.random.rand(num_samples_val, num_timesteps, num_channels) y_val = to_categorical(np.array([0, 1, 1])) best_model, best_params, best_model_type, knn_acc = find_architecture.find_best_architecture( X_train, y_train, X_val, y_val, verbose=False, subset_size=10, number_of_models=1, nr_epochs=1) assert hasattr(best_model, 'fit') self.assertIsNotNone(best_params) self.assertIsNotNone(best_model_type) assert 1 >= knn_acc >= 0
def test_find_best_architecture_with_class_weights(self): """Model should not ignore tiny class with huge class weight. Note that this test is non-deterministic, even though a seed was set. Note2 that this test is very slow, taking up 40% of all mcfly test time.""" tf.random.set_seed(1234) # Needs tensorflow API v2 X_train, y_train = _create_2_class_labeled_dataset( 1, 999) # very unbalanced X_val, y_val = _create_2_class_labeled_dataset(1, 99) X_test, y_test = _create_2_class_labeled_dataset(10, 10) class_weight = {0: 2, 1: 0.002} best_model, _, _, _ = find_architecture.find_best_architecture( X_train, y_train, X_val, y_val, verbose=False, subset_size=1000, number_of_models=5, nr_epochs=1, model_type='CNN', class_weight=class_weight) probabilities = best_model.predict_proba(X_test) predicted = probabilities.argmax(axis=1) np.testing.assert_array_equal(predicted, y_test.argmax(axis=1))
def create_dummy_model(): """ Function to aid the tests on saving and loading a model""" np.random.seed(123) num_timesteps = 100 num_channels = 2 num_samples_train = 5 num_samples_val = 3 X_train = np.random.rand( num_samples_train, num_timesteps, num_channels) y_train = to_categorical(np.array([0, 0, 1, 1, 1])) X_val = np.random.rand(num_samples_val, num_timesteps, num_channels) y_val = to_categorical(np.array([0, 1, 1])) best_model, best_params, best_model_type, knn_acc = find_architecture.find_best_architecture( X_train, y_train, X_val, y_val, verbose=False, subset_size=10, number_of_models=1, nr_epochs=1) return(best_model)
def create_dummy_model(): """ Function to aid the tests on saving and loading a model""" np.random.seed(123) num_timesteps = 100 num_channels = 2 num_samples_train = 5 num_samples_val = 3 X_train = np.random.rand( num_samples_train, num_timesteps, num_channels) y_train = to_categorical(np.array([0, 0, 1, 1, 1])) X_val = np.random.rand(num_samples_val, num_timesteps, num_channels) y_val = to_categorical(np.array([0, 1, 1])) best_model, best_params, best_model_type, knn_acc = find_architecture.find_best_architecture( X_train, y_train, X_val, y_val, verbose=False, subset_size=10, number_of_models=1, nr_epochs=1) return(best_model)
def test_find_best_architecture(self): """ Find_best_architecture should return a single model, parameters, type and valid knn accuracy.""" np.random.seed(123) num_timesteps = 100 num_channels = 2 num_samples_train = 5 num_samples_val = 3 X_train = np.random.rand( num_samples_train, num_timesteps, num_channels) y_train = to_categorical(np.array([0, 0, 1, 1, 1])) X_val = np.random.rand(num_samples_val, num_timesteps, num_channels) y_val = to_categorical(np.array([0, 1, 1])) best_model, best_params, best_model_type, knn_acc = find_architecture.find_best_architecture( X_train, y_train, X_val, y_val, verbose=False, subset_size=10, number_of_models=1, nr_epochs=1) assert hasattr(best_model, 'fit') self.assertIsNotNone(best_params) self.assertIsNotNone(best_model_type) assert 1 >= knn_acc >= 0