这是我收到的错误消息
Traceback (most recent call last):
File "/home/xxx/Documents/program/test.py", line 27, in <module>
model.load_weights('models/model.h5')
File "/home/xxx/Documents/program/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1391, in load_weights
saving.load_weights_from_hdf5_group(f, self.layers)
File "/home/xxx/Documents/program/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 732, in load_weights_from_hdf5_group
' layers.')
ValueError: You are trying to load a weight file containing 2 layers into a model with 0 layers.
从这个产生错误的最小示例开始
from tensorflow import keras
from data import get_data
X_train, y_train, X_val, y_val = get_data() # get some train and val data
model = keras.Sequential()
model.add(keras.layers.Dense(64, activation='relu'))
model.add(keras.layers.Dense(7, activation='softmax'))
model.compile(
optimizer=keras.optimizers.Adam(1e-4),
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.fit(
x=X_train,
y=y_train,
batch_size=500,
epochs=200,
verbose=2,
validation_data=(X_val, y_val)
)
model.save_weights('models/model.h5')
model.load_weights('models/model.h5')
直接运行它不会产生错误。但是,当我第二次运行该程序以注释掉试图加载权重的训练部分(从第10行到第25行)时,它给了我这个错误。
我正在使用Tensorflow 1.9.0和内置的Keras。
答案 0 :(得分:0)
如上所述,在keras顺序模式下似乎存在一个错误:https://github.com/keras-team/keras/issues/10417。
但是,您可以使用Keras Functional API来解决此问题(在构建具有复杂I / O和张量级联的棘手RNN模型时,您还会发现Functional API更加有用)。
使用model.save_weights()
方法保存神经网络的缺点是,必须在将.h5
权重加载到NN中之前调用模型体系结构。如果您保存整个模型(包括参数和体系结构),则会发现将经过训练的模型加载到Python对象中要容易得多。您可以使用model.save()
方法来实现。
### TRAINING CODE
import tensorflow as tf
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# some data
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
y_train_oh = tf.keras.utils.to_categorical(y_train)
y_val_oh = tf.keras.utils.to_categorical(y_val)
# Keras Functional API
x = tf.keras.Input(shape=(4,))
dense = tf.keras.layers.Dense(64, activation='relu')(x)
dense = tf.keras.layers.Dense(3, activation='softmax')(dense)
model = tf.keras.Model(inputs=x, outputs=dense)
model.compile(optimizer=tf.keras.optimizers.Adam(1e-4),
loss='categorical_crossentropy',
metrics=['accuracy'])
# training
model.fit(X_train, y_train_oh, 16, epochs=20, validation_data=(X_val, y_val_oh))
# save weights
model.save_weights('models/model_weights.h5')
# save weights AND architecture
model.save('models/model.h5')
### TESTING CODE
# Model loading using .h5 weights file
import tensorflow as tf
x = tf.keras.Input(shape=(4,))
dense = tf.keras.layers.Dense(64, activation='relu')(x)
dense = tf.keras.layers.Dense(3, activation='softmax')(dense)
model2 = tf.keras.Model(inputs=x, outputs=dense)
model2.load_weights('models/model_weights.h5')
# Model loading using .h5 model file
import tensorflow as tf
model3 = tf.keras.models.load_model('models/model.h5') # simpler API, but bigger filesize