【问题标题】:AttributeError: 'Field' object has no attribute 'vocab' preventing me to run the codeAttributeError: 'Field' 对象没有属性 'vocab' 阻止我运行代码
【发布时间】:2021-12-18 14:47:47
【问题描述】:

我找到了这段代码,我想看看我在最后一行打印的对象是什么。我是 nlp 领域的新手,所以请帮我修复此代码,因为它给出了AttributeError: 'Field' object has no attribute 'vocab'error。顺便说一句,我发现torchtext已经改变了,错误可能与这些改变有关,而且代码可能以前可以工作。

import spacy
from torchtext.legacy.data import Field
spacy_eng = spacy.load("en")
def tokenize_eng(text):
    return [tok.text for tok in spacy_eng.tokenizer(text)]

english = Field(
    tokenize=tokenize_eng, lower=True, init_token="<sos>", eos_token="<eos>"
)
print([english.vocab.stoi["<sos>"]])

【问题讨论】:

    标签: nlp torchtext


    【解决方案1】:

    您必须先为english Field 建立词汇表,然后才能尝试访问它。您将需要一个数据集来构建词汇表,这将是您要为其构建模型的数据集。您可以使用english.build_vocab(...)Here are the docsbuild_vocab

    另外,如果您想了解如何将您正在做的事情迁移到新版本的 torchtext,here is a good resource

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 2019-10-08
      • 1970-01-01
      • 2020-07-25
      • 2021-03-27
      • 2012-10-22
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      相关资源
      最近更新 更多