【发布时间】:2020-02-03 03:37:03
【问题描述】:
我正在尝试为此架构编写代码(问答模型:论文https://www.hindawi.com/journals/cin/2019/9543490/)并寻求帮助,如何从堆叠的 BiLSTM 层中获取隐藏状态矩阵 Hq 和 Ha。有人可以请教。
# Creating Embedding Layer for Query
# Considered fixed length as 40 for both question and answer as per research paper
embedding_layer1 = layers.Embedding(vocab_size_query, 300, weights=[embedding_matrix_query], input_length =40, trainable=False)
input_text1 =Input(shape=(40,), name="input_text")
x = embedding_layer1(input_text1)
# Creating Bidirectional layer for Query
# Each word in the context and question should be made aware of the nearby words occurring. We use a bi-directional recurrent neural network (LSTM’s) here.
x = Bidirectional(LSTM(128,recurrent_dropout=0.5,kernel_regularizer=regularizers.l2(0.001),return_sequences=True))(x)
x = Bidirectional(LSTM(128,recurrent_dropout=0.5,kernel_regularizer=regularizers.l2(0.001),return_sequences=True))(x)
flatten_1 = Flatten()(x)
## Creating Embedding Layer for Passage
embedding_layer2 = layers.Embedding(vocab_size_answer, 300, weights=[embedding_matrix_answer], input_length =40, trainable=False)
input_text2 =Input(shape=(40,), name="input_text")
x2 = embedding_layer2(input_text2)
# Creating Bidirectional layer for Passage
x2 = Bidirectional(LSTM(128,recurrent_dropout=0.5,kernel_regularizer=regularizers.l2(0.001),return_sequences=True))(x2)
x2 = Bidirectional(LSTM(128,recurrent_dropout=0.5,kernel_regularizer=regularizers.l2(0.001),return_sequences=True))(x2)
flatten_2 = Flatten()(x2)
【问题讨论】:
-
谢谢@Minh-TuanNguyen,但我的问题不同。如何从双向 LSTM 层获取隐藏状态矩阵。如果我们在上述架构的左边部分(问题序列)得到这个,我可以将相同的应用于右边部分(答案序列)。
-
如果我认为正确那么你想要得到的输出是 flatten 1 和 flatten 2,因为这些是堆叠的 BDLSTM 的输出,对吗?
-
@Minh-TuanNguyen 是的,我只是想了解我从 flatten_1 和 flatten_2 获得的输出 - 它们是模型架构所期望的隐藏状态矩阵 Hq 和 Ha。 flatten_1 的形状:
<tf.Tensor 'bidirectional_1/Identity:0' shape=(None, 40, 256) dtype=float32>flatten_2 的形状:<tf.Tensor 'bidirectional_3/Identity:0' shape=(None, 40, 256) dtype=float32> -
谢谢@Minh-TuanNguyen。那是对的。这些是我一直在寻找的矩阵。
标签: tensorflow keras nlp lstm question-answering