本文整理汇总了Python中keras.callbacks方法的典型用法代码示例。如果您正苦于以下问题:Python keras.callbacks方法的具体用法?Python keras.callbacks怎么用?Python keras.callbacks使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras
的用法示例。
在下文中一共展示了keras.callbacks方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 1e-3
if epoch > 180:
lr *= 0.5e-3
elif epoch > 160:
lr *= 1e-3
elif epoch > 120:
lr *= 1e-2
elif epoch > 80:
lr *= 1e-1
print('Learning rate: ', lr)
return lr
示例2: train_simple_inference_net
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def train_simple_inference_net(n_epochs=30):
inf_net = SimpleInferenceNet()
tr_ids, val_ids, te_ids = train_document_ids(), validation_document_ids(), test_document_ids()
tr_ids = list(train_document_ids())
train_Xy, inference_vectorizer = get_train_Xy(tr_ids, sections_of_interest=None, vocabulary_file=None, include_sentence_span_splits=False, include_raw_texts=True)
X_k, y_k = make_Xy_inference(train_Xy, inf_net.bc)
print("train data for inference task loaded!")
val_Xy = get_Xy(val_ids, inference_vectorizer, include_raw_texts=True)
X_kv, y_kv = make_Xy_inference(val_Xy, inf_net.bc)
print("val data loaded!")
filepath="inference.weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
with open("inference_model.json", "w") as outf:
outf.write(inf_net.model.to_json())
print("fitting inference model!")
inf_net.model.fit(X_k, y_k, validation_data=(X_kv, y_kv), callbacks=callbacks_list, epochs=n_epochs)
示例3: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 1e-3
if epoch > 30:
lr *= 1e-2
elif epoch > 15:
lr *= 1e-1
print('Learning rate: ', lr)
return lr
示例4: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 1e-3
if epoch > 150:
lr *= 1e-2
elif epoch > 100:
lr *= 1e-1
print('Learning rate: ', lr)
return lr
示例5: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 1e-3
if epoch > 160:
lr *= 1e-3
elif epoch > 120:
lr *= 1e-2
elif epoch > 80:
lr *= 1e-1
print('Learning rate: ', lr)
return lr
示例6: train
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
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))
示例7: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""
Learning Rate Schedule
"""
# Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs. Called automatically every
# epoch as part of callbacks during training.
lr = 1e-3
if epoch > 180:
lr *= 1e-4
elif epoch > 160:
lr *= 1e-3
elif epoch > 120:
lr *= 1e-2
elif epoch > 80:
lr *= 1e-1
print('Learning rate: ', lr)
return lr
示例8: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 0.001
epoch += 1
# if epoch >= 90:
# lr *= 5e-2
# elif epoch >= 60:
# lr *= 1e-1
# elif epoch >= 30:
# lr *= 5e-1
if epoch >= 150:
lr *= 0.1
print('Learning rate: ', lr)
return lr
示例9: train_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def train_model(model, epochs=10, min_size=5, max_size=20, callbacks=None, verboose=False):
input_dim = model.input_dim
output_dim = model.output_dim
batch_size = model.batch_size
sample_generator = get_sample(batch_size=batch_size, in_bits=input_dim, out_bits=output_dim,
max_size=max_size, min_size=min_size)
if verboose:
for j in range(epochs):
model.fit_generator(sample_generator, steps_per_epoch=10, epochs=j+1, callbacks=callbacks, initial_epoch=j)
print("currently at epoch {0}".format(j+1))
for i in [5,10,20,40]:
test_model(model, sequence_length=i, verboose=True)
else:
model.fit_generator(sample_generator, steps_per_epoch=10, epochs=epochs, callbacks=callbacks)
print("done training")
示例10: lengthy_test
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lengthy_test(model, testrange=[5,10,20,40,80], epochs=100, verboose=True):
ts = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
log_path = LOG_PATH_BASE + ts + "_-_" + model.name
tensorboard = TensorBoard(log_dir=log_path,
write_graph=False, #This eats a lot of space. Enable with caution!
#histogram_freq = 1,
write_images=True,
batch_size = model.batch_size,
write_grads=True)
model_saver = ModelCheckpoint(log_path + "/model.ckpt.{epoch:04d}.hdf5", monitor='loss', period=1)
callbacks = [tensorboard, TerminateOnNaN(), model_saver]
for i in testrange:
acc = test_model(model, sequence_length=i, verboose=verboose)
print("the accuracy for length {0} was: {1}%".format(i,acc))
train_model(model, epochs=epochs, callbacks=callbacks, verboose=verboose)
for i in testrange:
acc = test_model(model, sequence_length=i, verboose=verboose)
print("the accuracy for length {0} was: {1}%".format(i,acc))
return
示例11: lr_schedule
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 1e-3
if epoch > 180:
lr *= 0.5e-3
elif epoch > 160:
lr *= 1e-3
elif epoch > 120:
lr *= 1e-2
elif epoch > 80:
lr *= 1e-1
print('Learning rate: ', lr)
return lr
示例12: train
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def train(self):
'''训练模型'''
number_of_epoch = len(self.files_content) // self.config.batch_size
if not self.model:
self.build_model()
self.model.summary()
self.model.fit_generator(
generator=self.data_generator(),
verbose=True,
steps_per_epoch=self.config.batch_size,
epochs=number_of_epoch,
callbacks=[
keras.callbacks.ModelCheckpoint(self.config.weight_file, save_weights_only=False),
LambdaCallback(on_epoch_end=self.generate_sample_result)
]
)
示例13: main
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def main():
# Define X and y
# # Load data
PATH = "./data/64_64_1/offset_1.3/"
X = np.load(PATH + "basic_dataset_img.npz")
y = np.load(PATH + "basic_dataset_pts.npz")
X = X['arr_0']
y = y['arr_0'].reshape(-1, 136)
print("Define X and Y")
print("=======================================")
# Split train / test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print("Success of getting train / test dataset")
print("=======================================")
print("X_train: ", X_train.shape)
print("y_train: ", y_train.shape)
print("X_test: ", X_test.shape)
print("y_test: ", y_test.shape)
print("=======================================")
model.compile(loss=smoothL1, optimizer=keras.optimizers.Adam(lr=1e-3), metrics=['mape'])
print(model.summary())
# checkpoint
filepath="./mobilenet_checkpoints/smooth_L1-{epoch:02d}-{val_mean_absolute_percentage_error:.5f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
history = model.fit(X_train, y_train, batch_size=64, epochs=10000, shuffle=True,\
verbose=1, validation_data=(X_test, y_test), callbacks=callbacks_list)
# Save model
model.save("./model/face_landmark_dnn.h5")
print("=======================================")
print("Save Final Model")
print("=======================================")
示例14: main
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def main():
# Define X and y
# # Load data
PATH = "./data/64_64_1/offset_1.3/"
X = np.load(PATH + "basic_dataset_img.npz")
y = np.load(PATH + "basic_dataset_pts.npz")
X = X['arr_0']
y = y['arr_0'].reshape(-1, 136)
print("Define X and Y")
print("=======================================")
# Split train / test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print("Success of getting train / test dataset")
print("=======================================")
print("X_train: ", X_train.shape)
print("y_train: ", y_train.shape)
print("X_test: ", X_test.shape)
print("y_test: ", y_test.shape)
print("=======================================")
model.compile(loss=smoothL1, optimizer=keras.optimizers.Adam(lr=1e-3), metrics=['mape'])
print(model.summary())
# checkpoint
filepath="./basic_checkpoints/smooth_L1-{epoch:02d}-{val_mean_absolute_percentage_error:.5f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
history = model.fit(X_train, y_train, batch_size=64, epochs=10000, shuffle=True,\
verbose=1, validation_data=(X_test, y_test), callbacks=callbacks_list)
# Save model
model.save("./model/face_landmark_dnn.h5")
print("=======================================")
print("Save Final Model")
print("=======================================")
示例15: train_net
# 需要导入模块: import keras [as 别名]
# 或者: from keras import callbacks [as 别名]
def train_net(net,
train_gen,
val_gen,
train_num_examples,
val_num_examples,
num_epochs,
checkpoint_filepath,
start_epoch1):
checkpointer = ModelCheckpoint(
filepath=checkpoint_filepath,
verbose=1,
save_best_only=True)
tic = time.time()
net.fit_generator(
generator=train_gen,
samples_per_epoch=train_num_examples,
epochs=num_epochs,
verbose=True,
callbacks=[checkpointer],
validation_data=val_gen,
validation_steps=val_num_examples,
class_weight=None,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
shuffle=True,
initial_epoch=(start_epoch1 - 1))
logging.info("Time cost: {:.4f} sec".format(
time.time() - tic))