.to(device) 可以指定CPU 或者GPU
device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”) # 单GPU或者CPU
model.to(device)
#如果是多GPU
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model,device_ids=[0,1,2])
model.to(device)
.cuda()只使用GPU,建议使用to
targets=targets.cuda(non_blocking=True)#non_blocking=True 进行数据预取,python3.7丢弃了async,改为non_blocking。