我正在用我自己的语料库训练一个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模型的任何想法?
我发现自己是一种使用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")
然后你就可以阅读了。