【翻译】torch.dtype

参考链接: torch.dtype

在这里插入图片描述

原文及翻译:

Tensor Attributes  Tensor张量属性

Each torch.Tensor has a torch.dtype, torch.device, and torch.layout.
每个torch.Tensor张量都具有属性: torch.dtype、torch.device和torch.layout.

torch.dtype    torch.dtype属性 

class torch.dtype

A torch.dtype is an object that represents the data type of a 
torch.Tensor. PyTorch has nine different data types:
torch.dtype是一个对象,该对象表示一个torch.Tensor类型对象的数据类型.
PyTorch有九种不同的数据类型.

To find out if a torch.dtype is a floating point data type, the 
property is_floating_point can be used, which returns True if 
the data type is a floating point data type.
为了查看一个torch.dtype对象是否为浮点数据类型,可以使用属性
is_floating_point,即torch.is_floating_point(input),如果数据类型
是浮点数据类型,那么该属性返回的是True.

9中数据类型:

Data type dtype Tensor types
32-bit floating point torch.float32 or torch.float torch.*.FloatTensor
64-bit floating point torch.float64 or torch.double torch.*.DoubleTensor
16-bit floating point torch.float16 or torch.half torch.*.HalfTensor
8-bit integer (unsigned) torch.uint8 torch.*.ByteTensor
8-bit integer (signed) torch.int8 torch.*.CharTensor
16-bit integer (signed) torch.int16 or torch.short torch.*.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.*.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.*.LongTensor
Boolean torch.bool torch.*.BoolTensor

9中数据类型:

Data type 数据类型 dtype Tensor types 张量类型
32-bit floating point 32位浮点数 torch.float32 or torch.float torch.*.FloatTensor
64-bit floating point 64位浮点数 torch.float64 or torch.double torch.*.DoubleTensor
16-bit floating point 16位浮点数 torch.float16 or torch.half torch.*.HalfTensor
8-bit integer (unsigned) 8位无符号整数 torch.uint8 torch.*.ByteTensor
8-bit integer (signed) 8位有符号整数 torch.int8 torch.*.CharTensor
16-bit integer (signed) 16位有符号整数 torch.int16 or torch.short torch.*.ShortTensor
32-bit integer (signed) 32位有符号整数 torch.int32 or torch.int torch.*.IntTensor
64-bit integer (signed) 64位有符号整数 torch.int64 or torch.long torch.*.LongTensor
Boolean 布尔类型 torch.bool torch.*.BoolTensor

代码实验1:

Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000001D69872D330>
>>> a = torch.randn(3,4)
>>> a
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651],
        [ 1.1216,  0.8440,  0.1783,  0.6859]])
>>> a.dtype
torch.float32
>>> type(a)
<class 'torch.Tensor'>
>>> a.type()
'torch.FloatTensor'
>>> b = a.cuda()
>>> b
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651],
        [ 1.1216,  0.8440,  0.1783,  0.6859]], device='cuda:0')
>>> b.dtype
torch.float32
>>> type(b)
<class 'torch.Tensor'>
>>> b.type()
'torch.cuda.FloatTensor'
>>>
>>> a
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651],
        [ 1.1216,  0.8440,  0.1783,  0.6859]])
>>> a.dtype
torch.float32
>>> a.type()
'torch.FloatTensor'
>>> type(a)
<class 'torch.Tensor'>
>>> a
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651],
        [ 1.1216,  0.8440,  0.1783,  0.6859]])
>>>
>>>

在这里插入图片描述
原文及翻译:

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
一个torch.Tensor对象是包含单个数据类型元素的多维矩阵.
Torch defines nine CPU tensor types and nine GPU tensor types:
Torch 内部定义了9种CPU张量类型和9中GPU张量类型.
torch.Tensor is an alias for the default tensor type (torch.FloatTensor).
torch.FloatTensor是默认的张量类型,他具有一个别名,叫torch.Tensor.

代码实验2:创建张量

Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000001A03111D330>
>>>
>>> double_points = torch.ones(4, 2, dtype=torch.double)
>>> short_points = torch.tensor([[1, 2], [3, 4]], dtype=torch.short)
>>> short_points.dtype
torch.int16
>>> double_points.dtype
torch.float64
>>> short_points.type()
'torch.ShortTensor'
>>> double_points.type()
'torch.DoubleTensor'
>>> double_points = torch.zeros(10, 2).double()
>>> short_points = torch.ones(10, 2).short()
>>> short_points.type()
'torch.ShortTensor'
>>> double_points.type()
'torch.DoubleTensor'
>>> double_points = torch.zeros(10, 2)
>>> short_points = torch.ones(10, 2)
>>> short_points.type()
'torch.FloatTensor'
>>> double_points.type()
'torch.FloatTensor'
>>> short_points.dtype
torch.float32
>>> double_points.dtype
torch.float32
>>> torch.ones(4, 2, dtype=torch.int8)
tensor([[1, 1],
        [1, 1],
        [1, 1],
        [1, 1]], dtype=torch.int8)
>>> torch.ones(4, 2, dtype=)
KeyboardInterrupt
>>>
KeyboardInterrupt
>>> torch.ones(4, 2, dtype=torch.int32)
tensor([[1, 1],
        [1, 1],
        [1, 1],
        [1, 1]], dtype=torch.int32)
>>> torch.ones(4, 2, dtype=torch.bool)
tensor([[True, True],
        [True, True],
        [True, True],
        [True, True]])
>>>
>>>
>>>

猜你喜欢

转载自blog.csdn.net/m0_46653437/article/details/112760181