Word2Vec的原理是什么,如何用训练Word2Vec

Word2Vec是一种基于神经网络的词向量生成模型,通过训练预测上下文单词或中心单词来生成词向量。它包含两种不同的架构:跳字模型(Skip-gram)和连续词袋模型(Continuous Bag-of-Words, CBOW),它们在训练方式和结果表现上略有不同。

1跳字模型(Skip-gram):

在跳字模型中,模型的目标是根据中心单词预测周围的上下文单词。

例如,在句子"I enjoy playing football with my friends"中,对于中心单词"playing",跳字模型会尝试预测周围的上下文单词"enjoy"、"football"、"with"和"my"。
模型的输入是一个中心词向量,然后通过一个隐藏层来预测周围单词的概率分布。这个隐藏层通常称为投影层或投影矩阵。
使用反向传播算法,通过最大化词汇表中下一个单词的条件概率来更新模型参数,以优化模型的性能。

2连续词袋模型(CBOW):

在连续词袋模型中,模型的目标是根据上下文单词预测中心单词。例如,在句子"I enjoy playing football with my friends"中,对于上下文单词"enjoy"、"playing"、"with"和"friends",连续词袋模型会尝试预测中心单词"football"。
模型的输入是上下文词向量的平均值,然后通过一个隐藏层来预测中心单词。与跳字模型不同,连续词袋模型更关注整体的语义信息而不是局部的顺序关系。
同样,使用反向传播算法来更新模型参数,以优化模型的性能。

3Word2Vec的训练过程通常涉及以下步骤:

收集大规模的语料库,例如新闻文章、维基百科等。
对语料库进行预处理,包括分词、去除停用词、转换为小写等。
构建跳字模型或连续词袋模型,并定义网络结构、参数和超参数。
使用训练数据(上下文单词和中心单词)来训练模型。对于每个训练样本,通过前向传播计算预测值,并使用损失函数度量预测值与实际值之间的误差。
使用反向传播算法更新模型的参数,以减小损失函数的值。这个过程反复进行多个迭代,直到模型收敛或达到预定义的停止条件。
训练完成后,提取训练好的词向量,将其用于其他NLP任务,如文本分类、相似度计算等。
需要注意的是,Word2Vec的训练过程可以得到词向量表征,但它本身并不关注词语的语义含义。生成的词向量在向量空间中的相对位置可以反映出词之间的语义关系,例如语义相似性和词语类比。这使得词向量在各种自然语言处理任务中具有广泛应用的潜力。

from gensim.models import Word2Vec
from gensim.utils import simple_preprocess

# Example sentences for training
sentences = [
    "I enjoy playing football with my friends",
    "I love to read books",
    "Football is a popular sport"
]

# Preprocess the sentences
preprocessed_sentences = [simple_preprocess(sentence) for sentence in sentences]

# Train the Word2Vec model
model = Word2Vec(preprocessed_sentences, min_count=1)

# Get word vector for a specific word
word = "football"
vector = model.wv[word]
print(f"Word '{word}' vector: {vector}")

# Find most similar words
similar_words = model.wv.most_similar(positive=["football"])
print(f"Similar words for 'football': {similar_words}")

preprocessed_sentences: [['enjoy', 'playing', 'football', 'with', 'my', 'friends'], ['love', 'to', 'read', 'books'], ['football', 'is', 'popular', 'sport']]
vector shape: (100,)
Word 'football' vector: [-5.3622725e-04  2.3643016e-04  5.1033497e-03  9.0092728e-03
 -9.3029495e-03 -7.1168090e-03  6.4588715e-03  8.9729885e-03
 -5.0154282e-03 -3.7633730e-03  7.3805046e-03 -1.5334726e-03
 -4.5366143e-03  6.5540504e-03 -4.8601604e-03 -1.8160177e-03
  2.8765798e-03  9.9187379e-04 -8.2852151e-03 -9.4488189e-03
  7.3117660e-03  5.0702621e-03  6.7576934e-03  7.6286553e-04
  6.3508893e-03 -3.4053659e-03 -9.4640255e-04  5.7685734e-03
 -7.5216386e-03 -3.9361049e-03 -7.5115822e-03 -9.3004224e-04
  9.5381187e-03 -7.3191668e-03 -2.3337698e-03 -1.9377422e-03
  8.0774352e-03 -5.9308959e-03  4.5161247e-05 -4.7537349e-03
 -9.6035507e-03  5.0072931e-03 -8.7595871e-03 -4.3918253e-03
 -3.5099984e-05 -2.9618264e-04 -7.6612402e-03  9.6147414e-03
  4.9820566e-03  9.2331432e-03 -8.1579182e-03  4.4957972e-03
 -4.1370774e-03  8.2453492e-04  8.4986184e-03 -4.4621779e-03
  4.5175003e-03 -6.7869616e-03 -3.5484887e-03  9.3985079e-03
 -1.5776539e-03  3.2137157e-04 -4.1406299e-03 -7.6826881e-03
 -1.5080094e-03  2.4697948e-03 -8.8802812e-04  5.5336617e-03
 -2.7429771e-03  2.2600652e-03  5.4557943e-03  8.3459523e-03
 -1.4537406e-03 -9.2081428e-03  4.3705511e-03  5.7178497e-04
  7.4419067e-03 -8.1328390e-04 -2.6384138e-03 -8.7530091e-03
 -8.5655687e-04  2.8265619e-03  5.4014279e-03  7.0526553e-03
 -5.7031228e-03  1.8588186e-03  6.0888622e-03 -4.7980524e-03
 -3.1072616e-03  6.7976285e-03  1.6314745e-03  1.8991709e-04
  3.4736372e-03  2.1777629e-04  9.6188262e-03  5.0606038e-03
 -8.9173913e-03 -7.0415614e-03  9.0145587e-04  6.3925339e-03]
Similar words for 'football': [('my', 0.21617144346237183), ('enjoy', 0.0931011363863945), ('love', 0.09291724115610123), ('playing', 0.07963486015796661), ('with', 0.06285081058740616), ('friends', 0.0270574688911438), ('to', 0.0161347147077322), ('sport', -0.010839186608791351), ('books', -0.027750365436077118), ('popular', -0.052346765995025635)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值