GPU在大约16秒内训练了这个网络。 CPU大约需要13秒。 (我取消评论/评论进行测试的适当行)。谁能看到我的代码或pytorch安装有什么问题吗? (我已经检查了GPU是否可用,以及GPU上是否有足够的可用内存。
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
print(accelerator)
!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.0-{platform}-linux_x86_64.whl torchvision
print("done")
#########################
import torch
from datetime import datetime
startTime = datetime.now()
dtype = torch.float
device = torch.device("cpu") # Comment this to run on GPU
# device = torch.device("cuda:0") # Uncomment this to run on GPU
# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1024, 128, 8
# Create random Tensors to hold input and outputs.
x = torch.randn(N, D_in, device=device, dtype=dtype)
t = torch.randn(N, D_out, device=device, dtype=dtype)
# Create random Tensors for weights.
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)
w3 = torch.randn(D_out, D_out, device=device, dtype=dtype, requires_grad=True)
learning_rate = 1e-9
for i in range(10000):
y_pred = x.mm(w1).clamp(min=0).mm(w2).clamp(min=0).mm(w3)
loss = (y_pred - t).pow(2).sum()
if i % 1000 == 0:
print(i, loss.item())
loss.backward()
# Manually update weights using gradient descent
with torch.no_grad():
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad
# Manually zero the gradients after updating weights
w1.grad.zero_()
w2.grad.zero_()
print(datetime.now() - startTime)
答案 0 :(得分:6)
我看到您正在计时不应该计时的事物(dtype,device等的定义)。在这里,有趣的是创建输入,输出和权重张量。
startTime = datetime.now()
# Create random Tensors to hold input and outputs.
x = torch.randn(N, D_in, device=device, dtype=dtype)
t = torch.randn(N, D_out, device=device, dtype=dtype)
torch.cuda.synchronize()
print(datetime.now()-startTime)
# Create random Tensors for weights.
startTime = datetime.now()
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)
w3 = torch.randn(D_out, D_out, device=device, dtype=dtype, requires_grad=True)
torch.cuda.synchronize()
print(datetime.now()-startTime)
和训练循环
startTime = datetime.now()
for i in range(10000):
y_pred = x.mm(w1).clamp(min=0).mm(w2).clamp(min=0).mm(w3)
loss = (y_pred - t).pow(2).sum()
if i % 1000 == 0:
print(i, loss.item())
loss.backward()
# Manually update weights using gradient descent
with torch.no_grad():
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad
# Manually zero the gradients after updating weights
w1.grad.zero_()
w2.grad.zero_()
torch.cuda.synchronize()
print(datetime.now() - startTime)
我在装有GTX1080和非常好的CPU的机器上运行它,因此绝对计时较低,但是说明仍然有效。如果您打开Jupyter笔记本并在CPU上运行它:
0:00:00.001786 time to create input/output tensors
0:00:00.003359 time to create weight tensors
0:00:04.030797 time to run training loop
现在您将设备设置为cuda
,我们将其称为“冷启动”(此笔记本中的GPU以前没有运行过任何操作)
0:00:03.180510 time to create input/output tensors
0:00:00.000642 time to create weight tensors
0:00:03.534751 time to run training loop
您看到运行训练循环的时间减少了少量,但由于需要将张量从CPU移至GPU RAM,因此开销为3秒。
如果在不关闭Jupyter笔记本的情况下再次运行它:
0:00:00.000421 time to create input/output tensors
0:00:00.000733 time to create weight tensors
0:00:03.501581 time to run training loop
开销消失了,因为Pytorch使用caching memory allocator加快了速度。
您会注意到,训练循环上的加速非常小,这是因为您正在执行的操作是在非常小的张量上进行的。在处理小型体系结构和数据时,我总是进行快速测试,以查看是否可以通过在GPU上运行来实际获得任何收益。
例如,如果我设置N, D_in, H, D_out = 64, 5000, 5000, 8
,则训练循环在GTX1080上运行3.5秒,在CPU上运行85秒。