Keras - LSTM,每个时间步长嵌入2个字

问题描述 投票:0回答:1

下面的代码构造了一个LSTM模型。我想改变这个确切的模型,在开始时有一个嵌入层,在每个时间步骤接收2个不同的单词,嵌入它们(使用相同的嵌入层):它连接它们的嵌入,然后跟随我的模型的其余部分。

k_model = Sequential()

k_model.add(LSTM(int(document_max_num_words*1.5), input_shape=(document_max_num_words, num_features)))
k_model.add(Dropout(0.3))
k_model.add(Dense(num_categories))
k_model.add(Activation('sigmoid'))

k_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
python keras concatenation lstm embedding
1个回答
0
投票

如果我正确理解你的问题,假设输入数据的形状为(n_samples, n_timesteps, 2)(即每步两个单词),你可以使用TimeDistributed包装器实现你想要的:

from keras import layers
from keras import models

n_vocab = 1000
n_timesteps = 500
embed_dim = 128
words_per_step = 2

model = models.Sequential()
model.add(layers.TimeDistributed(layers.Embedding(n_vocab, embed_dim), input_shape=(n_timesteps, words_per_step)))
model.add(layers.TimeDistributed(layers.Flatten()))
# the rest of the model

model.summary()

型号摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
time_distributed_12 (TimeDis (None, 500, 2, 128)       128000    
_________________________________________________________________
time_distributed_13 (TimeDis (None, 500, 256)          0         
=================================================================
Total params: 128,000
Trainable params: 128,000
Non-trainable params: 0
_________________________________________________________________
© www.soinside.com 2019 - 2024. All rights reserved.