python - 训练 Fasttext 模型
问题描述
我想使用“gensim”库在 Python 中训练一个 Fasttext 模型。首先,我应该将每个句子标记为其单词,从而将每个句子转换为单词列表。然后,应将此列表附加到最终列表中。因此,最后,我将有一个包含所有标记化句子的嵌套列表:
word_punctuation_tokenizer = nltk.WordPunctTokenizer()
word_tokenized_corpus = []
for line in open('sentences.txt'):
new = line.strip()
new = word_punctuation_tokenizer.tokenize(new)
if len(new) != 0:
word_tokenized_corpus.append(new)
然后,模型应该如下构建:
embedding_size = 60
window_size = 40
min_word = 5
down_sampling = 1e-2
ft_model = FastText(word_tokenized_corpus,
size=embedding_size,
window=window_size,
min_count=min_word,
sample=down_sampling,
sg=1,
iter=100)
但是“word_tokenized_corpus”的句子数量很大,程序处理不了。是否可以通过将每个标记化的句子一个一个地赋予模型来训练模型,例如:?
for line in open('sentences.txt'):
new = line.strip()
new = word_punctuation_tokenizer.tokenize(new)
if len(new) != 0:
ft_model = FastText(new,
size=embedding_size,
window=window_size,
min_count=min_word,
sample=down_sampling,
sg=1,
iter=100)
这对最终结果有什么影响吗?是否可以在不必构建如此大的列表并将其保存在内存中的情况下训练模型?
解决方案
由于数据量很大,最好将文本文件转换成COR文件。然后,按以下方式阅读:
from gensim.test.utils import datapath
corpus_file = datapath('sentences.cor')
至于下一步:
model = FastText(size=embedding_size,
window=window_size,
min_count=min_word,
sample=down_sampling,
sg=1,
iter=100)
model.build_vocab(corpus_file=corpus_file)
total_words = model.corpus_total_words
model.train(corpus_file=corpus_file, total_words=total_words, epochs=5)
推荐阅读
- java - XML createElement 双引号 DocumentBuilder api
- spring-boot - 无法通过 Beanstalk 将图像上传到 S3 但在本地服务器中工作
- vue.js - Vuetify v-flex 中的 child-flex 选项是做什么用的?
- java - 在 wicket7 中形成并输入密钥提交
- javascript - 使用 javascript 生成的 html 代码不适用于可排序的 jquery
- windows - 打字稿错误 TS7017:元素隐式具有“任何”类型
- c++ - 用它的 processID 杀死一个进程
- css - SVG在没有脚本的情况下悬停n秒(工具提示的加载时间)后将光标从“cursor:wait”更改为“cursor:help”?
- nginx - 即使 url 中有 .php,如何将所有请求重定向到 index.php
- amazon-web-services - 我的免费套餐结束了吗?