在训练我自己的Glove模型时编码问题

问题描述 投票:0回答:1

我正在用我自己的语料库训练一个GloVe模型,我有麻烦来保存它/以utf-8格式加载它。

这是我尝试过的:

from glove import Corpus, Glove

#data
lines = [['woman', 'umbrella', 'silhouetted'], ['person', 'black', 'umbrella']]

#GloVe training
corpus = Corpus() 
corpus.fit(lines, window=4)
glove = Glove(no_components=4, learning_rate=0.1)
glove.fit(corpus.matrix, epochs=10, no_threads=8, verbose=True)
glove.add_dictionary(corpus.dictionary)
glove.save('glove.model.txt')

保存的文件glove.model.txt是不可读的,我无法使用utf-8编码保存它。

当我尝试阅读它时,例如通过以Word2Vec格式转换它:

from gensim.models.keyedvectors import KeyedVectors
from gensim.scripts.glove2word2vec import glove2word2vec
glove2word2vec(glove_input_file="glove.model.txt", 
word2vec_output_file="gensim_glove_vectors.txt")    

model = KeyedVectors.load_word2vec_format("gensim_glove_vectors.txt", binary=False)

我有以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

关于如何使用我自己的GloVe模型的任何想法?

python encoding nlp word-embedding glove
1个回答
0
投票

我发现自己是一种使用utf-8格式保存数据的方法,我在这里分享以防万一有人面临同样的问题

而不是使用手套保存方法glove.save('glove.model.txt')尝试自己模拟手套记录:

with open("results_glove.txt", "w") as f:
    for word in glove.dictionary:
        f.write(word)
        f.write(" ")
        for i in range(0, vector_size):
            f.write(str(glove.word_vectors[glove.dictionary[word]][i]))
            f.write(" ")
        f.write("\n")

然后你就可以阅读了。

© www.soinside.com 2019 - 2024. All rights reserved.