Exemple #1
0
    def test_early_stopping_condition_test(self):
        disable_sysout()
        with tempfile.TemporaryDirectory() as tmp_dir:
            batch_size = 50
            min_epoch = 10
            max_epoch = 15
            batch_iterator, test_batch_iterator, nb_classes = get_reuters_dataset(
                batch_size, 1000)
            layout = Layout(input_size=1000,
                            output_size=nb_classes,
                            output_activation='softmax')
            training = Training(
                objective=Objective('categorical_crossentropy'),
                optimizer=Optimizer(optimizer='Adam'),
                metric=Metric('categorical_accuracy'),
                stopping=AccuracyDecreaseStoppingCondition(
                    metric='categorical_accuracy',
                    noprogress_count=2,
                    min_epoch=min_epoch,
                    max_epoch=max_epoch),
                batch_size=batch_size)
            experiment_parameters = ExperimentParameters(
                use_default_values=True)
            experiment_parameters.layout_parameter('rows', 1)
            experiment_parameters.layout_parameter('blocks', 1)
            experiment_parameters.layout_parameter('layers', 1)
            experiment = Experiment('test__reuters_experiment',
                                    layout,
                                    training,
                                    batch_iterator,
                                    test_batch_iterator,
                                    CpuEnvironment(n_jobs=1, data_dir=tmp_dir),
                                    parameters=experiment_parameters)
            _assert_valid_training_parameters(experiment)

            blueprint = create_random_blueprint(experiment)
            trainer = ModelTrainer(batch_iterator, test_batch_iterator)
            model, history, _duration = trainer.train(blueprint,
                                                      cpu_device(),
                                                      save_best_model=False)
            self.assertTrue(
                len(history.epoch) >= min_epoch,
                'Should have trained for at least min epoch')
            self.assertTrue(
                len(history.epoch) <= max_epoch,
                'Should have trained for max epoch')
            self.assertIsNotNone(model, 'should have fit the model')
            score = model.evaluate_generator(
                test_batch_iterator,
                val_samples=test_batch_iterator.sample_count)
            self.assertIsNotNone(score, 'should have evaluated the model')
Exemple #2
0
    def test_save(self):
        disable_sysout()

        def custom_activation(x):
            return x

        register_custom_activation('custom_activation', custom_activation)
        register_custom_layer('custom_layer', CustomLayer,
                              {'output_dim': int_param(1, 100)})

        with tempfile.TemporaryDirectory() as tmp_dir:
            batch_size = 50
            batch_iterator, test_batch_iterator, nb_classes = get_reuters_dataset(
                batch_size, 1000)
            layout = Layout(input_size=1000,
                            output_size=nb_classes,
                            output_activation='softmax')
            training = Training(
                objective=Objective('categorical_crossentropy'),
                optimizer=Optimizer(optimizer='Adam'),
                metric=Metric('categorical_accuracy'),
                stopping=EpochStoppingCondition(10),
                batch_size=batch_size)
            experiment_parameters = ExperimentParameters(
                use_default_values=True)
            experiment_parameters.layout_parameter('rows', 1)
            experiment_parameters.layout_parameter('blocks', 1)
            experiment_parameters.layout_parameter('layers', 1)
            experiment_parameters.layout_parameter('block.layer_type',
                                                   'custom_layer')
            experiment = Experiment('test__reuters_experiment',
                                    layout,
                                    training,
                                    batch_iterator,
                                    test_batch_iterator,
                                    CpuEnvironment(n_jobs=1, data_dir=tmp_dir),
                                    parameters=experiment_parameters)

            blueprint = create_random_blueprint(experiment)
            model = ModelBuilder().build(blueprint, default_device())
            model.fit_generator(
                generator=batch_iterator,
                samples_per_epoch=batch_iterator.samples_per_epoch,
                nb_epoch=10,
                validation_data=test_batch_iterator,
                nb_val_samples=test_batch_iterator.sample_count)
            filepath = join(tmp_dir, 'model')
            model.save(filepath)
            model = load_keras_model(filepath)
            self.assertIsNotNone(model, 'Should have loaded the model')
