张量的实际原始数据并不是立即保存在张量结构中,而是保存在我们称之为「存储(Storage)」的地方,它是张量结构的一部分;一般张量存储可以通过 Allocator 选择是储存在计算机内存(CPU)还是显存(GPU)
参考 https://blog.csdn.net/gaishi_hero/article/details/81139045
# 指定使用第几张卡
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# 第一行代码
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 第二行代码
model.to(device)
mytensor = my_tensor.to(device)
use_cuda = True
if use_cuda and torch.cuda.is_available():
print('cuda ready...')
device = 'cuda:0'