to(device)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
img = img.to(device)
将所有最开始读取数据时的tensor变量copy一份到device所指定的GPU上去,之后的运算都在GPU上进行。
多个GPU:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = Model() if torch.cuda.device_count() > 1: model = nn.DataParallel(model,device_ids=[0,1,2]) model.to(device)
to(device)最先出现在Python成神之路。
共有 0 条评论