如何为 POS 标签生成 GloVe 嵌入? Python

How to generate GloVe embeddings for POS tags? Python

对于句子分析任务,我想获取与句子相关的 POS 标签序列并将其提供给我的模型,就好像 POS 标签是单词一样。

我正在使用 GloVe 来表示句子中的每个单词,并使用 SpaCy 来生成 POS 标签。然而,GloVe 嵌入对于 POS 标签没有多大意义。所以我将不得不以某种方式为每个 POS 标签创建嵌入。为 POS 标签创建嵌入的最佳方法是什么,以便我可以像输入句子一样将 POS 序列输入到我的模型中?谁能在 Python 中指出如何使用 GloVe 执行此操作的代码示例?

已添加上下文

我的任务是根据句子对的相似性(相似含义与不同含义)对句子对进行二元分类。

我想用词性标签作为词,这样词性标签就可以作为一个额外的信息来比较句子。我当前的模型不使用 LSTM 作为预测序列的方法。

大多数词嵌入模型仍然依赖于一个潜在的假设,即一个词的含义是由它的使用上下文引起的。例如,使用 skipgram 或连续词袋公式学习 word2vec 嵌入隐含地假设一个模型,其中单词的表示向量基于与目标词同时出现的上下文词,特别是通过学习创建最适合的嵌入解决从随机词对(所谓的负采样)中区分上下文共现的词对的分类任务。

但如果输入更改为一系列离散标签(POS 标签),则此假设似乎不需要保持准确或合理。词性标签有一个指定的含义,它并不是由被其他词性标签包围的上下文所真正诱导的,所以当将 POS 标签当作它们来处理时,用于生成词嵌入的标准学习任务不太可能起作用来自更小词汇量的单词。

在你的情况下,整体的句子分析任务是什么?

在使用手头的学习任务更新问题后添加。

假设您可以为每个句子示例创建 POS 输入向量。如果可能有 N 个不同的 POS 标签,则意味着您的输入将由一个来自词嵌入的向量和另一个长度为 N 的向量组成,其中组件 i 中的值表示输入句子中具有 POS 的术语数标签 P_i.

例如,假设唯一可能的 POS 标签是 'article'、'noun' 和 'verb',并且您有一个句子 ['article'、'noun'、'verb'、'noun']。然后这会转换为 [1, 2, 1],并且您可能希望通过句子的长度对其进行归一化。我们将第 1 个句子的输入称为 pos1,第 2 个句子的输入称为 pos2

我们将句子 1 的词嵌入向量输入称为 sentence1sentence1 将通过从单独的源(如预训练的 word2vec 模型或 fastText 或 GloVe)中查找每个词嵌入来计算,并将它们相加(使用连续的词袋)。 sentence2.

也一样

假设您的成批训练数据已经被处理成这些向量格式,因此给定的单个输入将是一个四元组向量:第 1 句查找的 CBOW 嵌入向量,第 2 句相同, 以及句子 1 的 POS 标签的计算离散表示向量, 和句子 2 相同.

可以使用此数据的模型可能是这样的:

from keras.engine.topology import Input
from keras.layers import Concatenate
from keras.layers.core import Activation, Dense
from keras.models import Model


sentence1 = Input(shape=word_embedding_shape)
sentence2 = Input(shape=word_embedding_shape)
pos1 = Input(shape=pos_vector_shape)
pos2 = Input(shape=pos_vector_shape)

# Note: just choosing 128 as an embedding space dimension or intermediate
# layer size... in your real case, you'd choose these shape params
# based on what you want to model or experiment with. They don't mean
# anything here.

sentence1_branch = Dense(128)(sentence1)
sentence1_branch = Activation('relu')(sentence1_branch)
# ... do whatever other sentence1-only stuff

sentence2_branch = Dense(128)(sentence2)
sentence2_branch = Activation('relu')(sentence2_branch)
# ... do whatever other sentence2-only stuff

pos1_embedding = Dense(128)(pos1)
pos1_branch = Activation('relu')(pos1_embedding)
# ... do whatever other pos1-only stuff

pos2_embedding = Dense(128)(pos2)
pos2_branch = Activation('relu')(pos2_embedding)
# ... do whatever other pos2-only stuff


unified = Concatenate([sentence1_branch, sentence2_branch,
                       pos1_branch, pos2_branch])
# ... do dense layers, whatever, to the concatenated intermediate 
# representations

# finally boil it down to whatever final prediction task you are using, 
# whether it is predicting a sentence similarity score (Dense(1)), 
# or predicting a binary label that indicates whether the sentence 
# pairs are similar or not (Dense(2) then followed by softmax activation, 
# or Dense(1) followed by some type of probability activation like sigmoid).

# Assume your data is binary labeled for similar sentences...
unified = Activation('softmax')(Dense(2)(unified))
unified.compile(loss='binary_crossentropy', other parameters)

# Do training to learn the weights...


# A separate model that will just produce the embedding output
# from a POS input vector, relying on weights learned from the
# training process.
pos_embedding_model = Model(inputs=[pos1], outputs=[pos1_embedding])