[英]How to use GPU in torch.nn.Conv2d() function
I'm not sure if this function that computes convolution uses the GPU, if not, how to modify it to use GPU?我不确定这个计算卷积的 function 是否使用 GPU,如果没有,如何修改它以使用 GPU?
import torch
import time
cuda = torch.device('cuda')
x = torch.randn(1,16,4,4)
t0 = time.time()
conv = torch.nn.Conv2d(16,1,(3,3))
res = conv(x)
torch.cuda.synchronize()
t1 = time.time()
print(t0, t1, t1-t0,"sec")
print(res.shape)
You can type this command at the beginning of your code:您可以在代码开头键入此命令:
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")
If it prints "Using cuda device" means that it is using GPU如果它打印“Using cuda device”表示它正在使用 GPU
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.