ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自然语言处理--gensim.word2vec 模块加载使用谷歌的预训练模型googlenews-vectors-negative300.bin.gz

2021-01-21 23:32:44  阅读:526  来源: 互联网

标签:bin word vectors gz print word2vec 向量


词向量将词的语义表示为训练语料库中上下文中的向量,可以把词向量看作是一个权重或分数的列表,列表中的每个权重或分数都对应于这个词在某个特定维度的含义。
很多公司都提供了预训练好的词向量模型,而且有很多针对各种编程语言的 NLP 库,可以让大家方便地使用这些预训练模型。如:谷歌公司的googlenews-vectors-negative300.bin.gz

from nlpia.data.loaders import get_data
from gensim.models.keyedvectors import KeyedVectors

# 下载googlenews-vectors-negative300.bin.gz
# word_vectors = get_data('word2vec')

# 加载原始二进制格式的模型
word_vectors = KeyedVectors.load_word2vec_format('xxx\\googlenews-vectors-negative300.bin.gz',
                                                    binary=True)
# 从谷歌新闻语料库中加载最常用的 20 万个词
# limit 参数:减少加载到内存中的词的数量
word_vectors_2w = KeyedVectors.load_word2vec_format('xxx\\googlenews-vectors-negative300.bin.gz',
                                                    binary=True, limit=200000)

# 词向量
# 数组中的每个浮点数表示向量的一个维度
print(word_vectors['phone'])

# 查找最近相邻词
# gensim.KeyedVectors.most_similar()方法提供了对于给定词向量,查找最近的相
# 邻词的有效方法。关键字参数 positive 接受一个待求和的向量列表; negative 参数来做减法,以排除不相关的词项。
# 参数 topn 用于指定返回结果中相关词项的数量。
print(word_vectors.most_similar(positive=['cooking', 'potatoes'], topn=5))
print(word_vectors.most_similar(positive=['germany', 'france'], topn=5))

# doesnt_match():检测不相关的词项
print(word_vectors.doesnt_match("potatoes milk cake computer".split()))

# 词向量的计算
print( word_vectors.most_similar(positive=['king', 'woman'],  negative=['man'], topn=2))

# 词项余弦相似度的计算
print(word_vectors.similarity('princess', 'queen'))

标签:bin,word,vectors,gz,print,word2vec,向量
来源: https://blog.csdn.net/fgg1234567890/article/details/112974650

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有