【问题标题】:Keras -- Input Shape for Embedding LayerKeras——嵌入层的输入形状
【发布时间】:2017-07-14 20:28:25
【问题描述】:

我正在尝试在 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 m

early = 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,440
Trainable params: 741,440
Non-trainable params: 0
_________________________________________________________________

【问题讨论】:

    标签: python machine-learning neural-network keras


    【解决方案1】:

    我通过向嵌入层添加input_shape 字段来修复此特定错误,如下所示:

    m.add(Embedding(features, embedding_dims, input_length=maxlen, input_shape=(features, ) ))
    

    features 是特征数 (29430)。

    【讨论】:

    • 新型号总结是什么?
    【解决方案2】:

    您对嵌入层的输入必须是一维的,因此您需要将数据重新整形为这种格式 (,n)。无论您传递给 input_length 什么都需要匹配 n 大小。

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 2021-05-20
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      相关资源
      最近更新 更多