コード例 #1
0
    def test_train_models_on_samples_with_x_and_y(self):
        """
        Model should be able to train using separated x and y values
        """
        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]))
        batch_size = 20

        custom_settings = get_default_settings()
        model_type = CNN(X_train.shape, 2, **custom_settings)
        hyperparams = model_type.generate_hyperparameters()
        model = model_type.create_model(**hyperparams)
        models = [(model, hyperparams, "CNN")]

        histories, _, _ = \
            find_architecture.train_models_on_samples(
                X_train, y_train, X_val, y_val, models,
                nr_epochs=1, subset_size=10, verbose=False,
                outputfile=None, early_stopping_patience='auto',
                batch_size=batch_size)
        assert len(histories) == 1
コード例 #2
0
    def test_train_models_on_samples_with_dataset(self):
        """
        Model should be able to train using a dataset as an input
        """
        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]))
        batch_size = 20

        data_train = tf.data.Dataset.from_tensor_slices(
            (X_train, y_train)).batch(batch_size)

        data_val = tf.data.Dataset.from_tensor_slices(
            (X_val, y_val)).batch(batch_size)

        custom_settings = get_default_settings()
        model_type = CNN(X_train.shape, 2, **custom_settings)
        hyperparams = model_type.generate_hyperparameters()
        model = model_type.create_model(**hyperparams)
        models = [(model, hyperparams, "CNN")]

        histories, val_metrics, val_losses = \
            find_architecture.train_models_on_samples(
                data_train, None, data_val, None, models,
                nr_epochs=1, subset_size=None, verbose=False,
                outputfile=None, early_stopping_patience='auto',
                batch_size=batch_size)
コード例 #3
0
ファイル: test_cnn.py プロジェクト: nitin0301/mcfly
 def test_cnn_starts_with_batchnorm(self):
     """ CNN models should always start with a batch normalization layer. """
     model_type = CNN((None, 20, 3), 2)
     model = model_type.create_model(**{
         "filters": [32, 32],
         "fc_hidden_nodes": 100
     })
     assert 'BatchNormalization' in str(type(
         model.layers[0])), 'Wrong layer type.'
コード例 #4
0
ファイル: test_cnn.py プロジェクト: nitin0301/mcfly
    def test_cnn_batchnorm_dim(self):
        """"The output shape of the batchnorm should be (None, nr_timesteps, nr_filters)"""
        model_type = CNN((None, 20, 3), 2)
        model = model_type.create_model(**{
            "filters": [32, 32],
            "fc_hidden_nodes": 100
        })

        batchnormlay = model.layers[2]
        assert batchnormlay.output_shape == (None, 20, 32)
コード例 #5
0
ファイル: test_cnn.py プロジェクト: nitin0301/mcfly
    def test_cnn_fc_nodes(self):
        """ CNN model should have number of dense nodes defined by user. """
        fc_hidden_nodes = 101
        model_type = CNN((None, 20, 3), 2)
        model = model_type.create_model(**{
            "filters": [32, 32],
            "fc_hidden_nodes": fc_hidden_nodes
        })

        dense_layer = [l for l in model.layers if 'Dense' in str(l)][0]
        assert dense_layer.output_shape[
            1] == fc_hidden_nodes, 'Wrong number of fc nodes.'
コード例 #6
0
ファイル: test_cnn.py プロジェクト: nitin0301/mcfly
    def test_cnn_enough_batchnorm(self):
        """CNN model should contain as many batch norm layers as it has activations layers"""
        model_type = CNN((None, 20, 3), 2)
        model = model_type.create_model(**{
            "filters": [32, 32],
            "fc_hidden_nodes": 100
        })

        batch_norm_layers = len(
            [l for l in model.layers if 'BatchNormalization' in str(l)])
        activation_layers = len(
            [l for l in model.layers if 'Activation' in str(l)])
        assert batch_norm_layers == activation_layers
コード例 #7
0
    def test_train_models_on_samples_with_generators(self):
        """
        Model should be able to train using a generator as an input
        """
        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]))
        batch_size = 20

        class DataGenerator(Sequence):
            def __init__(self, x_set, y_set, batch_size):
                self.x, self.y = x_set, y_set
                self.batch_size = batch_size

            def __len__(self):
                return math.ceil(len(self.x) / self.batch_size)

            def __getitem__(self, idx):
                batch_x = self.x[idx * self.batch_size:(idx + 1) *
                                 self.batch_size]
                batch_y = self.y[idx * self.batch_size:(idx + 1) *
                                 self.batch_size]
                return batch_x, batch_y

        data_train = DataGenerator(X_train, y_train, batch_size)
        data_val = DataGenerator(X_val, y_val, batch_size)

        custom_settings = get_default_settings()
        model_type = CNN(X_train.shape, 2, **custom_settings)
        hyperparams = model_type.generate_hyperparameters()
        model = model_type.create_model(**hyperparams)
        models = [(model, hyperparams, "CNN")]

        histories, _, _ = \
            find_architecture.train_models_on_samples(
                data_train, None, data_val, None, models,
                nr_epochs=1, subset_size=None, verbose=False,
                outputfile=None, early_stopping_patience='auto',
                batch_size=batch_size)
        assert len(histories) == 1
コード例 #8
0
ファイル: test_cnn.py プロジェクト: nitin0301/mcfly
    def test_cnn_metrics(self):
        """CNN model should be compiled with the metrics that we give it"""
        metrics = ['accuracy', 'mae']
        x_shape = (None, 20, 3)
        nr_classes = 2
        X_train, y_train = generate_train_data(x_shape, nr_classes)

        model_type = CNN(x_shape, nr_classes, metrics=metrics)
        model = model_type.create_model(**{
            "filters": [32, 32],
            "fc_hidden_nodes": 100
        })
        model.fit(X_train, y_train, epochs=1)

        model_metrics = [m.name for m in model.metrics]
        for metric in metrics:
            assert metric in model_metrics