Unable to access batch items in iterator - Torchtext Attribute Error: 'Field' object has no attribute 'vocab'
I am unable to access batch items from the Iterator object in Torchtext.
Following is the error

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

Code to Recreate Problem
#Access to Drive
from google.colab import drive
drive.mount ('/content/gdrive')

import numpy as np
import spacy
spacy_en = spacy.load(&quot;en&quot;)

def tokenize(text):
    return [tok.text for tok in spacy_en.tokenizer(text)]

import torch
from torchtext.legacy.data import Field, LabelField, Iterator

TEXT = Field(sequential=True, use_vocab=True, tokenize=tokenize, lower=True)
LABEL = LabelField(dtype = torch.long, use_vocab=False)  

fields = {&quot;text&quot;: (&quot;txt&quot;, TEXT), &quot;label&quot;: (&quot;lbl&quot;, LABEL)}

from torchtext.legacy.data import TabularDataset
train_data, test_data = TabularDataset.splits(path=&quot;/content/gdrive/MyDrive/Colab Notebooks/&quot;, 
                                              train=&quot;Strong_Train.csv&quot;, 
                                              test=&quot;Strong_Test.csv&quot;, format=&quot;csv&quot;, fields=fields)


device = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)

TEXT = Field(sequential=True, use_vocab=True, tokenize=tokenize, lower=True)
LABEL = LabelField(dtype = torch.long, use_vocab=False) 

TEXT.build_vocab(train_data, max_size=10000 )
LABEL.build_vocab(train_data) 

train_iterator = Iterator(train_data, batch_size=1, device=device)
for batch in train_iterator:
    print('hello')

My Analysis of Problem
As per the error description, 'Field' object is causing the problem; TEXT is the Field object in this code.
type(TEXT)
Output: torchtext.legacy.data.field.Field
Since the error says,

Field object has no attribute 'vocab'

accessing 'vocab' should give an error, but it doesn't give an error
TEXT.vocab
Output: &lt;torchtext.legacy.vocab.Vocab at 0x7fce37b91950&gt;
I am also able to get the length of vocab
len(TEXT.vocab)
Output: 2
So the question still remains, if there exists 'vocab' attribute in the Field object, why am I getting this error? and how do I resolve it?
Environment Specifics
Running the code on Google Colab
Torchtext version is 0.10.0

After you set your device, you are redefining TEXT and LABEL. That isn't necessary, and could cause issues. Also, you are setting use_vocab=False for your LabelField, and then building a vocab for it.
From the torchtext 0.8 docs:

use_vocab: Whether to use a Vocab object. If False, the data in this field should already be numerical. Default: True.

I would start by clearing up those issues and see if that resolves your error.
