在python中加载预训练的手套向量 [英] Load Pretrained glove vectors in python
问题描述
我已经从互联网上下载了预先训练的手套矢量文件.这是一个.txt文件.我无法加载和访问它.使用gensim可以很容易地加载和访问单词矢量二进制文件,但是当它是文本文件格式时,我不知道该怎么做.
I have downloaded pretrained glove vector file from the internet. It is a .txt file. I am unable to load and access it. It is easy to load and access a word vector binary file using gensim but I don't know how to do it when it is a text file format.
预先感谢
推荐答案
手套模型文件为文字-矢量格式.您可以打开文本文件进行验证.这是一小段代码,可用于加载预先训练的手套文件:
glove model files are in a word - vector format. You can open the textfile to verify this. Here is a small snippet of code you can use to load a pretrained glove file:
import numpy as np
def loadGloveModel(File):
print("Loading Glove Model")
f = open(File,'r')
gloveModel = {}
for line in f:
splitLines = line.split()
word = splitLines[0]
wordEmbedding = np.array([float(value) for value in splitLines[1:]])
gloveModel[word] = wordEmbedding
print(len(gloveModel)," words loaded!")
return gloveModel
然后,您只需使用GlovesModel变量即可访问单词向量.
You can then access the word vectors by simply using the gloveModel variable.
print gloveModel['hello']
这篇关于在python中加载预训练的手套向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!