简体   繁体   English

AttributeError: 'Field' object 没有属性 'vocab' 阻止我运行代码

[英]AttributeError: 'Field' object has no attribute 'vocab' preventing me to run the code

I have found this code and I wanna see what is the object that im printing in the last line.我找到了这段代码,我想看看我在最后一行打印的 object 是什么。 im new in field of nlp so please help me fix this code, because it gives AttributeError: 'Field' object has no attribute 'vocab' error.我是 nlp 领域的新手,所以请帮我修复此代码,因为它给出AttributeError: 'Field' object has no attribute 'vocab'错误。 by the way I have found out that torchtext has been changed and the error is probably related to these changes, and the code probably was working before.顺便说一句,我发现 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>"]])

You have to build the vocabulary for the english Field before you try to access it.在尝试访问它之前,您必须为english Field建立词汇表。 You will need a dataset to build the vocabulary, which will be the dataset you are looking to build a model for.您将需要一个数据集来构建词汇表,这将是您要为其构建 model 的数据集。 You can use english.build_vocab(...) .您可以使用english.build_vocab(...) Here are the docs for build_vocab .这是build_vocab的文档。

Also, if you would like to learn how to migrate what you are doing to the new version of torchtext, here is a good resource .此外,如果您想了解如何将您正在做的事情迁移到新版本的 torchtext, 这里是一个很好的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BucketIterator 抛出“Field”对象没有属性“vocab” - BucketIterator throws 'Field' object has no attribute 'vocab' AttributeError: &#39;TFSequenceClassifierOutput&#39; 对象没有属性 &#39;argmax&#39; - AttributeError: 'TFSequenceClassifierOutput' object has no attribute 'argmax' 获取“ AttributeError: 'float' object has no attribute 'lower' ” - Getting “ AttributeError: 'float' object has no attribute 'lower' ” AttributeError: 'WordList' object 没有属性 'split' - AttributeError: 'WordList' object has no attribute 'split' AttributeError:“示例”object 没有属性“文本” - AttributeError: 'Example' object has no attribute 'text' AttributeError: 'list' object 没有属性 'ents' - AttributeError: 'list' object has no attribute 'ents' AttributeError:“ InputLayer”对象没有属性“ W” - AttributeError:'InputLayer' object has no attribute 'W' AttributeError:“ Tensor”对象没有属性“ attention” - AttributeError: 'Tensor' object has no attribute 'attention' AttributeError:“ParentedTree”对象没有属性“标签” - AttributeError: 'ParentedTree' object has no attribute 'label' Pytorch 文本 AttributeError: &#39;BucketIterator&#39; 对象没有属性 - Pytorch Text AttributeError: ‘BucketIterator’ object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM