Sentence2Vec理解

绝地灬酷狼 2022-03-10 03:30 244阅读 0赞

论文原文:A simple but tough-to-beat baseline for sentence embedding

在这里插入图片描述

算法介绍

  • 先对一个句子 v s v_s vs中所有词的词向量进行加权平均,其中每个词向量的权重可以表示为 a a + p ( w i ) \frac{a}{a+p(w_i)} a+p(wi)a,其中a为超参数,p(w)为词w的频率, ∣ s ∣ |s| ∣s∣为句长, v w i v_{w_i} vwi为词向量。
       v s = 1 ∣ s ∣ ∑ i ∣ s ∣ a a + p ( w i ) v w i v_s = \frac{1}{|s| }\sum_{i}^{|s|} \frac{a}{a+p(w_i)} v_{w_i} vs=∣s∣1∑i∣s∣a+p(wi)avwi
  • 使用PCA/SVD对向量值进行修改。

    • 去除公共部分(也就是每个成分在主成分上的投影为 u T V s u^TV_s uTVs, 再乘以主成分u,得到 u T V s u u^TV_su uTVsu即为common部分,再用 V s V_s Vs减去公共部分得到最终的句向量),注意u是单位向量

注:pca算法推导和基础知识

代码

  1. import numpy as np
  2. from typing import Iterable, List
  3. from gensim.models.keyedvectors import BaseKeyedVectors
  4. from sklearn.decomposition import PCA
  5. Sentence = List[str]
  6. def word_vec(wv: BaseKeyedVectors, s: str):
  7. try:
  8. return wv.get_vector(s)
  9. except KeyError:
  10. return np.zeros(wv.vector_size)
  11. class SentenceVec:
  12. wv: BaseKeyedVectors
  13. u: np.array
  14. a: float
  15. def __init__(self, sentences: Iterable[Sentence], wv: BaseKeyedVectors, a: float = 1e-3):
  16. self.wv = wv
  17. self.a = a
  18. embedding_size = wv.vector_size
  19. sentence_set = []
  20. for sentence in sentences:
  21. vs = self.weighted_average(sentence)
  22. sentence_set.append(vs) # add to our existing re-calculated set of sentences
  23. # calculate PCA of this sentence set
  24. pca = PCA(n_components=embedding_size)
  25. pca.fit(np.array(sentence_set))
  26. u = pca.components_[0] # the PCA vector
  27. u = np.multiply(u, np.transpose(u)) # u x uT
  28. # pad the vector? (occurs if we have less sentences than embeddings_size)
  29. if len(u) < embedding_size:
  30. for i in range(embedding_size - len(u)):
  31. u = np.append(u, 0) # add needed extension for multiplication below
  32. # resulting sentence vectors, vs = vs -u x uT x vs
  33. sentence_vecs = []
  34. for vs in sentence_set:
  35. sub = np.multiply(u, vs)
  36. sentence_vecs.append(np.subtract(vs, sub))
  37. self.u = u
  38. self.vec = sentence_vecs
  39. def feature(self, sentence: Sentence):
  40. vs = self.weighted_average(sentence)
  41. return vs - vs * self.u
  42. def get_word_frequency(self, s) -> float:
  43. vocab = self.wv.vocab.get(s)
  44. return vocab.count / 10000000 if vocab else 0
  45. def weighted_average(self, sentence: Sentence):
  46. dim = self.wv.vector_size
  47. a = self.a
  48. vs = np.zeros(dim) # add all word2vec values into one vector for the sentence
  49. for word in sentence:
  50. a_value = a / (a + self.get_word_frequency(word)) # smooth inverse frequency, SIF
  51. vs = np.add(vs, np.multiply(a_value, word_vec(self.wv, word))) # vs += sif * word_vector
  52. vs = np.divide(vs, len(sentence)) # weighted average
  53. return vs

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读

    相关 word2vec

    Word2Vec 是一种词嵌入模型,用于将文本中的单词映射到一个固定大小的向量空间中。它的主要目的是通过计算单词之间的相似度来增强自然语言处理的性能。Word2Vec 通常用于

    相关 转载 通俗理解word2vec

    独热编码 独热编码即 One-Hot 编码,又称一位有效编码,其方法是使用N位状态寄存器来对N个状态进行编码,每个状态都有它独立的寄存器位,并且在任意时候,其中只有一位有

    相关 转载 通俗理解word2vec

    独热编码 独热编码即 One-Hot 编码,又称一位有效编码,其方法是使用N位状态寄存器来对N个状态进行编码,每个状态都有它独立的寄存器位,并且在任意时候,其中只有一位有

    相关 理解 TensorFlow 之 word2vec

    自然语言处理(英语:Natural Language Processing,简称NLP)是人工智能和语言学领域的分支学科。自然语言生成系统把计算机数据转化为自然语言。自然语言理

    相关 理解word2vec

    自然语言处理任务中要处理的对象是单词或者词组,单词可以看做是类别型特征,虽然tree-based模型可以采用类别特征,但包括神经网络在内的大部分机器学习模型只能处理数值型特征。