首页 > 解决方案 > next(iter(..)) 函数产生错误

问题描述

我在我的模型中应用此代码行

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、object 和 object。

我怎么解决这个问题?

标签: pythonmodeliteratortypeerrordataloader

解决方案


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

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

推荐阅读