Пример #1
0
    def run(self, train_batches, test_batches):
        # training and validation error collector
        ec = ErrorCollector()

        print("-----ModelTester-----")
        logging.info("-----ModelTester-----")

        for n_hidden in self.n_hidden:
            for n_layer in self.n_layer:
                if self.is_res_net:
                    self.models.append(
                        ResNetModel(n_in=self.n_in,
                                    n_hidden=n_hidden,
                                    n_out=self.n_out,
                                    n_layer=n_layer,
                                    activation='relu'))
                else:
                    for activation in self.activation:
                        # create models
                        self.models.append(
                            Model(n_in=self.n_in,
                                  n_hidden=n_hidden,
                                  n_out=self.n_out,
                                  n_layer=n_layer,
                                  activation=activation))

        for model in self.models:
            trainer = Trainer(model, train_batches, ec)
            for learning_rate in self.learning_rates:
                trainer.train(learning_rate, self.epochs, early_stop_lim=25)
                # print error plots
                ec.plotTrainTestError(model, train_batches.batch_size,
                                      learning_rate, self.epochs,
                                      model.activation)
                ec.plotTrainTestAcc(model, train_batches.batch_size,
                                    learning_rate, self.epochs,
                                    model.activation)
                ec.resetErrors()
                evaluator = Evaluator(model, test_batches,
                                      trainer.getSaveFilePath())
                test_loss, test_acc = evaluator.eval()
                trainer.resetBestScore()

                if self.best_test_acc == 0 or test_acc > self.best_test_acc:
                    self.best_test_acc = test_acc
                    self.best_model_param = 'Param_' + model.name + '_ep-' + str(
                        self.epochs) + '_hidu-' + str(
                            model.n_hidden) + '_hidl-' + str(
                                model.n_layer) + '_lr-' + str(learning_rate)

        print(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
        logging.info(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
Пример #2
0
    def run(self, train_batches, test_batches):
        # training and validation error collector
        ec = ErrorCollector()
        # setup logging
        log_file_name = 'logs' + os.sep + 'Model_Trainer.log'
        logging.basicConfig(filename=log_file_name, level=logging.INFO)

        print("-----ModelTester-----")
        logging.info("-----ModelTester-----")

        for n_hidden in self.n_hidden:
            for n_layer in self.n_layer:
                # create models
                self.models.append(
                    Model(n_in=self.n_in,
                          n_hidden=n_hidden,
                          n_out=self.n_out,
                          n_layer=n_layer))

        for model in self.models:
            trainer = Trainer(model, train_batches, ec)
            for learning_rate in self.learning_rates:
                trainer.train(learning_rate, self.epochs, early_stop_lim=25)
                # print error plots
                ec.plotTrainTestError(model, train_batches.batch_size,
                                      learning_rate, self.epochs)
                ec.plotTrainTestAcc(model, train_batches.batch_size,
                                    learning_rate, self.epochs)
                ec.resetErrors()
                evaluator = Evaluator(model, test_batches,
                                      trainer.getSaveFilePath())
                test_loss, test_acc = evaluator.eval()
                trainer.resetBestScore()

                if self.best_test_acc == 0 or test_acc > self.best_test_acc:
                    self.best_test_acc = test_acc
                    self.best_model_param = 'Param_ep-' + str(
                        self.epochs) + '_hidu-' + str(
                            model.n_hidden) + '_hidl-' + str(
                                model.n_layer) + '_lr-' + str(learning_rate)

        print(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
        logging.info(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
Пример #3
0
  def run(self, batches):
    # training and validation error collector
    ec = ErrorCollector()

    print("-----ModelTester-----")
    logging.info("-----ModelTester-----")

    # create models
    for n_hidden in self.n_hidden:
      for n_layer in self.n_layer:
        for rnn_unit in self.rnn_unit:
            print("RNN with max seq len: ", batches.max_seq_len)
            self.models.append(RnnModel(self.n_symbols, n_hidden, self.n_out,
                                        rnn_unit, batches.max_seq_len, n_layer, self.adam_optimizer))
    
    # training and evaluation
    plot_id = 0
    for model in self.models:
      trainer = Trainer(model, batches, ec)
      for learning_rate in self.learning_rates:
        plot_id += 1
        trainer.train(learning_rate, self.epochs, adam_optimizer=self.adam_optimizer, early_stopping=True, early_stop_lim=10)
        # print error plots
        ec.plotTrainTestError(model, batches.batch_size, learning_rate, self.epochs, plot_id)
        ec.resetErrors()
        evaluator = Evaluator(model, batches, trainer.getSaveFilePath())
        test_loss = evaluator.eval()

        # get best conversion time
        if trainer.best_epoch < self.best_conv_time:
          self.best_conv_time = trainer.best_epoch
          self.best_model_param = 'Param_' + model.name + '_ep-' + str(self.epochs) + '_hidu-' + str(model.n_hidden) + '_hidl-' + str(model.n_layer) + '_lr-' + str(learning_rate) + '_id-' + str(plot_id)

        # reset scores
        trainer.resetBestScore()

    # print convergence times
    ec.convergenceTimeMeanAndStd()

    print("-----ModelTester finished, best test error: [%.6f] and conv time (epochs): [%i] with model: %s " % (self.best_test_loss, self.best_conv_time, self.best_model_param))
    logging.info("-----ModelTester finished, best test error: [%.6f] and conv time (epochs): [%i] with model: %s " % (self.best_test_loss, self.best_conv_time, self.best_model_param))
Пример #4
0
    epochs = 5
    #learning_rate = 0.001
    learning_rate = 0.01
    model = Model(n_in=X.shape[1], n_hidden=300, n_out=26, n_layer=1)
    batch_size = 40

    # setup logging
    log_file_name = 'logs' + os.sep + 'Log' + '_ep' + str(
        epochs) + '_hidu' + str(model.n_hidden) + '_hidl' + str(
            model.n_layer) + '_lr' + str(learning_rate) + '.log'
    logging.basicConfig(filename=log_file_name, level=logging.INFO)

    # Batch Normalize
    bn = BatchNormalizer(X, C, batch_size=batch_size, shuffle=True)
    train_batches = bn.getBatches(X, C, is_validation=False)
    test_batches = bn.getBatches(X_tst, C_tst, test=True)

    print('examples to train: ', train_batches.examples_train.shape)

    # Training
    trainer = Trainer(model, train_batches, ec)
    trainer.train(learning_rate, epochs, early_stop_lim=25)

    # print error plots
    ec.plotTrainTestError(model, batch_size, learning_rate, epochs)
    ec.plotTrainTestAcc(model, batch_size, learning_rate, epochs)

    # Testing
    evaluator = Evaluator(model, test_batches, trainer.getSaveFilePath())
    evaluator.eval()