簡體   English   中英

AttributeError: 'Field' object 沒有屬性 'vocab' 阻止我運行代碼

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

我找到了這段代碼,我想看看我在最后一行打印的 object 是什么。 我是 nlp 領域的新手,所以請幫我修復此代碼,因為它給出AttributeError: 'Field' object has no attribute 'vocab'錯誤。 順便說一句,我發現 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>"]])

在嘗試訪問它之前,您必須為english Field建立詞匯表。 您將需要一個數據集來構建詞匯表,這將是您要為其構建 model 的數據集。 您可以使用english.build_vocab(...) 這是build_vocab的文檔。

此外,如果您想了解如何將您正在做的事情遷移到新版本的 torchtext, 這里是一個很好的資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM