简体   繁体   English

如何在 torch.nn.Conv2d() function 中使用 GPU

[英]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.

相关问题 torch.nn.conv2d 中参数的含义 - Meaning of parameters in torch.nn.conv2d torch.nn.conv2d 和 torch.nn.Linear 中的值 - values in torch.nn.conv2d and torch.nn.Linear torch.nn.conv2d 给出的结果与 torch.nn.functional.conv2d 不同 - torch.nn.conv2d does not give the same result as torch.nn.functional.conv2d PyTorch用torch.nn.functional.conv2d替换torch.nn.Conv2d - PyTorch replace torch.nn.Conv2d with torch.nn.functional.conv2d 为什么 torch.nn.Conv2d() 将图像分成 9 部分? - Why torch.nn.Conv2d() divides the image into 9 parts? 如何在PyTorch中的torch.nn.Conv2d层之后获取5维输出? - How to get a 5-Dimensional output after torch.nn.Conv2d layer in PyTorch? 为什么 torch.nn.Conv2d 在 '(n, n)' 和 'n' 参数之间有不同的结果? - Why torch.nn.Conv2d has different result between '(n, n)' and 'n' arguments? PyTorch:使用 torch.nn.Conv2d 卷积单通道图像 - PyTorch: Convolving a single channel image using torch.nn.Conv2d 为什么在执行 torch.nn.conv2d 操作时 requires_grad 从真变为假? - Why does requires_grad turns from true to false when doing torch.nn.conv2d operation? `Torch`中的卷积层(即`nn.SpatialConvolution`)和`Pytorch`中的卷积层(即`torch.nn.Conv2d`)有什么不同 - What's different between convolution layer in `Torch`(i.e `nn.SpatialConvolution`) and convolution layer in `Pytorch`(i.e `torch.nn.Conv2d`)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM