如何用PyTorch判断变量是否在CUDA上,并识别其数据类型?

2026-05-22 00:491阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计496个文字,预计阅读时间需要2分钟。

如何用PyTorch判断变量是否在CUDA上,并识别其数据类型?

我简单改写如下:

不多说,直接看代码吧!输入:inputs=Variable(torch.randn(2, 2))

我就废话不多说了,那就直接看代码吧~

inputs = Variable(torch.randn(2,2)) inputs.is_cuda # will return false inputs = Variable(torch.randn(2,2).cuda()) inputs.is_cuda # returns true

判断:

torch.is_tensor() #如果是pytorch的tensor类型返回true

torch.is_storage() # 如果是pytorch的storage类型返回ture

这里还有一个小技巧,如果需要判断tensor是否为空,可以如下

>>> a=torch.Tensor() >>> len(a) 0 >>> len(a) is 0 True

设置:通过一些内置函数,可以实现对tensor的精度, 类型,print打印参数等进行设置

torch.set_default_dtype(d) #对torch.tensor() 设置默认的浮点类型 torch.set_default_tensor_type() # 同上,对torch.tensor()设置默认的tensor类型 >>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32 torch.float32 >>> torch.set_default_dtype(torch.float64) >>> torch.tensor([1.2, 3]).dtype # a new floating point tensor torch.float64 >>> torch.set_default_tensor_type(torch.DoubleTensor) >>> torch.tensor([1.2, 3]).dtype # a new floating point tensor torch.float64 torch.get_default_dtype() #获得当前默认的浮点类型torch.dtype torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)#) ## 设置printing的打印参数

判断变量类型:下面两种方法都行

if isinstance(downsample, torch.nn.Module):
# if torch.type(downsample) != torch.IntTensor:

补充知识:pytorch:测试GPU是否可用

废话不多说,看代码吧~

import torch flag = torch.cuda.is_available() print(flag) ngpu= 1 # Decide which device we want to run on device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") print(device) print(torch.cuda.get_device_name(0)) print(torch.rand(3,3).cuda())

True cuda:0 GeForce GTX 1080 tensor([[0.9530, 0.4746, 0.9819], [0.7192, 0.9427, 0.6768], [0.8594, 0.9490, 0.6551]], device='cuda:0')

以上这篇pytorch判断是否cuda 判断变量类型方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

如何用PyTorch判断变量是否在CUDA上,并识别其数据类型?

本文共计496个文字,预计阅读时间需要2分钟。

如何用PyTorch判断变量是否在CUDA上,并识别其数据类型?

我简单改写如下:

不多说,直接看代码吧!输入:inputs=Variable(torch.randn(2, 2))

我就废话不多说了,那就直接看代码吧~

inputs = Variable(torch.randn(2,2)) inputs.is_cuda # will return false inputs = Variable(torch.randn(2,2).cuda()) inputs.is_cuda # returns true

判断:

torch.is_tensor() #如果是pytorch的tensor类型返回true

torch.is_storage() # 如果是pytorch的storage类型返回ture

这里还有一个小技巧,如果需要判断tensor是否为空,可以如下

>>> a=torch.Tensor() >>> len(a) 0 >>> len(a) is 0 True

设置:通过一些内置函数,可以实现对tensor的精度, 类型,print打印参数等进行设置

torch.set_default_dtype(d) #对torch.tensor() 设置默认的浮点类型 torch.set_default_tensor_type() # 同上,对torch.tensor()设置默认的tensor类型 >>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32 torch.float32 >>> torch.set_default_dtype(torch.float64) >>> torch.tensor([1.2, 3]).dtype # a new floating point tensor torch.float64 >>> torch.set_default_tensor_type(torch.DoubleTensor) >>> torch.tensor([1.2, 3]).dtype # a new floating point tensor torch.float64 torch.get_default_dtype() #获得当前默认的浮点类型torch.dtype torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)#) ## 设置printing的打印参数

判断变量类型:下面两种方法都行

if isinstance(downsample, torch.nn.Module):
# if torch.type(downsample) != torch.IntTensor:

补充知识:pytorch:测试GPU是否可用

废话不多说,看代码吧~

import torch flag = torch.cuda.is_available() print(flag) ngpu= 1 # Decide which device we want to run on device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") print(device) print(torch.cuda.get_device_name(0)) print(torch.rand(3,3).cuda())

True cuda:0 GeForce GTX 1080 tensor([[0.9530, 0.4746, 0.9819], [0.7192, 0.9427, 0.6768], [0.8594, 0.9490, 0.6551]], device='cuda:0')

以上这篇pytorch判断是否cuda 判断变量类型方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

如何用PyTorch判断变量是否在CUDA上,并识别其数据类型?