【问题标题】:dimension of the input layer for embeddings in KerasKeras 中嵌入的输入层维度
【发布时间】:2020-09-07 17:50:24
【问题描述】:

我不清楚在以下示例中指定输入维度Input(shape=(20,)) 或不指定Input(shape=(None,)) 是否有任何区别:

input_layer = Input(shape=(None,)) 
emb = Embedding(86, 300) (input_layer) 
lstm = Bidirectional(LSTM(300)) (emb) 
output_layer = Dense(10, activation="softmax") (lstm)   
model = Model(input_layer, output_layer)  
model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["acc"])  
history = model.fit(my_x, my_y, epochs=1, batch_size=632, validation_split=0.1)  

my_x (shape: 2000, 20) 包含引用字符的整数,而my_y 包含一些标签的 one-hot 编码。使用Input(shape=(None,)),我发现我可以使用model.predict(my_x[:, 0:10]),即我只能输入10 个字符而不是20 个字符:这怎么可能?我假设需要my_x 中的所有 20 个维度来预测相应的 y。

【问题讨论】:

    标签: keras embedding


    【解决方案1】:

    您对None 所说的是,您输入模型的序列具有严格的 20 长度。虽然模型通常需要固定长度的循环神经网络(如您在其中使用的 LSTM),但不要需要一个固定的序列长度。所以 LSTM 并不关心你的序列是否包含 20 或 100 个时间步,因为它只是循环它们。但是,当您将时间步数指定为 20 时,LSTM 预计为 20,如果没有得到它们,则会引发错误。

    有关更多信息,请参阅 Tim♦ 的 post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 2019-05-17
      • 2017-03-31
      • 2019-01-21
      • 2020-03-09
      • 1970-01-01
      • 2019-11-13
      相关资源
      最近更新 更多