Object has no attribute 'parameters'

I am running the following code:-

from tqdm.auto import tqdm

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dataloader:
        batch = {k: v.to(device) for k, v in batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)

This is a straight copy and paste from the HuggingFace documentation. Unfortunately, the code is returning the following error:-

'TFDistilBertForSequenceClassification' object has no attribute 'parameters'

I used the following boilerplate for the data loaders:-

from torch.utils.data import DataLoader

batch_size = 8

num_epochs = 3

train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)

eval_dataloader = DataLoader(val_dataset, batch_size=batch_size)

I would be happy to share any further code that might be useful though there is rather a lot of it so please just describe what you need.

The full Traceback is:-

0%
1/390 [00:00<03:43, 1.74it/s]
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  # Remove the CWD from sys.path while we load stuff.
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2897             try:
-> 2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1002

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
8 frames
<ipython-input-167-b86b10a2f8e7> in <module>()
      5 model.train()
      6 for epoch in range(num_epochs):
----> 7     for batch in train_dataloader:
      8         batch = {k: v.to(device) for k, v in batch.items()}
      9         outputs = model(**batch)

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    519             if self._sampler_iter is None:
    520                 self._reset()
--> 521             data = self._next_data()
    522             self._num_yielded += 1
    523             if self._dataset_kind == _DatasetKind.Iterable and \

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
    559     def _next_data(self):
    560         index = self._next_index()  # may raise StopIteration
--> 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    562         if self._pin_memory:
    563             data = _utils.pin_memory.pin_memory(data)

/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

<ipython-input-154-c53f3c6a8f83> in __getitem__(self, idx)
      9     def __getitem__(self, idx):
     10         item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
---> 11         item['labels'] = torch.tensor(self.labels[idx])
     12         return item
     13 

/usr/local/lib/python3.7/dist-packages/pandas/core/series.py in __getitem__(self, key)
    880 
    881         elif key_is_scalar:
--> 882             return self._get_value(key)
    883 
    884         if is_hashable(key):

/usr/local/lib/python3.7/dist-packages/pandas/core/series.py in _get_value(self, label, takeable)
    988 
    989         # Similar to Index.get_value, but we do not fall back to positional
--> 990         loc = self.index.get_loc(label)
    991         return self.index._get_values_for_loc(self, loc, label)
    992 

/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:
-> 2900                 raise KeyError(key) from err
   2901 
   2902         if tolerance is not None:

KeyError: 1002

I’m confused as the stack trace you are copying is not with the error you mention. The stack trace has a problem of indexing in the dataframe you apparently used for the labels, and as the final error says, it can’t find the label at index 1002.

1 Like

Thanks for the help Sylvain. I have copied the Trace more accurately below, hopefully it should make it clearer what was going on:-

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  # Remove the CWD from sys.path while we load stuff.
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2897             try:
-> 2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 923

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
8 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:
-> 2900                 raise KeyError(key) from err
   2901 
   2902         if tolerance is not None:

KeyError: 923

Some further information that I hope is useful.

Running this:-

train_dataset.encodings

Returns this:-

0s
train_dataset.encodings
{'input_ids': tensor([[  101,   155,  1942,  ...,     0,     0,     0],
        [  101, 27900,  7641,  ...,     0,     0,     0],
        [  101,   155,  1942,  ...,     0,     0,     0],
        ...,
        [  101,   109,  7414,  ...,     0,     0,     0],
        [  101,  2809,  1141,  ...,     0,     0,     0],
        [  101,  1448,  1111,  ...,     0,     0,     0]]), 'attention_mask': tensor([[1, 1, 1,  ..., 0, 0, 0],
        [1, 1, 1,  ..., 0, 0, 0],
        [1, 1, 1,  ..., 0, 0, 0],
        ...,
        [1, 1, 1,  ..., 0, 0, 0],
        [1, 1, 1,  ..., 0, 0, 0],
        [1, 1, 1,  ..., 0, 0, 0]])}

Running this:-

train_dataset.labels

Returns this:-

0s
train_dataset.labels
10      1
147     0
342     0
999     2
811     2
       ..
1095    2
1130    0
1294    1
860     1
1126    1
Name: sentiment, Length: 1040, dtype: int64

Again, the problem in the stack traces you are posting is an indexing error in your labels dataframe, nothing related to Transformers. Make sure to convert it to an array or reset its index.

1 Like

Thank you Sylvain,

I took your advise to reset the index on the Pandas dataframe and that worked!! Thank you so much - that was a huge help, I am very grateful!

1 Like