Exemple #3
0
    def test_train(self):
        disable_sysout()
        with tempfile.TemporaryDirectory() as tmp_dir:
            batch_size = 50
            batch_iterator, test_batch_iterator, nb_classes = get_reuters_dataset(
                batch_size, 1000)
            layout = Layout(input_size=1000,
                            output_size=nb_classes,
                            output_activation='softmax')
            training = Training(
                objective=Objective('categorical_crossentropy'),
                optimizer=Optimizer(optimizer='Adam'),
                metric=Metric('categorical_accuracy'),
                stopping=EpochStoppingCondition(10),
                batch_size=batch_size)
            experiment_parameters = ExperimentParameters(
                use_default_values=True)
            experiment_parameters.layout_parameter('rows', 1)
            experiment_parameters.layout_parameter('blocks', 1)
            experiment_parameters.layout_parameter('layers', 1)
            experiment = Experiment('test__reuters_experiment',
                                    layout,
                                    training,
                                    batch_iterator,
                                    test_batch_iterator,
                                    CpuEnvironment(n_jobs=1, data_dir=tmp_dir),
                                    parameters=experiment_parameters)

            blueprint = create_random_blueprint(experiment)
            model = ModelBuilder().build(blueprint, default_device())
            result = model.fit_generator(
                generator=batch_iterator,
                samples_per_epoch=batch_iterator.samples_per_epoch,
                nb_epoch=10,
                validation_data=test_batch_iterator,
                nb_val_samples=test_batch_iterator.sample_count)
            self.assertIsNotNone(result, 'should have fit the model')
            score = model.evaluate_generator(
                test_batch_iterator,
                val_samples=test_batch_iterator.sample_count)
            self.assertIsNotNone(score, 'should have evaluated the model')
Exemple #4
0
    def test_model_trainer(self):
        disable_sysout()
        with tempfile.TemporaryDirectory() as tmp_dir:
            batch_size = 50
            batch_iterator, test_batch_iterator, nb_classes = get_reuters_dataset(
                batch_size, 1000)
            layout = Layout(input_size=1000,
                            output_size=nb_classes,
                            output_activation='softmax')
            training = Training(
                objective=Objective('categorical_crossentropy'),
                optimizer=Optimizer(optimizer='Adam'),
                metric=Metric('categorical_accuracy'),
                stopping=EpochStoppingCondition(10),
                batch_size=batch_size)
            experiment_parameters = ExperimentParameters(
                use_default_values=True)
            experiment_parameters.layout_parameter('rows', 1)
            experiment_parameters.layout_parameter('blocks', 1)
            experiment_parameters.layout_parameter('layers', 1)
            experiment = Experiment('test__reuters_experiment',
                                    layout,
                                    training,
                                    batch_iterator,
                                    test_batch_iterator,
                                    CpuEnvironment(n_jobs=1, data_dir=tmp_dir),
                                    parameters=experiment_parameters)

            blueprint = create_random_blueprint(experiment)
            trainer = ModelTrainer(batch_iterator, test_batch_iterator)
            model_filename = join(tmp_dir, 'model')
            model, history, _duration = trainer.train(
                blueprint,
                cpu_device(),
                save_best_model=True,
                model_filename=model_filename)
            self.assertIsNotNone(model, 'should have fit the model')
            self.assertTrue(isfile(model_filename),
                            'Should have saved the model')
            self.assertIsNotNone(history, 'Should have the training history')
