词向量之加载word2vec和glove

1 Google用word2vec预训练了300维的新闻语料的词向量googlenews-vecctors-negative300.bin,解压后3.39个G。


可以用gensim加载进来,但是需要内存足够大。

#加载Google训练的词向量
import gensim
model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin',binary=True)
print(model['love'])


2 用Glove预训练的词向量也可以用gensim加载进来,只是在加载之前要多做一步操作,代码参考

Glove300维的词向量有5.25个G。

# 用gensim打开glove词向量需要在向量的开头增加一行:所有的单词数 词向量的维度
import gensim
import os
import shutil
import hashlib
from sys import platform
#计算行数,就是单词数
def getFileLineNums(filename):
	f = open(filename, 'r')
	count = 0
	for line in f:
		count += 1
	return count

#Linux或者Windows下打开词向量文件,在开始增加一行
def prepend_line(infile, outfile, line):
	with open(infile, 'r') as old:
		with open(outfile, 'w') as new:
			new.write(str(line) + "\n")
			shutil.copyfileobj(old, new)

def prepend_slow(infile, outfile, line):
	with open(infile, 'r') as fin:
		with open(outfile, 'w') as fout:
			fout.write(line + "\n")
			for line in fin:
				fout.write(line)

def load(filename):
	num_lines = getFileLineNums(filename)
	gensim_file = 'glove_model.txt'
	gensim_first_line = "{} {}".format(num_lines, 300)
	# Prepends the line.
	if platform == "linux" or platform == "linux2":
		prepend_line(filename, gensim_file, gensim_first_line)
	else:
		prepend_slow(filename, gensim_file, gensim_first_line)
	
	model = gensim.models.KeyedVectors.load_word2vec_format(gensim_file)

load('glove.840B.300d.txt')
生成的glove_model.txt就是可以直接用gensim打开的模型。



预训练词嵌入是一种常见的自然语言处理技术,它将单词映射到向量空间中的点,使得单词在语义上相近的点在向量空间中也相近。 Word2vecGloVe是两种常用的预训练词嵌入模型,可以通过使用Python中的gensim包来实现。 首先,我们需要准备一个文本语料库,可以是任何文本数据集。然后,我们可以使用gensim包中的Word2VecGloVe类来训练词嵌入模型。下面是一个简单的代码示例: ``` from gensim.models import Word2Vec from gensim.scripts.glove2word2vec import glove2word2vec from gensim.models import KeyedVectors # 使用Word2vec训练词嵌入模型 sentences = [['this', 'is', 'the', 'first', 'sentence'], ['this', 'is', 'the', 'second', 'sentence'], ['yet', 'another', 'sentence'], ['one', 'more', 'sentence'], ['and', 'the', 'final', 'sentence']] model_w2v = Word2Vec(sentences, size=100, min_count=1) # 使用GloVe训练词嵌入模型 glove_input_file = 'glove.6B.100d.txt' word2vec_output_file = 'glove.6B.100d.txt.word2vec' glove2word2vec(glove_input_file, word2vec_output_file) model_glove = KeyedVectors.load_word2vec_format(word2vec_output_file, binary=False) # 使用训练好的模型进行单词相似度计算 similarity_w2v = model_w2v.wv.similarity('first', 'second') similarity_glove = model_glove.similarity('first', 'second') print('Word2vec similarity:', similarity_w2v) print('GloVe similarity:', similarity_glove) ``` 以上代码中,我们首先使用Word2vec训练了一个词嵌入模型,然后使用GloVe训练了另一个模型。我们还使用gensim包中的similarity函数计算了两个模型中单词“first”“second”的相似度。 需要注意的是,GloVe模型在训练过程中需要使用预训练的GloVe向量文件作为输入,因此我们需要先将GloVe向量文件转换为Word2vec格式,然后再使用KeyedVectors类加载模型。 总的来说,Word2vecGloVe是两种非常有用的预训练词嵌入技术,可以帮助我们更好地理解处理自然语言数据。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值