Python keras.callbacks.EarlyStopping() Examples
The following are 30
code examples of keras.callbacks.EarlyStopping().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.callbacks
, or try the search function
.
Example #1
Source File: models.py From deepchem with MIT License | 7 votes |
def train(self, X, y, validation_data): print('Training model...') multitask = y.shape[1] > 1 if not multitask: num_positives = y.sum() num_sequences = len(y) num_negatives = num_sequences - num_positives self.model.fit( X, y, batch_size=128, nb_epoch=100, validation_data=validation_data, class_weight={ True: num_sequences / num_positives, False: num_sequences / num_negatives } if not multitask else None, callbacks=[EarlyStopping(monitor='val_loss', patience=10)], verbose=True)
Example #2
Source File: callbacks.py From keras-bert-ner with MIT License | 6 votes |
def NerCallbacks(id_to_tag, best_fit_params=None, mask_tag=None, log_path=None): """模型训练过程中的回调函数 """ callbacks = [Accuracy(id_to_tag, mask_tag, log_path)] if best_fit_params is not None: early_stopping = EarlyStopping( monitor="val_crf_accuracy", patience=best_fit_params.get("early_stop_patience")) reduce_lr_on_plateau = ReduceLROnPlateau( monitor="val_crf_accuracy", verbose=1, mode="max", factor=best_fit_params.get("reduce_lr_factor"), patience=best_fit_params.get("reduce_lr_patience")) model_check_point = ModelCheckpoint( best_fit_params.get("save_path"), monitor="val_crf_accuracy", verbose=2, mode="max", save_best_only=True) callbacks.extend([early_stopping, reduce_lr_on_plateau, model_check_point]) return callbacks
Example #3
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_patience(): class DummyModel(object): def __init__(self): self.stop_training = False early_stop = callbacks.EarlyStopping(monitor='val_loss', patience=2) early_stop.model = DummyModel() losses = [0.0860, 0.1096, 0.1040, 0.1019] # Should stop after epoch 3, as the loss has not improved after patience=2 epochs. epochs_trained = 0 early_stop.on_train_begin() for epoch in range(len(losses)): epochs_trained += 1 early_stop.on_epoch_end(epoch, logs={'val_loss': losses[epoch]}) if early_stop.model.stop_training: break assert epochs_trained == 3
Example #4
Source File: Stock_Prediction_Model_Stateless_LSTM.py From StockRecommendSystem with MIT License | 6 votes |
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 #5
Source File: train.py From YOLO-3D-Box with MIT License | 6 votes |
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 #6
Source File: train_neural.py From fancy-cnn with MIT License | 6 votes |
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 #7
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #8
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_patience(): class DummyModel(object): def __init__(self): self.stop_training = False early_stop = callbacks.EarlyStopping(monitor='val_loss', patience=2) early_stop.model = DummyModel() losses = [0.0860, 0.1096, 0.1040, 0.1019] # Should stop after epoch 3, as the loss has not improved after patience=2 epochs. epochs_trained = 0 early_stop.on_train_begin() for epoch in range(len(losses)): epochs_trained += 1 early_stop.on_epoch_end(epoch, logs={'val_loss': losses[epoch]}) if early_stop.model.stop_training: break assert epochs_trained == 3
Example #9
Source File: chatbot.py From Intelligent-Projects-Using-Python with MIT License | 6 votes |
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 #10
Source File: baseline.py From MELD with GNU General Public License v3.0 | 6 votes |
def train_model(self): checkpoint = ModelCheckpoint(self.PATH, monitor='val_loss', verbose=1, save_best_only=True, mode='auto') if self.modality == "audio": model = self.get_audio_model() model.compile(optimizer='adadelta', loss='categorical_crossentropy', sample_weight_mode='temporal') elif self.modality == "text": model = self.get_text_model() model.compile(optimizer='adadelta', loss='categorical_crossentropy', sample_weight_mode='temporal') elif self.modality == "bimodal": model = self.get_bimodal_model() model.compile(optimizer='adam', loss='categorical_crossentropy', sample_weight_mode='temporal') early_stopping = EarlyStopping(monitor='val_loss', patience=10) model.fit(self.train_x, self.train_y, epochs=self.epochs, batch_size=self.batch_size, sample_weight=self.train_mask, shuffle=True, callbacks=[early_stopping, checkpoint], validation_data=(self.val_x, self.val_y, self.val_mask)) self.test_model()
Example #11
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #12
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #13
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #14
Source File: train.py From PJ_NLP with Apache License 2.0 | 6 votes |
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 #15
Source File: train.py From MalConv-keras with MIT License | 6 votes |
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 #16
Source File: finetuning.py From DeepMoji with MIT License | 6 votes |
def finetuning_callbacks(checkpoint_path, patience, verbose): """ Callbacks for model training. # Arguments: checkpoint_path: Where weight checkpoints should be saved. patience: Number of epochs with no improvement after which training will be stopped. # Returns: Array with training callbacks that can be passed straight into model.fit() or similar. """ cb_verbose = (verbose >= 2) checkpointer = ModelCheckpoint(monitor='val_loss', filepath=checkpoint_path, save_best_only=True, verbose=cb_verbose) earlystop = EarlyStopping(monitor='val_loss', patience=patience, verbose=cb_verbose) return [checkpointer, earlystop]
Example #17
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #18
Source File: model.py From PyMLProjects with MIT License | 6 votes |
def init_logging_callbacks(self,log_dir=LOG_DIR_ROOT): self.checkpoint = ModelCheckpoint(filepath="%s/weights-improvement-{epoch:02d}-{loss:.4f}.hdf5" % (log_dir),\ monitor='loss',\ verbose=1,\ save_best_only=True,\ mode='min') self.early_stopping = EarlyStopping(monitor='loss',\ min_delta=0,\ patience=PATIENCE,\ verbose=0,\ mode='auto') now = datetime.utcnow().strftime("%Y%m%d%H%M%S") log_dir = "{}/run/{}".format(LOG_DIR_ROOT,now) self.tensorboard = TensorBoard(log_dir=log_dir,\ write_graph=True,\ write_images=True) self.callbacks = [self.early_stopping,\ self.tensorboard,\ self.checkpoint]
Example #19
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_patience(): class DummyModel(object): def __init__(self): self.stop_training = False early_stop = callbacks.EarlyStopping(monitor='val_loss', patience=2) early_stop.model = DummyModel() losses = [0.0860, 0.1096, 0.1040, 0.1019] # Should stop after epoch 3, as the loss has not improved after patience=2 epochs. epochs_trained = 0 early_stop.on_train_begin() for epoch in range(len(losses)): epochs_trained += 1 early_stop.on_epoch_end(epoch, logs={'val_loss': losses[epoch]}) if early_stop.model.stop_training: break assert epochs_trained == 3
Example #20
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #21
Source File: run_utils.py From deep-mlsa with Apache License 2.0 | 6 votes |
def get_callbacks(config_data, appendix=''): ret_callbacks = [] model_stored = False callbacks = config_data['callbacks'] if K._BACKEND == 'tensorflow': tensor_board = TensorBoard(log_dir=os.path.join('logging', config_data['tb_log_dir']), histogram_freq=10) ret_callbacks.append(tensor_board) for callback in callbacks: if callback['name'] == 'early_stopping': ret_callbacks.append(EarlyStopping(monitor=callback['monitor'], patience=callback['patience'], verbose=callback['verbose'], mode=callback['mode'])) elif callback['name'] == 'model_checkpoit': model_stored = True path = config_data['output_path'] basename = config_data['output_basename'] base_path = os.path.join(path, basename) opath = os.path.join(base_path, 'best_model{}.h5'.format(appendix)) save_best = bool(callback['save_best_only']) ret_callbacks.append(ModelCheckpoint(filepath=opath, verbose=callback['verbose'], save_best_only=save_best, monitor=callback['monitor'], mode=callback['mode'])) return ret_callbacks, model_stored
Example #22
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #23
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_patience(): class DummyModel(object): def __init__(self): self.stop_training = False early_stop = callbacks.EarlyStopping(monitor='val_loss', patience=2) early_stop.model = DummyModel() losses = [0.0860, 0.1096, 0.1040, 0.1019] # Should stop after epoch 3, as the loss has not improved after patience=2 epochs. epochs_trained = 0 early_stop.on_train_begin() for epoch in range(len(losses)): epochs_trained += 1 early_stop.on_epoch_end(epoch, logs={'val_loss': losses[epoch]}) if early_stop.model.stop_training: break assert epochs_trained == 3
Example #24
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_EarlyStopping_reuse(): np.random.seed(1337) 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], epochs=20) 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], epochs=20) assert len(hist.epoch) >= patience
Example #25
Source File: multiclass_DenseResNet.py From CNNArt with Apache License 2.0 | 5 votes |
def fPredict(X_test, y_test, model_name, sOutPath, patchSize, batchSize): weight_name = model_name[0] # model_json = model_name[1] + '.json' # model_all = model_name[0] + '.hdf5' _, sPath = os.path.splitdrive(sOutPath) sPath, sFilename = os.path.split(sOutPath) # sFilename, sExt = os.path.splitext(sFilename) # f = h5py.File(weight_name, 'r+') # del f['optimizer_weights'] # f.close() model = load_model(weight_name) opti = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=1)] # model.compile(loss='categorical_crossentropy', optimizer=opti, metrics=['accuracy']) # model.load_weights(weight_name) model.summary() score_test, acc_test = model.evaluate(X_test, y_test, batch_size=batchSize) prob_pre = model.predict(X_test, batchSize, 0) y_pred = np.argmax(prob_pre, axis=1) y_test = np.argmax(y_test, axis=1) confusion_mat = confusion_matrix(y_test, y_pred) # modelSave = model_name[:-5] + '_pred.mat' modelSave = sOutPath + '/' + sFilename + '_result.mat' sio.savemat(modelSave, {'prob_pre': prob_pre, 'score_test': score_test, 'acc_test': acc_test, 'confusion_mat': confusion_mat}) ############################################################################### ## OPTIMIZATIONS ## ###############################################################################
Example #26
Source File: multiclass_InceptionNet.py From CNNArt with Apache License 2.0 | 5 votes |
def fPredict(X_test, y_test, model_name, sOutPath, patchSize, batchSize): weight_name = model_name[0] # model_json = model_name[1] + '.json' # model_all = model_name[0] + '.hdf5' _, sPath = os.path.splitdrive(sOutPath) sPath, sFilename = os.path.split(sOutPath) # sFilename, sExt = os.path.splitext(sFilename) # f = h5py.File(weight_name, 'r+') # del f['optimizer_weights'] # f.close() model = load_model(weight_name) opti = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=1)] # model.compile(loss='categorical_crossentropy', optimizer=opti, metrics=['accuracy']) # model.load_weights(weight_name) model.summary(); score_test, acc_test = model.evaluate(X_test, y_test, batch_size=batchSize) prob_pre = model.predict(X_test, batchSize, 0) y_pred = np.argmax(prob_pre, axis=1) y_test = np.argmax(y_test, axis=1) confusion_mat = confusion_matrix(y_test, y_pred) # modelSave = model_name[:-5] + '_pred.mat' modelSave = sOutPath + '/' + sFilename + '_result.mat' sio.savemat(modelSave, {'prob_pre': prob_pre, 'score_test': score_test, 'acc_test': acc_test, 'confusion_mat': confusion_mat})
Example #27
Source File: multiclass_DenseNet.py From CNNArt with Apache License 2.0 | 5 votes |
def fPredict(X_test, y_test, model_name, sOutPath, patchSize, batchSize): weight_name = model_name[0] #model_json = model_name[1] + '_json' #model_all = model_name[0] + '.hdf5' _, sPath = os.path.splitdrive(sOutPath) sPath, sFilename = os.path.split(sOutPath) #sFilename, sExt = os.path.splitext(sFilename) #f = h5py.File(weight_name, 'r+') #del f['optimizer_weights'] #f.close() model=load_model(weight_name) opti = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=1)] #model.compile(loss='categorical_crossentropy', optimizer=opti, metrics=['accuracy']) #model.load_weights(weight_name) model.summary() score_test, acc_test = model.evaluate(X_test, y_test, batch_size=batchSize) prob_pre = model.predict(X_test, batchSize, 0) y_pred=np.argmax(prob_pre,axis=1) y_test=np.argmax(y_test,axis=1) confusion_mat=confusion_matrix(y_test,y_pred) # modelSave = model_name[:-5] + '_pred.mat' modelSave = sOutPath + '/' + sFilename + '_result.mat' sio.savemat(modelSave, {'prob_pre': prob_pre, 'score_test': score_test, 'acc_test': acc_test, 'confusion_mat':confusion_mat}) ############################################################################### ## OPTIMIZATIONS ## ###############################################################################
Example #28
Source File: multiclass_InceptionNet.py From CNNArt with Apache License 2.0 | 5 votes |
def fPredict(X_test, y_test, model_name, sOutPath, patchSize, batchSize): weight_name = model_name[0] #model_json = model_name[1] + '_json' #model_all = model_name[0] + '.hdf5' _, sPath = os.path.splitdrive(sOutPath) sPath, sFilename = os.path.split(sOutPath) #sFilename, sExt = os.path.splitext(sFilename) #f = h5py.File(weight_name, 'r+') #del f['optimizer_weights'] #f.close() model=load_model(weight_name) opti = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) callbacks = [EarlyStopping(monitor='val_loss', patience=10, verbose=1)] #model.compile(loss='categorical_crossentropy', optimizer=opti, metrics=['accuracy']) #model.load_weights(weight_name) model.summary(); score_test, acc_test = model.evaluate(X_test, y_test, batch_size=batchSize) prob_pre = model.predict(X_test, batchSize, 0) y_pred=np.argmax(prob_pre,axis=1) y_test=np.argmax(y_test,axis=1) confusion_mat=confusion_matrix(y_test,y_pred) # modelSave = model_name[:-5] + '_pred.mat' modelSave = sOutPath + '/' + sFilename + '_result.mat' sio.savemat(modelSave, {'prob_pre': prob_pre, 'score_test': score_test, 'acc_test': acc_test, 'confusion_mat':confusion_mat})
Example #29
Source File: test_callbacks.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_EarlyStopping(): np.random.seed(1337) (X_train, y_train), (X_test, y_test) = get_test_data(num_train=train_samples, num_test=test_samples, input_shape=(input_dim,), classification=True, num_classes=num_classes) y_test = np_utils.to_categorical(y_test) y_train = np_utils.to_categorical(y_train) model = Sequential() model.add(Dense(num_hidden, input_dim=input_dim, activation='relu')) model.add(Dense(num_classes, 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, epochs=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, epochs=20)
Example #30
Source File: model.py From lstm-siamese-text-similarity with MIT License | 5 votes |
def update_model(self, saved_model_path, new_sentences_pair, is_similar, embedding_meta_data): """ Update trained siamese model for given new sentences pairs Steps Involved: 1. Pass the each from sentences from new_sentences_pair to bidirectional LSTM encoder. 2. Merge the vectors from LSTM encodes and passed to dense layer. 3. Pass the dense layer vectors to sigmoid output layer. 4. Use cross entropy loss to train weights Args: model_path (str): model path of already trained siamese model new_sentences_pair (list): list of tuple of new sentences pairs is_similar (list): target value 1 if same sentences pair are similar otherwise 0 embedding_meta_data (dict): dict containing tokenizer and word embedding matrix Returns: return (best_model_path): path of best model """ tokenizer = embedding_meta_data['tokenizer'] train_data_x1, train_data_x2, train_labels, leaks_train, \ val_data_x1, val_data_x2, val_labels, leaks_val = create_train_dev_set(tokenizer, new_sentences_pair, is_similar, self.max_sequence_length, self.validation_split_ratio) model = load_model(saved_model_path) model_file_name = saved_model_path.split('/')[-1] new_model_checkpoint_path = saved_model_path.split('/')[:-2] + str(int(time.time())) + '/' new_model_path = new_model_checkpoint_path + model_file_name model_checkpoint = ModelCheckpoint(new_model_checkpoint_path + model_file_name, save_best_only=True, save_weights_only=False) early_stopping = EarlyStopping(monitor='val_loss', patience=3) tensorboard = TensorBoard(log_dir=new_model_checkpoint_path + "logs/{}".format(time.time())) model.fit([train_data_x1, train_data_x2, leaks_train], train_labels, validation_data=([val_data_x1, val_data_x2, leaks_val], val_labels), epochs=50, batch_size=3, shuffle=True, callbacks=[early_stopping, model_checkpoint, tensorboard]) return new_model_path