@菜鸟初学keras错误一 20190620
初次使用keras,不知道model.compile,不晓得model.fit,不懂的如何保存checkpoint等模型。
经过一番挣扎后发现,model相关的保存可以通过Modelcheckpoint()来保存
原版可以看https://www.tensorflow.org/api_docs/python/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]))
就是在运行的时候报错: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的,求告知