Python keras.callbacks.EarlyStopping() Examples

The following are code examples for showing how to use keras.callbacks.EarlyStopping(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: dialectal_arabic_segmenter   Author: qcri   File: dialects_segmenter_v3.x.py    GNU Lesser General Public License v3.0 7 votes vote down vote up
def build_model(model_path, max_features, word_embedding_dim, maxlen, nb_seg_tags, lstm_dim):

    model = Sequential()
    model.add(Embedding(max_features, word_embedding_dim, input_length=maxlen, name='word_emb', mask_zero=True))
    model.add(Dropout(0.5))
    model.add(Bidirectional(LSTM(lstm_dim,return_sequences=True)))
    model.add(Dropout(0.5))
    model.add(TimeDistributed(Dense(nb_seg_tags)))
    crf = ChainCRF()
    model.add(crf)
    model.compile(loss=crf.sparse_loss,
                   optimizer= RMSprop(0.01),
                   metrics=['sparse_categorical_accuracy'])
    #model.compile('adam', loss=crf.sparse_loss, metrics=['sparse_categorical_accuracy'])
    #early_stopping = EarlyStopping(patience=10, verbose=1)
    #checkpointer = ModelCheckpoint(options.model + "/seg_keras_weights.hdf5",verbose=1,save_best_only=True)
    eprint(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ' Loading saved model:'+model_path + '/seg_keras_weights.hdf5')

    #model.load_weights(model_path + '/seg_keras_weights.hdf5')

    return model 
Example 2
Project: fancy-cnn   Author: textclf   File: train_neural.py    MIT License 6 votes vote down vote up
def train_sequential(model, X, y, where_to_save, fit_params=None, monitor='val_acc'):
    # TODO: DOCUMENT once thoroughly tested
    # Watch out: where_to_save might be inside fit_params

    if fit_params is None:
        fit_params = {
            "batch_size": 32,
            "nb_epoch": 45,
            "verbose": True,
            "validation_split": 0.15,
            "show_accuracy": True,
            "callbacks": [EarlyStopping(verbose=True, patience=5, monitor=monitor),
                          ModelCheckpoint(where_to_save, monitor=monitor, verbose=True, save_best_only=True)]
        }
    print 'Fitting! Hit CTRL-C to stop early...'
    history = "Nothing to show"
    try:
        history = model.fit(X, y, **fit_params)
    except KeyboardInterrupt:
        print "Training stopped early!"
        history = model.history

    return history 
Example 3
Project: dialectal_arabic_segmenter   Author: qcri   File: dialects_segmenter.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def load_model(model_path, max_features, word_embedding_dim, maxlen, nb_seg_tags, lstm_dim):

    model = Sequential()
    model.add(Embedding(max_features, word_embedding_dim, input_length=maxlen, name='word_emb', mask_zero=True))
    model.add(Dropout(0.5))
    model.add(Bidirectional(LSTM(lstm_dim,return_sequences=True)))
    model.add(Dropout(0.5))
    model.add(TimeDistributed(Dense(nb_seg_tags)))
    crf = ChainCRF()
    model.add(crf)
    model.compile(loss=crf.sparse_loss,
                   optimizer= RMSprop(0.01),
                   metrics=['sparse_categorical_accuracy'])
    #model.compile('adam', loss=crf.sparse_loss, metrics=['sparse_categorical_accuracy'])
    #early_stopping = EarlyStopping(patience=10, verbose=1)
    #checkpointer = ModelCheckpoint(options.model + "/seg_keras_weights.hdf5",verbose=1,save_best_only=True)
    eprint(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ' Loading saved model:'+model_path + '/seg_keras_weights.hdf5')

    model.load_weights(model_path + '/seg_keras_weights.hdf5')

    return model 
Example 4
Project: OverwatchML   Author: sshh12   File: OverwatchPredictSR.py    MIT License 6 votes vote down vote up
def train_model(model, *args, **kwargs):
    
    # print(model.summary())

    history = model.fit(*args, **kwargs, 
                        shuffle=True, 
                        validation_split=.10,
                        verbose=0, 
                        callbacks=[EarlyStopping(patience=30)])
    
    return history


# In[7]:

# Predict SR 
Example 5
Project: EUSIPCO2017   Author: ronggong   File: keras_cnn_jordi_laosheng.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def f_nn(params):
    print ('Params testing: ', params)

    model_merged = f_nn_model(params['filter_density'], params['pool_n_row'], params['pool_n_col'], params['dropout'])

    callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=0)]

    hist = model_merged.fit([X_train, X_train, X_train, X_train, X_train, X_train],
              Y_train,
              validation_data=([X_validation, X_validation, X_validation, X_validation, X_validation, X_validation], Y_validation),
              callbacks=callbacks,
              nb_epoch=500,
              batch_size=128,
              verbose=0)

    score, acc = model_merged.evaluate([X_validation, X_validation, X_validation, X_validation, X_validation, X_validation], Y_validation, batch_size = 128, verbose = 0)
    print('Test accuracy:', acc, 'nb_epoch:', len(hist.history['acc']))

    return {'loss': -acc, 'status': STATUS_OK} 
Example 6
Project: EUSIPCO2017   Author: ronggong   File: keras_cnn_square_laosheng_choi.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def train_model(file_path_model):
    """
    train final model save to model path
    """
    model_0 = squareFilterChoiCNN()

    callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=0)]

    hist = model_0.fit(X_train,
              Y_train,
              validation_data=(X_validation, Y_validation),
              callbacks=callbacks,
              nb_epoch=500,
              batch_size=128)

    nb_epoch = len(hist.history['acc'])

    model_1 = squareFilterChoiCNN()

    model_1.fit(X_train_validation,
                    Y_train_validation,
                    nb_epoch=nb_epoch,
                    batch_size=128)

    model_1.save(file_path_model) 
Example 7
Project: EUSIPCO2017   Author: ronggong   File: keras_cnn_jordi_danAll.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def f_nn(params):
    print ('Params testing: ', params)

    model_merged = f_nn_model(params['filter_density'], params['pool_n_row'], params['pool_n_col'], params['dropout'])

    callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=0)]

    hist = model_merged.fit([X_train, X_train, X_train, X_train, X_train, X_train],
                            Y_train,
                            validation_data=(
                            [X_validation, X_validation, X_validation, X_validation, X_validation, X_validation],
                            Y_validation),
                            callbacks=callbacks,
                            nb_epoch=500,
                            batch_size=64,
                            verbose=0)

    score, acc = model_merged.evaluate(
        [X_validation, X_validation, X_validation, X_validation, X_validation, X_validation], Y_validation,
        batch_size=64, verbose=0)

    print('Test accuracy:', acc, 'nb_epoch:', len(hist.history['acc']))

    return {'loss': -acc, 'status': STATUS_OK} 
Example 8
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 6 votes vote down vote up
def LSTM(self, argsDict):
        self.paras.batch_size             = argsDict["batch_size"]
        self.paras.model['dropout']       = argsDict['dropout']
        self.paras.model['activation']    = argsDict["activation"]
        self.paras.model['optimizer']     = argsDict["optimizer"]
        self.paras.model['learning_rate'] = argsDict["learning_rate"]

        print(self.paras.batch_size, self.paras.model['dropout'], self.paras.model['activation'], self.paras.model['optimizer'], self.paras.model['learning_rate'])

        model = self.lstm_model()
        model.fit(self.train_x, self.train_y,
              batch_size=self.paras.batch_size,
              epochs=self.paras.epoch,
              verbose=0,
              callbacks=[EarlyStopping(monitor='loss', patience=5)]
              )

        score, mse = model.evaluate(self.test_x, self.test_y, verbose=0)
        y_pred=model.predict(self.test_x)
        reca=Recall_s(self.test_y,y_pred)
        return -reca 
Example 9
Project: YOLO-3D-Box   Author: scutan90   File: train.py    MIT License 6 votes vote down vote up
def train(model, image_data, y_true, log_dir='logs/'):
    '''retrain/fine-tune the model'''
    model.compile(optimizer='adam', loss={
        # use custom yolo_loss Lambda layer.
        'yolo_loss': lambda y_true, y_pred: y_pred})

    logging = TensorBoard(log_dir=log_dir)
    checkpoint = ModelCheckpoint(log_dir + "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5",
        monitor='val_loss', save_weights_only=True, save_best_only=True)
    early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')

    model.fit([image_data, *y_true],
              np.zeros(len(image_data)),
              validation_split=.1,
              batch_size=32,
              epochs=30,
              callbacks=[logging, checkpoint, early_stopping])
    model.save_weights(log_dir + 'trained_weights.h5')
    # Further training. 
Example 10
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, rnn_arch=[2,16,32,64,1], drop_out=0.3,
            model_file="lstm_model.hdf5", new=True, min_delta = 0.0001, patience = 50, dense_activation='tanh'):
        """Train a RNN using the input data
        rnn_arch: list containing the number of neurons per layer (the number of hidden layers
            is defined implicitly)
        drop_out: drop out used in the RNN
        model_file: name of the file where the model is saved
        new: if True, a new model is created, otherwise an existent model is used        
        """        
        if new:
            self.model = self._build_lstm_model(rnn_arch, drop_out, dense_activation)
            adam = Adam(lr = 5e-5)
            self.model.compile(loss='mean_squared_error', optimizer=adam)
        else:
            self.model = load_model(model_file)
        self.checkpointer = ModelCheckpoint(filepath=model_file, verbose=0, save_best_only=True)
        self.early_stopping = EarlyStopping(monitor='val_loss', min_delta=min_delta, patience=patience, verbose=0, mode='auto')
        self.trainable_count = int(np.sum([K.count_params(p) for p in set(self.model.trainable_weights)])) 
Example 11
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, rnn_arch=[2,16,32,64,1], drop_out=0.3,
            model_file="lstm_model.hdf5", new=True, min_delta = 0.0001, patience = 50, dense_activation='tanh'):
        """Train a RNN using the input data
        rnn_arch: list containing the number of neurons per layer (the number of hidden layers
            is defined implicitly)
        drop_out: drop out used in the RNN
        model_file: name of the file where the model is saved
        new: if True, a new model is created, otherwise an existent model is used        
        """        
        if new:
            self.model = self._build_lstm_model(rnn_arch, drop_out, dense_activation)
            adam = Adam(lr = 5e-5)
            self.model.compile(loss='mean_squared_error', optimizer=adam)
        else:
            self.model = load_model(model_file)
        self.checkpointer = ModelCheckpoint(filepath=model_file, verbose=0, save_best_only=True)
        self.early_stopping = EarlyStopping(monitor='val_loss', min_delta=min_delta, patience=patience, verbose=0, mode='auto')
        self.trainable_count = int(np.sum([K.count_params(p) for p in set(self.model.trainable_weights)])) 
Example 12
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, rnn_arch=[2,16,32,64,1], drop_out=0.3,
            model_file="lstm_model.hdf5", new=True, min_delta = 0.0001, patience = 50):
        """Train a RNN using the input data
        rnn_arch: list containing the number of neurons per layer (the number of hidden layers
            is defined implicitly)
        drop_out: drop out used in the RNN
        model_file: name of the file where the model is saved
        new: if True, a new model is created, otherwise an existent model is used        
        """        
        if new:
            self.model = self._build_lstm_model(rnn_arch, drop_out)
            adam = Adam(lr = 5e-5)
            self.model.compile(loss='mean_squared_error', optimizer=adam)
        else:
            self.model = load_model(model_file)
        self.checkpointer = ModelCheckpoint(filepath=model_file, verbose=0, save_best_only=True)
        self.early_stopping = EarlyStopping(monitor='val_loss', min_delta=min_delta, patience=patience, verbose=0, mode='auto')
        self.trainable_count = int(np.sum([K.count_params(p) for p in set(self.model.trainable_weights)])) 
Example 13
Project: Intelligent-Projects-Using-Python   Author: PacktPublishing   File: chatbot.py    MIT License 6 votes vote down vote up
def train_model(self,model,X_train,X_test,y_train,y_test):
        input_y_train = self.include_start_token(y_train)
        print(input_y_train.shape)
        input_y_test = self.include_start_token(y_test)
        print(input_y_test.shape)
        early = EarlyStopping(monitor='val_loss',patience=10,mode='auto')

        checkpoint = ModelCheckpoint(self.outpath + 's2s_model_' + str(self.version) + '_.h5',monitor='val_loss',verbose=1,save_best_only=True,mode='auto')
        lr_reduce = ReduceLROnPlateau(monitor='val_loss',factor=0.5, patience=2, verbose=0, mode='auto')
        model.fit([X_train,input_y_train],y_train, 
		      epochs=self.epochs,
		      batch_size=self.batch_size, 
		      validation_data=[[X_test,input_y_test],y_test], 
		      callbacks=[early,checkpoint,lr_reduce], 
		      shuffle=True)
        return model 
Example 14
Project: big-phoney   Author: repp   File: prediction_model_trainer.py    GNU General Public License v3.0 6 votes vote down vote up
def train(self, prediction_model, weights_path, validation_size=0.2, epochs=100):
        h0 = np.zeros((self.char_input_train.shape[0], prediction_model.hidden_nodes))
        c0 = np.zeros((self.char_input_train.shape[0], prediction_model.hidden_nodes))
        inputs = list(self.phone_input_train.swapaxes(0, 1))
        outputs = list(self.phone_output_train.swapaxes(0, 1))

        callbacks = []
        if validation_size > 0:
            checkpointer = ModelCheckpoint(filepath=weights_path, verbose=1, save_best_only=True)
            stopper = EarlyStopping(monitor='val_loss', patience=3)
            callbacks = [checkpointer, stopper]

        prediction_model.training_model.compile(optimizer='adam', loss='categorical_crossentropy')
        prediction_model.training_model.fit([self.char_input_train, h0, c0] + inputs, outputs,
                                            batch_size=256,
                                            epochs=epochs,
                                            validation_split=validation_size,
                                            callbacks=callbacks)

        if validation_size == 0:
            prediction_model.training_model.save_weights(weights_path) 
Example 15
Project: SimpleMC   Author: ja-vazquez   File: kerasnet.py    GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, params, logL, split=0.8, model=None):
        """Construct predictor from training data."""
        super(KerasNetInterpolation, self).__init__(params, logL, split)

        if model is None:
            self.model = self._default_architecture()
        else:
            self.model = model

        callbacks = [EarlyStopping(monitor='val_loss', mode='min',
                                   min_delta=0.001,
                                   patience=10,
                                   restore_best_weights=True)]

        self.history = self.model.fit(self.params_training,
                                      self.logL_training,
                                      validation_data=(self.params_testing,
                                                       self.logL_testing),
                                      epochs=300,
                                      callbacks=callbacks)

####################### This is the original _default_architecture. Uncomment for recovery 
Example 16
Project: Radical_CR_Encoder   Author: huajianjiu   File: ShapeEmbedding.py    Apache License 2.0 6 votes vote down vote up
def visualize_embedding(picklename):
    f = open(picklename, "rb")
    (full_vocab, real_vocab_number, chara_bukken_revised, word_vocab, char_vocab,
     x1_train, x2_train, x3_train, y_train, x1_val, x2_val, x3_val, y_val,
     x1_test, x2_test, x3_test, y_test) \
        = pickle.load(f)
    f.close()
    print("2")
    model = build_sentence_rnn(real_vocab_number=real_vocab_number, classes=2,
                               char_shape=True, word=False, char=False,
                               cnn_encoder=True, highway=None,
                               nohighway="linear",
                               attention=True, shape_filter=True,
                               char_filter=True)
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=['categorical_crossentropy', "acc"], )
    model.load_weights("checkpoints/Rakuten10000model4_bestloss.hdf5")
    tensor_name = ["time_distributed_1"]
    # tensor_name += next(filter(lambda x: x.name == 'embedding', model.layers)).W.name
    tb_cb = TensorBoard(log_dir="./tflog/", histogram_freq=1, embeddings_freq=1, embeddings_layer_names=tensor_name) # need to fix to get the exact embeddings
    stopper = EarlyStopping(monitor='val_loss', patience=10)
    model.fit(x1_train, y_train, validation_data=(x1_val, y_val), epochs=30, batch_size=BATCH_SIZE,
              callbacks=[tb_cb, stopper]) 
