python-3.x - 如何为 POS 标签生成 GloVe 嵌入? Python

标签 python-3.x machine-learning nlp spacy word-embedding

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

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

添加了上下文

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

我想使用 POS 标签作为单词,以便 POS 标签作为额外的信息来比较句子。我当前的模型不使用 LSTM 作为预测序列的方法。

最佳答案

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

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

您的情况的整体句子分析任务是什么?

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

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

例如,假设唯一可能的 POS 标签是“文章”、“名词”和“动词”,并且您有一个带有 [“文章”、“名词”、“动词”、“名词”] 的句子。然后它会转换为 [1, 2, 1],并且您可能想根据句子的长度对其进行标准化。我们将第 1 句的输入称为 pos1,将第 2 句的输入称为 pos2

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

假设您的批量训练数据已被处理为这些向量格式,因此给定的单个输入将是向量的 4 元组:句子 1 的查找 CBOW 嵌入向量,句子 2 的查找 CBOW 嵌入向量,以及计算出句子 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])

关于python-3.x - 如何为 POS 标签生成 GloVe 嵌入? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804717/

相关文章:

python - OpenCV 无法正确写入输出文件 (Python)

python - 网格数据插值

python - 在 Tensorflow 中获取未知张量的形状

python - roget python脚本未知错误

django - 在 Django 中,如何防止 "Save with update_fields did not affect any rows."错误?

python - base64编码是否对输入进行哈希处理?

Python:为什么我的线性回归图给出了许多凌乱的彩色线?

machine-learning - 用于向量到字符序列转换的 LSTM

tensorflow - BERT - 池化输出与序列输出的第一个向量不同

python - 如何让 POS n-gram 更有效?