python - Calculate word embeddings using fasttext
问题描述
I am trying to calculate the word embeddings using fasttext for the following sentence.
a = 'We are pencil in the hands'
I dont have any pretrained model, so how do i go about it?
解决方案
您需要一张经过训练的嵌入表。
您可以从FastText 网站下载预训练的嵌入,并使用它们提供的代码来加载嵌入。你甚至不需要为此安装 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
然后,您只需从字典中提取。
或者,您可以按照教程在文本数据上自己训练 fasttext 。用于训练词嵌入的数据集的合理最小值是数十万个词。
推荐阅读
- android - 背景图像上的 UI 组件
- python - 为什么我添加轮换后我的程序变得非常滞后,我该如何解决这个问题?
- python-3.x - 如何交换箱线图中的 x 标签?
- swift - AVAudioPlayer NSOSStatusErrorDomain 错误
- python - 如何将来自表模型的 HTML 表数据中的记录添加到另一个表模型中?
- c# - 获取 List.Max 的索引?
- symfony4 - MappingException:“需要扩展 DOM。”
- html - 如何使用 Handlebars 将表单中的复选框插入 SQL?
- python - 如何从 gui 中收到的多行输入写入 python 文件?
- sql-server-2012 - 即使有空值也加入 2 个表并对每一行求和