如何处理Tensorflow模型.predict()值错误



我的代码中出现以下错误

WARNING:tensorflow:Model was constructed with shape (None, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (None,).

这是我的代码

import numpy as np
import tensorflow as tf
inum = np.array([[1,1,2],[2,25,6],[32,4,7],[8,9,0]], dtype="float")
onum = np.array([3,56,135,72],dtype="float")
l0 = tf.keras.layers.Dense(units=4, input_shape=(3,))
l1 = tf.keras.layers.Dense(units=4)
l2 = tf.keras.layers.Dense(units=4)
l3 = tf.keras.layers.Dense(units=1)
model = tf.keras.Sequential([l0,l1,l2,l3])
model.compile(loss="mean_squared_error",optimizer=tf.keras.optimizers.Adam(0.1))
history = model.fit(inum,onum,epochs=1200,verbose=False)
model.predict([2,2,4])

我是机器学习的新手,现在已经知道该怎么做了。

非常感谢您的帮助。

使用

Model.predict([[2,2,4]])

因为keras模型将输入视为一批数据。所以,即使你只想输入一个形状为[3]的数据,你也应该像我一样把它包装成[1,3]的数据

最新更新