1D卷积神经网络与2D数组

huangapple go评论70阅读模式
英文:

1D Convolutional Neural Network with 2D Array

问题

I am experiencing great confusion when setting up 1 channel ECG data input sizes for a 1D CNN.

For simplicity sake, the data is structured like this:

  • I have 500 segments
  • Each segment has a corresponding label, one of three classes 1.0, 2.0 or 3.0
  • In each segment, there are 100 raw ECG samples (1 channel) i.e. [1.53, 1.67, 1.56...]

I have tried to adapt the input-shape from this question so that would mean my (examples, time_steps, features) would be (500, 100, 1) and thus my input shape could be (100,1) or even (None, 1). However when I run the following:


# shape
# x_train shape: (500, 100)
# y_train shape: (500,)

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(100, 1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu')) # Not sure if this line is necssary
model.add(Dense(3, activation='softmax')) # Converge to 3 outputs
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

I get the following error when calling model.fit() :

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 500, 1), found shape=(None, 100)

If I then replace the shape with (None, 500, 1) and re-run I get this error on model.add(Dense(n_features, activation='relu')):

The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)

Any ideas how to resolve this? Is it down to the shape of input? Perhaps I have poorly misinterpreted the docs.

英文:

I am experiencing great confusion when setting up 1 channel ECG data input sizes for a 1D CNN.

For simplicity sake, the data is structured like this:

  • I have 500 segments
  • Each segment has a corresponding label, one of three classes 1.0, 2.0 or 3.0
  • In each segment, there are 100 raw ECG samples (1 channel) i.e. [1.53, 1.67, 1.56...]

I have tried to adapt the input-shape from this question so that would mean my (examples, time_steps, features) would be (500, 100, 1) and thus my input shape could be (100,1) or even (None, 1). However when I run the following:


# shape
# x_train shape: (500, 100)
# y_train shape: (500,)

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(100, 1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu')) # Not sure if this line is necssary
model.add(Dense(3, activation='softmax')) # Converge to 3 outputs
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

I get the following error when calling model.fit() :

    ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 500, 1), found shape=(None, 100)

If I then replace the shape with (None, 500, 1) and re-run I get this error on model.add(Dense(n_features, activation='relu')):

The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)

Any ideas how to resolve this? Is it down to the shape of input? Perhaps I have poorly misinterpreted the docs.

答案1

得分: 1

我认为你需要为批次添加一个维度,实际上你不必指定其大小。所以也许尝试 input_shape = (None, 100)(None, 100, 1)

英文:

I think you need to add a dimension for the batch, which you don't actually have to specify the size of. So maybe try input_shape = (None, 100) or (None, 100, 1)

huangapple
  • 本文由 发表于 2023年4月17日 00:38:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定