1

I apply this code lines in my model

train_data_loader = create_data_loader(df_train, tokenizer, MAX_LEN, BATCH_SIZE)

data =next(iter(train_data_loader))

but I got this error

TypeError                                 Traceback (most recent call last)
<ipython-input-39-8edd470666f3> in <module>()
----> 1 data =next(iter(train_data_loader))

3 frames
/usr/local/lib/python3.6/dist-packages/torch/_utils.py in reraise(self)
    393             # (https://bugs.python.org/issue2651), so we work around it.
    394             msg = KeyErrorMessage(msg)
--> 395         raise self.exc_type(msg)

TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "<ipython-input-21-cb3ac03ca3d1>", line 30, in __getitem__
    'targets': torch.tensor(target, dtype=torch.long)
TypeError: new(): invalid data type 'str'

my dataset contains 3 columns with types of int64, object, and object.

How can I solve this problem?

AFB
  • 83
  • 1
  • 3
  • 10

2 Answers2

1

Please check your y labels, they should be label encoded and of type int.

from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
df["Label"] = label_encoder.fit_transform(df["Label"])
-1

Cannot comment yet hence posting this as an answer. Be more specific - where's the code for your custom dataset class?
Apparently there's a problem with your 'target' variable. show more code, nobody will be able to help this way.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 20 '22 at 13:03
  • It isn't an answer! It addresses the question by asking the OP to be clearer by showing more code. – SRISHTI GUREJA Jul 21 '22 at 14:04