出现此问题是因为在转储历史时,它无法转储完整模型。所以在加载的时候,找不到自定义类。
我注意到keras.callbacks.History 对象有一个属性model,它的不完整转储是导致此问题的原因。
你说:
我只想腌制 keras 的历史,而不是模型
所以以下是一种解决方法:
hist = model.fit(X, Y, ...)
hist.model = None
只需将model 属性设置为None,就可以成功转储和加载历史对象!
以下是 MVCE:
from keras.models import Sequential
from keras.layers import Conv2D, Dense, Flatten, Layer
import keras.backend as K
import numpy as np
import pickle
# MyLayer from https://keras.io/layers/writing-your-own-keras-layers/
class MyLayer(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1], self.output_dim),
initializer='uniform',
trainable=True)
super(MyLayer, self).build(input_shape) # Be sure to call this at the end
def call(self, x):
return K.dot(x, self.kernel)
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), input_shape=(28,28,3), activation='sigmoid'))
model.add(Flatten())
model.add(MyLayer(10))
model.add(Dense(3, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
model.summary()
X = np.random.randn(64, 28, 28, 3)
Y = np.random.randint(0, high=2, size=(64,1))
hist = model.fit(X, Y, batch_size=8)
hist.model = None
with open('hist.pkl', 'wb') as f:
pickle.dump(hist, f)
with open('hist.pkl', 'rb') as f:
hist_reloaded = pickle.load(f)
print(hist.history)
print(hist_reloaded.history)
输出:
{'acc': [0.484375], 'loss': [6.140302091836929]}
{'acc': [0.484375], 'loss': [6.140302091836929]}
附:如果想用自定义层保存 keras 模型,this 应该会有所帮助。