如何设置一维CNN的形状

时间:2019-05-02 18:41:06

标签: python keras conv-neural-network

我看过多个文章,阐述了如何为1D CNN设置形状。他们大多数都像这样的帖子: Dimension of shape in conv1D

我做了这篇文章中的一些回答,但仍然不知道我要去哪里。下面是我的代码和追溯。

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train, labels, test_size=0.20, random_state=101)

train_r =np.expand_dims(X_train, axis=2)

train_r.shape
(36513, 43, 1)

conv_model = models.Sequential()
conv_model.add(layers.Conv1D(32, (3), activation='relu' , input_shape=(36513,43,1)))
conv_model.add(layers.Flatten())
conv_model.add(layers.Conv1D(16, (3), activation='relu'))
conv_model.add(layers.Flatten())
conv_model.add(layers.Dense(64, activation='relu'))
conv_model.add(layers.Dense(1, activation='sigmoid'))
conv_model.compile(loss='binary_crossentropy', optimizer= "adam", metrics=[f1])
callbacks = [EarlyStopping(monitor='val_f1', patience=10), PlotLearning()]
conv_model.fit(train_r, y_train, validation_split = 1/12
              ,epochs = num_epochs, batch_size = 1, callbacks = callbacks)

跟踪状态:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-e607bddce9ea> in <module>
      1 conv_model = models.Sequential()
----> 2 conv_model.add(layers.Conv1D(32, (3), activation='relu' , input_shape=(36513,43,1)))
      3 conv_model.add(layers.Flatten())
      4 conv_model.add(layers.Conv1D(16, (3), activation='relu'))
      5 conv_model.add(layers.Flatten())

C:\ProgramData\Anaconda3\envs\tensorflowenvironment\lib\site-packages\keras\engine\sequential.py in add(self, layer)
    163                     # and create the node connecting the current layer
    164                     # to the input layer we just created.
--> 165                     layer(x)
    166                     set_inputs = True
    167             else:

C:\ProgramData\Anaconda3\envs\tensorflowenvironment\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
    412                 # Raise exceptions in case the input is not compatible
    413                 # with the input_spec specified in the layer constructor.
--> 414                 self.assert_input_compatibility(inputs)
    415 
    416                 # Collect input shapes to build layer.

C:\ProgramData\Anaconda3\envs\tensorflowenvironment\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)
    309                                      self.name + ': expected ndim=' +
    310                                      str(spec.ndim) + ', found ndim=' +
--> 311                                      str(K.ndim(x)))
    312             if spec.max_ndim is not None:
    313                 ndim = K.ndim(x)

ValueError: Input 0 is incompatible with layer conv1d_28: expected ndim=3, found ndim=4

1 个答案:

答案 0 :(得分:1)

进行以下更改后,一切都会正常运行

在input_shape中不要提及批次尺寸。

conv_model.add(layers.Conv1D(32, (3), activation='relu' , input_shape=(43,1)))

删除Conv1D层之间的Flatten()层,仅在Dense层之前需要。

conv_model.add(layers.Flatten())