@菜鳥初學keras錯誤一 20190620

@菜鳥初學keras錯誤一 20190620

記錄一下今天的錯誤

初次使用keras,不知道model.compile,不曉得model.fit,不懂的如何保存checkpoint等模型。
經過一番掙扎後發現,model相關的保存可以通過Modelcheckpoint()來保存

Modelcheckpoint介紹

原版可以看https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/ModelCheckpoint

tf.keras.callbacks.ModelCheckpoint

解釋:

Class ModelCheckpoint
Inherits From: Callback

Defined in tensorflow/python/keras/callbacks.py.

Save the model after every epoch.

filepath can contain named formatting options, which will be filled the value of epoch and keys in logs (passed in on_epoch_end).

For example: if filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5, then the model checkpoints will be saved with the epoch number and the validation loss in the filename.


### Arguments:

 1. filepath: string, path to save the model file.
 2. monitor: quantity to monitor.
 3. verbose: verbosity mode, 0 or 1.
 4. save_best_only: if save_best_only=True, the latest best model according to the quantity monitored will not be overwritten.
 5. mode: one of {auto, min, max}. If save_best_only=True, the decision to overwrite the current save file is made based on either the maximization or the minimization of the monitored quantity. For val_acc, this should be max, for val_loss this should be min, etc. In auto mode, the direction is automatically inferred from the name of the monitored quantity.
 6. save_weights_only: if True, then only the model's weights will be saved (model.save_weights(filepath)), else the full model is saved (model.save(filepath)).
 7. period: Interval (number of epochs) between checkpoints.

用法

可以參考http://frankchen.xyz/2018/04/19/keras-reuse-model/

保存模型

如下,我們預定義保存的hdf5文件名,再初始化ModelCheckpoint,將其加入Keras的callback裏(即每個batch結束後做的事情),那麼模型就會在每次batch結束後對比,保存最好的模型。

from keras.callbacks import ModelCheckpoint
# create model
model = Sequential()
model.add(...)
model.add(...)
model.add(...)
# Compile model
model.compile(...)
# checkpoint
filepath="weights-{epoch:02d}-{val_acc:.2f}.hdf5" #文件名
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# Fit the model
model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, callbacks=[checkpoint], verbose=0)

加載模型

注意,之前保存的只是模型的weights,(ps:想要保存全部模型可以將save_weights_only=False,True是隻保存weight_model)重新load需要再次定義模型結構再load weights並再次combine,例如

from keras.callbacks import ModelCheckpoint
# create model
model = Sequential()
model.add(...)
model.add(...)
model.add(...)
# load weights
model.load_weights("weights.best.hdf5")
# Compile model 
model.compile(...)
# estimate accuracy 
scores = model.evaluate(X, Y, verbose=0)
print('{}: {:.2%}'.format(model.metrics_names[1], scores[1]))

如果之前選擇了連模型結構也一起保存(即在ModelCheckpoint中選擇save_weights_only=False),那麼load就很簡單,

from keras.callbacks import ModelCheckpoint
from keras.models import load_model
# create model
model = Sequential()
model.add(...)
model.add(...)
model.add(...)
# Compile model
model.compile(...)
# checkpoint
filepath="weights-best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max', save_weights_only=False)
# Fit the model
model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, callbacks=[checkpoint], verbose=0)
# Load the model
model= load_model(filepath)
scores=model.evaluate(X, Y,verbose=0)
print('{}: {:.2%}'.format(model.metrics_names[1], scores[1]))

然後出現了一個大bug

就是在運行的時候報錯:AttributeError: ‘ModelCheckpoint’ object has no attribute ‘on_train_batch_begin’
死活不知道怎麼弄,後來看到https://xbuba.com/questions/55112713
靈光一現,發現在import的時候出錯了

應該將
from keras.callbacks import Modelcheckpoint
改爲從tensorflow中導入,即
from tensorflow.python.keras.callbacks import ModelCheckpoint

然後就能跑代碼了,但是依然還有很多問題,這幾天弄一下model.compile()的相關知識
ps:學個新知識好慢啊,至今還沒有弄明白這個model.fit之類的和tf.session()的適用情況?
ps:有沒有人曉得如何將合成的5D tensor轉換爲4D的,求告知

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章