训练我自己的手套模型时出现编码问题

2024-05-29 08:12:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用我自己的语料库训练一个手套模型,我很难以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格式:

^{pr2}$

我有以下错误:

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

你知道我怎么用我自己的手套模型吗?在


Tags: no模型txtmodeldictionary格式corpusutf
1条回答
网友
1楼 · 发布于 2024-05-29 08:12:21

我刚刚找到了一种以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")

那你就可以读了。在

相关问题 更多 >

    热门问题