1 回答

TA貢獻1811條經驗 獲得超6個贊
現在,在運行訓練循環一段時間后,如果我決定將學習速率更改為.002,我是否必須運行與模型相關的所有代碼(模型結構,然后是優化等)?
您可以在訓練期間或加載模型后更新學習速率。
請記住,學習速率不屬于模型架構,它屬于優化器(在模型編譯期間分配)。學習速率是一個超參數,用于調節梯度下降期間權重更新的大?。ū硎救缦?nbsp;alpha):
因此,在初始訓練之后,您可以加載(保存的)模型,使用新的學習速率更新優化器(并可能將自定義對象分配給編譯器),然后繼續訓練。請記住,在長時間訓練模型后更改優化程序本身可能會產生較差的準確性結果,因為您的模型現在必須根據新優化器的權重計算重新校準。
如何從停止訓練時的先前狀態加載?
在 Keras 中,您可以選擇保存/加載整個模型(包括架構、權重、優化器狀態,或僅保存權重,或僅保存/加載架構(源)。
要保存/加載整個模型:
from keras.models import load_model
model.save('my_model.h5')
model = load_model('my_model.h5')
要僅保存/加載模型權重:
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5')
您還可以在模型加載期間分配自定義對象:
model = load_model(filepath, custom_objects={'loss': custom_loss})
另一個問題是,如果我重新啟動PC,并使用我之前在這里共享的檢查點代碼運行jupyter單元格,這會替換以前保存的文件嗎?
取決于檢查點中使用的文件路徑:“如果文件路徑是權重。{epoch:02d}-{val_loss:.2f}.hdf5,則模型檢查點將與紀元編號和驗證丟失一起保存在文件名中“。因此,如果對文件路徑使用唯一格式,則可以避免覆蓋以前保存的模型。源
加載保存的文件和權重并從那里恢復訓練的理想方法是什么?
例:
# Define model
model = keras.models.Sequential()
model.add(L.InputLayer([None],dtype='int32'))
model.add(L.Embedding(len(all_words),50))
model.add(keras.layers.Bidirectional(L.SimpleRNN(5,return_sequences=True)))
# Define softmax layer for every time step (hence TimeDistributed layer)
stepwise_dense = L.Dense(len(all_words),activation='softmax')
stepwise_dense = L.TimeDistributed(stepwise_dense)
model.add(stepwise_dense)
import keras.backend as K
# compile model with adam optimizer
model.compile('adam','categorical_crossentropy')
# print learning rate
print(f"Model learning rate is: {K.get_value(model.optimizer.lr):.3f}")
# train model
model.fit_generator(generate_batches(train_data), len(train_data)/BATCH_SIZE,
callbacks=[EvaluateAccuracy()], epochs=1)
# save model (weights, architecture, optimizer state)
model.save('my_model.h5')
# delete existing model
del model
結果
Model learning rate is: 0.001
Epoch 1/1
1341/1343 [============================>.] - ETA: 0s - loss: 0.4288
Measuring validation accuracy...
Validation accuracy: 0.93138
from keras.models import load_model
# create new adam optimizer with le-04 learning rate (previous: 1e-03)
adam = keras.optimizers.Adam(lr=1e-4)
# load model
model = load_model('my_model.h5', compile=False)
# compile model and print new learning rate
model.compile(adam, 'categorical_crossentropy')
print(f"Model learning rate is: {K.get_value(model.optimizer.lr):.4f}")
# train model for 3 more epochs with new learning rate
print("Training model: ")
model.fit_generator(generate_batches(train_data),len(train_data)/BATCH_SIZE,
callbacks=[EvaluateAccuracy()], epochs=3,)
結果:
Model learning rate is: 0.0001
Training model:
Epoch 1/3
1342/1343 [============================>.] - ETA: 0s - loss: 0.0885
Measuring validation accuracy...
Validation accuracy: 0.93568
1344/1343 [==============================] - 41s - loss: 0.0885
Epoch 2/3
1342/1343 [============================>.] - ETA: 0s - loss: 0.0768
Measuring validation accuracy...
Validation accuracy: 0.93925
1344/1343 [==============================] - 39s - loss: 0.0768
Epoch 3/3
1343/1343 [============================>.] - ETA: 0s - loss: 0.0701
Measuring validation accuracy...
Validation accuracy: 0.94180
添加回答
舉報