python - 如何在自训练的word2vec模型中删除单词

标签 python word2vec

我得到了一个自训练的word2vec模型(2G,以“.model”结尾)。我将模型转换成文本文件(超过50G,以“.txt”结尾),因为我必须在我的其他python代码中使用文本文件。我试图通过删除不需要的单词来减小文本文件的大小。我已经建立了一个词汇集,其中包含我需要的所有单词。如何过滤模型中不需要的词?

我已尝试为文本文件构建字典,但我的 RAM 不足。

emb_dict = dict()
with open(emb_path, "r", encoding="utf-8") as f:
    lines = f.readlines()
    for l in lines:
        word, embedding = l.strip().split(' ',1)
        emb_dict[word] = embedding

我在想是否可以删除“.model”文件中的单词。我该怎么做?任何帮助将不胜感激!

最佳答案

如果没有更精确的代码,很难进一步回答,但您可以批处理对文本文件的分析

lines_to_keep = []
new_file = "some_path.txt"
words_to_keep = set(some_words)
with open(emb_path, "r", encoding="utf-8") as f:
    for l in f:
        word, embedding = l.strip().split(' ',1)
        if word in words_to_keep:
            lines_to_keep.append(l.strip())
        if lines_to_keep and len(lines_to_keep) % 1000 == 0:
            with open(new_file, "a") as f:
                f.write("\n".join(lines_to_keep)
            lines_to_keep = []

关于python - 如何在自训练的word2vec模型中删除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987014/

相关文章:

python - asyncio.create_task 装饰器不会同时执行

python - 如何按字母顺序对大型文本文件进行排序?

python - 如何检查字典中是否存在键值对?

python - 齿轮已加载,但功能不起作用(discord.py)

python - 在 NLP 中合并相关词

javascript - 使用 python 抓取隐藏的 href

python - 用于文本分类算法的 word2Vec 向量表示

python - 对短语使用 word2vec

gensim - 有没有办法从 KeyedVectors 词汇表中删除单词?

tensorflow - Word2Vec 应该训练多少个 epoch?推荐的训练数据集是什么?