Python - PyTorch: IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I am working with PyTorch on a Text Classification problem with BERT. This is the PyTorch Dataset format I am using but when I try to access the inputs from the Dataset I get an error.
PyTorch Dataset
The Dataset Returns a Dictionary containing : ids, mask, token_type_ids, targets
class JigsawDataset:
    def __init__(self, df, train_transforms = None):
        self.comment_text = df[&quot;comment_text&quot;].values
        self.target = df[&quot;toxic&quot;].values
        self.tokenizer = config.BERT_TOKENIZER
        self.max_len = config.MAX_LEN
        self.langs = df[&quot;lang&quot;].values
        self.train_transforms = train_transforms

    def __len__(self):
        return len(self.comment_text)

    def __getitem__(self, item):
        comment_text = str(self.comment_text[item])
        comment_text = &quot; &quot;.join(comment_text.split())
        lang = self.langs[item]
        
        if self.train_transforms:
            comment_text, _ = self.train_transforms(data=(comment_text, lang))['data']

        inputs = self.tokenizer.encode_plus(
            comment_text,
            None,
            add_special_tokens=True,
            max_length=self.max_len,
            pad_to_max_length=True,
            truncation=True,
        )

        ids = inputs[&quot;input_ids&quot;]
        mask = inputs[&quot;attention_mask&quot;]
        token_type_ids = inputs[&quot;token_type_ids&quot;]

        data_loader_dict = {}
        data_loader_dict[&quot;ids&quot;] = torch.tensor(ids, dtype=torch.long)
        data_loader_dict[&quot;mask&quot;] = torch.tensor(mask, dtype=torch.long)
        data_loader_dict[&quot;token_type_ids&quot;] = torch.tensor(token_type_ids, dtype=torch.long)
        data_loader_dict[&quot;targets&quot;] = torch.tensor(self.target[item], dtype=torch.float)
        
        
        return data_loader_dict

Relevant Code which Gives Error
In this case I am trying to load only 1 Sample and make it to the format of the PyTorch Dataset
df = pd.read_csv(&quot;dataset.csv&quot;)
df = df.head(1) # Trying with only 1 Sample
dataset = JigsawDataset(df)

ids = dataset[&quot;ids&quot;]    # Error occurs at this line
mask = dataset[&quot;mask&quot;]
token_type_ids = [&quot;token_type_ids&quot;]

Error
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-78-4608dd623cac&gt; in &lt;module&gt;
      3 dataset = JigsawDataset(df)
      4 
----&gt; 5 ids = dataset[&quot;ids&quot;]    # Error occurs at this line
      6 mask = dataset[&quot;mask&quot;]
      7 token_type_ids = [&quot;token_type_ids&quot;]

&lt;ipython-input-40-121d8aa71516&gt; in __getitem__(self, item)
     13 
     14     def __getitem__(self, item):
---&gt; 15         comment_text = str(self.comment_text[item])
     16         comment_text = &quot; &quot;.join(comment_text.split())
     17         lang = self.langs[item]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

How to fix this?

I figured out the problem.
Incorrect Code
ids = dataset[&quot;ids&quot;]    
mask = dataset[&quot;mask&quot;]
token_type_ids = [&quot;token_type_ids&quot;]

Correct Code
ids = dataset[0][&quot;ids&quot;]    
mask = dataset[0][&quot;mask&quot;]
token_type_ids = [0][&quot;token_type_ids&quot;]

The problem was that &quot;ids&quot;, &quot;mask&quot; and &quot;token_type_ids&quot; are Dictionary Keys. JigsawDataset returns a dictionary for each sample. So in order to access a sample we need to specify the index ([0]) before specifying the key.
