我创建并保存 Keras 顺序模型:
model = Sequential()
model.add(Input(shape=(20)))
model.add(Dense(100, activation='relu')) function is used for the nuerons
model.add(Dense(1, activation='sigmoid'))
optimizer = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(loss='mse', optimizer=optimizer, metrics=['accuracy'])
model.fit(test_data_x, test_data_y, epochs=150, batch_size=8)
model.save('TestNN.h5')
然后我加载并尝试预测:
loaded_model = keras.models.load_model('TestNN.h5')
pred = loaded_model.predict(test_data)
我收到此错误:
ValueError: Exception encountered when calling layer 'sequential' (type Sequential).
Input 0 of layer "dense" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
Call arguments received by layer 'sequential' (type Sequential):
• inputs=tf.Tensor(shape=(None,), dtype=float32)
• training=False
• mask=None
我是 Keras 世界的新手,但我认为这个顺序模型非常简单。我不太确定我做错了什么,非常感谢任何帮助。
PS。我正在使用 Keras v2.12.0
我本来希望为二元分类构建一个简单的顺序模型,但是我无法使用 Keras Predict() 函数。
您的输入形状应该是表示单个样本形状的元组。您的输入数据是什么形状?
假设它是形状为 224x224x3 的 RGB 图像,那么您的代码应该这样更新:
Input(shape=(224,224,3))
批量大小将从拟合函数中解释