BucketIterator抛出“字段"对象没有属性"vocab" [英] BucketIterator throws 'Field' object has no attribute 'vocab'

查看:979
本文介绍了BucketIterator抛出“字段"对象没有属性"vocab"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个新问题,我在没有任何解决方案的情况下找到的参考文献首先第二. 我是PyTorch的新手,在使用torchtextPyTorch中创建文本数据批处理时,面对AttributeError: 'Field' object has no attribute 'vocab'.

It's not a new question, references I found without any solution working for me first and second. 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.

紧随本书Deep Learning with PyTorch我写了与本书中解释的示例相同的例子.

Following up the book Deep Learning with PyTorch I wrote the same example as explained in the book.

以下是代码段:

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)

这是堆栈跟踪:

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'

如果不使用BucketIterator,我还能用什么来获得类似的结果 输出?

If not using BucketIterator, what else I can use to get a similar output?

推荐答案

您还没有为LABEL字段构建vocab.

You haven't built the vocab for the LABEL field.

TEXT.build_vocab(train, ...)之后,运行LABEL.build_vocab(test),其余的将运行.

After TEXT.build_vocab(train, ...), run LABEL.build_vocab(test), and the rest will run.

这篇关于BucketIterator抛出“字段"对象没有属性"vocab"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