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

六月ゝ 毕业季﹏ 提交于 2019-12-11 01:36:55

问题


Returned iterator of BucketIterator is giving an error.

train_iterator = data.BucketIterator.splits(
    train_data, 
    batch_size=BATCH_SIZE, 
    device=device
)

Value of train_data also looks good.

print(vars(train_data.examples[10]))
{'text': ['plus',
  "you've",
  'added',
  'commercials',
  'to',
  'the',
  'experience...',
  'tacky.'],
 'label': 'positive'}

Stack trace:

print([x for x in train_iterator])

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-f14edc400c88> in <module>()
----> 1 [x for x in train_iter]

<ipython-input-50-f14edc400c88> in <listcomp>(.0)
----> 1 [x for x in train_iter]

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

~/anaconda3/envs/env/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/envs/env/lib/python3.6/site-packages/torchtext/data/field.py in process(self, batch, device)
    235         """
    236         padded = self.pad(batch)
--> 237         tensor = self.numericalize(padded, device=device)
    238         return tensor
    239 

~/anaconda3/envs/env/lib/python3.6/site-packages/torchtext/data/field.py in numericalize(self, arr, device)
    334         if self.use_vocab:
    335             if self.sequential:
--> 336                 arr = [[self.vocab.stoi[x] for x in ex] for ex in arr]
    337             else:
    338                 arr = [self.vocab.stoi[x] for x in arr]

~/anaconda3/envs/env/lib/python3.6/site-packages/torchtext/data/field.py in <listcomp>(.0)
    334         if self.use_vocab:
    335             if self.sequential:
--> 336                 arr = [[self.vocab.stoi[x] for x in ex] for ex in arr]
    337             else:
    338                 arr = [self.vocab.stoi[x] for x in arr]

~/anaconda3/envs/env/lib/python3.6/site-packages/torchtext/data/field.py in <listcomp>(.0)
    334         if self.use_vocab:
    335             if self.sequential:
--> 336                 arr = [[self.vocab.stoi[x] for x in ex] for ex in arr]
    337             else:
    338                 arr = [self.vocab.stoi[x] for x in arr]

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

May I know what is it that we are missing here?

来源:https://stackoverflow.com/questions/53300155/attributeerror-field-object-has-no-attribute-vocab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!