Example 17
Project: PJ_NLP   Author: moxiu2012   File: train.py    Apache License 2.0 6 votes vote down vote up
def train():
    # load data
    train_dataset = Dataset(training=True)
    dev_dataset = Dataset(training=False)

    # model
    MODEL = name_model[model_name]
    model = MODEL(train_dataset.vocab_size, conf.n_classes, train_dataset.emb_mat)

    # callback
    my_callback = MyCallback()
    f1 = F1(dev_dataset.gen_batch_data(), dev_dataset.steps_per_epoch)
    checkpointer = ModelCheckpoint('data/{}.hdf5'.format(model_name), save_best_only=True)
    early_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='auto')

    # train
    model.compile(optimizer=keras.optimizers.Adam(),
                  loss=keras.losses.categorical_crossentropy, metrics=['acc'])
    model.fit_generator(train_dataset.gen_batch_data(),
                        steps_per_epoch=train_dataset.steps_per_epoch,
                        verbose=0,
                        epochs=conf.epochs, callbacks=[my_callback, checkpointer, early_stop, f1])
    keras.models.save_model(model, conf.model_path.format(model_name)) 
Example 18
Project: workspace_2017   Author: nwiizo   File: test_callbacks.py    MIT License 6 votes vote down vote up
def test_EarlyStopping_reuse():
    patience = 3
    data = np.random.random((100, 1))
    labels = np.where(data > 0.5, 1, 0)
    model = Sequential((
        Dense(1, input_dim=1, activation='relu'),
        Dense(1, activation='sigmoid'),
    ))
    model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])
    stopper = callbacks.EarlyStopping(monitor='acc', patience=patience)
    weights = model.get_weights()

    hist = model.fit(data, labels, callbacks=[stopper])
    assert len(hist.epoch) >= patience

    # This should allow training to go for at least `patience` epochs
    model.set_weights(weights)
    hist = model.fit(data, labels, callbacks=[stopper])
    assert len(hist.epoch) >= patience 
Example 19
Project: deeplogic   Author: nuric   File: train.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def train():
  """Train the given model saving weights to model_file."""
  # Setup callbacks
  callbacks = [C.ModelCheckpoint(filepath=MODEL_WF,
                                 verbose=1,
                                 save_best_only=True,
                                 save_weights_only=True),
               ThresholdStop(),
               C.EarlyStopping(monitor='loss', patience=10, verbose=1),
               C.TerminateOnNaN()]
  # Big data machine learning in the cloud
  ft = "data/{}_task{}.txt"
  model = create_model(iterations=ARGS.iterations)
  # For long running training swap in stateful checkpoint
  callbacks[0] = StatefulCheckpoint(MODEL_WF, MODEL_SF,
                                    verbose=1, save_best_only=True,
                                    save_weights_only=True)
  tasks = ARGS.tasks or range(1, 13)
  traind = LogicSeq.from_files([ft.format("train", i) for i in tasks], ARGS.batch_size, pad=ARGS.pad)
  vald = LogicSeq.from_files([ft.format("val", i) for i in tasks], ARGS.batch_size, pad=ARGS.pad)
  model.fit_generator(traind, epochs=ARGS.epochs,
                      callbacks=callbacks,
                      validation_data=vald,
                      verbose=1, shuffle=True,
                      initial_epoch=callbacks[0].get_last_epoch()) 
Example 20
Project: MalConv-keras   Author: j40903272   File: train.py    MIT License 6 votes vote down vote up
def train(model, max_len=200000, batch_size=64, verbose=True, epochs=100, save_path='../saved/', save_best=True):
    
    # callbacks
    ear = EarlyStopping(monitor='val_acc', patience=5)
    mcp = ModelCheckpoint(join(save_path, 'malconv.h5'), 
                          monitor="val_acc", 
                          save_best_only=save_best, 
                          save_weights_only=False)
    
    history = model.fit_generator(
        utils.data_generator(x_train, y_train, max_len, batch_size, shuffle=True),
        steps_per_epoch=len(x_train)//batch_size + 1,
        epochs=epochs, 
        verbose=verbose, 
        callbacks=[ear, mcp],
        validation_data=utils.data_generator(x_test, y_test, max_len, batch_size),
        validation_steps=len(x_test)//batch_size + 1)
    return history 
Example 21
Project: Kaggle-Statoil-Challenge   Author: adodd202   File: VGG16_BottleNeck.py    MIT License 5 votes vote down vote up
def get_callbacks(filepath, patience=2):
   es = EarlyStopping('val_loss', patience=10, mode="min")
   msave = ModelCheckpoint(filepath, save_best_only=True)
   return [es, msave] 
Example 22
Project: EUSIPCO2017   Author: Veleslavia   File: training.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def train(self):
        model = self.model_module.build_model(IRMAS_N_CLASSES)

        early_stopping = EarlyStopping(monitor='val_loss', patience=EARLY_STOPPING_EPOCH)
        save_clb = ModelCheckpoint(
            "{weights_basepath}/{model_path}/".format(
                weights_basepath=MODEL_WEIGHT_BASEPATH,
                model_path=self.model_module.BASE_NAME) +
            "epoch.{epoch:02d}-val_loss.{val_loss:.3f}-fbeta.{val_fbeta_score:.3f}"+"-{key}.hdf5".format(
                key=self.model_module.MODEL_KEY),
            monitor='val_loss',
            save_best_only=True)
        lrs = LearningRateScheduler(lambda epoch_n: self.init_lr / (2**(epoch_n//SGD_LR_REDUCE)))
        model.summary()
        model.compile(optimizer=self.optimizer,
                      loss='categorical_crossentropy',
                      metrics=['accuracy', fbeta_score])

        history = model.fit_generator(self._batch_generator(self.X_train, self.y_train),
                                      samples_per_epoch=self.model_module.SAMPLES_PER_EPOCH,
                                      nb_epoch=MAX_EPOCH_NUM,
                                      verbose=2,
                                      callbacks=[save_clb, early_stopping, lrs],
                                      validation_data=self._batch_generator(self.X_val, self.y_val),
                                      nb_val_samples=self.model_module.SAMPLES_PER_VALIDATION,
                                      class_weight=None,
                                      nb_worker=1)

        pickle.dump(history.history, open('{history_basepath}/{model_path}/history_{model_key}.pkl'.format(
            history_basepath=MODEL_HISTORY_BASEPATH,
            model_path=self.model_module.BASE_NAME,
            model_key=self.model_module.MODEL_KEY),
            'w')) 
Example 23
Project: dsl-char-cnn   Author: boknilev   File: cnn_multifilter_cv.py    MIT License 5 votes vote down vote up
def train_and_evaluate_model(model, X_train, Y_train, X_test, Y_test, y_test, fold):
    # y_test is labels, Y_test is categorical labels
    
    print('Train...')
    stopping = EarlyStopping(monitor='val_loss', patience='10')
    model_filename = "cnn_model_gpu_multifilter_fold{}.hdf5".format(fold)
    checkpointer = ModelCheckpoint(filepath=model_filename, verbose=1, save_best_only=True)
    model.fit(X_train, Y_train,
              batch_size=batch_size,
              nb_epoch=nb_epoch,
              validation_data=(X_test, Y_test), 
              callbacks=[stopping,checkpointer],
              verbose=2)
    probabilities = model.predict(X_test, batch_size=batch_size)
    predictions = probabilities.argmax(axis=-1)
    acc = accuracy_score(y_test, predictions)
    print('Accuracy score (final model): {}'.format(acc))
    best_model = load_model(model_filename)
    probabilities = best_model.predict(X_test, batch_size=batch_size)
    predictions = probabilities.argmax(axis=-1)
    best_acc = accuracy_score(y_test, predictions)
    print('Accuracy score (best model): {}'.format(best_acc))
    return best_acc
    

# run cross validation (based on: https://github.com/fchollet/keras/issues/1711) 
Example 24
Project: BlurbGenreCollection-HMC   Author: uhh-lt   File: main.py    Apache License 2.0 5 votes vote down vote up
def train(model, save = True, early_stopping = True, validation = True):
    """
    Trains a neural network, can use early_stopping and validationsets
    """

    print("Traning Model...")
    callbacks_list = []
    lr_decay = LearningRateScheduler(schedule=lambda epoch: args.learning_rate * (args.learning_decay ** epoch))
    callbacks_list.append(lr_decay)
    if early_stopping:
        early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.001, patience=3, verbose=0, mode='auto')
        callbacks_list.append(early_stopping)
    if validation:
        metrics_callback = Metrics_eval(validation_data = (data['X_dev'], data['y_dev']))
        callbacks_list.append(metrics_callback)
        model.fit(data['X_train'], data['y_train'], batch_size=args.batch_size, epochs=args.epochs,
         verbose=1, callbacks=callbacks_list, validation_data=(data['X_dev'], data['y_dev'])) # starts training
    else:
        metrics_callback = Metrics_eval(validation_data = (data['X_test'], data['y_test']))
        callbacks_list.append(metrics_callback)
        model.fit(data['X_train'], data['y_train'], batch_size=args.batch_size, epochs=args.epochs, verbose=1,
         callbacks = callbacks_list)


    if save:
        save_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'models')
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        print("Saving current Model")
        model.save(os.path.join(save_path, args.filename + '.h5')) 
Example 25
Project: kutils   Author: subpic   File: model_helper.py    MIT License 5 votes vote down vote up
def _callbacks(self):
        """Setup callbacks"""
        p = self.params
        log_dir = os.path.join(self.params.logs_root, self.model_name())
        if p.histogram_freq:
            valid_gen = self.make_generator(self.ids[self.ids.set=='validation'], 
                                            deterministic=True, fixed_batches=True)
            tb_callback = TensorBoardWrapper(valid_gen, log_dir=log_dir, 
                                      write_images=p.write_images, 
                                      histogram_freq=p.histogram_freq, 
                                      write_graph=p.write_graph)
        else:
            tb_callback = TensorBoard(log_dir=log_dir,
                                      write_graph=p.write_graph, 
                                      histogram_freq=0, 
                                      write_images=p.write_images)
        
        tb_callback.set_model(self.model)
        best_model_path = os.path.join(self.params.models_root, 
                                       self.model_name() + '_best_weights.h5')
        make_dirs(best_model_path)
        checkpointer = ModelCheckpoint(filepath = best_model_path, verbose=0,
                                       monitor  = p.monitor_metric, 
                                       mode     = p.monitor_mode, 
                                       period   = p.checkpoint_period,
                                       save_best_only    = p.save_best_only,
                                       save_weights_only = True)
        earlystop = EarlyStopping(monitor=p.monitor_metric, 
                                  patience=p.early_stop_patience, 
                                  mode=p.monitor_mode)
        return [tb_callback, earlystop, checkpointer] 
Example 26
Project: OverwatchML   Author: sshh12   File: OverwatchPredictHeroSR.py    MIT License 5 votes vote down vote up
def get_hero_model(hero, hero_data=None, from_file=False):
    
    if from_file:
        
        model = load_model(os.path.join('..', 'models', '{}-sr.h5'.format(hero)))
        
        scaler_X = joblib.load(os.path.join('..', 'models', '{}-sr.pkl'.format(hero)))
        
        return None, model, scaler_X
    
    X, y, scaler_X = hero_data

    model = get_model(hero)
    
    ## Callbacks ##
    reduce_LR = ReduceLROnPlateau(monitor='val_loss', factor=0.7, patience=10, min_lr=1e-8, verbose=0)
    e_stopping = EarlyStopping(patience=35)
    checkpoint = ModelCheckpoint(os.path.join('..', 'models', '{}-sr.h5'.format(hero)), 
                                 monitor='val_acc_metric', 
                                 verbose=0,
                                 save_best_only=True)
    ###############
    
    joblib.dump(scaler_X, os.path.join('..', 'models', '{}-sr.pkl'.format(hero)))

    history = train_model(model, X, y, epochs=2000, batch_size=64, callbacks=[reduce_LR, e_stopping, checkpoint])
    
    return history, model, scaler_X


# In[7]:

# Predict 
Example 27
Project: NeuroTrajectory   Author: RovisLab   File: grid_search.py    GNU General Public License v3.0 5 votes vote down vote up
def train(self):
        model = self.model_train(np.shape(self.data_set.X_train), self.current_parameters)
        early_stopper = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=5, verbose=0, mode='auto')

        params_csv = list()
        for p in self.current_parameters:
            params_csv.append(str(p))

        params_csv.append(str(self.epochs))
        params_csv.append(str(self.batch_size))

        print(params_csv)

        history = TrainingHistoryPlot(self.path, self.data_set, params_csv)
        model.fit(self.data_set.X_train, self.data_set.Y_train,
                  batch_size=self.batch_size,
                  epochs=self.epochs,
                  verbose=1,
                  validation_data=(self.data_set.X_valid, self.data_set.Y_valid),
                  callbacks=[early_stopper, history])

        score = model.evaluate(self.data_set.X_valid, self.data_set.Y_valid, verbose=0)

        params_csv.append(str(score[1]))
        row = params_csv
        self.writer.writerow(row)

        K.clear_session() 
Example 28
Project: NeuroTrajectory   Author: RovisLab   File: genome.py    GNU General Public License v3.0 5 votes vote down vote up
def train_and_score(self, model_train, dataset, path):
        logging.info("Getting training samples")
        logging.info("Compling Keras model")

        batch_size = self.geneparam['batch_size']
        epochs = self.geneparam['epochs']

        parameters = list()

        for p in self.all_possible_genes:
            if p is 'batch_size':
                continue
            elif p is 'epochs':
                continue
            else:
                parameters.append(self.geneparam[p])

        print(parameters)
        input_shape = np.shape(dataset.X_train)
        model = model_train(input_shape, parameters)

        parameters.append(self.geneparam['batch_size'])
        parameters.append(self.geneparam['epochs'])
        # Helper: Early stopping.
        early_stopper = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=5, verbose=0, mode='auto')
        history = TrainingHistoryPlot(path, dataset, parameters)
        model.fit(dataset.X_train, dataset.Y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  verbose=1,
                  validation_data=(dataset.X_valid, dataset.Y_valid),
                  callbacks=[early_stopper, history])

        score = model.evaluate(dataset.X_valid, dataset.Y_valid, verbose=0)
        K.clear_session()
        # we do not care about keeping any of this in memory -
        # we just need to know the final scores and the architecture

        # 1 is accuracy. 0 is loss.
        return score[1] 
