next(iter(..))函数生成错误

时间:2020-06-20 08:16:11

标签: python model iterator typeerror dataloader

我将这些代码行应用到我的模型中

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

data =next(iter(train_data_loader))

但是我遇到了这个错误

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'

我的数据集包含3列,类型分别为int64,对象和对象。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

请检查您的y标签,它们应为标签编码且类型为int。

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