警告:tensorflow:未将“ input_shape”传递给第一层的顺序模型无法重新加载其优化器状态

时间:2019-05-23 10:09:59

标签: python tensorflow keras deep-learning jupyter-notebook

我对Jupyter笔记本非常陌生。我正在关注其中保存了模型然后用于创建新模型的教程,但是它在第一层中显示了input_shape的错误。我在输入层(第一层)中添加了input_shape,但仍然向我显示了相同的错误。如何解决?

我也尝试通过将文件的扩展名更改为.h5,但仍然出现警告错误

[1]
model=tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu, input_shape=(4,4,512)))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

[2]
model.save('epic_num_reader.model')

[3]
new_model= tf.keras.models.load_model('epic_num_reader.model')

此new_model代码生成警告错误:

WARNING:tensorflow:Sequential models without an `input_shape` passed to the first layer cannot reload their optimizer state. As a result, your model isstarting with a freshly initialized optimizer.

1 个答案:

答案 0 :(得分:0)

已解决:)

(x_train, y_train), (x_test, y_test) = mnist.load_data() 
x_train = tf.keras.utils.normalize(x_train, axis=1).reshape(x_train.shape[0], -1)
x_test = tf.keras.utils.normalize(x_test, axis=1).reshape(x_test.shape[0], -1)

model = tf.keras.models.Sequential()
#model.add(tf.keras.layers.Flatten())   
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu, input_shape= x_train.shape[1:]))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))