Example 29
Project: EUSIPCO2017   Author: ronggong   File: keras_dnn_danAll.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def f_nn(params):
    from keras.models import Sequential
    from keras.layers.core import Dense, Dropout, Activation

    print ('Params testing: ', params)
    model = Sequential()
    model.add(Dense(output_dim=params['units1'], input_dim = X_train.shape[1]))
    model.add(Activation(params['activation']))
    model.add(Dropout(params['dropout1']))

    model.add(Dense(output_dim=params['units1'], init = "glorot_uniform"))
    model.add(Activation(params['activation']))
    model.add(Dropout(params['dropout1']))

    if params['choice']['layers'] == 'three':
        model.add(Dense(output_dim=params['units1'], init="glorot_uniform"))
        model.add(Activation(params['activation']))
        model.add(Dropout(params['dropout1']))

    model.add(Dense(29))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=params['optimizer'], metrics = ['accuracy'])

    callbacks = [EarlyStopping(monitor='val_loss', patience=5, verbose=0)]

    model.fit(X_train, Y_train,
              nb_epoch=params['nb_epochs'],
              batch_size=params['batch_size'],
              validation_data = (X_validation, Y_validation),
              callbacks=callbacks,
              verbose = 0)

    score, acc = model.evaluate(X_validation, Y_validation, batch_size = 128, verbose = 0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK} 
Example 30
Project: EUSIPCO2017   Author: ronggong   File: keras_dnn_laosheng.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def f_nn(params):
    from keras.models import Sequential
    from keras.layers.core import Dense, Dropout, Activation

    print ('Params testing: ', params)
    model = Sequential()
    model.add(Dense(output_dim=params['units1'], input_dim = X_train.shape[1]))
    model.add(Activation(params['activation']))
    model.add(Dropout(params['dropout1']))

    model.add(Dense(output_dim=params['units1'], init = "glorot_uniform"))
    model.add(Activation(params['activation']))
    model.add(Dropout(params['dropout1']))

    if params['choice']['layers'] == 'three':
        model.add(Dense(output_dim=params['units1'], init="glorot_uniform"))
        model.add(Activation(params['activation']))
        model.add(Dropout(params['dropout1']))

    model.add(Dense(29))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=params['optimizer'], metrics = ['accuracy'])

    callbacks = [EarlyStopping(monitor='val_loss', patience=5, verbose=0)]

    model.fit(X_train, Y_train,
              nb_epoch=params['nb_epochs'],
              batch_size=params['batch_size'],
              validation_data = (X_validation, Y_validation),
              callbacks=callbacks,
              verbose = 0)

    score, acc = model.evaluate(X_validation, Y_validation, batch_size = 128, verbose = 0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK} 
Example 31
Project: carnd-cloning   Author: jdleesmiller   File: model.py    MIT License 5 votes vote down vote up
def train(model, nb_epoch, patience,
        nb_train, batch_files_train, nb_val, batch_files_val,
        save_stem=None):
    """
    Train the model.
    Stop training when validation error increases and save the model and the
    weights with the best validation error.
    """

    callbacks = [EarlyStopping(patience=patience)]
    if save_stem is not None:
        with open(save_stem + '.json', 'w') as model_file:
            model_file.write(model.to_json())
        callbacks.append(ModelCheckpoint(
            save_stem + '.h5', save_best_only=True, save_weights_only=True))

    training_generator = generate_data(batch_files_train)
    validation_generator = generate_data(batch_files_val)

    history = model.fit_generator(
        training_generator,
        samples_per_epoch=nb_train,
        nb_epoch=nb_epoch,
        validation_data=validation_generator,
        nb_val_samples=nb_val,
        callbacks=callbacks)

    return history 
Example 32
Project: group-ksparse-temporal-cnns   Author: srph25   File: keraswtacnn.py    MIT License 5 votes vote down vote up
def train(self, X_train, X_val=None):
        assert(isinstance(X_train, np.ndarray) or isinstance(X_train, Sequence))
        if X_val is not None:
            assert(isinstance(X_val, np.ndarray) or isinstance(X_val, Sequence))
        # Fit!
        hdf5 = self.results_dir + '/autoencoder.hdf5'
        if os.path.exists(hdf5):
            os.remove(hdf5)
        model_checkpoint = ModelCheckpoint(filepath=hdf5, save_best_only=True if X_val is not None else False, save_weights_only=True)
        early_stopping = EarlyStopping(patience=self.config['patience'])
        terminate_on_nan = TerminateOnNaN()
        if isinstance(X_train, np.ndarray):
            self.autoencoder.fit(X_train, X_train,
                                 batch_size=self.config['batch_size'],
                                 epochs=self.config['epochs'],
                                 verbose=1,
                                 callbacks=[model_checkpoint, early_stopping, terminate_on_nan] if X_val is not None else [model_checkpoint, terminate_on_nan],
                                 validation_data=(X_val, X_val) if X_val is not None else None)
        elif isinstance(X_train, Sequence):
            self.autoencoder.fit_generator(X_train,
                                           epochs=self.config['epochs'],
                                           verbose=1,
                                           callbacks=[model_checkpoint, early_stopping, terminate_on_nan] if X_val is not None else [model_checkpoint, terminate_on_nan],
                                           validation_data=X_val if X_val is not None else None,
                                           shuffle=True, workers=14)
        self.autoencoder.load_weights(hdf5)
        self.autoencoder_base.save_weights(hdf5)
        self.plot_decoder_weights() 
Example 33
Project: group-ksparse-temporal-cnns   Author: srph25   File: keraswtacnn.py    MIT License 5 votes vote down vote up
def train_classifier(self, X_train, Y_train=None, X_val=None, Y_val=None, stacked=True):
        assert((isinstance(X_train, np.ndarray) and isinstance(Y_train, np.ndarray)) or isinstance(X_train, Sequence))
        if X_val is not None:
            assert((isinstance(X_val, np.ndarray) and isinstance(Y_val, np.ndarray)) or isinstance(X_val, Sequence))
        hdf5 = self.results_dir + '/supervised_' + str(stacked) + '.hdf5'
        if os.path.exists(hdf5):
            os.remove(hdf5)
        model_checkpoint = ModelCheckpoint(filepath=hdf5, save_best_only=True if (X_val is not None and Y_val is not None) else False, save_weights_only=True)
        early_stopping = EarlyStopping(patience=self.config['patience'])
        terminate_on_nan = TerminateOnNaN()
        if stacked is True:
            model = self.stacked_classifier
            model_base = self.stacked_classifier_base
        else:
            model = self.endtoend_classifier
            model_base = self.endtoend_classifier_base
        if isinstance(X_train, np.ndarray) and isinstance(Y_train, np.ndarray):
            model.fit(X_train, Y_train,
                      batch_size=self.config['batch_size'],
                      epochs=self.config['epochs'],
                      verbose=1,
                      callbacks=[model_checkpoint, early_stopping, terminate_on_nan] if (X_val is not None and Y_val is not None) else [model_checkpoint, terminate_on_nan],
                      validation_data=(X_val, Y_val) if (X_val is not None and Y_val is not None) else None,
                      class_weight=compute_class_weight('balanced', np.unique(np.argmax(Y_train, axis=-1)), np.argmax(Y_train, axis=-1)))
        elif isinstance(X_train, Sequence):
            model.fit_generator(X_train,
                                epochs=self.config['epochs'],
                                verbose=1,
                                callbacks=[model_checkpoint, early_stopping, terminate_on_nan] if (X_val is not None and Y_val is not None) else [model_checkpoint, terminate_on_nan],
                                validation_data=X_val if X_val is not None else None,
                                class_weight=compute_class_weight('balanced', np.unique(np.argmax(Y_train, axis=-1)), np.argmax(Y_train, axis=-1)) if Y_train is not None else None,
                                shuffle=True, workers=14)
        model.load_weights(hdf5)
        model_base.save_weights(hdf5) 
Example 34
Project: hwr-address   Author: jsyqrt   File: hccrn.py    GNU General Public License v3.0 5 votes vote down vote up
def model_train(model, dataset, batch_size, weights_path, history_path, nb_epoch=200, samples_per_epoch=1000000):
	checkpointer = ModelCheckpoint(filepath=weights_path, verbose=1, save_best_only=False, save_weights_only=True)
	#lrate = LearningRateScheduler(step_decay)
	lrate = ReduceLROnPlateau(monitor='loss', factor=0.3, patience=5, verbose=0, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0.00001)
	#early_stop = EarlyStopping(monitor='loss', patience=5, verbose=0, mode='auto')
	history = model.fit_generator(input_data.generate_data(dataset, batch_size), samples_per_epoch, nb_epoch, callbacks=[checkpointer, lrate])
	with open(history_path, 'w') as f:
		f.write(str(history.history))
	return model 
Example 35
Project: Q-A-Recommender-System-Machine-Learning   Author: Yuanxiang-Wu   File: deeplearning.py    Apache License 2.0 5 votes vote down vote up
def model_Train(X_tr, Y_tr, arch, actfn='sigmoid', last_act='sigmoid', reg_coeff=0.0,
                num_epoch=100, batch_size=1000, sgd_lr=1e-5, sgd_decay=0.0, sgd_mom=0.0,
                    sgd_Nesterov=False, EStop=False):
    call_ES = EarlyStopping(monitor='val_acc', patience=6, mode='auto')
    model = gen_Model(num_units=arch, actfn=actfn, reg_coeff=reg_coeff, last_act=last_act)
    sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

    if EStop:
        model.fit(X_tr, Y_tr, nb_epoch=num_epoch, batch_size=batch_size, callbacks=[call_ES],
                   validation_split=0.1, validation_data=None, shuffle=True)
    else:
        model.fit(X_tr, Y_tr, batch_size=100, nb_epoch=10, shuffle=True, verbose=1,show_accuracy=True,validation_split=0.2)
    return model 
Example 36
Project: YOLO3-keras   Author: aboerzel   File: train.py    MIT License 5 votes vote down vote up
def create_callbacks(saved_weights_name, tensorboard_logs, model_to_save):
    makedirs(tensorboard_logs)

    early_stop = EarlyStopping(
        monitor='loss',
        min_delta=0.01,
        patience=5,
        mode='min',
        verbose=1
    )
    checkpoint = CustomModelCheckpoint(
        model_to_save=model_to_save,
        filepath=saved_weights_name,  # + '{epoch:02d}.h5',
        monitor='loss',
        verbose=1,
        save_best_only=True,
        mode='min',
        period=1
    )
    reduce_on_plateau = ReduceLROnPlateau(
        monitor='loss',
        factor=0.1,
        patience=2,
        verbose=1,
        mode='min',
        min_delta=0.01,
        cooldown=0,
        min_lr=0
    )
    tensorboard = CustomTensorBoard(
        log_dir=tensorboard_logs,
        write_graph=True,
        write_images=True,
    )
    return [early_stop, checkpoint, reduce_on_plateau, tensorboard] 
Example 37
Project: what-celebrity   Author: dansbecker   File: model.py    MIT License 5 votes vote down vote up
def model_from_thumbnails(train_x, train_y, val_x, val_y):
    n_obs, n_channels, n_rows, n_cols = train_x.shape
    n_classes = y.shape[1]

    model = Sequential()
    model.add(Convolution2D(32, 2, 2, border_mode='valid',
                            activation='relu',
                            input_shape=(n_channels, n_rows, n_cols)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(64, 2, 2, border_mode='valid',
                            activation='relu'))
    model.add(Convolution2D(64, 2, 2, border_mode='valid',
                            activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(64, 2, 2, border_mode='valid',
                            activation='relu'))

    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(100, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(100, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_classes, activation='softmax'))
    optimizer = Adam()
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    stopper = EarlyStopping(monitor='val_loss', patience=15, verbose=0, mode='auto')

    model.fit(train_x, train_y, shuffle=True,
                        nb_epoch=100, validation_data=(val_x, val_y),
                        callbacks = [stopper])
    return model 
Example 38
Project: RPGOne   Author: RTHMaK   File: trainer.py    Apache License 2.0 5 votes vote down vote up
def _get_callbacks(self):
        """
         Returns a set of Callbacks which are used to perform various functions within Keras' .fit method.
         Here, we use an early stopping callback to add patience with respect to the validation metric and
         a Lambda callback which performs the model specific callbacks which you might want to build into
         a model, such as re-encoding some background knowledge.

         Additionally, there is also functionality to create Tensorboard log files. These can be visualised
         using 'tensorboard --logdir /path/to/log/files' after training.
        """
        early_stop = EarlyStopping(monitor=self.validation_metric, patience=self.patience)
        model_callbacks = LambdaCallback(on_epoch_begin=lambda epoch, logs: self._pre_epoch_hook(epoch),
                                         on_epoch_end=lambda epoch, logs: self._post_epoch_hook(epoch))
        callbacks = [early_stop, model_callbacks]

        if self.tensorboard_log is not None:
            if K.backend() == 'theano':
                raise ConfigurationError("Tensorboard logging is only compatibile with Tensorflow. "
                                         "Change the backend using the KERAS_BACKEND environment variable.")
            tensorboard_visualisation = TensorBoard(log_dir=self.tensorboard_log,
                                                    histogram_freq=self.tensorboard_histogram_freq)
            callbacks.append(tensorboard_visualisation)

        if self.debug_params:
            debug_callback = LambdaCallback(on_epoch_end=lambda epoch, logs:
                                            self._debug(self.debug_params["layer_names"],
                                                        self.debug_params.get("masks", []), epoch))
            callbacks.append(debug_callback)
            return CallbackList(callbacks)

        # Some witchcraft is happening here - we don't specify the epoch replacement variable
        # checkpointing string, because Keras does that within the callback if we specify it here.
        if self.save_models:
            checkpointing = ModelCheckpoint(self.model_prefix + "_weights_epoch={epoch:d}.h5",
                                            save_best_only=True, save_weights_only=True,
                                            monitor=self.validation_metric)
            callbacks.append(checkpointing)

        return CallbackList(callbacks) 
Example 39
Project: Radical_CR_Encoder   Author: huajianjiu   File: ShapeEmbedding.py    Apache License 2.0 5 votes vote down vote up
def train_and_test_model(model, x_train, y_train, x_val, y_val, x_test, y_test, model_name, early_stop=False):
    # model = to_multi_gpu(model)
    print(model_name)
    reducelr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5)
    if early_stop:
        stopper = EarlyStopping(monitor='val_loss', patience=10)
    checkpoint_loss = ModelCheckpoint(filepath="checkpoints/" + model_name + "_bestloss.hdf5", monitor="val_loss",
                                      verbose=VERBOSE, save_best_only=True, mode="min")
    print("complisng...")
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=['categorical_crossentropy', "acc"], )
    print("fitting...")
    if early_stop:
        result = model.fit(x_train, y_train, validation_data=(x_val, y_val), verbose=VERBOSE,
                           epochs=EPOCHS, batch_size=BATCH_SIZE, callbacks=[reducelr, stopper, checkpoint_loss])
    else:
        result = model.fit(x_train, y_train, validation_data=(x_val, y_val), verbose=VERBOSE,
                           epochs=EPOCHS, batch_size=BATCH_SIZE, callbacks=[reducelr, checkpoint_loss])
    model.load_weights("checkpoints/" + model_name + "_bestloss.hdf5")
    print("testing...")
    scores = model.evaluate(x_test, y_test, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))
    print("%s: %.2f%%" % (model.metrics_names[2], scores[2] * 100))

    predict_value(model, model_name, x_test)

    return result 
Example 40
Project: world_models   Author: llSourcell   File: arch.py    MIT License 5 votes vote down vote up
def train(self, rnn_input, rnn_output, validation_split = 0.2):

        earlystop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, verbose=1, mode='auto')
        callbacks_list = [earlystop]

        self.model.fit(rnn_input, rnn_output,
            shuffle=True,
            epochs=EPOCHS,
            batch_size=BATCH_SIZE,
            validation_split=validation_split,
            callbacks=callbacks_list)

        self.model.save_weights('./rnn/weights.h5') 
