简体   繁体   English

BucketIterator 抛出“Field”对象没有属性“vocab”

[英]BucketIterator throws 'Field' object has no attribute 'vocab'

It's not a new question, references I found without any solution working for me first and second .这不是一个新问题,我找到了没有任何解决方案的参考文献firstsecond I'm a newbie to PyTorch, facing AttributeError: 'Field' object has no attribute 'vocab' while creating batches of the text data in PyTorch using torchtext .我是 PyTorch 的新手,面临AttributeError: 'Field' object has no attribute 'vocab'同时使用torchtextPyTorch创建批量文本数据。

Following up the book Deep Learning with PyTorch I wrote the same example as explained in the book.Deep Learning with PyTorch这本书之后,我编写了与书中解释的相同的示例。

Here's the snippet:这是片段:

from torchtext import data
from torchtext import datasets
from torchtext.vocab import GloVe

TEXT = data.Field(lower=True, batch_first=True, fix_length=20)
LABEL = data.Field(sequential=False)
train, test = datasets.IMDB.splits(TEXT, LABEL)

print("train.fields:", train.fields)
print()
print(vars(train[0]))  # prints the object



TEXT.build_vocab(train, vectors=GloVe(name="6B", dim=300),
                 max_size=10000, min_freq=10)

# VOCABULARY
# print(TEXT.vocab.freqs)  # freq
# print(TEXT.vocab.vectors)  # vectors
# print(TEXT.vocab.stoi)  # Index

train_iter, test_iter = data.BucketIterator.splits(
    (train, test), batch_size=128, device=-1, shuffle=True, repeat=False)  # -1 for cpu, None for gpu

# Not working (FROM BOOK)
# batch = next(iter(train_iter))

# print(batch.text)
# print()
# print(batch.label)

# This also not working (FROM Second solution)
for i in train_iter:
    print (i.text)
    print (i.label)

Here's the stacktrace:这是堆栈跟踪:

AttributeError                            Traceback (most recent call last)
<ipython-input-33-433ec3a2ca3c> in <module>()
      7 
      8 
----> 9 for i in train_iter:
     10     print (i.text)
     11     print (i.label)

/anaconda3/lib/python3.6/site-packages/torchtext/data/iterator.py in __iter__(self)
    155                     else:
    156                         minibatch.sort(key=self.sort_key, reverse=True)
--> 157                 yield Batch(minibatch, self.dataset, self.device)
    158             if not self.repeat:
    159                 return

/anaconda3/lib/python3.6/site-packages/torchtext/data/batch.py in __init__(self, data, dataset, device)
     32                 if field is not None:
     33                     batch = [getattr(x, name) for x in data]
---> 34                     setattr(self, name, field.process(batch, device=device))
     35 
     36     @classmethod

/anaconda3/lib/python3.6/site-packages/torchtext/data/field.py in process(self, batch, device)
    199         """
    200         padded = self.pad(batch)
--> 201         tensor = self.numericalize(padded, device=device)
    202         return tensor
    203 

/anaconda3/lib/python3.6/site-packages/torchtext/data/field.py in numericalize(self, arr, device)
    300                 arr = [[self.vocab.stoi[x] for x in ex] for ex in arr]
    301             else:
--> 302                 arr = [self.vocab.stoi[x] for x in arr]
    303 
    304             if self.postprocessing is not None:

/anaconda3/lib/python3.6/site-packages/torchtext/data/field.py in <listcomp>(.0)
    300                 arr = [[self.vocab.stoi[x] for x in ex] for ex in arr]
    301             else:
--> 302                 arr = [self.vocab.stoi[x] for x in arr]
    303 
    304             if self.postprocessing is not None:

AttributeError: 'Field' object has no attribute 'vocab'

If not using BucketIterator, what else I can use to get a similar output?如果不使用 BucketIterator,我还能用什么来获得类似的输出?

You haven't built the vocab for the LABEL field.您还没有为 LABEL 字段构建词汇。

After TEXT.build_vocab(train, ...) , run LABEL.build_vocab(train) , and the rest will run.TEXT.build_vocab(train, ...) ,运行LABEL.build_vocab(train) ,其余的将运行。

暂无
暂无

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

相关问题 Pytorch 文本 AttributeError: &#39;BucketIterator&#39; 对象没有属性 - Pytorch Text AttributeError: ‘BucketIterator’ object has no attribute AttributeError: 'Vocab' object 没有属性 'stoi' - AttributeError: 'Vocab' object has no attribute 'stoi' 无法访问迭代器中的批处理项目 - Torchtext 属性错误:“字段”对象没有属性“词汇” - Unable to access batch items in iterator - Torchtext Attribute Error: 'Field' object has no attribute 'vocab' AttributeError: 'Example' object 在使用来自 torchtext 的 build_vocab() 构建 vocab 时没有属性 'Insult' - AttributeError: 'Example' object has no attribute 'Insult' when build vocab using build_vocab() from torchtext &#39;KeyedVectors&#39; 对象没有属性 &#39;wv&#39; / 词汇属性已从 Gensim 4.0.0 中的 KeyedVector 中删除 - 'KeyedVectors' object has no attribute 'wv' / The vocab attribute was removed from KeyedVector in Gensim 4.0.0 NLP `BucketIterator` 和 `build_vocab_from_iterator` 之间的数据处理 - NLP data processing between `BucketIterator` and `build_vocab_from_iterator` 属性错误&#39;module&#39;对象没有属性“Field” - Attribute Error 'module' object has no attribute “Field” &#39;AutoField&#39;对象没有属性&#39;remote_field&#39; - 'AutoField' object has no attribute 'remote_field' 对象“项目”没有属性“ ro_field” - Object 'Item' has no attribute 'ro_field' 自定义Django模型字段中的“对象没有属性” - “object has no attribute” in custom Django model field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM