【翻译】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 typedtypeTensor types
32-bit floating pointtorch.float32 or torch.floattorch.*.FloatTensor
64-bit floating pointtorch.float64 or torch.doubletorch.*.DoubleTensor
16-bit floating pointtorch.float16 or torch.halftorch.*.HalfTensor
8-bit integer (unsigned)torch.uint8torch.*.ByteTensor
8-bit integer (signed)torch.int8torch.*.CharTensor
16-bit integer (signed)torch.int16 or torch.shorttorch.*.ShortTensor
32-bit integer (signed)torch.int32 or torch.inttorch.*.IntTensor
64-bit integer (signed)torch.int64 or torch.longtorch.*.LongTensor
Booleantorch.booltorch.*.BoolTensor

9中数据类型:

Data type 数据类型dtypeTensor types 张量类型
32-bit floating point 32位浮点数torch.float32 or torch.floattorch.*.FloatTensor
64-bit floating point 64位浮点数torch.float64 or torch.doubletorch.*.DoubleTensor
16-bit floating point 16位浮点数torch.float16 or torch.halftorch.*.HalfTensor
8-bit integer (unsigned) 8位无符号整数torch.uint8torch.*.ByteTensor
8-bit integer (signed) 8位有符号整数torch.int8torch.*.CharTensor
16-bit integer (signed) 16位有符号整数torch.int16 or torch.shorttorch.*.ShortTensor
32-bit integer (signed) 32位有符号整数torch.int32 or torch.inttorch.*.IntTensor
64-bit integer (signed) 64位有符号整数torch.int64 or torch.longtorch.*.LongTensor
Boolean 布尔类型torch.booltorch.*.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]])
>>>
>>>
>>>
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值