Example 41
Project: world_models   Author: llSourcell   File: arch.py    MIT License 5 votes vote down vote up
def train(self, data, validation_split = 0.2):

        earlystop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, verbose=1, mode='auto')
        callbacks_list = [earlystop]

        self.model.fit(data, data,
                shuffle=True,
                epochs=EPOCHS,
                batch_size=BATCH_SIZE,
                validation_split=validation_split,
                callbacks=callbacks_list)
        
        self.model.save_weights('./vae/weights.h5') 
Example 42
Project: FormicID   Author: naturalis   File: logger.py    MIT License 5 votes vote down vote up
def build_es(
    monitor="val_loss", min_delta=0, patience=10, verbose=1, mode="min"
):
    """For initializing EarlyStopping. Training will stop when validation loss
    is not decreasing anymore. This monitors validation loss.

    Args:
        monitor (str): quantity to be monitored. Defaults to val_loss.
        min_delta (int): minimum change in the monitored quantity to qualify
            as an improvement, i.e. an absolute change of less than min_delta,
            will count as no improvement. Defaults to 0.
        patience (int): number of epochs with no improvement after which
            training will be stopped. Defaults to 10.
        verbose (int): verbosity mode. Defaults to 1.
        mode (str): one of {auto, min, max}. In min mode, training will stop
            when the quantity monitored has stopped decreasing; in max mode it
            will stop when the quantity monitored has stopped increasing; in
            auto mode, the direction is automatically inferred from the name
            of the monitored quantity. Defaults to min.

    Returns:
        EarlyStopping callback

    """
    es = EarlyStopping(
        monitor=monitor,
        min_delta=min_delta,
        patience=patience,
        verbose=verbose,
        mode=mode,
    )

    return es


# Reduce learning rate on plateau
############################################################################### 
Example 43
Project: PClub-Project   Author: rharish101   File: sentiment_wv.py    MIT License 5 votes vote down vote up
def train_nn(tweets=None, labels=None, nn_model=None):
    if not tweets and not labels:
        tweets, labels = init_with_wv()
    elif tweets and labels:
        pass
    else:
        print "One of tweets or labels given, but not the other"
        return
    if not nn_model:
        max_tweet_len = max([len(tweet) for tweet in tweets])
        nn_model = get_nn(max_tweet_len)

    # Callbacks (extra features)
    tb_callback = TensorBoard(log_dir='./Tensorboard/' + str(time.time()))
    early_stop = EarlyStopping(monitor='loss', min_delta=0.1, patience=10)
    lr_reducer = ReduceLROnPlateau(monitor='loss', factor=0.5, min_lr=0.00001,
                                patience=3, epsilon=0.2)

    nn_model.fit(tweets, labels, epochs=10, batch_size=32, callbacks=
                [tb_callback, early_stop, lr_reducer], validation_split=0.2)
    nn_model.save('model_nn.h5')
    print "Saved model"
    del tweets
    del labels
    tweets_test, labels_test, _ = init_with_vocab(type_data='test')
    print nn_model.evaluate(tweets_test, labels_test, batch_size=32) 
Example 44
Project: SumQE   Author: nlpaueb   File: train_BiGRU.py    MIT License 5 votes vote down vote up
def train(train_path, human_metric, path_to_save, mode, **params):
    """
    Train the BiGRU model
    :param train_path: Path to the train data in order to load them.
    :param human_metric: The metric for which the model is trained. It is needed only on 'Single Task' mode.
    :param path_to_save: The path where we will save the model. If SAVE_MODELS=True.
    :param mode: Depending on your choice : ['Single Task', 'Multi Task-1', 'Multi Task-5'].
    :returns The trained model
    """

    K.clear_session()

    train_x, train_y = load_train_data(train_path=train_path, human_metric=human_metric, mode=mode)

    # First dimension of shape is not used
    model = compile_bigrus_attention(
        shape=(300, 300),
        n_hidden_layers=params['HL'],
        hidden_units_size=params['HU'],
        dropout_rate=params['D'],
        word_dropout_rate=params['WD'],
        lr=params['LR'],
        mode=mode
    )

    early = EarlyStopping(monitor='val_loss', patience=10, verbose=1, baseline=None, restore_best_weights=False)

    model.fit(train_x, train_y, epochs=params["EPOCHS"], batch_size=params['BS'], callbacks=[early])

    if SAVE_MODELS:
        model.save(path_to_save)

    return model 
Example 45
Project: keras-bert-ner   Author: liushaoweihua   File: callbacks.py    MIT License 5 votes vote down vote up
def best_fit_callbacks(self, callback_configs):
        # add best-fit callbacks
        early_stop_patience = callback_configs.get("early_stop_patience")
        early_stopping = EarlyStopping(monitor="val_crf_accuracy", patience=early_stop_patience)
        self.callbacks.append(early_stopping)
        
        reduce_lr_patience = callback_configs.get("reduce_lr_patience")
        reduce_lr_factor = callback_configs.get("reduce_lr_factor")
        reduce_lr_on_plateau = ReduceLROnPlateau(monitor="val_crf_accuracy", verbose=1, mode="max", factor=reduce_lr_factor, patience=reduce_lr_patience)
        self.callbacks.append(reduce_lr_on_plateau)
        
        save_path = callback_configs.get("save_path")
        checkpoint = ModelCheckpoint(save_path, monitor="val_crf_accuracy", verbose=2, mode="max", save_best_only=True)
        self.callbacks.append(checkpoint)
        return self.callbacks 
Example 46
Project: keras-bert-ner   Author: liushaoweihua   File: callbacks-checkpoint.py    MIT License 5 votes vote down vote up
def best_fit_callbacks(self, callback_configs):
        # add best-fit callbacks
        early_stop_patience = callback_configs.get("early_stop_patience")
        early_stopping = EarlyStopping(monitor="val_crf_accuracy", patience=early_stop_patience)
        self.callbacks.append(early_stopping)
        
        reduce_lr_patience = callback_configs.get("reduce_lr_patience")
        reduce_lr_factor = callback_configs.get("reduce_lr_factor")
        reduce_lr_on_plateau = ReduceLROnPlateau(monitor="val_crf_accuracy", verbose=1, mode="max", factor=reduce_lr_factor, patience=reduce_lr_patience)
        self.callbacks.append(reduce_lr_on_plateau)
        
        save_path = callback_configs.get("save_path")
        checkpoint = ModelCheckpoint(save_path, monitor="val_crf_accuracy", verbose=2, mode="max", save_best_only=True)
        self.callbacks.append(checkpoint)
        return self.callbacks 
Example 47
Project: workspace_2017   Author: nwiizo   File: test_callbacks.py    MIT License 5 votes vote down vote up
def test_EarlyStopping():
    (X_train, y_train), (X_test, y_test) = get_test_data(nb_train=train_samples,
                                                         nb_test=test_samples,
                                                         input_shape=(input_dim,),
                                                         classification=True,
                                                         nb_class=nb_class)
    y_test = np_utils.to_categorical(y_test)
    y_train = np_utils.to_categorical(y_train)
    model = Sequential()
    model.add(Dense(nb_hidden, input_dim=input_dim, activation='relu'))
    model.add(Dense(nb_class, activation='softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])
    mode = 'max'
    monitor = 'val_acc'
    patience = 0
    cbks = [callbacks.EarlyStopping(patience=patience, monitor=monitor, mode=mode)]
    history = model.fit(X_train, y_train, batch_size=batch_size,
                        validation_data=(X_test, y_test), callbacks=cbks, nb_epoch=20)

    mode = 'auto'
    monitor = 'val_acc'
    patience = 2
    cbks = [callbacks.EarlyStopping(patience=patience, monitor=monitor, mode=mode)]
    history = model.fit(X_train, y_train, batch_size=batch_size,
                        validation_data=(X_test, y_test), callbacks=cbks, nb_epoch=20) 
Example 48
Project: MCLNN   Author: fadymedhat   File: trainingcallbacks.py    MIT License 5 votes vote down vote up
def prepare_callbacks(configuration, fold_weights_path, data_loader):
    callback_list = []

    # remote_callback = callbacks.RemoteMonitor(root='http://localhost:9000')
    # callback_list.append(remote_callback)

    # early stopping
    early_stopping_callback = callbacks.EarlyStopping(monitor=configuration.STOPPING_CRITERION,
                                                      patience=configuration.WAIT_COUNT,
                                                      verbose=0,
                                                      mode='auto')
    callback_list.append(early_stopping_callback)

    # save weights at the end of epoch
    weights_file_name_format = 'weights.epoch{epoch:02d}-val_loss{val_loss:.2f}-val_acc{val_acc:.4f}.hdf5'
    checkpoint_callback = ModelCheckpoint(os.path.join(fold_weights_path, weights_file_name_format),
                                          monitor='val_loss', verbose=0,
                                          save_best_only=False, mode='auto')
    callback_list.append(checkpoint_callback)

    # free space of stored weights of early epochs
    directory_house_keeping_callback = DirectoryHouseKeepingCallback(fold_weights_path)
    callback_list.append(directory_house_keeping_callback)

    # call for visualization if it is enabled
    if configuration.SAVE_SEGMENT_PREDICTION_IMAGE_PER_EPOCH == True:
        segment_plot_callback = SegmentPlotCallback(configuration=configuration,
                                                    data_loader=data_loader)
        callback_list.append(segment_plot_callback)

    return callback_list 
Example 49
Project: pycorrector   Author: shibing624   File: train.py    Apache License 2.0 5 votes vote down vote up
def train(train_path='', test_path='', save_vocab_path='', attn_model_path='',
          batch_size=64, epochs=100, maxlen=400, hidden_dim=128, dropout=0.2,
          vocab_max_size=50000, vocab_min_count=5, gpu_id=0):
    source_texts, target_texts = build_dataset(train_path)
    test_input_texts, test_target_texts = build_dataset(test_path)

    # load or save word dict
    if os.path.exists(save_vocab_path):
        vocab2id = load_word_dict(save_vocab_path)
    else:
        print('Training data...')
        vocab2id = read_vocab(source_texts + target_texts, max_size=vocab_max_size, min_count=vocab_min_count)
        num_encoder_tokens = len(vocab2id)
        max_input_texts_len = max([len(text) for text in source_texts])

        print('input_texts:', source_texts[0])
        print('target_texts:', target_texts[0])
        print('num of samples:', len(source_texts))
        print('num of unique input tokens:', num_encoder_tokens)
        print('max sequence length for inputs:', max_input_texts_len)
        save_word_dict(vocab2id, save_vocab_path)

    id2vocab = {int(j): i for i, j in vocab2id.items()}
    print('The vocabulary file:%s, size: %s' % (save_vocab_path, len(vocab2id)))
    model = Seq2seqAttnModel(len(vocab2id),
                             attn_model_path=attn_model_path,
                             hidden_dim=hidden_dim,
                             dropout=dropout,
                             gpu_id=gpu_id
                             ).build_model()
    evaluator = Evaluate(model, attn_model_path, vocab2id, id2vocab, maxlen)
    earlystop = EarlyStopping(monitor='val_loss', patience=3, verbose=1, mode='auto')
    model.fit_generator(data_generator(source_texts, target_texts, vocab2id, batch_size, maxlen),
                        steps_per_epoch=(len(source_texts) + batch_size - 1) // batch_size,
                        epochs=epochs,
                        validation_data=get_validation_data(test_input_texts, test_target_texts, vocab2id, maxlen),
                        callbacks=[evaluator, earlystop]) 
Example 50
Project: ImageDataGenerator-MultiLabel   Author: you359   File: train.py    MIT License 4 votes vote down vote up
def train(train_data_dir, validation_data_dir, model_path):
    # Pre-Trained CNN Model using imagenet dataset for pre-trained weights
    # base_model = Xception(input_shape=(img_width, img_height, 3), weights='imagenet', include_top=False)
    base_model = InceptionV3(input_shape=(img_width, img_height, 3), include_top=False)

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    predictions = layers.Dense(nb_classes, activation='sigmoid')(x)

    # add your top layer block to your base model
    model = Model(base_model.input, predictions)
    print(model.summary())

    for layer in model.layers:
        layer.trainable = True
        layer.kernel_regularizer = l2(0.05)

    model.compile(optimizer='nadam',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       horizontal_flip=True)

    validation_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                        target_size=(img_width, img_height),
                                                        batch_size=batch_size,
                                                        class_mode='multi_categorical')

    validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
                                                                  target_size=(img_width, img_height),
                                                                  batch_size=batch_size,
                                                                  class_mode='multi_categorical')

    # save weights of best training epoch: monitor either val_loss or val_acc
    final_acc_weights_path = os.path.join(os.path.abspath(model_path), 'model_acc_weights.h5')
    final_loss_weights_path = os.path.join(os.path.abspath(model_path), 'model_loss_weights.h5')

    callbacks_list = [
        ModelCheckpoint(final_acc_weights_path, monitor='val_acc', verbose=1, save_best_only=True),
        ModelCheckpoint(final_loss_weights_path, monitor='val_loss', verbose=1, save_best_only=True),
        # EarlyStopping(monitor='val_loss', patience=15, verbose=0),
        TensorBoard(log_dir='graph/train', histogram_freq=0, write_graph=True)
    ]

    # fine-tune the model
    model.fit_generator(train_generator,
                        epochs=nb_epoch,
                        validation_data=validation_generator,
                        callbacks=callbacks_list)

    # save model
    model_json = model.to_json()
    with open(os.path.join(os.path.abspath(model_path), 'model.json'), 'w') as json_file:
        json_file.write(model_json) 
