首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

【从矩阵到图像的类型转换1】:Pytorch中tensor的类型、数据类型以及数据类型间的转换

  • 25-03-03 18:41
  • 4299
  • 5796
blog.csdn.net

一、Pytorch中tensor的类型与数据类型

torch.Tensor是一种包含元素的多维矩阵,但这些元素属于单一数据类型元素(与list不同,list可以在同一序列中存储不一样类型的元素)。

1.0 默认类型与数据类型

torch.Tensor是默认的tensor类型(torch.FlaotTensor)的简称,并且默认放置于CPU上,使用该类型声明的时候会生成类型为torch.FloatTensor,数据类型为torch.float32的数据。

torch.ensor是默认的tensor类型(torch.LongTensor)的简称,并且默认放置于CPU上,使用该类型声明的时候会生成类型为torch.LongTensor,数据类型为torch.int64的数据。

注意:不存在torch.cuda.Tensor或者torch.cuda.tensor

import torch
print("测试开始")

print("==默认Tensor数据类型==")
tensor= torch.Tensor([1,2])
print("type(tensor):",type(tensor))
print("tensor.type():",tensor.type())
print("tensor.dtype:",tensor.dtype)
print("tensor.is_cuda:",tensor.is_cuda)


print("==默认tensor数据类型==")
tensor= torch.tensor([1,2])
print("type(tensor):",type(tensor))
print("tensor.type():",tensor.type())
print("tensor.dtype:",tensor.dtype)
print("tensor.is_cuda:",tensor.is_cuda)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

image-20210318093904908

1.1 tensor类型与数据类型总览

Pytorch中定义了8种CPU张量类型和对应的8种GPU张量类型,(相同的CPU张量,GPU张量类型分别对应的存储数据类型相同),具体对应关系见下表:

Data typeCPU tensorGPU tensor
32-bit floating point -> torch.float32torch.FloatTensortorch.cuda.FloatTensor
64-bit floating point -> torch.float64torch.DoubleTensortorch.cuda.DoubleTensor
16-bit floating point -> torch.float16torch.HalfTensortorch.cuda.HalfTensor
8-bit integer (unsigned) -> torch.uint8torch.ByteTensortorch.cuda.ByteTensor
8-bit integer (signed) -> torch.int8torch.CharTensortorch.cuda.CharTensor
16-bit integer (signed) -> torch.int16torch.ShortTensortorch.cuda.ShortTensor
32-bit integer (signed) -> torch.int32torch.IntTensortorch.cuda.IntTensor
64-bit integer (signed) -> torch.int64torch.LongTensortorch.cuda.LongTensor

1.2 tensor类型与数据类型的显示

针对这16种tensor变量使用type()函数得出的结果都是

求解具体的张量类型需要使用,tensor_name.type()

求解对应的数据存储类型需要使用,tensor_name.dtype

举例:见1.0

1.3 tensor数据是否属于cuda显示

求解具体tensor是否属于cuda类型需要使用,tensor_name.iscuda

举例:见1.0

1.4 举例:16种类型的矩阵初始化和数据类型显示

import torch
print("测试开始")

print("===torch.FloatTensor===")
tensor = torch.FloatTensor([1,2])
print("type(FloatTensor):",type(tensor))
print("FloatTensor.type():",tensor.type())
print("FloatTensor.dtype:",tensor.dtype)
print("FloatTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.FloatTensor===")
tensor = torch.cuda.FloatTensor([1,2])
print("type(cuda_FloatTensor):",type(tensor))
print("cuda_FloatTensor.type():",tensor.type())
print("cuda_FloatTensor.dtype:",tensor.dtype)
print("cuda_FloatTensor.is_cuda:",tensor.is_cuda)

print("===torch.DoubleTensor===")
tensor = torch.DoubleTensor([1,2])
print("type(DoubleTensor):",type(tensor))
print("DoubleTensor.type():",tensor.type())
print("DoubleTensor.dtype:",tensor.dtype)
print("DoubleTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.DoubleTensor===")
tensor = torch.cuda.DoubleTensor([1,2])
print("type(cuda_DoubleTensor):",type(tensor))
print("cuda_DoubleTensor.type():",tensor.type())
print("cuda_DoubleTensor.dtype:",tensor.dtype)
print("cuda_DoubleTensor.is_cuda:",tensor.is_cuda)

