for epoch in range(100): for batch in dataloader: optimizer.zero_grad() x = batch[:, :-1, :] y = batch[:, 1:, :]报错TypeError: list indices must be integers or slices, not tuple
时间: 2024-03-09 19:46:03 浏览: 123
Pytorch-Classification_MNIST:用Pytorch对MNIST数据集进行分类
这个错误很可能是因为你的dataloader返回的是元组而不是列表。你可以尝试将其转换为列表,如下所示:
```
for epoch in range(100):
for batch in dataloader:
optimizer.zero_grad()
batch = list(batch)
x = batch[:, :-1, :]
y = batch[:, 1:, :]
```
或者你可以在创建dataloader时指定返回的元素类型为列表,如下所示:
```
dataloader = DataLoader(dataset, batch_size=32, collate_fn=list)
```
阅读全文