Example 51
Project: ANN   Author: waynezv   File: ANN_large_v23.py    MIT License 4 votes vote down vote up
def train_mlp(self, input, output):
        self.in_real = input.data['real']
        self.in_imag = input.data['imag']
        self.out_real = output.data['real']
        self.out_imag = output.data['imag']

        (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape
        in_dim = i_dim_x*i_dim_y*i_dim_z
        input_data = self.in_real.reshape(in_dim, 1)

        (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape
        out_dim = o_dim_x*o_dim_y*o_dim_z
        output_data = self.out_real.reshape(out_dim, 1)

        model = Sequential()
        model.add(Dense(200, input_dim=in_dim, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(200))#, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(out_dim))#, init='uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='sgd',\
                metrics=['accuracy'])

        early_stop = EarlyStopping(monitor='val_loss', patience=2)
        hist = model.fit(input_data, output_data, nb_epoch=50, \
                         batch_size=64, validation_split=0.2, \
                         shuffle=True, callbacks=[early_stop])
        print(hist.history)
        #TODO: batch train
        model.train_on_batch()

        # Save model
        model_to_save_json = model.to_json()
        open('model_architecture.json', 'w').write(model_to_save_json)
        model_to_save_yaml = model.to_yaml()
        open('model_architecture.yaml', 'w').write(model_to_save_yaml)
        model.save_weights('weights.h5') 
Example 52
Project: ANN   Author: waynezv   File: ANN.py    MIT License 4 votes vote down vote up
def train_mlp(self, input, output):
        self.in_real = input.data['real']
        self.in_imag = input.data['imag']
        self.out_real = output.data['real']
        self.out_imag = output.data['imag']

        (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape
        in_dim = i_dim_x*i_dim_y*i_dim_z
        input_data = self.in_real.reshape(in_dim, 1)

        (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape
        out_dim = o_dim_x*o_dim_y*o_dim_z
        output_data = self.out_real.reshape(out_dim, 1)

        model = Sequential()
        model.add(Dense(200, input_dim=in_dim, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(200))#, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(out_dim))#, init='uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='sgd',\
                metrics=['accuracy'])

        early_stop = EarlyStopping(monitor='val_loss', patience=2)
        hist = model.fit(input_data, output_data, nb_epoch=50, \
                         batch_size=64, validation_split=0.2, \
                         shuffle=True, callbacks=[early_stop])
        print(hist.history)
        #TODO: batch train
        model.train_on_batch()

        # Save model
        model_to_save_json = model.to_json()
        open('model_architecture.json', 'w').write(model_to_save_json)
        model_to_save_yaml = model.to_yaml()
        open('model_architecture.yaml', 'w').write(model_to_save_yaml)
        model.save_weights('weights.h5') 
Example 53
Project: ANN   Author: waynezv   File: ANN_large_v22.py    MIT License 4 votes vote down vote up
def train_mlp(self, input, output):
        self.in_real = input.data['real']
        self.in_imag = input.data['imag']
        self.out_real = output.data['real']
        self.out_imag = output.data['imag']

        (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape
        in_dim = i_dim_x*i_dim_y*i_dim_z
        input_data = self.in_real.reshape(in_dim, 1)

        (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape
        out_dim = o_dim_x*o_dim_y*o_dim_z
        output_data = self.out_real.reshape(out_dim, 1)

        model = Sequential()
        model.add(Dense(200, input_dim=in_dim, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(200))#, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(out_dim))#, init='uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='sgd',\
                metrics=['accuracy'])

        early_stop = EarlyStopping(monitor='val_loss', patience=2)
        hist = model.fit(input_data, output_data, nb_epoch=50, \
                         batch_size=64, validation_split=0.2, \
                         shuffle=True, callbacks=[early_stop])
        print(hist.history)
        #TODO: batch train
        model.train_on_batch()

        # Save model
        model_to_save_json = model.to_json()
        open('model_architecture.json', 'w').write(model_to_save_json)
        model_to_save_yaml = model.to_yaml()
        open('model_architecture.yaml', 'w').write(model_to_save_yaml)
        model.save_weights('weights.h5') 
Example 54
Project: ANN   Author: waynezv   File: ANN_large_v3.py    MIT License 4 votes vote down vote up
def train_mlp(self, input, output):
        self.in_real = input.data['real']
        self.in_imag = input.data['imag']
        self.out_real = output.data['real']
        self.out_imag = output.data['imag']

        (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape
        in_dim = i_dim_x*i_dim_y*i_dim_z
        input_data = self.in_real.reshape(in_dim, 1)

        (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape
        out_dim = o_dim_x*o_dim_y*o_dim_z
        output_data = self.out_real.reshape(out_dim, 1)

        model = Sequential()
        model.add(Dense(200, input_dim=in_dim, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(200))#, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(out_dim))#, init='uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='sgd',\
                metrics=['accuracy'])

        early_stop = EarlyStopping(monitor='val_loss', patience=2)
        hist = model.fit(input_data, output_data, nb_epoch=50, \
                         batch_size=64, validation_split=0.2, \
                         shuffle=True, callbacks=[early_stop])
        print(hist.history)
        #TODO: batch train
        model.train_on_batch()

        # Save model
        model_to_save_json = model.to_json()
        open('model_architecture.json', 'w').write(model_to_save_json)
        model_to_save_yaml = model.to_yaml()
        open('model_architecture.yaml', 'w').write(model_to_save_yaml)
        model.save_weights('weights.h5') 
Example 55
Project: ANN   Author: waynezv   File: ANN_large_v24.py    MIT License 4 votes vote down vote up
def train_mlp(self, input, output):
        self.in_real = input.data['real']
        self.in_imag = input.data['imag']
        self.out_real = output.data['real']
        self.out_imag = output.data['imag']

        (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape
        in_dim = i_dim_x*i_dim_y*i_dim_z
        input_data = self.in_real.reshape(in_dim, 1)

        (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape
        out_dim = o_dim_x*o_dim_y*o_dim_z
        output_data = self.out_real.reshape(out_dim, 1)

        model = Sequential()
        model.add(Dense(200, input_dim=in_dim, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(200))#, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(out_dim))#, init='uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='sgd',\
                metrics=['accuracy'])

        early_stop = EarlyStopping(monitor='val_loss', patience=2)
        hist = model.fit(input_data, output_data, nb_epoch=50, \
                         batch_size=64, validation_split=0.2, \
                         shuffle=True, callbacks=[early_stop])
        print(hist.history)
        #TODO: batch train
        model.train_on_batch()

        # Save model
        model_to_save_json = model.to_json()
        open('model_architecture.json', 'w').write(model_to_save_json)
        model_to_save_yaml = model.to_yaml()
        open('model_architecture.yaml', 'w').write(model_to_save_yaml)
        model.save_weights('weights.h5') 
Example 56
Project: ANN   Author: waynezv   File: ANN_large.py    MIT License 4 votes vote down vote up
def train_mlp(self, input, output):
        self.in_real = input.data['real']
        self.in_imag = input.data['imag']
        self.out_real = output.data['real']
        self.out_imag = output.data['imag']

        (i_dim_x, i_dim_y, i_dim_z) = self.in_real.shape
        in_dim = i_dim_x*i_dim_y*i_dim_z
        input_data = self.in_real.reshape(in_dim, 1)

        (o_dim_x, o_dim_y, o_dim_z) = self.out_real.shape
        out_dim = o_dim_x*o_dim_y*o_dim_z
        output_data = self.out_real.reshape(out_dim, 1)

        model = Sequential()
        model.add(Dense(200, input_dim=in_dim, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(200))#, init='uniform'))
        model.add(Activation('relu'))
        #  model.add(Dropout(0.25))

        model.add(Dense(out_dim))#, init='uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='sgd',\
                metrics=['accuracy'])

        early_stop = EarlyStopping(monitor='val_loss', patience=2)
        hist = model.fit(input_data, output_data, nb_epoch=50, \
                         batch_size=64, validation_split=0.2, \
                         shuffle=True, callbacks=[early_stop])
        print(hist.history)
        #TODO: batch train
        model.train_on_batch()

        # Save model
        model_to_save_json = model.to_json()
        open('model_architecture.json', 'w').write(model_to_save_json)
        model_to_save_yaml = model.to_yaml()
        open('model_architecture.yaml', 'w').write(model_to_save_yaml)
        model.save_weights('weights.h5') 
Example 57
Project: AI_Competition   Author: Decalogue   File: run_capsule_char.py    MIT License 4 votes vote down vote up
def train():
    model_dir = myconf.model.dir
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    maxlen = int(myconf.model.maxlen)
    max_features = int(myconf.model.max_features)
    batch_size = int(myconf.model.batch_size)
    epochs = int(myconf.model.epochs)

    train_data = pd.read_csv(myconf.data.train)
    train_data[myconf.data.src_field] = train_data.apply(lambda x: eval(x[1]), axis=1)
    val_data = pd.read_csv(myconf.data.val)
    val_data[myconf.data.src_field] = val_data.apply(lambda x: eval(x[1]), axis=1)
    if myconf.data.combine == 'yes':
        train_data = pd.concat([train_data, val_data])

    X_train = train_data[myconf.data.src_field].values
    Y_train = [pd.get_dummies(train_data[k])[vocab].values for k in fields]

    tokenizer = text.Tokenizer(num_words=None)
    tokenizer.fit_on_texts(X_train)
    word_index = tokenizer.word_index
    if not os.path.exists(myconf.model.tokenizer):
        with open(myconf.model.tokenizer, 'wb') as handle:
            pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)

    emb_w2v = embeddings(tokenizer, word_index, src="w2v")

    list_tokenized_train = tokenizer.texts_to_sequences(X_train)
    input_train = sequence.pad_sequences(list_tokenized_train, maxlen=maxlen)
    if myconf.data.combine != 'yes':
        X_val = val_data[myconf.data.src_field].values
        Y_val = [pd.get_dummies(val_data[k])[vocab].values for k in fields]
        list_tokenized_val = tokenizer.texts_to_sequences(X_val)
        input_val = sequence.pad_sequences(list_tokenized_val, maxlen=maxlen)

    for i, k in enumerate(fields):
        log([k, "max_f1", "f1", "max_roc_auc", "roc_auc", "precision", "recall"])
        print('\n', k)
        model = TextClassifier().model(embeddings_matrix, maxlen, word_index, 4)
        if i == 0:
            plot_model(model, to_file='capsule.png', show_shapes=True, show_layer_names=False)
        # file_path = model_dir + k + "_{epoch:02d}.hdf5"
        file_path = model_dir + k + "_{epoch:02d}-{val_loss:.2f}.hdf5"
        checkpoint = ModelCheckpoint(file_path, verbose=2, save_weights_only=True)

        # earlystop = EarlyStopping(monitor='val_f1', patience=3, restore_best_weights=True)
        # checkpoint = ModelCheckpoint(file_path, verbose=2, save_weights_only=True, save_best_only=True)
        metrics = Metrics()
        callbacks_list = [checkpoint, metrics]
        # callbacks_list = [metrics, checkpoint, earlystop]
        if myconf.data.combine == 'yes':
            history = model.fit(input_train, Y_train[i], batch_size=batch_size, epochs=epochs,
                             validation_split=0.1, callbacks=callbacks_list, verbose=2)
        else:
            history = model.fit(input_train, Y_train[i], batch_size=batch_size, epochs=epochs,
                             validation_data=(input_val, Y_val[i]), callbacks=callbacks_list, verbose=2)
        del model
        del history
        gc.collect()
        K.clear_session() 
Example 58
Project: AI_Competition   Author: Decalogue   File: run_bigru_char.py    MIT License 4 votes vote down vote up
def train():
    model_dir = myconf.model.dir
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    maxlen = int(myconf.model.maxlen)
    max_features = int(myconf.model.max_features)
    batch_size = int(myconf.model.batch_size)
    epochs = int(myconf.model.epochs)

    train_data = pd.read_csv(myconf.data.train)
    train_data[myconf.data.src_field] = train_data.apply(lambda x: eval(x[1]), axis=1)
    val_data = pd.read_csv(myconf.data.val)
    val_data[myconf.data.src_field] = val_data.apply(lambda x: eval(x[1]), axis=1)
    if myconf.data.combine == 'yes':
        train_data = pd.concat([train_data, val_data])
    X_train = train_data[myconf.data.src_field].values
    Y_train = [pd.get_dummies(train_data[k])[vocab].values for k in fields]

    tokenizer = text.Tokenizer(num_words=None)
    tokenizer.fit_on_texts(X_train)
    word_index = tokenizer.word_index
    if not os.path.exists(myconf.model.tokenizer):
        with open(myconf.model.tokenizer, 'wb') as handle:
            pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)

    emb_w2v = embeddings(tokenizer, word_index, src="w2v")

    list_tokenized_train = tokenizer.texts_to_sequences(X_train)
    input_train = sequence.pad_sequences(list_tokenized_train, maxlen=maxlen)
    if myconf.data.combine != 'yes':
        X_val = val_data[myconf.data.src_field].values
        Y_val = [pd.get_dummies(val_data[k])[vocab].values for k in fields]
        list_tokenized_val = tokenizer.texts_to_sequences(X_val)
        input_val = sequence.pad_sequences(list_tokenized_val, maxlen=maxlen)

    for i, k in enumerate(fields):
        log([k, "max_f1", "f1", "max_roc_auc", "roc_auc", "precision", "recall"])
        print('\n', k)
        model = TextClassifier().model(emb_w2v, maxlen, word_index, 4)
        # model = TextClassifier2().model(emb_w2v, maxlen, word_index, 4)
        if i == 0:
            plot_model(model, to_file='birgu.png', show_shapes=True, show_layer_names=False)
        # file_path = model_dir + k + "_{epoch:02d}.hdf5"
        file_path = model_dir + k + "_{epoch:02d}-{val_loss:.2f}.hdf5"
        checkpoint = ModelCheckpoint(file_path, verbose=2, save_weights_only=True)

        # earlystop = EarlyStopping(monitor='val_f1', patience=5, restore_best_weights=True)
        # checkpoint = ModelCheckpoint(file_path, verbose=2, save_weights_only=True, save_best_only=True)
        metrics = Metrics()
        callbacks_list = [checkpoint, metrics]
        # callbacks_list = [metrics, checkpoint, earlystop]
        if myconf.data.combine == 'yes':
            history = model.fit(input_train, Y_train[i], batch_size=batch_size, epochs=epochs,
                             validation_split=0.1, callbacks=callbacks_list, verbose=2)
        else:
            history = model.fit(input_train, Y_train[i], batch_size=batch_size, epochs=epochs,
                             validation_data=(input_val, Y_val[i]), callbacks=callbacks_list, verbose=2)
        del model
        del history
        gc.collect()
        K.clear_session() 
Example 59
Project: chartAnalyst   Author: huima58   File: chart_analyst.py    Apache License 2.0 4 votes vote down vote up
def predict(train_imgs, train_labels, test_imgs, test_labels, x_pix_num=x_pix_num_default, y_pix_num=y_pix_num_default,
            use_saved_weights=False, weights_file_name=''):
    model = Sequential()
    # use partial VGG16 model
    model.add(ZeroPadding2D((1, 1), input_shape=(1, y_pix_num, x_pix_num)))
    
    base_filter_num = 64
    model.add(Convolution2D(base_filter_num, 3, 3, activation='relu', name='conv1_1'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))  # added this layer to reduce the input size
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(base_filter_num, 3, 3, activation='relu', name='conv1_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(base_filter_num * 2, 3, 3, activation='relu', name='conv2_1'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(base_filter_num *2, 3, 3, activation='relu', name='conv2_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(128, init='uniform', activation='tanh'))
    model.add(Dropout(0.25))
    model.add(Dense(64, init='uniform', activation='tanh'))
    model.add(Dense(3, init='uniform', activation='softmax'))
    
    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer='adadelta',
                  metrics=['accuracy'])
    
    if use_saved_weights:
        model.load_weights(weights_file_name)  #need to install h5py
    else:
        start_time = datetime.today()
        checkpointer = ModelCheckpoint(filepath=weights_file_name, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
        earlyStopping = EarlyStopping(monitor='val_acc', patience=10, verbose=1, mode='max')
        model.fit(train_imgs, train_labels,
                        nb_epoch=30,
                        verbose=1,
                        batch_size=70,
                        validation_split=0.1,
                        callbacks=[checkpointer, earlyStopping])
        model.load_weights(weights_file_name)
        end_time = datetime.today()
        print "----trained time is from " + str(start_time) + " to " + str(end_time)
        
    predict_rst = model.predict_proba(test_imgs, verbose=0)
    return predict_rst 
Example 60
Project: subsync   Author: tympanix   File: train_ann.py    Apache License 2.0 4 votes vote down vote up
def train_ann():
    X, Y = extract_features()

    # Only consider first media file for now
    X, Y = X[0], Y[0]

    shape = (len(X), 1)
    model = ann_model(shape)

    filename = "out/ann.hdf5"

    checkpoint = ModelCheckpoint(filepath=filename, monitor='val_loss', verbose=0, save_best_only=True)
    cutoff = EarlyStopping(monitor='val_loss', min_delta=1E-3, mode='min', patience=5)

    model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.001), metrics=['accuracy'])

    X, Y = prepare_data(X, Y, balance=True)

    print("Label 1:", len(Y[Y==1]))
    print("Label 0:", len(Y[Y==0]))

    # Permutate training data in random order
    rand = np.random.permutation(np.arange(len(Y)))
    X = X[rand]
    Y = Y[rand]

    options = {
        'epochs': 200,
        'batch_size': 32,
        'shuffle': True,
        'validation_split': 0.3,
        'verbose': 2,
        'callbacks': [checkpoint, cutoff]
    }

    print("Training neural network:", filename)
    hist = model.fit(X, Y, **options)

    print('val_loss:', min(hist.history['val_loss']))
    print('val_acc:', max(hist.history['val_acc']))

    with open('out/ann.hist', 'wb') as hist_file:
        pickle.dump(hist.history, hist_file) 
Example 61
Project: cactus-maml   Author: kylehkhsu   File: baselines.py    MIT License 4 votes vote down vote up
def embedding_mlp(num_classes=FLAGS.way, num_shots=FLAGS.shot, num_tasks=FLAGS.num_tasks,
                  num_encoding_dims=FLAGS.num_encoding_dims, test_set=FLAGS.test_set, dataset=FLAGS.dataset,
                  units=FLAGS.units, dropout=FLAGS.dropout):
    import keras
    from keras.layers import Dense, Dropout
    from keras.losses import categorical_crossentropy
    from keras.callbacks import EarlyStopping
    from keras import backend as K

    if dataset != 'celeba':
        _, _, _, X_test, Y_test, Z_test = get_data(dataset, num_encoding_dims, test_set)
        task_generator = TaskGenerator(num_classes=num_classes, num_train_samples_per_class=num_shots, num_samples_per_class=num_shots+5)
        partition = task_generator.get_partition_from_labels(Y_test)
        partitions = [partition]
    else:
        _, _, _, X_test, attributes_test, Z_test = get_data(dataset, num_encoding_dims, test_set)
        task_generator = TaskGenerator(num_classes=num_classes, num_train_samples_per_class=num_shots, num_samples_per_class=num_shots+5)
        partitions = task_generator.get_celeba_task_pool(attributes_test)
    tasks = task_generator.get_tasks(num_tasks=num_tasks, partitions=partitions)

    train_accuracies, test_accuracies = [], []

    start = time.time()
    for i_task, task in enumerate(tqdm(tasks)):
        if (i_task + 1) % (num_tasks // 10) == 0:
            tqdm.write('test {}, accuracy {:.5}'.format(i_task + 1, np.mean(test_accuracies)))
        ind_train_few, Y_train_few, ind_test_few, Y_test_few = task
        Z_train_few, Z_test_few = Z_test[ind_train_few], Z_test[ind_test_few]
        Y_train_few, Y_test_few = keras.utils.to_categorical(Y_train_few, num_classes=num_classes), keras.utils.to_categorical(Y_test_few, num_classes=num_classes)

        model = keras.Sequential()
        model.add(Dense(units=units, activation='relu', input_dim=Z_train_few.shape[1]))
        model.add(Dropout(rate=dropout))
        model.add(Dense(units=num_classes, activation='softmax'))
        model.compile(loss=categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy'])
        early_stopping = EarlyStopping(monitor='val_loss', patience=2)
        model.fit(Z_train_few, Y_train_few, batch_size=Z_train_few.shape[0], epochs=500, verbose=0, validation_data=(Z_test_few, Y_test_few), callbacks=[early_stopping])
        train_score = model.evaluate(Z_train_few, Y_train_few, verbose=0)
        train_accuracies.append(train_score[1])
        test_score = model.evaluate(Z_test_few, Y_test_few, verbose=0)
        test_accuracies.append(test_score[1])
        K.clear_session()

    print('units={}, dropout={}'.format(units, dropout))
    print('{}-way {}-shot embedding mlp: {:.5} with 95% CI {:.5} over {} tests'.format(num_classes, num_shots, np.mean(test_accuracies), 1.96*np.std(test_accuracies)/np.sqrt(num_tasks), num_tasks))
    print('Mean training accuracy: {:.5}; standard deviation: {:.5}'.format(np.mean(train_accuracies), np.std(train_accuracies)))
    print('{} few-shot classification tasks: {:.5} seconds.'.format(num_tasks, time.time() - start)) 
Example 62
Project: deep-learning-keras   Author: arnaudvl   File: nn.py    MIT License 4 votes vote down vote up
def _callbacks(self,i=0,run=0):
        """
        Define callbacks to log performance metrics, apply early stopping,
        reduce the learning rate and save best models during training.
        """
        os.chdir(self.save_dir)
        
        callbacks = [EarlyStopping(monitor=self._val_loss_callback,
                                   patience=self.early_stopping_epochs,
                                   verbose=1,
                                   mode=self._mode),
                     ModelCheckpoint(self.model_name + '-fold-' + str('%02d' % (i + 1)) + '-run-' + str('%02d' % (run + 1)) + '.check',
                                     monitor=self._val_loss_callback,
                                     verbose=1,
                                     mode=self._mode,
                                     save_best_only=True),
                    ReduceLROnPlateau(monitor=self._val_loss_callback,
                                      patience=int(self.early_stopping_epochs*self._early_stopping_lr),
                                      factor=self.learning_rate_factor,
                                      verbose=1,
                                      mode=self._mode,
                                      epsilon=0.0001)]
        
        if self.X_train is not None and self.X_val is not None:
        
            if self.X_aux_train is not None and self.X_aux_val is not None:
                training_data = (self.X_train,self.y_train,self.X_aux_train)
                validation_data = (self.X_val,self.y_val,self.X_aux_val)
            else:
                training_data = (self.X_train,self.y_train)
                validation_data = (self.X_val,self.y_val)
            
            callbacks.insert(0,MetricsLog(training_data=training_data,
                                          validation_data=validation_data,
                                          metric=self._val_loss))
        else:
            
            if self.X_aux is not None:
                training_data=(self.X,self.y,self.X_aux)
            else:
                training_data = (self.X,self.y)
            
            callbacks.insert(0,MetricsLog(training_data=training_data,
                                          metric=self._val_loss))
        
        return callbacks 
Example 63
Project: gccaps   Author: tqbl   File: training.py    MIT License 4 votes vote down vote up
def _create_callbacks():
    """Create a list of training callbacks.

    Up to four callbacks are included in the list:
      * A callback for saving models.
      * A callback for using TensorBoard.
      * An optional callback for learning rate decay.
      * An optional callback for early stopping.

    Returns:
        list: List of Keras callbacks.
    """
    # Create callbacks for computing various metrics and logging them
    callbacks = [F1ScoreLogger(), EERLogger(), CSVLogger(cfg.history_path)]

    # Create callback to save model after every epoch
    model_path = cfg.model_path
    path = os.path.join(model_path, 'gccaps.{epoch:02d}-{val_acc:.4f}.hdf5')
    callbacks.append(ModelCheckpoint(filepath=path,
                                     monitor='val_acc',
                                     verbose=0,
                                     save_best_only=False,
                                     save_weights_only=False,
                                     mode='auto',
                                     period=1,
                                     ))

    # Create callback for TensorBoard logs
    callbacks.append(TensorBoard(cfg.log_path, batch_size=cfg.batch_size))

    lr_decay = cfg.learning_rate['decay']
    if lr_decay < 1.:
        # Create callback to decay learning rate
        def _lr_schedule(epoch, lr):
            decay = epoch % cfg.learning_rate['decay_rate'] == 0
            return lr * lr_decay if decay else lr
        callbacks.append(LearningRateScheduler(schedule=_lr_schedule))

    if cfg.n_epochs == -1:
        # Create callback to use an early stopping condition
        callbacks.append(EarlyStopping(monitor='val_loss',
                                       min_delta=0,
                                       patience=5,
                                       ))

    return callbacks 
Example 64
Project: NeuroTrajectory   Author: RovisLab   File: main.py    GNU General Public License v3.0 4 votes vote down vote up
def one_train(path, data_set, model_function, parameters):
    os.makedirs(path)
    os.makedirs(path + '/models')
    os.makedirs(path + '/plots')
    os.makedirs(path + '/confusion_matrix')
    os.makedirs(path + '/conf_matrix_csv')
    os.makedirs(path + '/conf_matrix_details')

    batch_size = 0
    epochs = 0

    params = list()
    for p in parameters:
        message = str(p) + ' : '
        value = input(message)

        if p == 'batch_size':
            batch_size = int(value)
        elif p == 'epochs':
            epochs = int(value)
        else:
            params.append(str(value))

    train_model = model_function(np.shape(data_set.X_train), params)
    early_stopper = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=5, verbose=0, mode='auto')

    params.append(batch_size)
    params.append(epochs)
    history = TrainingHistoryPlot(path, data_set, params)

    print("Training...")
    train_model.fit(data_set.X_train, data_set.Y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(data_set.X_valid, data_set.Y_valid),
                    callbacks=[early_stopper, history])
    print("Done!")

    score = train_model.evaluate(data_set.X_valid, data_set.Y_valid, verbose=0)

    file = open(path + '/result.txt', 'w')
    file.write('Test loss : ' + str(score[0]) + '\n')
    file.write('Test acc : ' + str(score[1]) + '\n')
    file.close() 
Example 65
Project: zlyang-seq2seq-gec   Author: young-zonglin   File: basic_model.py    MIT License 4 votes vote down vote up
def fit_generator(self, observe=False, error_text='',
                      beam_width=3, beamsearch_interval=10, is_latin=False):
        train_start = float(time.time())
        early_stopping = EarlyStopping(monitor=self.hyperparams.early_stop_monitor,
                                       patience=self.hyperparams.early_stop_patience,
                                       min_delta=self.hyperparams.early_stop_min_delta,
                                       mode=self.hyperparams.early_stop_mode,
                                       verbose=1)
        # callback_instance.set_model(self.model) => set_model方法由Keras调用
        lr_scheduler = self.hyperparams.lr_scheduler
        save_url = \
            self.this_model_save_dir + os.path.sep + \
            'epoch_{epoch:04d}-{'+self.hyperparams.early_stop_monitor+':.5f}' + '.h5'
        model_saver = ModelCheckpoint(save_url,
                                      monitor=self.hyperparams.early_stop_monitor,
                                      mode=self.hyperparams.early_stop_mode,
                                      save_best_only=True, save_weights_only=True, verbose=1)
        observer = Observer(use_beamsearch=observe, custom_model=self, error_text=error_text,
                            beam_width=beam_width, beamsearch_interval=beamsearch_interval,
                            is_latin=is_latin)
        history = self.model.fit_generator(reader.generate_batch_data_file(self.train_fname,
                                                                           self.tokenizer,
                                                                           self.input_len,
                                                                           self.output_len,
                                                                           self.batch_size,
                                                                           self.vocab_size,
                                                                           self.pad, self.cut,
                                                                           self.corpus_open_encoding),
                                           validation_data=reader.generate_batch_data_file(self.val_fname,
                                                                                           self.tokenizer,
                                                                                           self.input_len,
                                                                                           self.output_len,
                                                                                           self.batch_size,
                                                                                           self.vocab_size,
                                                                                           self.pad, self.cut,
                                                                                           self.corpus_open_encoding),
                                           validation_steps=self.val_samples_count / self.batch_size,
                                           steps_per_epoch=self.train_samples_count / self.batch_size,
                                           epochs=self.hyperparams.train_epoch_times, verbose=1,
                                           callbacks=[model_saver, lr_scheduler, early_stopping, observer])
        tools.show_save_record(self.this_model_save_dir, history, train_start)

    # TODO 评价指标 
Example 66
Project: MTL-suite   Author: LLNL   File: DNNRegressor.py    MIT License 4 votes vote down vote up
def _fit(self, x, y, **kwargs):
        """
        Train model on given data x and y.
        Args:
            x (numpy.array): input data matrix (covariates).
            y (numpy.array): label vector (outcome).
        Returns:
            None.
        """
        self.logger.info('Traning process is about to start.')

        self.ndimensions = x.shape[1]  # dimension of the data

        # architecture depend on data dimension
        self._create_model((x.shape[1],))
        self.logger.info('Model architecture created.')

        monitor = 'val_loss'

        filename = '{}.hdf5'.format(self.__str__())
        filename = os.path.join(self.output_directory, filename)

        # callbacks
        early_stopping = cllbck.EarlyStopping(monitor=monitor, patience=10,
                                              verbose=0)
        checkpoint = cllbck.ModelCheckpoint(filename, monitor=monitor,
                                            verbose=0, save_best_only=True)
#        tbCallBack = cllbck.TensorBoard(log_dir='./log', histogram_freq=0,
#                                        write_graph=True, write_images=True)
#        reduce_lr = cllbck.ReduceLROnPlateau(monitor='val_loss', factor=0.2,
#                                             patience=5, min_lr=0.001)
        callbacks_list = [early_stopping, checkpoint]  # , reduce_lr]  # tbCallBack

        self.logger.info('Training process started.')

        # train NN model on input data
        self.model.fit(x, y, epochs=self.nb_epochs, verbose=0,
                       batch_size=self.batch_size, callbacks=callbacks_list,
                       validation_split=0.2)

        # load best model' weights from file
        self.model.load_weights(filename)
        self.logger.info('Training process finalized.') 
Example 67
Project: MTL-suite   Author: LLNL   File: DNNClassifier.py    MIT License 4 votes vote down vote up
def _fit(self, x, y, **kwargs):
        """
        Train model on given data x and y.
        Args:
            x (numpy.array): input data matrix (covariates).
            y (numpy.array): label vector (outcome).
        Returns:
            None.
        """
        self.logger.info('Traning process is about to start.')

        self.ndimensions = x.shape[1]  # dimension of the data

        # architecture depend on data dimension
        self._create_model((x.shape[1],))
        self.logger.info('Model architecture created.')

        monitor = 'val_loss'

        filename = '{}.hdf5'.format(self.__str__())
        filename = os.path.join(self.output_directory, filename)

        # callbacks
        early_stopping = cllbck.EarlyStopping(monitor=monitor, patience=10,
                                              verbose=0)
        checkpoint = cllbck.ModelCheckpoint(filename, monitor=monitor,
                                            verbose=0, save_best_only=True)
#        tbCallBack = cllbck.TensorBoard(log_dir='./log', histogram_freq=0,
#                                        write_graph=True, write_images=True)
#        reduce_lr = cllbck.ReduceLROnPlateau(monitor='val_loss', factor=0.2,
#                                             patience=5, min_lr=0.001)
        callbacks_list = [early_stopping, checkpoint]  # , reduce_lr]  # tbCallBack

        self.logger.info('Training process started.')

        # train NN model on input data
        self.model.fit(x, y, epochs=self.nb_epochs, verbose=0,
                       batch_size=self.batch_size, callbacks=callbacks_list,
                       validation_split=0.2)

        # load best model' weights from file
        self.model.load_weights(filename)
        self.logger.info('Training process finalized.') 
Example 68
Project: DA-RNN_manoeuver_anticipation   Author: michetonu   File: DA_RNN_anticipation.py    Apache License 2.0 4 votes vote down vote up
def fit_model(self, X_train_head, X_train_outside, X_train_gaze,
                  y_train, y_train_domain, batch_size=128, epochs=1000, patience=30):
        """Fit the model on a training set.
        
        The training sets must be divided in batches that contain both the source and target domain, 
        in order to perform adaptation.
                    
        Parameters
        ----------
        X_train_head : np.ndarray
            Head features. Shape = (n_samples, n_timestamps, n_features)
        X_train_outside : np.ndarray
            Context features. Shape = (n_samples, n_timestamps, n_features)
        X_train_gaze : np.ndarray
            Gaze features. Shape = (n_samples, n_timestamps, n_features)
        y_train : np.ndarray
            Action labels, encoded as integers.
        y_train_domain : np.ndarray
            Binary domain labels, encoded as integers.
        batch_size : int, optional
            Size of the batches for training. Default = 128.
        epochs : int, optional
            Number of epochs to run the training. Default = 1000.
        patience: int, optional
            Number of epochs to wait without an improvement in the validation loss for early stopping. Default = 30.
            
        Returns
        -------
        type : keras.History
            The history of the trained model as a keras.History object.
        """

        early_stopping = EarlyStopping(monitor='val_loss', patience=patience)

        model_history = self.model.fit(
            x=[X_train_head, X_train_outside,
               X_train_gaze],
            y=[y_train, y_train_domain],
            epochs=epochs, batch_size=batch_size,
            validation_split=0.2,
            callbacks=[early_stopping])

        return model_history 
Example 69
Project: cor-asv-ann   Author: ASVLeipzig   File: seq2seq.py    Apache License 2.0 4 votes vote down vote up
def train(self, filenames, val_filenames=None):
        '''train model on given text files.
        
        Pass the character sequences of lines in `filenames`, paired into
        source and target (and possibly, source confidence values),
        to the loop training model weights with stochastic gradient descent.
        The generator will open the file, looping over the complete set (epoch)
        as long as validation error does not increase in between (early stopping).
        
        Validate on a random fraction of lines automatically separated before,
        unless `val_filenames` is given, in which case only those files are used
        for validation.
        '''
        from keras.callbacks import EarlyStopping, TerminateOnNaN
        from .callbacks import StopSignalCallback, ResetStatesCallback
        from .keras_train import fit_generator_autosized, evaluate_generator_autosized

        num_lines = self.map_files(filenames)
        self.logger.info('Training on "%d" files with %d lines', len(filenames), num_lines)
        if val_filenames:
            num_lines = self.map_files(val_filenames)
            self.logger.info('Validating on "%d" files with %d lines', len(val_filenames), num_lines)
            split_rand = None
        else:
            self.logger.info('Validating on random 20% lines from those files')
            split_rand = np.random.uniform(0, 1, (num_lines,)) # reserve split fraction at random line numbers
        
        # Run training
        earlystopping = EarlyStopping(monitor='val_loss', patience=3, verbose=1,
                                      mode='min', restore_best_weights=True)
        callbacks = [earlystopping, TerminateOnNaN(),
                     StopSignalCallback(logger=self.logger)]
        history = fit_generator_autosized(
            self.encoder_decoder_model,
            self.gen_data(filenames, split_rand, train=True),
            epochs=self.epochs,
            workers=1,
            # (more than 1 would effectively increase epoch size)
            use_multiprocessing=not self.scheduled_sampling,
            # (cannot access session/graph for scheduled sampling in other process,
            #  cannot access model for reset callback in other process)
            validation_data=self.gen_data(val_filenames or filenames, split_rand, train=False),
            verbose=1 if self.progbars else 0,
            callbacks=callbacks)
        
        if 'val_loss' in history.history:
            self.logger.info('training finished with val_loss %f',
                             min(history.history['val_loss']))
            if (np.isnan(history.history['val_loss'][-1]) or
                earlystopping.stopped_epoch == 0):
                # recover weights (which TerminateOnNaN prevented EarlyStopping from doing)
                self.encoder_decoder_model.set_weights(earlystopping.best_weights)
            self._resync_decoder()
            self.status = 2
        else:
            self.logger.critical('training failed')
            self.status = 1 
Example 70
Project: dlopt   Author: acamero   File: nn.py    GNU General Public License v3.0 4 votes vote down vote up
def __init__(self,
                 model_filename="trained-model.hdf5",
                 optimizer='Adam',
                 optimizer_params={'learning_rate':5e-5},
                 monitor='val_loss',
                 min_delta=1e-5,
                 patience=50,
                 metrics=['mae', 'mse', 'msle', 'mape'],
                 seed=0,
                 verbose=0,
                 **kwargs):
        super().__init__(seed=seed,
                         verbose=verbose,
                         **kwargs)
        self.checkpointer = ModelCheckpoint(filepath=model_filename,
                                            verbose=verbose,
                                            save_best_only=True)

        if optimizer == 'Adadelta':
            self.optimizer = Adadelta(**optimizer_params)
        elif optimizer == 'Adagrad':
            self.optimizer = Adagrad(**optimizer_params)
        elif optimizer == 'Adam':
            self.optimizer = Adam(**optimizer_params)
        elif optimizer == 'Adamax':
            self.optimizer = Adamax(**optimizer_params)
        elif optimizer == 'Nadam':
            self.optimizer = Nadam(**optimizer_params)
        elif optimizer == 'RMSprop':
            self.optimizer = RMSprop(**optimizer_params)
        elif optimizer == 'SGD':
            self.optimizer = SGD(**optimizer_params)
        else:
            raise Exception("Unknown optimizer ", optimizer)

        if self.verbose > 1:
            print("Optimizer ", optimizer, str(self.optimizer.get_config()))
        self.early_stopping = EarlyStopping(monitor=monitor,
                                            min_delta=min_delta,
                                            patience=patience,
                                            verbose=verbose,
                                            mode='auto')
        self.metrics = metrics 
Example 71
Project: squirrel-not-squirrel   Author: eyadgaran   File: train.py    MIT License 4 votes vote down vote up
def train():
    # Initialize session
    SimpleMLDatabase().initialize()

    # Build the Dataset
    dataset_kwargs = {'project': 'squirrel', 'name': 'squirrel', 'registered_name': 'SquirrelDataset',
                          'label_columns': ['label'], 'strict': False, 'save_method': 'cloud_pickled'}
    pipeline_kwargs = {
        'project': 'squirrel', 'name': 'squirrel', 'registered_name': 'RandomSplitPipeline',
        'fitted': True, 'train_size': 0.8, 'validation_size': 0.0, 'test_size': 0.2,
        'shuffle': True, 'random_state': 38, 'strict': False, 'save_method': 'cloud_pickled',
        'transformers': [
             ('load_images', ImageLoader()),
             ('crop', CropImageToSquares()),
             ('resize', ResizeImage(final_dims=(224, 224))),
             ('df_to_matrix', DataframeToMatrix()),
             ('preprocess_tuple', KerasInceptionV3ImagePreprocessor()),
             ('encode', InceptionV3Encoder()),
        ]
    }

    early = EarlyStopping(monitor='val_acc', min_delta=0.001, patience=10, verbose=1, mode='auto')
    model_kwargs = {
        'project': 'squirrel', 'name': 'squirrel', 'registered_name': 'RetrainedTopModel',
        'save_method': 'cloud_keras_hdf5',
        'params': {'batch_size': 32, 'callbacks': [early],
                   'epochs': 500, 'steps_per_epoch': 100, 'validation_steps': 50,
                   'use_multiprocessing': False, 'workers': 0}
    }

    dataset = DatasetCreator.retrieve_or_create(**dataset_kwargs)
    pipeline = PipelineCreator.retrieve_or_create(dataset=dataset, **pipeline_kwargs)
    # Preprocess for training speed
    # encode_all_images(pipeline.get_dataset_split(TRAIN_SPLIT)[0], pipeline, TRAIN_SPLIT)
    # encode_all_images(pipeline.get_dataset_split(TEST_SPLIT)[0], pipeline, TEST_SPLIT)
    # model = ModelCreator.retrieve_or_create(pipeline=pipeline, **model_kwargs)

    # Use preprocessed data
    model = RetrainedTopModel(**model_kwargs)
    model.add_pipeline(pipeline)
    train_generator = preprocessed_generator(pipeline, split=TRAIN_SPLIT, return_y=True, infinite_loop=True, **model.get_params())
    validation_generator = preprocessed_generator(pipeline, split=TEST_SPLIT, return_y=True, infinite_loop=True, **model.get_params())
    model.fit(train_generator, validation_generator)
    model.params.pop('callbacks', [])
    model.save()

    # Evaluate Metrics
    metrics_to_score = ['RocAucMetric', 'F1ScoreMetric', 'AccuracyMetric', 'TprMetric', 'FprMetric']
    dataset_splits = [TRAIN_SPLIT, TEST_SPLIT]
    for cls, dataset_split in product(metrics_to_score, dataset_splits):
        metric_kwargs = {'registered_name': cls, 'dataset_split': dataset_split}
        metric = MetricCreator.retrieve_or_create(model=model, **metric_kwargs)
        print(metric.name, metric.values) 
Example 72
Project: mica-deep-mcca   Author: usc-sail   File: DeepMCCA.py    MIT License 4 votes vote down vote up
def train_model(model, data_list, epoch_num, batch_size, feature_dim):
    """
    trains the model
    # Arguments
        .... inputs?
        epoch_num: number of epochs to train the model
        batch_size: the size of batches
    # Returns
        the trained model
    """

    # Unpacking the data
    # the data_list is arranged thus:
    # [[(train_x, train_y), (val_x, val_y), (test_x, test_y) ]_(1), {}_(2),...]
    train_x_list = [i[0][0] for i in data_list]
    val_x_list = [i[1][0] for i in data_list]
    test_x_list = [i[2][0] for i in data_list]
    
    # for later
    test_y_list = [i[2][1] for i in data_list]

    # it is done to return the best model based on the validation loss
    checkpointer = ModelCheckpoint(filepath="weights_%d_dim.{epoch:02d}-{val_loss:.4f}.hdf5" % (feature_dim), 
                                        verbose=1, save_best_only=True, save_weights_only=True)
    early_stopping = EarlyStopping(min_delta = 1e-4, patience = 5)

    # used dummy Y because labels are not used in the loss function
    model.fit(train_x_list, np.zeros(len(train_x_list[0])),
              batch_size=batch_size, epochs=epoch_num, shuffle=True,
              validation_data=(val_x_list, np.zeros(len(val_x_list[0]))),
              callbacks=[checkpointer])

    #model_names_ = glob.glob('weights*5')
    #model.load_weights(model_names_[-1])

    results = model.evaluate(test_x_list, np.zeros(len(test_x_list[0])), batch_size=batch_size, verbose=1)

    print('loss on test data: ', results)

    results = model.evaluate(val_x_list, np.zeros(len(val_x_list[0])), batch_size=batch_size, verbose=1)
    print('loss on validation data: ', results)
    return model 
Example 73
Project: Intelligent-Projects-Using-Python   Author: PacktPublishing   File: TransferLearning_reg.py    MIT License 4 votes vote down vote up
def train_model(self,file_list,labels,n_fold=5,batch_size=16,epochs=40,dim=224,lr=1e-5,model='ResNet50'):
		model_save_dest = {}
		k = 0
		kf = KFold(n_splits=n_fold, random_state=0, shuffle=True)

		for train_index,test_index in kf.split(file_list):


			k += 1
			file_list = np.array(file_list)
			labels   = np.array(labels)
			train_files,train_labels  = file_list[train_index],labels[train_index]
			val_files,val_labels  = file_list[test_index],labels[test_index]
			
			if model == 'Resnet50':
				model_final = self.resnet_pseudo(dim=224,freeze_layers=10,full_freeze='N')
			
			if model == 'VGG16':
				model_final = self.VGG16_pseudo(dim=224,freeze_layers=10,full_freeze='N') 
			
			if model == 'InceptionV3':
				model_final = self.inception_pseudo(dim=224,freeze_layers=10,full_freeze='N')
				
			adam = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
			model_final.compile(optimizer=adam, loss=["mse"],metrics=['mse'])
			reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.50,patience=3, min_lr=0.000001)
			early = EarlyStopping(monitor='val_loss', patience=10, mode='min', verbose=1)
			logger = CSVLogger('keras-5fold-run-01-v1-epochs_ib.log', separator=',', append=False)
			checkpoint = ModelCheckpoint(
								'kera1-5fold-run-01-v1-fold-' + str('%02d' % (k + 1)) + '-run-' + str('%02d' % (1 + 1)) + '.check',
								monitor='val_loss', mode='min',
								save_best_only=True,
								verbose=1) 
			callbacks = [reduce_lr,early,checkpoint,logger]
			train_gen = DataGenerator(train_files,train_labels,batch_size=32,n_classes=len(self.class_folders),dim=(self.dim,self.dim,3),shuffle=True)
			val_gen = DataGenerator(val_files,val_labels,batch_size=32,n_classes=len(self.class_folders),dim=(self.dim,self.dim,3),shuffle=True)
			model_final.fit_generator(train_gen,epochs=epochs,verbose=1,validation_data=(val_gen),callbacks=callbacks)
			model_name = 'kera1-5fold-run-01-v1-fold-' + str('%02d' % (k + 1)) + '-run-' + str('%02d' % (1 + 1)) + '.check'
			del model_final
			f = h5py.File(model_name, 'r+')
			del f['optimizer_weights']
			f.close()
			model_final = keras.models.load_model(model_name)
			model_name1 = self.outdir + str(model) + '___' + str(k) 
			model_final.save(model_name1)
			model_save_dest[k] = model_name1
				
		return model_save_dest

	# Hold out dataset validation function 
Example 74
Project: Intelligent-Projects-Using-Python   Author: PacktPublishing   File: TransferLearning_ffd.py    MIT License 4 votes vote down vote up
def train_model(self,train_dir,val_dir,n_fold=5,batch_size=16,epochs=40,dim=224,lr=1e-5,model='ResNet50'):
        if model == 'Resnet50':
            model_final = self.resnet_pseudo(dim=224,freeze_layers=10,full_freeze='N')
        if model == 'VGG16':
            model_final = self.VGG16_pseudo(dim=224,freeze_layers=10,full_freeze='N') 
        if model == 'InceptionV3':
            model_final = self.inception_pseudo(dim=224,freeze_layers=10,full_freeze='N')
            
        train_file_names = glob.glob(f'{train_dir}/*/*')
        val_file_names = glob.glob(f'{val_dir}/*/*')
        train_steps_per_epoch = len(train_file_names)/float(batch_size)
        val_steps_per_epoch = len(val_file_names)/float(batch_size)
        train_datagen = ImageDataGenerator(horizontal_flip = True,vertical_flip = True,width_shift_range = 0.1,height_shift_range = 0.1,
                channel_shift_range=0,zoom_range = 0.2,rotation_range = 20,preprocessing_function=pre_process)
        val_datagen = ImageDataGenerator(preprocessing_function=pre_process)
        train_generator = train_datagen.flow_from_directory(train_dir,
        target_size=(dim,dim),
        batch_size=batch_size,
        class_mode='categorical')
        val_generator = val_datagen.flow_from_directory(val_dir,
        target_size=(dim,dim),
        batch_size=batch_size,
        class_mode='categorical')
        print(train_generator.class_indices)
        joblib.dump(train_generator.class_indices,f'{self.outdir}/class_indices.pkl')
        adam = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        model_final.compile(optimizer=adam, loss=["categorical_crossentropy"],metrics=['accuracy'])
        reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.50,patience=3, min_lr=0.000001)
        early = EarlyStopping(monitor='val_loss', patience=10, mode='min', verbose=1)
        logger = CSVLogger(f'{self.outdir}/keras-epochs_ib.log', separator=',', append=False)
        model_name = f'{self.outdir}/keras_transfer_learning-run.check'
        checkpoint = ModelCheckpoint(
                model_name,
                monitor='val_loss', mode='min',
                save_best_only=True,
                verbose=1) 
        callbacks = [reduce_lr,early,checkpoint,logger]
        model_final.fit_generator(train_generator,steps_per_epoch=train_steps_per_epoch,epochs=epochs,verbose=1,validation_data=(val_generator),validation_steps=val_steps_per_epoch,callbacks=callbacks,
                                                                                                                  class_weight={0:0.012,1:0.12,2:0.058,3:0.36,4:0.43})
        #model_final.fit_generator(train_generator,steps_per_epoch=1,epochs=epochs,verbose=1,validation_data=(val_generator),validation_steps=1,callbacks=callbacks)
        
        del model_final
        f = h5py.File(model_name, 'r+')
        del f['optimizer_weights']
        f.close()
        model_final = keras.models.load_model(model_name)
        model_to_store_path = f'{self.outdir}/{model}' 
        model_final.save(model_to_store_path)
        return model_to_store_path,train_generator.class_indices

# Hold out dataset validation function 
Example 75
Project: malware-prediction-rnn   Author: mprhode   File: Experiments.py    Apache License 2.0 4 votes vote down vote up
def set_up_model(self):
		# Shuffle data 
		#self.x_train, self.y_train, self.x_val, self.y_val = extract_val_set_binary(self.x_train, self.y_train, 0.1)
		self.x_train, self.y_train = unison_shuffled_copies([self.x_train, self.y_train])
		self.x_test, self.y_test, self.test_idxs = unison_shuffled_copies([self.x_test, self.y_test, self.test_idxs])
		#self.x_val, self.y_val = unison_shuffled_copies([self.x_val, self.y_val])

		# scale data by test data mean and variance
		means, stdvs = get_mean_and_stdv(self.x_train)
		self.x_train = scale_array(self.x_train, means, stdvs)
		self.x_test = scale_array(self.x_test, means, stdvs)
		#self.x_val = scale_array(self.x_val, means, stdvs)

		# Output size - in future delete any cols for categorical which are all zero 
		models = [generate_model(
			self.x_train, self.y_train, config, model_type=self.model_type
			) for config in self.configurations]

		reset_states = ResetStatesCallback()
		preds = []
		#early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=, verbose=0, mode='auto')
		
		print("train, test set size (x):", self.x_train.shape, self.x_test.shape)
		print("train mal:",self.y_train.flatten().tolist().count(1) , "ben:",self.y_train.flatten().tolist().count(0) , "test mal:", self.y_test.flatten().tolist().count(1), "ben:", self.y_test.flatten().tolist().count(0))
		for model, config in zip(models, self.configurations):
			h = model.fit(
				self.x_train, self.y_train, 
				batch_size=config["batch_size"], 
				epochs=config["epochs"],
				shuffle=True,	
				verbose=0,
				callbacks=[reset_states]
			)

			d = config["description"]

			self.metrics["train_acc_{}".format(d)] = h.history["acc".format(d)]
			pred_Y = model.predict(self.x_test, batch_size=config["batch_size"])
			preds.append(pred_Y)
			#self.metrics["val_preds_{}"] = model.predict(self.x_val, batch_size=config["batch_size"]).flatten().tolist()
			#self.metrics["val_truth"] = self.y_val.flatten().tolist()
			self.metrics["test_idxs"] = self.test_idxs
			self.metrics["preds_{}".format(d)] = pred_Y.flatten()
			
			self.metrics["acc_{}".format(d)] = accuracy_score(
				self.y_test.flatten().round(), 
				self.metrics["preds_{}".format(d)].round()
			)
			
			print(d, "acc :", self.metrics["acc_{}".format(d)], "f1", f1_score(self.y_test.flatten().round(), 
				self.metrics["preds_{}".format(d)].round()))

		self.write_up_models(models, preds)
		return False 
Example 76
Project: RPGOne   Author: RTHMaK   File: language_model.py    Apache License 2.0 4 votes vote down vote up
def train_model(self, sentences, word_dim=50, factor_base=2, num_epochs=20,
                    tokenize=True, use_lstm=False):
        _, input_array, factored_target_arrays = self.process_data(
                sentences, is_training=True, factor_base=factor_base, tokenize=tokenize)
        vocab_size = self.data_indexer.get_vocab_size()
        num_factors = len(factored_target_arrays)
        model_input = Input(shape=input_array.shape[1:], dtype='int32') # (batch_size, num_words)
        embedding = Embedding(input_dim=vocab_size, output_dim=word_dim, mask_zero=True)
        embedded_input = embedding(model_input) # (batch_size, num_words, word_dim)
        regularized_embedded_input = Dropout(0.5)(embedded_input)

        # Bidirectional RNNs = Two RNNs, with the second one processing the input
        # backwards, and the two outputs concatenated.
        # Return sequences returns output at every timestep, instead of just the last one.
        rnn_model = LSTM if use_lstm else SimpleRNN

        # Since we will project the output of the lstm down to factor_base in the next
        # step anyway, it is okay to project it down a bit now. So output_dim = word_dim/2
        # This minimizes the number of parameters significantly. All four Ws in LSTM
        # will now be half as big as they would be if output_dim = word_dim.
        forward_rnn = rnn_model(output_dim=int(word_dim/2), return_sequences=True, name='forward_rnn')
        backward_rnn = rnn_model(output_dim=int(word_dim/2), go_backwards=True,
                                 return_sequences=True, name='backward_rnn')
        forward_rnn_out = forward_rnn(regularized_embedded_input)  # (batch_size, num_words, word_dim/2)
        backward_rnn_out = backward_rnn(regularized_embedded_input)  # (batch_size, num_words, word_dim/2)
        bidirectional_rnn_out = merge([forward_rnn_out, backward_rnn_out],
                                      mode='concat')  # (batch_size, num_words, word_dim)
        regularized_rnn_out = Dropout(0.2)(bidirectional_rnn_out)

        model_outputs = []
        # Make as many output layers as there are factored target arrays, and the same size
        for i in range(num_factors):
            # TimeDistributed(layer) makes layer accept an additional time distribution.
            # i.e. if layer takes a n-dimensional input, TimeDistributed(layer) takes a
            # n+1 dimensional input, where the second dimension is time (or words in the
            # sentence). We need this now because RNN above returns one output per timestep
            factor_output = TimeDistributed(Dense(units=factor_base,
                                                  activation='softmax',
                                                  name='factor_output_%d' % i))
            model_outputs.append(factor_output(regularized_rnn_out))  # (batch_size, num_words, factor_base)

        # We have num_factors number of outputs in the model. So, the effective output shape is
        # [(batch_size, num_words, factor_base)] * num_factors
        model = Model(inputs=model_input, outputs=model_outputs)
        model.compile(loss='categorical_crossentropy', optimizer='adam')
        model.summary()
        early_stopping = EarlyStopping()
        model.fit(input_array, factored_target_arrays, nb_epoch=num_epochs, validation_split=0.1,
                  callbacks=[early_stopping])
        self.model = model 
Example 77
Project: FormicID   Author: naturalis   File: logger.py    MIT License 4 votes vote down vote up
def build_mc(
    config,
    monitor="val_loss",
    verbose=0,
    mode="auto",
    save_best_only=True,
    period=1,
):
    """Callback object for saving Model Checkpoints. Saves the models at
    certain checkpoints as `.h5` files.

    Args:
        config (Bunch object): The JSON configuration Bunch object.
        monitor (str): Quantity to monitor. Defaults to `val_loss`.
        verbose (int): Verbosity mode, 0 or 1. Defaults to 0.
        save_best_only (Bool): If save_best_only=True, the latest best model
            according to the quantity monitored will not be overwritten.
            Defaults to True
        mode (str): One of {auto, min, max}. If save_best_only=True, the
            decision to overwrite the current save file is made based on
            either the maximization or the minimization of the monitored
            quantity. For val_acc, this should be max, for val_loss this
            should be min, etc. In auto mode, the direction is automatically
            inferred from the name of the monitored quantity. Defaults to
            `auto`.
        period (int): Interval (number of epochs) between checkpoints.

    Returns:
        ModelCheckpoint callback

    """
    output_dir = config.checkpoint_dir
    filepath = os.path.join(
        output_dir, "weights_{epoch:02d}-{val_loss:.2f}.hdf5"
    )
    mcp = ModelCheckpoint(
        filepath=filepath,
        monitor=monitor,
        verbose=verbose,
        mode=mode,
        save_best_only=save_best_only,
        period=period,
    )

    return mcp


# EarlyStopping
############################################################################### 
Example 78
Project: SumQE   Author: nlpaueb   File: train_Bert.py    MIT License 4 votes vote down vote up
def train(train_path, human_metric, mode, path_to_save, **params):
    """
    Train the Bert Model.
    :param train_path: Path to the train data in order to load them.
    :param human_metric: The metric for which the model will be trained at. It is needed only on 'Single Task' mode.
    :param mode: Depending on your choice : ['Single Task', 'Multi Task-1', 'Multi Task-5'].
    :param path_to_save: The path where the model will be saved. If SAVE_MODELS=True.
    :return: The trained model.
    """

    train_data = dict(np.load(train_path, allow_pickle=True).item())

    train_input_dict = {
        'word_inputs': train_data['word_inputs'],
        'pos_inputs': train_data['pos_inputs'],
        'seg_inputs': train_data['seg_inputs']
    }

    train_human_metric = None

    if mode == 'Single Task':
        train_human_metric = train_data[human_metric]

    elif mode == 'Multi Task-1' or mode == 'Multi Task-5':
        q1 = train_data['Q1'].reshape(-1, 1)
        q2 = train_data['Q2'].reshape(-1, 1)
        q3 = train_data['Q3'].reshape(-1, 1)
        q4 = train_data['Q4'].reshape(-1, 1)
        q5 = train_data['Q5'].reshape(-1, 1)
        train_human_metric = np.concatenate((q1, q2, q3, q4, q5), axis=1)

    early = EarlyStopping(monitor='val_loss', patience=1, verbose=0, restore_best_weights=False)

    # First dimension of shape is not used
    model = compile_bert(shape=(512, 512), dropout_rate=params['D'], lr=params['LR'], mode=mode)

    model.fit(x=train_input_dict, y=train_human_metric, batch_size=params['BS'],
              epochs=10, validation_split=0.1, callbacks=[early])

    if SAVE_MODELS:
        model.save(path_to_save)

    return model 
Example 79
Project: Time-series-Classification   Author: AziziShekoofeh   File: model.py    Apache License 2.0 4 votes vote down vote up
def train(self, uid, batch_size, es, nb_epoch, verbose):
        print('-' * 30)
        print('Fitting model...')
        print('-' * 30)
        callbacks_list = []
        logs_dir = os.path.join(s.intermediate_folder, 'logs', self.log_dir)
        if not os.path.isdir(logs_dir):
            os.mkdir(logs_dir)

        model_json = self.model.to_json()
        model_log_dir = os.path.join(logs_dir, 'model_logs')
        if not os.path.isdir(model_log_dir):
            os.mkdir(model_log_dir)

        with open(os.path.join(model_log_dir, uid + '.json'), 'w') as outfile:
            json.dump(model_json, outfile)

        train_log_dir = os.path.join(logs_dir, 'train_logs')
        if not os.path.isdir(train_log_dir):
            os.mkdir(train_log_dir)

        with open(os.path.join(train_log_dir, uid + '.csv'), 'w') as csv_file:
            writer = csv.writer(csv_file)
            for key, value in self.training_params_dict.items():
                writer.writerow([key, value])

        validation_data = (self.validation_seq, self.validation_label[:])

        reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.9,
                                      patience=15, min_lr=1e-9,
                                      epsilon=0.001, verbose=1)

        model_checkpoint_dir = os.path.join(s.intermediate_folder, 'model_checkpoints')
        if not os.path.exists(model_checkpoint_dir):
            os.mkdir(model_checkpoint_dir)

        model_checkpoint = ModelCheckpoint(os.path.join(model_checkpoint_dir, uid + '.hdf5'),
                                           monitor='val_acc', save_best_only=True)
        callbacks_list.append(model_checkpoint)
        history = AUCHistory(validation_data)
        callbacks_list.append(history)
        callbacks_list.append(reduce_lr)
        if es:
            es = EarlyStopping(monitor='val_loss', min_delta=1e-4, patience=2, verbose=1)
            callbacks_list.append(es)

        csv_log_dir = os.path.join(logs_dir, 'csv_logs')
        if not os.path.isdir(csv_log_dir):
            os.mkdir(csv_log_dir)
        csv_logger = CSVLogger(os.path.join(csv_log_dir, uid + '.log'))
        callbacks_list.append(csv_logger)

        callbacks_list.append(reduce_lr)

        self.model.fit(self.train_seq, self.train_label, batch_size=batch_size, epochs=nb_epoch, verbose=verbose,
                       shuffle=True, callbacks=callbacks_list, validation_data=validation_data) 
Example 80
Project: CityEnergyAnalyst   Author: architecture-building-systems   File: nn_trainer_resume.py    MIT License 4 votes vote down vote up
def neural_trainer_resume(inputs_x, targets_t, model, scalerX, scalerT, locator, autoencoder):
    '''
    This function executes the training if the NN
    :param inputs_x:
    :param targets_t:
    :param locator:
    :return:
    '''

    inputs_x_rows, inputs_x_cols = inputs_x.shape

    # scaling and normalizing inputs
    inputs_x = scalerX.transform(inputs_x)
    targets_t = scalerT.transform(targets_t)
    encoding_dim = int(np.ceil(inputs_x_cols/2)+np.ceil(inputs_x_cols * 0.1))
    over_complete_dim =int(encoding_dim*2)
    AE_input_dim=int(inputs_x_cols)

    if autoencoder:
        # sparsing inputs: use this if you have more than 50 input features
        input_AEI = Input(shape=(AE_input_dim,))
        encoded = Dense(over_complete_dim, activation='relu')(input_AEI)
        encoded = Dense(encoding_dim, activation='softplus')(encoded)

        decoded = Dense(over_complete_dim, activation='softplus')(encoded)
        decoded = Dense(inputs_x_cols, activation='relu')(decoded)

        autoencoder = Model(input_AEI, decoded)
        autoencoder.compile(optimizer='Adamax', loss='mse')
        autoencoder.fit(inputs_x,inputs_x,epochs=1000, batch_size= 100000, shuffle=True)
        encoder = Model(input_AEI, encoded)
        encoded_x=encoder.predict(inputs_x)
        inputs_x=encoded_x

    validation_split = 0.3
    e_stop_limit = 10

    # define early stopping to avoid overfitting
    estop = EarlyStopping(monitor='val_loss', min_delta=0, patience=e_stop_limit, verbose=1, mode='auto')

    # Fit the model
    model.fit(inputs_x, targets_t, validation_split=validation_split, epochs=10, shuffle=True, batch_size=100000,
              callbacks=[estop])

    json_NN_path, weight_NN_path = locator.get_neural_network_model()
    model_json = model.to_json()
    with open(json_NN_path, "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(weight_NN_path)
    print("neural network properties saved")

    model_resume = locator.get_neural_network_resume()
    model.save(model_resume)  # creates a HDF5 file 'model_resume.h5'
    print("neural network model saved")

    del inputs_x
    del targets_t
    del model