【问题标题】:How to use custom embeddings with keras LSTM?如何在 keras LSTM 中使用自定义嵌入?
【发布时间】:2020-05-14 05:45:53
【问题描述】:

我想将预训练的词嵌入与 LSTM 一起使用。

那是我已经有了表格的模型:

embedding_for_word = model[word]

我有以下形式的数据:

1. "word1 word2 word3" label 0
2. "word4 word5 word6 word7" label 1
3. "word8 word9" label 1
...
..
.

我知道对于标准 LSTM(如果时间步长是固定的)我们可以:

model = Sequential()
model.add(LSTM(N, input_shape=(n_timesteps, 1), return_sequences=True))
model.add(TimeDistributed(Dense(1, activation='sigmoid')))
model.compile(loss='binary_crossentropy', optimizer='adam')

如何顺序输入表单:

batch_1[embedding_word1,
embedding_word2,embedding_word3 .. some_end_of_sequence_character] --> label 0
batch_2[embedding_word4,
embedding_word5,embedding_word,embedding_word7,some_end_of_sequence_character] --> label 1
...
..
.

对于上面的示例,我如何设计数据并创建模型(对于模型,我只是询问输入层的外观)?

假设:

size_of_embeddings = K batch_size = B

【问题讨论】:

    标签: python keras nlp lstm recurrent-neural-network


    【解决方案1】:

    您应该有一些东西可以将缺失的单词填充到序列的长度。 可能“句子结尾”嵌入会很有趣。

    所以,你需要:

    • 一个具有形状的数组:(total_sentences, words, embedding_size) - 这是X,输入
    • 一个具有形状的数组:(total_sentences, total_labels) - 这是Y,输出

    然后您可以使用fit(X, Y),也许可以使用火车测试拆分。

    模型的第一层可以是LSTM(input_shape=(words, embedding_size), ...)

    【讨论】:

    • keras staticdynamic (或上下文化)学习的嵌入?
    • @A.B,我不明白你的问题。嵌入是可训练的权重。你选择是否训练他们。上下文与框架没有任何关系,但与数据以及您如何训练模型有关。
    • 谢谢您的回复。我对“静态”与“动态”嵌入方法感到困惑。例如,在嵌入层后跟 lstm 进行下一个单词预测,它会被称为动态嵌入方法,因为 lstm 确实强制这些“可训练的权重”具有上下文,对吗?
    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2021-10-25
    相关资源
    最近更新 更多