Exemple #5
0
@author: julien
'''
from multiprocessing import Pool
import tempfile
import unittest

from minos.experiment.experiment import ExperimentParameters, Experiment
from minos.experiment.training import Training, EpochStoppingCondition
from minos.model.design import create_random_blueprint
from minos.model.model import Layout, Objective, Optimizer, Metric
from minos.train.utils import default_device, CpuEnvironment
from minos.utils import disable_sysout
from tests.fixtures import get_reuters_dataset

batch_size = 50
batch_iterator, test_batch_iterator, nb_classes = get_reuters_dataset(
    batch_size, 1000)


def multiprocess_fit(blueprint):
    disable_sysout()
    from minos.model.build import ModelBuilder
    model = ModelBuilder().build(blueprint, default_device())
    model.fit_generator(generator=batch_iterator,
                        samples_per_epoch=batch_iterator.samples_per_epoch,
                        nb_epoch=10,
                        validation_data=test_batch_iterator,
                        nb_val_samples=test_batch_iterator.sample_count)


class TrainTest(unittest.TestCase):
    def test_train(self):
Exemple #6
0
    def test_ga_search(self):
        with tempfile.TemporaryDirectory() as tmp_dir:
            epoch = 3
            generations = 2
            batch_size = 50
            batch_iterator, test_batch_iterator, nb_classes = get_reuters_dataset(
                batch_size, 1000)
            layout = Layout(input_size=1000,
                            output_size=nb_classes,
                            output_activation='softmax')
            training = Training(
                objective=Objective('categorical_crossentropy'),
                optimizer=Optimizer(optimizer='Adam'),
                metric=Metric('categorical_accuracy'),
                stopping=EpochStoppingCondition(epoch),
                batch_size=batch_size)
            experiment_parameters = ExperimentParameters(
                use_default_values=False)
            experiment_parameters.layout_parameter('rows', 1)
            experiment_parameters.layout_parameter('blocks', 1)
            experiment_parameters.layout_parameter('layers', 1)
            experiment_parameters.layer_parameter('Dense.output_dim',
                                                  int_param(10, 500))
            experiment_parameters.all_search_parameters(True)

            experiment_label = 'test__reuters_experiment'
            experiment = Experiment(experiment_label,
                                    layout,
                                    training,
                                    batch_iterator,
                                    test_batch_iterator,
                                    CpuEnvironment(n_jobs=2, data_dir=tmp_dir),
                                    parameters=experiment_parameters)
            run_ga_search_experiment(experiment,
                                     population_size=2,
                                     generations=2)
            self.assertTrue(isfile(experiment.get_log_filename()),
                            'Should have logged')
            self.assertTrue(isfile(experiment.get_step_data_filename(0)),
                            'Should have logged')
            self.assertTrue(isfile(experiment.get_step_log_filename(0)),
                            'Should have logged')
            blueprints = load_experiment_blueprints(
                experiment_label, 0, Environment(data_dir=tmp_dir))
            self.assertTrue(
                len(blueprints) > 0, 'Should have saved/loaded blueprints')
            model = ModelBuilder().build(blueprints[0], cpu_device())
            disable_sysout()
            model.fit_generator(
                generator=batch_iterator,
                samples_per_epoch=batch_iterator.samples_per_epoch,
                nb_epoch=5,
                validation_data=test_batch_iterator,
                nb_val_samples=test_batch_iterator.sample_count)
            score = model.evaluate_generator(
                test_batch_iterator,
                val_samples=test_batch_iterator.sample_count)
            self.assertTrue(score[1] > 0, 'Should have valid score')

            step, population = load_experiment_checkpoint(experiment)
            self.assertEqual(generations - 1, step,
                             'Should have loaded checkpoint')
            self.assertIsNotNone(population, 'Should have loaded checkpoint')
            blueprint = load_experiment_best_blueprint(
                experiment.label,
                environment=CpuEnvironment(n_jobs=2, data_dir=tmp_dir))
            model = ModelBuilder().build(blueprint,
                                         cpu_device(),
                                         compile_model=False)
            self.assertIsNotNone(
                model,
                'Should have loaded and built best model from experiment')