AttributeError: 'Field' object has no attr

2019-07-26 20:59发布

问题:

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?