还是报错:AttributeError: 'tuple' object has no attribute 'to'
时间: 2023-06-22 10:39:55 浏览: 87
求解报错:AttributeError:module ‘os’ has no attribute ‘exit’
这个错误提示是因为有些元组中不是张量类型,所以不能直接使用 `.to()` 方法进行转移。你可以在训练循环中加入类型判断,只对张量类型进行转移。可以像下面这样修改代码:
```python
for batch_idx, batch_data in enumerate(train_loader):
head = batch_data[0]
tail = batch_data[1]
relation = batch_data[2]
if isinstance(head, torch.Tensor):
head = head.to(device)
if isinstance(tail, torch.Tensor):
tail = tail.to(device)
if isinstance(relation, torch.Tensor):
relation = relation.to(device)
```
这样就可以避免因为元组中出现非张量类型而导致的错误。
阅读全文