print("===torch.HalfTensor===")
tensor = torch.HalfTensor([1,2])
print("type(cuda_DoubleTensor):",type(tensor))
print("HalfTensor.type():",tensor.type())
print("HalfTensor.dtype:",tensor.dtype)
print("HalfTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.HalfTensor===")
tensor = torch.cuda.HalfTensor([1,2])
print("type(cuda_HalfTensor):",type(tensor))
print("cuda_HalfTensor.type():",tensor.type())
print("cuda_HalfTensor.dtype:",tensor.dtype)
print("cuda_HalfTensor.is_cuda:",tensor.is_cuda)

print("===torch.ByteTensor===")
tensor = torch.ByteTensor([1,2])
print("type(ByteTensor):",type(tensor))
print("ByteTensor.type():",tensor.type())
print("ByteTensor.dtype:",tensor.dtype)
print("ByteTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.ByteTensor===")
tensor = torch.cuda.ByteTensor([1,2])
print("type(cuda_ByteTensor):",type(tensor))
print("cuda_ByteTensor.type():",tensor.type())
print("cuda_ByteTensor.dtype:",tensor.dtype)
print("cuda_ByteTensor.is_cuda:",tensor.is_cuda)

print("===torch.CharTensor===")
tensor = torch.CharTensor([1,2])
print("type(CharTensor):",type(tensor))
print("CharTensor.type():",tensor.type())
print("CharTensor.dtype:",tensor.dtype)
print("CharTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.CharTensor===")
tensor = torch.cuda.CharTensor([1,2])
print("type(cuda_CharTensor):",type(tensor))
print("cuda_CharTensor.type():",tensor.type())
print("cuda_CharTensor.dtype:",tensor.dtype)
print("cuda_CharTensor.is_cuda:",tensor.is_cuda)

print("===torch.ShortTensor===")
tensor = torch.ShortTensor([1,2])
print("type(ShortTensor):",type(tensor))
print("ShortTensor.type():",tensor.type())
print("ShortTensor.dtype:",tensor.dtype)
print("ShortTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.ShortTensor===")
tensor = torch.cuda.ShortTensor([1,2])
print("type(cuda_ShortTensor):",type(tensor))
print("cuda_ShortTensor.type():",tensor.type())
print("cuda_ShortTensor.dtype:",tensor.dtype)
print("cuda_ShortTensor.is_cuda:",tensor.is_cuda)

print("===torch.IntTensor===")
tensor = torch.IntTensor([1,2])
print("type(IntTensor):",type(tensor))
print("IntTensor.type():",tensor.type())
print("IntTensor.dtype:",tensor.dtype)
print("IntTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.IntTensor===")
tensor = torch.cuda.IntTensor([1,2])
print("type(cuda_IntTensor):",type(tensor))
print("cuda_IntTensor.type():",tensor.type())
print("cuda_IntTensor.dtype:",tensor.dtype)
print("cuda_IntTensor.is_cuda:",tensor.is_cuda)

print("===torch.LongTensor===")
tensor = torch.LongTensor([1,2])
print("type(LongTensor):",type(tensor))
print("LongTensor.type():",tensor.type())
print("LongTensor.dtype:",tensor.dtype)
print("LongTensor.is_cuda:",tensor.is_cuda)
print("===torch.cuda.LongTensor===")
tensor = torch.cuda.LongTensor([1,2])
print("type(cuda_LongTensor):",type(tensor))
print("cuda_LongTensor.type():",tensor.type())
print("cuda_LongTensor.dtype:",tensor.dtype)
print("cuda_LongTensor.is_cuda:",tensor.is_cuda)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

image-20210317222913066

二、tensor数据类型间的转换

2.0 数据转换的必要

相同类型的tensor才能做运算,是否在CPU和GPU同种,是否有相同的数据存储类型

2.1 CPU张量类型和GPU张量类型的转换

cpu_tensor.cuda()
gpu_tensor.cpu()
  • 1
  • 2

举例

import torch
print("测试开始")

print("==默认数据类型==")
tensor= torch.FloatTensor([1,2])
print("初始化CPU张量是否在GPU上:",tensor.is_cuda)
tensor.cuda()
print("CPU张量使用.cuda()后是否在GPU上:",tensor.is_cuda)
tensor = tensor.cuda()
print("CPU张量使用.cuda()并重新赋值后是否在GPU上:",tensor.is_cuda)

