当我尝试适合我的模型时CNN ValueError

时间:2018-01-30 08:41:41

标签: python machine-learning

这是我的代码:

from keras import optimizers from keras.layers import Convolution1D, Dense, MaxPooling1D, Flatten

model = Sequential() 

# my CNN layers
model.add(Conv1D(101, 101, strides=1, padding='same', dilation_rate=1, input_shape=(None, 120)))
model.add(Activation('relu')) model.add(MaxPooling1D(pool_size=2, padding='same', strides=None))
model.add(Dense(2048)) model.add(Activation('relu'))
model.add(Dense(100)) model.add(Activation('sigmoid'))

model.compile(optimizer=optimizers.Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])
model.fit(training_trainX_train, training_trainY_train, epochs=2, batch_size=100, verbose=1)

但是我收到了这个错误:ValueError: Error when checking model input: expected conv1d_8_input to have 3 dimensions, but got array with shape (27660, 120)

这是我训练集的形状:
training_trainX_train.shape = (27660, 120)
training_trainY_train.shape = (27660, 101)

1 个答案:

答案 0 :(得分:0)

添加model.add(Flatten())将解决此问题

model.add(Conv1D(101, 101, strides=1, padding='same', dilation_rate=1,       input_shape=(None, 120)))
model.add(Activation('relu')) model.add(MaxPooling1D(pool_size=2, padding='same', strides=None))
model.add(Flatten())
model.add(Dense(2048)) model.add(Activation('relu'))
model.add(Dense(100)) model.add(Activation('sigmoid'))

有关详情,请查看https://github.com/keras-team/keras/issues/6351