我正在尝试在Keras中实现一个卷积自编码器,其中的层类似于下面这样。我的数据有1108行和29430列。
def build(features, embedding_dims, maxlen, filters, kernel_size): m = keras.models.Sequential() m.add(Embedding(features, embedding_dims, input_length=maxlen)) m.add(Dropout(0.2)) m.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1, input_shape=(len(xx), features) )) m.add(MaxPooling1D()) m.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1, input_shape=(None, len(xx), features) )) m.add(UpSampling1D()) m.summary() m.compile(optimizer="adagrad", loss='mse', metrics=['accuracy']) return mearly = keras.callbacks.EarlyStopping( monitor='val_loss', patience=10, verbose=1, mode='min')model = build(len(xx[0]), 60, 11900, 70, 3)model.fit(xx, xx, batch_size=4000, nb_epoch=10000,validation_split=0.1, callbacks=[early])
然而,我收到一个错误,显示ValueError: Error when checking input: expected embedding_1_input to have shape (None, 11900) but got array with shape (1108, 29430)
。为什么第一层会期望形状为(None, maxlen),而不是数据的实际大小呢?
我还会展示我的模型摘要:
_________________________________________________________________Layer (type) Output Shape Param # =================================================================embedding_1 (Embedding) (None, 11900, 60) 714000 _________________________________________________________________dropout_1 (Dropout) (None, 11900, 60) 0 _________________________________________________________________conv1d_1 (Conv1D) (None, 11898, 70) 12670 _________________________________________________________________max_pooling1d_1 (MaxPooling1 (None, 5949, 70) 0 _________________________________________________________________conv1d_2 (Conv1D) (None, 5947, 70) 14770 _________________________________________________________________up_sampling1d_1 (UpSampling1 (None, 11894, 70) 0 =================================================================Total params: 741,440Trainable params: 741,440Non-trainable params: 0_________________________________________________________________
回答:
我通过在Embedding层中添加input_shape
字段解决了这个特定错误,如下所示:
m.add(Embedding(features, embedding_dims, input_length=maxlen, input_shape=(features, ) ))
features
是特征的数量(29430)。