我试图了解如何在Keras上编写Conv1D模型,但我一直遇到维数不匹配错误。现在x_train = [1000,294](1000个项目和294个特征)和y_train = [1000,9](1000个项目和9个标签)。我不断收到预期的错误消息3维,但有2维。但是我尝试修复它,它再次出现。 github上的一些问题建议使用Flatten()
,但是它已经存在并且没有运气。知道我缺少什么吗?谢谢
我得到的错误
代码包含在下面
tb = [keras.callbacks.TensorBoard(log_dir='./dllogs')]
main_input = Input(shape=(294, ), dtype='float32')
x_train = np.array(x_train)
y_train = np.array(y_train)
x_dev = np.array(x_dev)
y_dev = np.array(y_dev)
x = Conv1D(128, 5, activation='relu')(main_input)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(35)(x)
x = Flatten()(x)
x = Dropout(0.25)(x)
x = Dense(128, activation='relu')(x)
preds = Dense(pred_dim, activation='softmax')(x)
model = Model(inputs=main_input, outputs=preds)
model.compile(optimizer='adam', loss='kullback_leibler_divergence', metrics=['accuracy'])
print(model.summary())
history_NN = model.fit(x_train, y_train, batch_size=BATCHSIZE, epochs=EPOCHS, callbacks=tb, validation_data=(x_dev, y_dev))
答案
结果代码
tb = [keras.callbacks.TensorBoard(log_dir='./dllogs')]
main_input = Input(shape=(294, 1))#changed
x_train = np.array(x_train)
y_train = np.array(y_train)
x_dev = np.array(x_dev)
y_dev = np.array(y_dev)
x_train = np.expand_dims(x_train, axis=-1) #changed
x_dev = np.expand_dims(x_dev, axis=-1) #changed
x = Conv1D(128, 5, activation='relu')(main_input)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(11)(x)
x = Flatten()(x)
x = Dropout(0.25)(x)
x = Dense(128, activation='relu')(x)
preds = Dense(pred_dim, activation='softmax')(x)
model = Model(inputs=main_input, outputs=preds)
model.compile(optimizer='adam', loss='kullback_leibler_divergence', metrics=['accuracy'])
print(model.summary())
history_NN = model.fit(x_train, y_train, batch_size=BATCHSIZE, epochs=EPOCHS, callbacks=tb, validation_data=(x_dev, y_dev))
答案 0 :(得分:1)
Conv1D
层需要形状为(sequence_length, num_features)
的序列输入。看来您的序列长度为294,具有 one 功能;因此,每个输入样本都需要具有(294,1)
的形状(而不是(294,)
)。要解决此问题,您可以使用np.expand_dims
向输入数据添加大小为1的第三维:
x_train = np.expand_dims(x_train, axis=-1)
x_dev = np.expand_dims(x_dev, axis=-1)
main_input = Input(shape=(294, 1)) # fix the input shape here as well