由于数据形状导致的卷积神经网络中的值误差

Value error in convolutional neural network due to data shape

我正在尝试使用 CNN 预测时间序列数据中的数字峰值,但一直出现数据形状错误。我的数据如下所示:

由于时间序列的长度不一,我补了X。X_train和X_test,从numpy数组转tensor后的形状为:

我是keras的新手,我对第一个Conv1D层中的input_size非常不确定。根据这个 post () 我选择它作为 (2297, 1)(520, 1),但其中 none 有效。 Keras 的文档说输入形状应该是 (batch_size, feature_size, channels),但是省略了 batch_size

from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import Adam

#for structure of X and y, see explanation above

X_padded = tf.keras.preprocessing.sequence.pad_sequences(X)

X_train, X_test, y_train, y_test = train_test_split(X_padded, y, test_size=0.25, random_state=33)


X_train = tf.convert_to_tensor(X_train)
X_test = tf.convert_to_tensor(X_test)
y_train = tf.convert_to_tensor(y_train)
y_test = tf.convert_to_tensor(y_test)

model = keras.Sequential()
model.add(Conv1D(filters=16, kernel_size=3, activation = 'relu', strides = 1, padding = 'same', input_shape=(2297, 1)))
model.add(Dropout(0.1))
model.add(Conv1D(filters=32, kernel_size=3, activation = 'relu', strides = 1, padding = 'same'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(9, activation='softmax')) # '9' because there are 9 possible peak counts in the data

model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])

progress = model.fit(X_train, y_train, epochs = 15, validation_data = (X_test, y_test), verbose=1)

错误:

 ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 2297]

这可能是什么问题?

我能够解决它。在用户 'rnso'.

的回答中 给出了正确的输入形状

我将 X_train 和 X_test(numpy.arrays)塑造成

X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)

并在 Conv1D 语句中将 input_shape 声明为 input_shape=(ncols, 1)

input_shape=(2297, 1)