首页 > 解决方案 > 尝试预测简单的二维输入时,Tensorflow 返回错误

问题描述

我正在运行以下代码,试图创建一个模型来预测 [a,b] --> [x,y]

model = tf.keras.Sequential([keras.layers.Dense(units=2, input_shape=[2])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([[-1.0,1],[2,2]], dtype=float)
ys = np.array([[-2.0,2],[5,6]], dtype=float)
model.fit(xs, ys, epochs=1000)
res = model.predict([[2,2]])

我收到以下错误:

Traceback (most recent call last):
  File "app.py", line 9, in <module>
    res = model.predict([[2,2]])
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1096, in predict
    x, check_steps=True, steps_name='steps', steps=steps)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 2382, in _standardize_user_data
    exception_prefix='input')
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_utils.py", line 362, in standardize_input_data
    ' but got array with shape ' + str(data_shape))
ValueError: Error when checking input: expected dense_input to have shape (2,) but got array with shape (1,)

不确定我在这里做错了什么,因为 predict() 的输入是一个非常好的二维数组。

标签: pythontensorflow

解决方案


推荐阅读