gpt4 book ai didi

python - 了解 FastText 多语言

转载 作者:行者123 更新时间:2023-12-01 00:43:34 26 4
gpt4 key购买 nike

我正在使用 FastText ( fastText_multilingual ) 的修改版本,它可以让我对齐两种语言的单词。

我试图理解他们的 fasttext.py,尤其是 Fast Vector class 。在示例文件 align_your_own.ipynb 中作者展示了如何衡量两个单词之间的相似性。我想对整个单词集迭代该过程,而不是每次都测量单个单词的相似性。为此,我需要了解如何访问这些 FastVector 对象。这就是为什么我试图理解 Fast 向量类。

我被困在这里:

 def __init__(self, vector_file='', transform=None):
"""Read in word vectors in fasttext format"""
self.word2id = {}

# Captures word order, for export() and translate methods
self.id2word = []

print('reading word vectors from %s' % vector_file)
with open(vector_file, 'r') as f:
(self.n_words, self.n_dim) = \
(int(x) for x in f.readline().rstrip('\n').split(' '))
self.embed = np.zeros((self.n_words, self.n_dim))
for i, line in enumerate(f):
elems = line.rstrip('\n').split(' ')
self.word2id[elems[0]] = i
self.embed[i] = elems[1:self.n_dim+1]
self.id2word.append(elems[0])

我从未在 python 中创建过类,所以这让事情对我来说更加困难。这些是我无法深入理解的几行:

 1. (self.n_words, self.n_dim) = \
2. self.word2id = {}, self.id2word = [],
3. self.embed = np.zeros((self.n_words, self.n_dim))

这些是我的问题:

  • 1 上的"=\" 是什么意思?
  • word2idid2wordembed 是在哪里定义的?它们是python的关键字吗?

最佳答案

行尾的反斜杠告诉 Python 将当前逻辑行扩展到下一个物理行。在您的情况下,您可以将这两行视为一行:

(self.n_words, self.n_dim) = (int(x) for x in f.readline().rstrip('\n').split(' '))

在 Python 中,变量是在您第一次为其赋值时创建的 ( https://www.w3schools.com/python/python_variables.asp )。因此,word2id、id2word 和 embed 不是关键字;它们是在为它们分配值时创建的。

关于python - 了解 FastText 多语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57165579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com