我无法训练 CNN 模型,如何训练 CNN 模型
原始数据集形状为 (343889, 80),最后一列为标签。训练和测试集分割完成
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=10)
- shape of - 训练数据集 (240722, 80)
- shape of - 训练标签 (240722,)
- shape of - 测试数据集 (103167, 80)
- shape of - 测试标签 (103167,)
给出模型下面
inputShape = (240722,80)
# Now Working currently
model = Sequential()
#model.add(Flatten())
model.add(Input(shape=inputShape))
#model.add(Dense(1, activation='relu'))
model.add(Conv1D(filters=20, kernel_size=10, activation='relu'))
#model.add(MaxPooling1D(pool_size=79))
#model.add(Flatten())
model.add(Dense(9))
model.compile(optimizer='adam', loss='mse')
的模型摘要是
Model: "sequential_31"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_32 (Conv1D) (None, 240713, 20) 16020
dense_21 (Dense) (None, 240713, 9) 189
=================================================================
Total params: 16,209
Trainable params: 16,209
Non-trainable params: 0
_________________________________________________________________
当我们运行 model.fit() 命令时,它会给出以下错误。
model.fit(X_train, y_train, epochs=5, verbose=0)
收到的错误是
ValueError Traceback (most recent call last)
<ipython-input-132-cfd36b37e182> in <module>()
----> 1 model.fit(X_train, y_train, epochs=5, verbose=0)
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 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_31" is incompatible with the layer: expected shape=(None, 240722, 80), found shape=(None, 80)
Original dataset shape is (343889, 80) and last column is of Labels. The training and testing set split is done
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=10)
- shape of - training dataset (240722, 80)
- shape of - training Labels (240722,)
- shape of - testing dataset (103167, 80)
- shape of - testing Labels (103167,)
The model is given below
inputShape = (240722,80)
# Now Working currently
model = Sequential()
#model.add(Flatten())
model.add(Input(shape=inputShape))
#model.add(Dense(1, activation='relu'))
model.add(Conv1D(filters=20, kernel_size=10, activation='relu'))
#model.add(MaxPooling1D(pool_size=79))
#model.add(Flatten())
model.add(Dense(9))
model.compile(optimizer='adam', loss='mse')
The Summary of the model is
Model: "sequential_31"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_32 (Conv1D) (None, 240713, 20) 16020
dense_21 (Dense) (None, 240713, 9) 189
=================================================================
Total params: 16,209
Trainable params: 16,209
Non-trainable params: 0
_________________________________________________________________
When we run the model.fit() command it gives the following error.
model.fit(X_train, y_train, epochs=5, verbose=0)
Error received is
ValueError Traceback (most recent call last)
<ipython-input-132-cfd36b37e182> in <module>()
----> 1 model.fit(X_train, y_train, epochs=5, verbose=0)
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 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_31" is incompatible with the layer: expected shape=(None, 240722, 80), found shape=(None, 80)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Keras 预计您的模型将收到一批张量:
shape=(None, 240722, 80)
。由于您将输入形状设置为inputShape = (240722,80)
。None
标记表示未知大小的维度,通常是批量大小。 Keras 模型中的形状是关于数据的各个点,而不是批量数据。将输入形状更改为:
inputShape = (80,)
Keras expects that your model will recieve a batch of tensors with:
shape=(None, 240722, 80)
. Since you set the input shape toinputShape = (240722,80)
.The
None
marker denotes a dimension of unknown size, often the batch size. Shapes in Keras models are about the individual points of data and not the batched data.Change your input shape to:
inputShape = (80,)
model.add(Input(shape=shape))
的形状应为 (80,)。The shape of
model.add(Input(shape=shape))
should be of (80,).