调用图层 "sequential_4" 时遇到异常(类型 Sequential)
Exception encountered when calling layer "sequential_4" (type Sequential)
这是创建、编译和拟合单层模型的简单代码
X = tf.cast(tf.constant(X),dtype=tf.float32)
y = tf.cast(tf.constant(y),dtype=tf.float32)
#设置随机种子
tf.random.set_seed(42)
#1.create 使用顺序模型的模型 API
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
#2.Compile 模特
model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),metrics=["mae"])
#拟合模型
model.fit(X,y,epochs = 5)
但最后我收到这个错误。
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, 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 227, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_6" (type Sequential).
Input 0 of layer "dense_7" 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
为什么 Sequential_6 和 dense_7???这是单层。
您忘记了批次维度。您对模型的输入必须具有 (batch_size, features)
的形状。尝试这样的事情:
X = tf.cast(tf.constant([0.5]),dtype=tf.float32)
y = tf.cast(tf.constant([0.6]),dtype=tf.float32)
X = tf.expand_dims(X, axis=0)
y = tf.expand_dims(y, axis=0)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),metrics=["mae"])
model.fit(X,y,epochs = 5)
Sequential_6
是你的模型名称,dense_7
是Dense
层的名称。每次您 运行 您的模型,名称中的数字都会递增。
这是创建、编译和拟合单层模型的简单代码
X = tf.cast(tf.constant(X),dtype=tf.float32)
y = tf.cast(tf.constant(y),dtype=tf.float32)
#设置随机种子
tf.random.set_seed(42)
#1.create 使用顺序模型的模型 API
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
#2.Compile 模特
model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),metrics=["mae"])
#拟合模型
model.fit(X,y,epochs = 5)
但最后我收到这个错误。
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, 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 227, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_6" (type Sequential).
Input 0 of layer "dense_7" 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
为什么 Sequential_6 和 dense_7???这是单层。
您忘记了批次维度。您对模型的输入必须具有 (batch_size, features)
的形状。尝试这样的事情:
X = tf.cast(tf.constant([0.5]),dtype=tf.float32)
y = tf.cast(tf.constant([0.6]),dtype=tf.float32)
X = tf.expand_dims(X, axis=0)
y = tf.expand_dims(y, axis=0)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),metrics=["mae"])
model.fit(X,y,epochs = 5)
Sequential_6
是你的模型名称,dense_7
是Dense
层的名称。每次您 运行 您的模型,名称中的数字都会递增。