我正在使用 CNN 解决分类问题。我有 data.csv 文件(15000 个样本/行和 271 列),其中第一列是类标签(总共 4 个类),其他 270 列是特征(6 个长度为 45 的不同信号连接在一起,即 6X45=270)。
问题:我想提供长度为 270 的单个样本作为向量(6 X 45,所有 6 个信号都有不同的含义)但是当在卷积中将单个样本 reshape 为(6 行,45 列)时我在维度上遇到错误。
我的 CNN 模型:
X, y = load_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
num_classes = 4
X_train = X_train.reshape(X_train.shape[0], 6, 45).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 6, 45).astype('float32')
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=5, input_shape=(6, 45)))
model.add(MaxPooling1D(pool_size=5 ))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
如何 reshape 我的数据,即 CNN 将每个样本视为长度为 45 的 6 个信号并与窗口 5 的核卷积。
你需要像这样 reshape 你的数据
X_train.reshape(num_of_examples, num_of_features, num_of_signals)
并将模型中的 input_shape
更改为 (45, 6)
。请参阅下面的示例代码,
X = np.random.randn(4000,270)
y = np.ones((4000,1))
y[0:999] = 2
y[1000:1999] = 3
y[2000:2999] = 0
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
num_classes = 4
X_train = X_train.reshape(X_train.shape[0], 45, 6).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 45, 6).astype('float32')
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=5, input_shape=(45, 6)))
model.add(MaxPooling1D(pool_size=5 ))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
我是一名优秀的程序员,十分优秀!