【发布时间】:2019-09-15 18:41:45
【问题描述】:
我正在尝试使用 fasttext 计算以下句子的词嵌入。
a = 'We are pencil in the hands'
我没有任何预训练模型,那我该怎么做呢?
【问题讨论】:
标签: python python-3.x nlp nltk fasttext
我正在尝试使用 fasttext 计算以下句子的词嵌入。
a = 'We are pencil in the hands'
我没有任何预训练模型,那我该怎么做呢?
【问题讨论】:
标签: python python-3.x nlp nltk fasttext
您需要一张经过训练的嵌入表。
您可以从 FastText website 下载预训练的嵌入,并使用它们提供的代码来加载嵌入。你甚至不需要为此安装 FastText:
import io
def load_vectors(fname):
fin = io.open(fname, 'r', encoding='utf-8', newline='\n', errors='ignore')
n, d = map(int, fin.readline().split())
data = {}
for line in fin:
tokens = line.rstrip().split(' ')
data[tokens[0]] = map(float, tokens[1:])
return data
然后你就从字典里拿起那个。
或者,您可以通过关注tutorial 自己在文本数据上训练 fasttext。训练词嵌入的合理最小数据集是数十万个词。
【讨论】: