我只是在pytorch上的初学者教程,并注意到将张量(基本上与numpy阵列相同)在GPU 上放置张量(基本相同)的众多方法之一与其他方法相比,可疑的数量:
import time
import torch
if torch.cuda.is_available():
print('time =', time.time())
x = torch.randn(4, 4)
device = torch.device("cuda")
print('time =', time.time())
y = torch.ones_like(x, device=device) # directly create a tensor on GPU => 2.5 secs??
print('time =', time.time())
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
a = torch.ones(5)
print(a.cuda())
print('time =', time.time())
else:
print('I recommend you get CUDA to work, my good friend!')
输出(仅限时间):
time = 1551809363.28284
time = 1551809363.282943
time = 1551809365.7204516 # (!)
time = 1551809365.7236063
版本详细信息:
1 CUDA device: GeForce GTX 1050, driver version 415.27
CUDA = 9.0.176
PyTorch = 1.0.0
cuDNN = 7401
Python = 3.5.2
GCC = 5.4.0
OS = Linux Mint 18.3
Linux kernel = 4.15.0-45-generic
您可以看到此操作(" y = ...")比其余组合(.003秒)更长的时间(2.5秒)。我对此感到困惑,因为我希望所有这些方法基本上都这样做。我尝试确保此行中的类型为32位或具有不同的形状,但没有任何改变。
当我重新排序命令时,最高命令的任何命令都需要2.5秒。因此,这使我相信在此处发生的设备的一次性设置,并且未来的GPU分配将更快。