Keras中嵌入件的输入层尺寸
我不清楚在以下示例中指定输入维度Keras中嵌入件的输入层尺寸,keras,embedding,Keras,Embedding,我不清楚在以下示例中指定输入维度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) m
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
包含一些标签的一个热编码。有了Input(shape=(None,)
,我发现我可以使用model.predict(my_x[:,0:10])
,也就是说,我只能输入10个字符,而不是20个:这怎么可能?我假设在my_x
中的所有20个维度都需要预测相应的y。你用None
说的是,你输入到模型中的序列的严格长度是20。虽然一个模型通常需要一个固定的长度,但递归神经网络(正如你在那里使用的LSTM)不需要一个固定的序列长度。因此,LSTM并不关心序列是包含20个时间步还是100个时间步,因为它只是在它们上面循环。但是,当您将时间步长的数量指定为20时,LSTM预期为20,如果没有得到它们,则会引发错误
有关更多信息,请参阅Tim的这篇文章♦