由于无法训练 CNN 模型,如何训练 CNN 模型

原文标题How can I train a CNN model as I am unable to train the CNN Model

原始数据集形状是 (343889, 80),最后一列是标签。训练集和测试集拆分完成

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=10)
  1. 形状 – 训练数据集 (240722, 80)
  2. 形状 – 训练标签 (240722,)
  3. 形状 – 测试数据集 (103167, 80)
  4. 形状 – 测试标签 (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)

原文链接:https://stackoverflow.com//questions/71514834/how-can-i-train-a-cnn-model-as-i-am-unable-to-train-the-cnn-model

回复

我来回复
  • LittleLulatsch的头像
    LittleLulatsch 评论

    Keras 预计您的模型将收到一批张量:shape=(None, 240722, 80)。由于您将输入形状设置为inputShape = (240722,80)

    None标记表示未知大小的维度,通常是批量大小。 Keras 模型中的形状是关于数据的各个点,而不是批量数据。

    将输入形状更改为:inputShape = (80,)

    1年前 0条评论
  • Loris Pilotto的头像
    Loris Pilotto 评论

    model.add(Input(shape=shape))的形状应该是(80,)。

    1年前 0条评论