Keras中Callback函数的使用
回調(diào)函數(shù)是一組在訓(xùn)練的特定階段被調(diào)用的函數(shù)集,你可以使用回調(diào)函數(shù)來觀察訓(xùn)練過程中網(wǎng)絡(luò)內(nèi)部的狀態(tài)和統(tǒng)計(jì)信息。通過傳遞回調(diào)函數(shù)列表到模型的.fit()中,即可在給定的訓(xùn)練階段調(diào)用該函數(shù)集中的函數(shù)。
【Tips】雖然我們稱之為回調(diào)“函數(shù)”,但事實(shí)上Keras的回調(diào)函數(shù)是一個(gè)類,回調(diào)函數(shù)只是習(xí)慣性稱呼
Callback
例子No.1:官網(wǎng)示例?
保存訓(xùn)練過程中最好的模型
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
?
np.random.seed(1671) ?# for reproducibility
?
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 ? # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
?
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
?
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
#
X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
?
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
?
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
?
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax
?
model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
model.summary()
?
#model.load_weights("weight_bst.hdf5")
?
model.compile(loss='categorical_crossentropy',
? ? ? ? ? ? ? optimizer=OPTIMIZER,
? ? ? ? ? ? ? metrics=['accuracy'])
?
#保存.fit()過程中最好的模型
checkpoint=ModelCheckpoint("/weights.hdf5",verbose=1,save_best_only=True)
?
history = model.fit(X_train, Y_train,
? ? ? ? ? ? ? ? ? ? batch_size=BATCH_SIZE, epochs=5,
? ? ? ? ? ? ? ? ? ? verbose=VERBOSE, validation_split=VALIDATION_SPLIT,
? ? ? ? ? ? ? ? ? ? callbacks=[checkpoint])
?
例子No.2:保存每一個(gè)改進(jìn)CheckPoint模型
應(yīng)用Checkpoint時(shí),應(yīng)在每次訓(xùn)練中觀察到改進(jìn)時(shí)輸出模型權(quán)重。
Checkpoint設(shè)置成當(dāng)驗(yàn)證數(shù)據(jù)集的分類精度提高時(shí)保存網(wǎng)絡(luò)權(quán)重(monitor=’val_acc’ and mode=’max’)。權(quán)重存儲(chǔ)在一個(gè)包含評(píng)價(jià)的文件中(weights-improvement – { val_acc = .2f } .hdf5)。
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
?
np.random.seed(1671) ?# for reproducibility
?
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 ? # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
?
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
?
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
#
X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
?
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
?
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
?
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax
?
model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
model.summary()
?
#model.load_weights("weight_bst.hdf5")
?
model.compile(loss='categorical_crossentropy',
? ? ? ? ? ? ? optimizer=OPTIMIZER,
? ? ? ? ? ? ? metrics=['accuracy'])
?
#保存.fit()訓(xùn)練過程中,精確度提高的模型
filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint=ModelCheckpoint(filepath,monitor='val_acc',verbose=1,save_best_only=True,mode='max')
?
history = model.fit(X_train, Y_train,
? ? ? ? ? ? ? ? ? ? batch_size=BATCH_SIZE, epochs=5,
? ? ? ? ? ? ? ? ? ? verbose=VERBOSE, validation_split=VALIDATION_SPLIT,
? ? ? ? ? ? ? ? ? ? callbacks=[checkpoint])
程序運(yùn)行結(jié)果:?
在epoch=5次訓(xùn)練中,val_acc均提高,所以每次都保存了一個(gè)新的模型
?
例子No.3:保存最佳的Checkpoint模型
如果驗(yàn)證精度提高的話,一個(gè)更簡(jiǎn)單的Checkpoint策略是將模型權(quán)重保存到相同的文件中。
這可以使用上述相同的代碼輕松完成,并將輸出文件名更改為固定(不包括評(píng)價(jià)或次數(shù)的信息)。
在這種情況下,只有當(dāng)驗(yàn)證數(shù)據(jù)集上的模型的分類精度提高到到目前為止最好的時(shí)候,才會(huì)將模型權(quán)重寫入文件“weights.best.hdf5”。
?
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
?
np.random.seed(1671) ?# for reproducibility
?
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 ? # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
?
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
?
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
#
X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
?
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
?
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
?
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax
?
model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
model.summary()
?
#model.load_weights("weight_bst.hdf5")
?
model.compile(loss='categorical_crossentropy',
? ? ? ? ? ? ? optimizer=OPTIMIZER,
? ? ? ? ? ? ? metrics=['accuracy'])
?
#保存訓(xùn)練過程中最好的模型
filepath="weight_bst.hdf5"
checkpoint=ModelCheckpoint(filepath,monitor='val_acc',verbose=1,save_best_only=True,mode='max')
?
history = model.fit(X_train, Y_train,
? ? ? ? ? ? ? ? ? ? batch_size=BATCH_SIZE, epochs=5,
? ? ? ? ? ? ? ? ? ? verbose=VERBOSE, validation_split=VALIDATION_SPLIT,
? ? ? ? ? ? ? ? ? ? callbacks=[checkpoint)])
'''
score=model.evaluate(X_test,Y_test,verbose=VERBOSE)
print(score)
'''
程序運(yùn)行結(jié)果:
這是一個(gè)在你的實(shí)驗(yàn)中需要經(jīng)常用到的方便的Checkpoint策略。它將確保你的最佳模型被保存,以便稍后使用。它避免了輸入代碼來手動(dòng)跟蹤,并在訓(xùn)練時(shí)序列化最佳模型。
?
例子No.4:加載之前保存過的Checkpoint模型
Checkpoint只包括模型權(quán)重。它假定你了解網(wǎng)絡(luò)結(jié)構(gòu)。這也可以序列化成JSON或YAML格式。
在下面的示例中,模型結(jié)構(gòu)是已知的,并且最好的權(quán)重從先前的實(shí)驗(yàn)中加載,然后存儲(chǔ)在weights.best.hdf5文件的工作目錄中。
那么將該模型用于對(duì)整個(gè)數(shù)據(jù)集進(jìn)行預(yù)測(cè)。
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
?
np.random.seed(1671) ?# for reproducibility
?
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 ? # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
?
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
?
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
#
X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
?
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
?
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
?
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax
?
model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
model.summary()
?
#加載模型
model.load_weights("weight_bst.hdf5")
?
model.compile(loss='categorical_crossentropy',
? ? ? ? ? ? ? optimizer=OPTIMIZER,
? ? ? ? ? ? ? metrics=['accuracy'])
?
?
score=model.evaluate(X_test,Y_test,verbose=VERBOSE)
print(score)
?
?例子:LearningRateScheduler調(diào)整學(xué)習(xí)率
from keras.callbacks import LearningRateScheduler
def scheduler(epoch):
? ? if epoch % 100 == 0 and epoch != 0:
? ? ? ? lr = K.get_value(model.optimizer.lr)
? ? ? ? K.set_value(model.optimizer.lr, lr * 0.1)
? ? ? ? print("lr changed to {}".format(lr * 0.1))
? ? return K.get_value(model.optimizer.lr)
?
reduce_lr = LearningRateScheduler(scheduler)
model.fit(train_x, train_y, batch_size=32, epochs=5, callbacks=[reduce_lr])
?
例子:ReduceLROnPlateau自動(dòng)減少學(xué)習(xí)率?
from keras.callbacks import ReduceLROnPlateau
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,patience=5, min_lr=0.001)
model.fit(X_train, Y_train, callbacks=[reduce_lr])
?
例子:CSVLoggerb保存訓(xùn)練結(jié)果?
csv_logger = CSVLogger('training.log')
model.fit(X_train, Y_train, callbacks=[csv_logger])
?
例子:輕量級(jí)自定義回調(diào)函數(shù)LambdaCallback
# Print the batch number at the beginning of every batch.
batch_print_callback = LambdaCallback(
? ? on_batch_begin=lambda batch,logs: print(batch))
?
# Plot the loss after every epoch.
import numpy as np
import matplotlib.pyplot as plt
plot_loss_callback = LambdaCallback(
? ? on_epoch_end=lambda epoch, logs: plt.plot(np.arange(epoch),
? ? ? ? ? ? ? ? ? ? ? logs['loss']))
?
# Terminate some processes after having finished model training.
processes = ...
cleanup_callback = LambdaCallback(
? ? on_train_end=lambda logs: [
? ? p.terminate() for p in processes if p.is_alive()])
?
model.fit(...,
? ? ? callbacks=[batch_print_callback,
? ? ? ? ?plot_loss_callback,
? ? ? ? ?cleanup_callback])
?
?例子:查看TensorBoard監(jiān)控
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
?
np.random.seed(1671) ?# for reproducibility
?
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 ? # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
?
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
?
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
#
X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
?
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
?
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
?
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax
?
model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
model.summary()
?
?
model.compile(loss='categorical_crossentropy',
? ? ? ? ? ? ? optimizer=OPTIMIZER,
? ? ? ? ? ? ? metrics=['accuracy'])
?
?
history = model.fit(X_train, Y_train,
? ? ? ? ? ? ? ? ? ? batch_size=BATCH_SIZE, epochs=5,
? ? ? ? ? ? ? ? ? ? verbose=VERBOSE, validation_split=VALIDATION_SPLIT,
? ? ? ? ? ? ? ? ? ? callbacks=[TensorBoard(log_dir="./log")])
?
?
score=model.evaluate(X_test,Y_test,verbose=VERBOSE)
print(score)
?在命令行輸入命令:
運(yùn)行結(jié)果:
?
?
編寫自己的回調(diào)函數(shù)
我們可以通過繼承keras.callbacks.Callback編寫自己的回調(diào)函數(shù),回調(diào)函數(shù)通過類成員self.model訪問訪問,該成員是模型的一個(gè)引用。
這里是一個(gè)簡(jiǎn)單的保存每個(gè)batch的loss的回調(diào)函數(shù)
class LossHistory(keras.callbacks.Callback):
? ? def on_train_begin(self, logs={}):
? ? ? ? self.losses = []
?
? ? def on_batch_end(self, batch, logs={}):
? ? ? ? self.losses.append(logs.get('loss'))
?
例子:記錄損失函數(shù)的歷史數(shù)據(jù)
class LossHistory(keras.callbacks.Callback):
? ? def on_train_begin(self, logs={}):
? ? ? ? self.losses = []
?
? ? def on_batch_end(self, batch, logs={}):
? ? ? ? self.losses.append(logs.get('loss'))
?
model = Sequential()
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
?
history = LossHistory()
model.fit(X_train, Y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
?
print history.losses
# outputs
'''
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
參考:https://blog.csdn.net/weixin_38517705/article/details/82971147
?
總結(jié)
以上是生活随笔為你收集整理的Keras中Callback函数的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用keras的cifar10.load
- 下一篇: CV】keras_resnet 在cif