本文整理匯總了Python中keras.callbacks.ReduceLROnPlateau方法的典型用法代碼示例。如果您正苦於以下問題:Python callbacks.ReduceLROnPlateau方法的具體用法?Python callbacks.ReduceLROnPlateau怎麽用?Python callbacks.ReduceLROnPlateau使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.callbacks
的用法示例。
在下文中一共展示了callbacks.ReduceLROnPlateau方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_call_back
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def get_call_back():
"""
定義call back
:return:
"""
checkpoint = ModelCheckpoint(filepath='/tmp/ctpn.{epoch:03d}.h5',
monitor='val_loss',
verbose=1,
save_best_only=False,
save_weights_only=True,
period=5)
# 驗證誤差沒有提升
lr_reducer = ReduceLROnPlateau(monitor='loss',
factor=0.1,
cooldown=0,
patience=10,
min_lr=1e-4)
log = TensorBoard(log_dir='log')
return [lr_reducer, checkpoint, log]
示例2: _create_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def _create_callbacks(self, saved_weights_name, model_to_save):
checkpoint = CustomModelCheckpoint(
model_to_save=model_to_save,
filepath=saved_weights_name + 'ex-{epoch:03d}--loss-{loss:08.3f}.h5',
monitor='loss',
verbose=0,
save_best_only=True,
mode='min',
period=1
)
reduce_on_plateau = ReduceLROnPlateau(
monitor='loss',
factor=0.1,
patience=2,
verbose=0,
mode='min',
epsilon=0.01,
cooldown=0,
min_lr=0
)
tensor_board = TensorBoard(
log_dir=self.__logs_directory
)
return [checkpoint, reduce_on_plateau, tensor_board]
示例3: train_model
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
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
示例4: NerCallbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
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
示例5: fit
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def fit(self, train_X, val_X, nb_epoch=50, batch_size=100):
print 'Training variational autoencoder'
optimizer = Adadelta(lr=2.)
self.vae.compile(optimizer=optimizer, loss=self.vae_loss)
self.vae.fit(train_X[0], train_X[1],
shuffle=True,
epochs=nb_epoch,
batch_size=batch_size,
validation_data=(val_X[0], val_X[1]),
callbacks=[ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.01),
EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=5, verbose=1, mode='auto'),
CustomModelCheckpoint(self.encoder, self.save_model, monitor='val_loss', save_best_only=True, mode='auto')
]
)
return self
示例6: test_ReduceLROnPlateau_patience
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def test_ReduceLROnPlateau_patience():
class DummyOptimizer(object):
def __init__(self):
self.lr = K.variable(1.0)
class DummyModel(object):
def __init__(self):
self.optimizer = DummyOptimizer()
reduce_on_plateau = callbacks.ReduceLROnPlateau(monitor='val_loss',
patience=2)
reduce_on_plateau.model = DummyModel()
losses = [0.0860, 0.1096, 0.1040]
lrs = []
for epoch in range(len(losses)):
reduce_on_plateau.on_epoch_end(epoch, logs={'val_loss': losses[epoch]})
lrs.append(K.get_value(reduce_on_plateau.model.optimizer.lr))
# The learning rates should be 1.0 except the last one
assert all([lr == 1.0 for lr in lrs[:-1]]) and lrs[-1] < 1.0
示例7: main
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def main():
(train_x, train_y), (test_x, test_y) = load_data()
model = models.Sequential([
layers.Conv2D(64, (3, 3), padding='same', activation='relu', input_shape=(None, None, 1)),
layers.MaxPooling2D(), # 19 -> 9
layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
layers.MaxPooling2D(), # 9 -> 4
layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
layers.MaxPooling2D(), # 4 -> 2
layers.GlobalAveragePooling2D(),
layers.Dropout(0.25),
layers.Dense(64, activation='relu'),
layers.Dense(80, activation='softmax'),
])
model.summary()
model.compile(optimizer='rmsprop',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 當標準評估停止提升時,降低學習速率
reduce_lr = ReduceLROnPlateau(verbose=1)
history = model.fit(train_x, train_y, epochs=100,
validation_data=(test_x, test_y),
callbacks=[reduce_lr])
savefig(history, start=10)
model.save('model.v1.0.h5', include_optimizer=False)
示例8: create_model
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def create_model(x_train, y_train, x_test, y_test):
model = Sequential()
model.add(Dense(44, input_shape=(784,)))
model.add(Activation({{choice(['relu', 'sigmoid'])}}))
model.add(Dense(44))
model.add(Activation({{choice(['relu', 'sigmoid'])}}))
model.add(Dense(10))
model.compile(loss='mae', metrics=['mse'], optimizer="adam")
es = EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=10)
rlr = ReduceLROnPlateau(factor=0.1, patience=10)
_ = model.fit(x_train, y_train, epochs=1, verbose=0, callbacks=[es, rlr],
batch_size=24, validation_data=(x_test, y_test))
mae, mse = model.evaluate(x_test, y_test, verbose=0)
print('MAE:', mae)
return {'loss': mae, 'status': STATUS_OK, 'model': model}
示例9: get_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def get_callbacks(model_file, initial_learning_rate=0.0001, learning_rate_drop=0.5, learning_rate_epochs=None,
learning_rate_patience=50, logging_file="training.log", verbosity=1,
early_stopping_patience=None):
callbacks = list()
callbacks.append(ModelCheckpoint(model_file,monitor='val_acc', save_best_only=True,verbose=verbosity, save_weights_only=True))
# callbacks.append(ModelCheckpoint(model_file, save_best_only=True, save_weights_only=True))
callbacks.append(CSVLogger(logging_file, append=True))
if learning_rate_epochs:
callbacks.append(LearningRateScheduler(partial(step_decay, initial_lrate=initial_learning_rate,
drop=learning_rate_drop, epochs_drop=learning_rate_epochs)))
else:
callbacks.append(ReduceLROnPlateau(factor=learning_rate_drop, patience=learning_rate_patience,
verbose=verbosity))
if early_stopping_patience:
callbacks.append(EarlyStopping(verbose=verbosity, patience=early_stopping_patience))
return callbacks
示例10: fit
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def fit(self, features, labels, validation_split, epochs=50):
"""
Trains the neural net on the data provided.
:param features: Numpy array of training data.
:param labels: Numpy array of target (label) data.
:param validation_split: Float between 0 and 1. Percentage of training data to use for validation
:param epochs: Max number of times to train over dataset.
"""
self.model.fit(x=features, y=labels, epochs=epochs, verbose=1,
callbacks=[ReduceLROnPlateau(), EarlyStopping(patience=3)], validation_split=validation_split,
shuffle=True)
for layer in self.model.layers[:self._NUM_BOTTOM_LAYERS_TO_RETRAIN]:
layer.trainable = False
for layer in self.model.layers[self._NUM_BOTTOM_LAYERS_TO_RETRAIN:]:
layer.trainable = True
self.model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
self.model.fit(x=features, y=labels, epochs=50, verbose=1,
callbacks=[ReduceLROnPlateau(), EarlyStopping(patience=3)], validation_split=validation_split,
shuffle=True)
示例11: callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def callbacks(self):
"""
:return:
"""
# TODO: Add ReduceLROnPlateau callback
cbs = []
tb = TensorBoard(log_dir=self.log_dir,
write_graph=True,
write_images=True)
cbs.append(tb)
best_model_filename = self.model_name + '_best.h5'
best_model = os.path.join(self.checkpoint_dir, best_model_filename)
save_best = ModelCheckpoint(best_model, save_best_only=True)
cbs.append(save_best)
checkpoints = ModelCheckpoint(filepath=self.checkpoint_file, verbose=1)
cbs.append(checkpoints)
reduce_lr = ReduceLROnPlateau(patience=1, verbose=1)
cbs.append(reduce_lr)
return cbs
示例12: main
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def main(rootdir, case, results):
train_x, train_y, valid_x, valid_y, test_x, test_y = get_data(args.dataset, case)
input_shape = (train_x.shape[1], train_x.shape[2])
num_class = train_y.shape[1]
if not os.path.exists(rootdir):
os.makedirs(rootdir)
filepath = os.path.join(rootdir, str(case) + '.hdf5')
saveto = os.path.join(rootdir, str(case) + '.csv')
optimizer = Adam(lr=args.lr, clipnorm=args.clip)
pred_dir = os.path.join(rootdir, str(case) + '_pred.txt')
if args.train:
model = creat_model(input_shape, num_class)
early_stop = EarlyStopping(monitor='val_acc', patience=15, mode='auto')
reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.1, patience=5, mode='auto', cooldown=3., verbose=1)
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='auto')
csv_logger = CSVLogger(saveto)
if args.dataset=='NTU' or args.dataset == 'PKU':
callbacks_list = [csv_logger, checkpoint, early_stop, reduce_lr]
else:
callbacks_list = [csv_logger, checkpoint]
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
model.fit(train_x, train_y, validation_data=[valid_x, valid_y], epochs=args.epochs,
batch_size=args.batch_size, callbacks=callbacks_list, verbose=2)
# test
model = creat_model(input_shape, num_class)
model.load_weights(filepath)
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
scores = get_activation(model, test_x, test_y, pred_dir, VA=10, par=9)
results.append(round(scores, 2))
開發者ID:microsoft,項目名稱:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代碼行數:37,代碼來源:va-rnn.py
示例13: learn
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def learn():
(train_x, train_y, sample_weight), (test_x, test_y) = load_data()
datagen = ImageDataGenerator(horizontal_flip=True,
vertical_flip=True)
train_generator = datagen.flow(train_x, train_y, sample_weight=sample_weight)
base = VGG16(weights='imagenet', include_top=False, input_shape=(None, None, 3))
for layer in base.layers[:-4]:
layer.trainable = False
model = models.Sequential([
base,
layers.BatchNormalization(),
layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
layers.GlobalAveragePooling2D(),
layers.BatchNormalization(),
layers.Dense(64, activation='relu'),
layers.BatchNormalization(),
layers.Dropout(0.20),
layers.Dense(80, activation='softmax')
])
model.compile(optimizer=optimizers.RMSprop(lr=1e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
reduce_lr = ReduceLROnPlateau(verbose=1)
model.fit_generator(train_generator, epochs=400,
steps_per_epoch=100,
validation_data=(test_x[:800], test_y[:800]),
callbacks=[reduce_lr])
result = model.evaluate(test_x, test_y)
print(result)
model.save('12306.image.model.h5', include_optimizer=False)
示例14: init_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def init_callbacks(self):
# self.callbacks.append(
# ModelCheckpoint(
# filepath=os.path.join(self.config.checkpoint_dir, '%s-{epoch:02d}-{val_loss:.2f}.hdf5' % self.config.exp_name),
# monitor=self.config.checkpoint_monitor,
# mode=self.config.checkpoint_mode,
# save_best_only=self.config.checkpoint_save_best_only,
# save_weights_only=self.config.checkpoint_save_weights_only,
# verbose=self.config.checkpoint_verbose,
# )
# )
# self.callbacks.append(
# TensorBoard(
# log_dir=self.config.tensorboard_log_dir,
# write_graph=self.config.tensorboard_write_graph,
# )
# )
#學習率衰減
reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=1/math.e,
verbose=1, patience=self.patience, min_lr=self.min_lr)
self.callbacks.append(reduce_lr)
# if hasattr(self.config,"comet_api_key"):
# from comet_ml import Experiment
# experiment = Experiment(api_key=self.config.comet_api_key, project_name=self.config.exp_name)
# experiment.disable_mp()
# experiment.log_multiple_params(self.config)
# self.callbacks.append(experiment.get_keras_callback())
示例15: callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import ReduceLROnPlateau [as 別名]
def callbacks(logdir):
model_checkpoint = ModelCheckpoint("weights_train/weights.{epoch:02d}-{loss:.2f}.h5", monitor='loss', verbose=1, period=10)
tensorboard_callback = TensorBoard(log_dir=logdir, write_graph=True, write_images=True, histogram_freq=1)
plateau_callback = ReduceLROnPlateau(monitor='loss', factor=0.99, verbose=1, patience=0, min_lr=0.00001)
#return [CheckPoints(), tensorboard_callback, LrReducer()]
return [model_checkpoint, tensorboard_callback, plateau_callback, LrReducer()]