1. 设置设备
# 定义训练的设备device = torch.device("cuda" if torch.cuda.is_available else "cpu")
2. 将模型和优化器放到设备上
model = Classify()# 将模型放到设备上model = model.to(device)loss_fn = nn.CrossEntropyLoss()# 将损失函数放到设备上loss_fn = loss_fn.to(device)
3. 将数据(imgs,targets)放到设备上
# 将imgs和targets放到设备上去imgs = imgs.to(device)targets = targets.to(device)
