python - 尝试预测简单的二维输入时,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() 的输入是一个非常好的二维数组。
解决方案
推荐阅读
- python - Selenium 中的 Headless Chrome 默认不启用 Cookie
- reactjs - 是的,Formik :类型实例化太深并且可能是无限的。TS2589
- javascript - 调用单击事件时更改变量内部值的可能方法?
- javascript - 如何确保在隐藏启动屏幕之前加载主屏幕上的所有资产 React Native
- python - 在 Plotly 图中重新排序轴
- c++ - 有没有简单的方法来检查一个值是否不能被另一种类型安全地表示?
- python - Pandas/SQL 共现计数
- javascript - 如何将 OnEdit 脚本与具有非静态位置的单元格一起使用(Google 表格)?
- google-earth-engine - 使用减速器时如何忽略 GEE 中的空值?
- android - 启用App修改Android系统的Dark/Night Mode设置