最近復(fù)現(xiàn)代碼過程中,需要用到 torchtext.data 中的 Field 類。本篇博客記錄使用過程中的問題及解決方式。
- 注意
torchtext
版本不宜過新
在較新版本的 torchtext.data
里面并沒有 Field
方法,這一點(diǎn)需要注意。
啟示:在復(fù)現(xiàn)別人代碼時(shí),應(yīng)同時(shí)復(fù)制他們使用環(huán)境的版本信息。
- 運(yùn)行下述代碼:
from torchtext.data import Field
SRC = Field(tokenize = tokenize_en,
init_token = '<sos>',
eos_token = '<eos>',
fix_length = max_length,
lower = True,
batch_first = True,
sequential=True)
TRG = Field(tokenize = tokenize_en,
init_token = '<sos>',
eos_token = '<eos>',
fix_length = max_length,
lower = True,
batch_first = True,
sequential=True)
print(SRC.vocab.stoi["<sos>"])
print(TRG.vocab.stoi["<sos>"])
報(bào)錯(cuò)信息:
print(SRC.vocab.stoi["<sos>"]) # 2
AttributeError: 'Field' object has no attribute 'vocab'
于是查看 Field
類的定義,尋找和詞表建立相關(guān)的函數(shù),發(fā)現(xiàn)其 build_vocab()
函數(shù)中有建立詞表的操作, build_vocab()
函數(shù)定義如下:
class Field(RawField):
...
def build_vocab(self, *args, **kwargs):
"""Construct the Vocab object for this field from one or more datasets.
Arguments:
Positional arguments: Dataset objects or other iterable data
sources from which to construct the Vocab object that
represents the set of possible values for this field. If
a Dataset object is provided, all columns corresponding
to this field are used; individual columns can also be
provided directly.
Remaining keyword arguments: Passed to the constructor of Vocab.
"""
counter = Counter()
sources = []
for arg in args:
if isinstance(arg, Dataset):
sources += [getattr(arg, name) for name, field in
arg.fields.items() if field is self]
else:
sources.append(arg)
for data in sources:
for x in data:
if not self.sequential:
x = [x]
try:
counter.update(x)
except TypeError:
counter.update(chain.from_iterable(x))
specials = list(OrderedDict.fromkeys(
tok for tok in [self.unk_token, self.pad_token, self.init_token,
self.eos_token] + kwargs.pop('specials', [])
if tok is not None))
self.vocab = self.vocab_cls(counter, specials=specials, **kwargs)
...
解決方式:在程序中 Field
定義后添加 SRC.build_vocab()
和 TRG.build_vocab()
,程序變成:
SRC.build_vocab()
TRG.build_vocab()
print(SRC.vocab.stoi["<sos>"]) # 輸出結(jié)果:2
print(TRG.vocab.stoi["<sos>"]) # 輸出結(jié)果:2
至此,程序就會(huì)順利執(zhí)行啦!文章來源:http://www.zghlxwxcb.cn/news/detail-539560.html
參考資料文章來源地址http://www.zghlxwxcb.cn/news/detail-539560.html
- python - BucketIterator 拋出 ‘Field’ 對(duì)象沒有屬性 ‘vocab’ - IT工具網(wǎng) (coder.work)
- ImportError: cannot import name ‘Field‘ from ‘torchtext.data‘, No module named “l(fā)egacy“_no module named 'torchtext.legacy_御用廚師的博客-CSDN博客
到了這里,關(guān)于成功解決 AttributeError: ‘Field‘ object has no attribute ‘vocab‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!