本文整理匯總了Python中keras.callbacks.LearningRateScheduler方法的典型用法代碼示例。如果您正苦於以下問題:Python callbacks.LearningRateScheduler方法的具體用法?Python callbacks.LearningRateScheduler怎麽用?Python callbacks.LearningRateScheduler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.callbacks
的用法示例。
在下文中一共展示了callbacks.LearningRateScheduler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def get_callbacks(self,log, model_prefix='Model'):
"""
Creates a list of callbacks that can be used during training to create a
snapshot ensemble of the model.
Args:
model_prefix: prefix for the filename of the weights.
Returns: list of 3 callbacks [ModelCheckpoint, LearningRateScheduler,
SnapshotModelCheckpoint] which can be provided to the 'fit' function
"""
if not os.path.exists(self.save_dir+'/weights/'):
os.makedirs(self.save_dir+'/weights/')
callback_list = [callbacks.ModelCheckpoint(self.save_dir+"/weights/weights_{epoch:002d}.h5", monitor="val_capsnet_acc",
save_best_only=True, save_weights_only=False),
callbacks.LearningRateScheduler(schedule=self._cosine_anneal_schedule),
SnapshotModelCheckpoint(self.T, self.M, fn_prefix=self.save_dir+'/weights/%s' % model_prefix), log]
return callback_list
示例2: get_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def get_callbacks(self, model_prefix='Model'):
"""
Creates a list of callbacks that can be used during training to create a
snapshot ensemble of the model.
Args:
model_prefix: prefix for the filename of the weights.
Returns: list of 3 callbacks [ModelCheckpoint, LearningRateScheduler,
SnapshotModelCheckpoint] which can be provided to the 'fit' function
"""
if not os.path.exists('weights/'):
os.makedirs('weights/')
callback_list = [ModelCheckpoint('weights/%s-Best.h5' % model_prefix,
monitor='val_acc',
save_best_only=True, save_weights_only=True),
LearningRateScheduler(schedule=self._cosine_anneal_schedule),
SnapshotModelCheckpoint(self.T,
self.M,
fn_prefix='weights/%s' % model_prefix)]
return callback_list
示例3: full_run
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def full_run(dataset, out_weights="weights.h5"):
cache_file = dataset.split("/")[-1] + ".p"
dataset = open_dataset(dataset, dataset_cache=cache_file)
cdrs, lbls, masks = dataset["cdrs"], dataset["lbls"], dataset["masks"]
sample_weight = np.squeeze((lbls * 1.7 + 1) * masks)
model = ab_seq_model(dataset["max_cdr_len"])
rate_schedule = lambda e: 0.001 if e >= 10 else 0.01
model.fit([cdrs, np.squeeze(masks)],
lbls, batch_size=32, epochs=18,
sample_weight=sample_weight,
callbacks=[LearningRateScheduler(rate_schedule)])
model.save_weights(out_weights)
示例4: test_LearningRateScheduler
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def test_LearningRateScheduler():
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='sgd',
metrics=['accuracy'])
cbks = [callbacks.LearningRateScheduler(lambda x: 1. / (1. + x))]
model.fit(X_train, y_train, batch_size=batch_size,
validation_data=(X_test, y_test), callbacks=cbks, epochs=5)
assert (float(K.get_value(model.optimizer.lr)) - 0.2) < K.epsilon()
示例5: train
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def train():
data = load_train_data()
data = data.reshape((data.shape[0],data.shape[1],data.shape[2],1))
data = data.astype('float32')/255.0
# model selection
if args.pretrain: model = load_model(args.pretrain, compile=False)
else:
if args.model == 'DnCNN': model = models.DnCNN()
# compile the model
model.compile(optimizer=Adam(), loss=['mse'])
# use call back functions
ckpt = ModelCheckpoint(save_dir+'/model_{epoch:02d}.h5', monitor='val_loss',
verbose=0, period=args.save_every)
csv_logger = CSVLogger(save_dir+'/log.csv', append=True, separator=',')
lr = LearningRateScheduler(step_decay)
# train
history = model.fit_generator(train_datagen(data, batch_size=args.batch_size),
steps_per_epoch=len(data)//args.batch_size, epochs=args.epoch, verbose=1,
callbacks=[ckpt, csv_logger, lr])
return model
示例6: get_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [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
示例7: train
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def train(inbalance_size):
(X_train, y_train), (X_test, y_test) = inbalanced_mnist(inbalance_size)
y_train = y_train[:, :10]
y_test = y_test[:, :10]
model = create_models()
model.compile("adam", "categorical_crossentropy", ["acc"])
scheduler = LearningRateScheduler(step_decay)
f1 = F1Callback(model, X_test, y_test)
history = model.fit(X_train, y_train, validation_data=(X_test, y_test), callbacks=[scheduler, f1],
batch_size=128, epochs=100, verbose=0).history
max_acc = max(history["val_acc"])
max_f1 = max(f1.f1_log)
print(f"{inbalance_size} {max_acc:.04} {max_f1:.04}")
示例8: train
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
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'))
示例9: model_train
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
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
示例10: train
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def train(model, data, args):
"""
Training a CapsuleNet
:param model: the CapsuleNet model
:param data: a tuple containing training and testing data, like `((x_train, y_train), (x_test, y_test))`
:param args: arguments
:return: The trained model
"""
(x_train, y_train), (x_test, y_test) = data
log = callbacks.CSVLogger(args.save_dir + '/log.csv')
checkpoint = callbacks.ModelCheckpoint(args.save_dir + '/weights-{epoch:02d}.h5', monitor='val_capsnet_acc',
save_best_only=False, save_weights_only=True, verbose=1)
lr_decay = callbacks.LearningRateScheduler(schedule=lambda epoch: args.lr * (args.lr_decay ** epoch))
model.compile(optimizer=optimizers.Adam(lr=args.lr),
loss=[margin_loss, 'mse'],
loss_weights=[1., args.lam_recon],
metrics={'capsnet': 'accuracy'})
def train_generator(x, y, batch_size, shift_fraction=0.):
train_datagen = ImageDataGenerator(width_shift_range=shift_fraction,
height_shift_range=shift_fraction)
generator = train_datagen.flow(x, y, batch_size=batch_size)
while 1:
x_batch, y_batch = generator.next()
yield ([x_batch, y_batch], [y_batch, x_batch])
model.fit_generator(generator=train_generator(x_train, y_train, args.batch_size, args.shift_fraction),
steps_per_epoch=int(y_train.shape[0] / args.batch_size),
epochs=args.epochs,
shuffle = True,
validation_data=[[x_test, y_test], [y_test, x_test]],
callbacks=snapshot.get_callbacks(log,model_prefix=model_prefix))
model.save_weights(args.save_dir + '/trained_model.h5')
print('Trained model saved to \'%s/trained_model.h5\'' % args.save_dir)
return model
示例11: define_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def define_callbacks(early_stopping_delta, early_stopping_epochs, use_lr_strategy=True, initial_lr=0.005, lr_drop_koef=0.66, epochs_to_drop=5, model_checkpoint_dir=None):
early_stopping = EarlyStopping(monitor='val_loss', min_delta=early_stopping_delta, patience=early_stopping_epochs, verbose=1)
callbacks_list = [early_stopping]
if model_checkpoint_dir is not None:
model_checkpoint = ModelCheckpoint(os.path.join(model_checkpoint_dir,'weights.{epoch:02d}-{val_loss:.2f}.h5'), monitor='val_loss', save_best_only=True, verbose=0)
callbacks_list.append(model_checkpoint)
if use_lr_strategy:
epochs_to_drop = float(epochs_to_drop)
loss_history = LossHistory(initial_lr, lr_drop_koef, epochs_to_drop)
lrate = LearningRateScheduler(lambda epoch: step_decay(initial_lr, lr_drop_koef, epochs_to_drop, epoch))
callbacks_list.append(loss_history)
callbacks_list.append(lrate)
return callbacks_list
示例12: get_lr_scheduler
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def get_lr_scheduler(self):
def scheduler(epoch):
epoch_interval = K.get_value(self.epoch_interval)
if epoch != 0 and (epoch + 1) % epoch_interval == 0:
lr = K.get_value(self.lr)
decay = K.get_value(self.decay)
K.set_value(self.lr, lr * decay)
if self.verbose:
print(self.get_config())
return K.get_value(self.lr)
return LearningRateScheduler(scheduler)
示例13: train
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def train(model, args, input_generator, callbacks=[], num_samples=10):
lr_schedule = TextureNetLearningSchedule(
args.batch_size, args.num_iterations_per_epoch,
initial_lr=args.learn_rate, min_lr=args.min_learn_rate,
cliff=20)
callbacks.append(LearningRateScheduler(lr_schedule))
try:
model.fit_generator(
generate_training_batches(args, input_generator),
samples_per_epoch=args.num_iterations_per_epoch, nb_epoch=args.num_epochs,
callbacks=callbacks
)
except KeyboardInterrupt:
print('Stopping training...')
示例14: gen_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [as 別名]
def gen_callbacks(self, callbacks_list):
lrate = LearningRateScheduler(self.step_decay)
callbacks_list.append(lrate)
#loss_history = LossHistory(self.step_decay)
#callbacks_list.append(loss_history)
return callbacks_list
示例15: get_callbacks
# 需要導入模塊: from keras import callbacks [as 別名]
# 或者: from keras.callbacks import LearningRateScheduler [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, save_best_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