print("="*10)
tensor= torch.cuda.FloatTensor([1,2])
print("初始化GPU张量是否在GPU上:",tensor.is_cuda)
tensor.cpu()
print("GPU张量使用.cpu()并重新赋值后是否在GPU上:",tensor.is_cuda)
tensor = tensor.cpu()
print("GPU张量使用.cpu()并重新赋值后是否在GPU上:",tensor.is_cuda)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

image-20210317203626826

2.2 不涉及到CPU与GPU的其它数据类型转换

2.2.1 tensor.int()将该tensor投射为int类型

八种CPU类型全部转换成torch.IntTensor,数据存储类型为torch.int32类型

八种GPU类型全部转换成torch.cuda.IntTensor,数据存储类型为torch.int32类型

2.2.2 tensor.long() 将tensor投射为long类型

八种CPU类型全部转换成torch.LongTensor,数据存储类型为torch.int64类型

八种GPU类型全部转换成torch.cuda.LongTensor,数据存储类型为torch.int64类型

2.2.3 tensor.half()将tensor投射为半精度浮点类型

八种CPU类型全部转换成torch.HalfTensor,数据存储类型为torch.float16类型

八种GPU类型全部转换成torch.cuda.HalfTensor,数据存储类型为torch.float16类型

2.2.4 tensor.double()将该tensor投射为double类型

八种CPU类型全部转换成torch.DoubleTensor,数据存储类型为torch.float64类型

八种GPU类型全部转换成torch.cuda.DoubleTensor,数据存储类型为torch.float64类型

2.2.5 tensor.float()将该tensor投射为float类型

八种CPU类型全部转换成torch.FloatTensor,数据存储类型为torch.float32类型

八种GPU类型全部转换成 torch.cuda.FloatTensor,数据存储类型为torch.float32类型

2.2.6 tensor.char()将该tensor投射为char类型

八种CPU类型全部转换成torch.CharTensor,数据存储类型为torch.int8类型

八种GPU类型全部转换成torch.cuda.CharTensor,数据存储类型为torch.int8类型

2.2.7 tensor.byte()将该tensor投射为byte类型

八种CPU类型全部转换成torch.ByteTensor,数据存储类型为torch.uint8类型

八种GPU类型全部转换成torch.cuda.ByteTensor,数据存储类型为torch.uint8类型

2.2.8 tensor.short()将tensor投射为short类型

八种CPU类型全部转换成torch.ShortTensor,数据存储类型为torch.int16类型

八种GPU类型全部转换成torch.cuda.ShortTensor,数据存储类型为torch.int16类型

2.3 使用tensor.type()函数

这个函数的作用是将该tensor转换为另一个tensor的type,可以同步完成转换CPU类型和GPU类型,如torch.IntTensor–>torch.cuda.floatTensor

语法如下:

tensor_new = tensor.type(new_type=None, async=False)
  • 1

举例:

import torch
print("测试开始")
tensor = torch.FloatTensor([1,2])
print("before type function:",tensor.type())
tensor = tensor.type(torch.int32)
print("after type function:",tensor.type())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

image-20210318001735082

2.4 使用type_as(tesnor)将张量转换为给定类型的张量

这个函数的作用是将该tensor转换为另一个tensor的type,可以同步完成转换CPU类型和GPU类型,如torch.IntTensor–>torch.cuda.floatTensor

如果张量已经是指定类型,则不会进行转换

语法如下:

tensor_new = tensor1.type_as(tensor2)
  • 1

举例:

import torch
print("测试开始")
tensor1 = torch.FloatTensor([1,2])
tensor2 = torch.IntTensor([1,2])
print("before type_as function:",tensor1.type())
tensor1 = tensor1.type_as(tensor2)
print("after type function:",tensor1.type())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果:
image-20210318001826667

LAST、参考文献

pytorch判断变量类型和是否是cuda_怡宝2号-CSDN博客
torch.Tensor - PyTorch中文文档
pytorch: tensor类型的构建与相互转换_JNing-CSDN博客
Pytorch中支持的tensor的数据类型及它们的相互转换 - 知乎
pytorch知识一tensor数据声明、类型转换。微调rensnet34的注意点。_yangdeshun888的博客-CSDN博客
Pytorch 基本的数据类型_洪流之源-CSDN博客_pytorch 数据类型

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树人工智能深度学习416632 人正在系统学习中
注:本文转载自blog.csdn.net的呆呆象呆呆的文章"https://blog.csdn.net/qq_41554005/article/details/114964670"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top