张量流Keras拟合value_error
我是张量流新手。我尝试将 X 和 y 拟合为 shape=8 float64 张量 X 作为特征集,y 作为目标集。
X = np.array([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0])
y = np.array([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])
X = tf.constant(X)
y = tf.constant(y)
我的代码适合模型如下:
tf.random.set_seed(42)
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),
metrics=tf.keras.metrics.mae)
model.fit(X, y, epochs=5)
最后出现如下错误:
Epoch 1/5
-------------------------------------------------------------
--------------
ValueError Traceback (most
recent call last)
<ipython-input-62-f2b0f2566f13> in <module>()
12
13
---> 14 model.fit(X, y, epochs=5)
1 frames
/usr/local/lib/python3.7/dist-
packages/tensorflow/python/framework/func_graph.py in
autograph_handler(*args, **kwargs)
1145 except Exception as e: #
pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise
e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-
packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 228, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
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:
• inputs=tf.Tensor(shape=(None,), dtype=float64)
• training=True
• mask=None
我已经将我的 Keras、tensorflow 和 numpy 更新到最新版本。我也尝试过不同的时代。
I am new to tensorflow. i've tried to fit X and y both shape=8 float64 tensors X as feature set and y as target set.
X = np.array([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0])
y = np.array([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])
X = tf.constant(X)
y = tf.constant(y)
my code to fit model as per below:
tf.random.set_seed(42)
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),
metrics=tf.keras.metrics.mae)
model.fit(X, y, epochs=5)
and finally error came up as per below:
Epoch 1/5
-------------------------------------------------------------
--------------
ValueError Traceback (most
recent call last)
<ipython-input-62-f2b0f2566f13> in <module>()
12
13
---> 14 model.fit(X, y, epochs=5)
1 frames
/usr/local/lib/python3.7/dist-
packages/tensorflow/python/framework/func_graph.py in
autograph_handler(*args, **kwargs)
1145 except Exception as e: #
pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise
e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-
packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 228, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
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:
• inputs=tf.Tensor(shape=(None,), dtype=float64)
• training=True
• mask=None
i already updated my Keras, tensorflow and numpy to the latest versions. i tried different epochs too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于输入数据的形状。
工作示例代码
输出
Issue is with shape of the input data.
Working sample code
Output
请将您的输入数据设置为:
输出
Kindly set your input data as:
Output