Esempio n. 1
0
    def add_model(self, model, x_train, y_train, x_test, y_test):
        """add one model while will be trained to history list

        Returns:
            History object.
        """
        model.compile(loss=categorical_crossentropy,
                      optimizer=Adadelta(),
                      metrics=['accuracy'])
        if self.verbose:
            model.summary()
        ModelTrainer(model, x_train, y_train, x_test, y_test,
                     self.verbose).train_model()
        loss, accuracy = model.evaluate(x_test, y_test, verbose=self.verbose)
        model.save(os.path.join(self.path, str(self.model_count) + '.h5'))

        ret = {
            'model_id': self.model_count,
            'loss': loss,
            'accuracy': accuracy
        }
        self.history.append(ret)
        self.history_configs.append(extract_config(model))
        self.model_count += 1

        return ret
Esempio n. 2
0
 def _remove_duplicate(self, models):
     """Remove the duplicate in the history_models"""
     ans = []
     for model_a in models:
         model_a_config = extract_config(model_a)
         if model_a_config not in self.history_configs:
             ans.append(model_a)
     return ans
Esempio n. 3
0
    def add_model(self, model, x_train, y_train, x_test, y_test):
        """add one model while will be trained to history list

        Returns:
            History object.
        """
        loss, accuracy = ModelTrainer(model, x_train, y_train, x_test, y_test,
                                      False).train_model(**self.trainer_args)

        accuracy += 0.005 * len(
            Graph(model, False).extract_descriptor().skip_connections)
        accuracy = min(accuracy, 1)

        model.save(os.path.join(self.path, str(self.model_count) + '.h5'))
        plot_model(model,
                   to_file=os.path.join(self.path,
                                        str(self.model_count) + '.png'),
                   show_shapes=True)

        model_id = self.model_count
        ret = {'model_id': model_id, 'loss': loss, 'accuracy': accuracy}
        self.history.append(ret)
        self.history_configs.append(extract_config(model))
        self.model_count += 1
        self.descriptors[Graph(model, False).extract_descriptor()] = True

        # Update best_model text file
        if model_id == self.get_best_model_id():
            file = open(os.path.join(self.path, 'best_model.txt'), 'w')
            file.write('best model: ' + str(model_id))
            file.close()

        if self.verbose:
            print('Model ID:', model_id)
            print('Loss:', loss)
            print('Accuracy', accuracy)
        return ret