-
判断GPU是否可用
pythontorch.cuda.is_available() -
张量
Torch 定义了 10 种张量类型,包括 CPU 和 GPU 形式,如下表所示:
数据类型 dtype CPU张量 GPU张量 32位浮点数 torch.float32、torch.float torch.FloatTensor torch.cuda.FloatTensor 64位浮点数 torch.float64、torch.double torch. DoubleTensor torch.cuda.DoubleTensor 16位浮点数 torch.float16、torch.half torch.HalfTensor torch.cuda.HalfTensor 16位浮点数 torch.bfloat16 torch.BFloat16Tensor torch.cuda.BFloat16Tensor 32位复数 torch.complex32、torch.chalf / / 64位复数 torch.complex64、torch.cfloat torch.complex / 128位复数 torch.complex128、torch.cdouble / / 8位整型(无符号) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor 8位整型(有符号) torch.int8 torch.CharTensor torch.cuda.CharTensor 16位整型(有符号) torch.int16、torch.short torch.ShortTensor torch.cuda.ShortTensor 32位整型(有符号) torch.int32、torch.int torch.IntTensor torch.cuda.IntTensor 64位整型(有符号) torch.int64、torch.long torch.LongTensor torch.cuda.LongTensor 布尔 torch.bool torch.BoolTensor torch.cuda.BoolTensor 量化的8位整型(无符号) torch.quint8 torch.ByteTensor / 量化8位整型(有符号) torch.qint8 torch.CharTensor / 量化的32位整型(有符号) torch.qint32 torch.IntTensor / 量化4位整型(无符号) torch.quint4x2 torch.ByteTensor / torch.tensor()和torch.Tensor()的区别:
torch.tensor 是一个工厂函数,它接收多种类型的输入,包括原始数据(如列表、元组或张量)、数据类型和计算设备的指定以及是否需要开启自动求导功能的指示。它的返回值总是新创建的一个张量,即使输入本身就是张量。
torch.Tensor 是一个类,它用于创建空的张量。它的参数可以是张量的形状(shape)或者是另一个张量。该类的实例化不会改变输入张量的内容,而是返回一个新的张量对象。
虽然 torch.tensor 和 torch.Tensor 在功能上是相似的,都用于创建PyTorch中的张量,但 torch.Tensor 作为类,提供了更多灵活性,并且在使用时可以避免一些复杂的类型推断问题。
torch中默认的数据类型为32位浮点型
pythontensor1 = torch.Tensor(5) tensor1.dtype输出:
pythontorch.float32更改默认数据类型:
pythontorch.set_default_tensor_type(torch.FloatTensor)获取默认数据类型:
pythontorch.get_default_dtype()输出:
pythontorch.float32初始化张量
通过传入List初始化:
pythontensor2 = torch.Tensor([[1, 2], [3, 4]]) tensor2输出:
pythontensor([[1., 2.], [3., 4.]])通过传入数组初始化:
array = np.array([[5, 6], [7, 8]]) tensor3 = torch.Tensor(array) tensor3输出:
pythontensor([[5., 6.], [7., 8.]])按类型初始化:
python# 32位浮点数 tensor1 = torch.FloatTensor([1]) # 64位浮点数 tensor2 = torch.DoubleTensor([2]) # 16位浮点数 tensor3 = torch.HalfTensor([3]) # 32位整型 tensor4 = torch.IntTensor([4]) # 64位整型 tensor5 = torch.LongTensor([5]) # 布尔类型 tensor6 = torch.BoolTensor([0, 1, 0, 1]) print(f"tensor1:{tensor1}") print(f"tensor2:{tensor2}") print(f"tensor3:{tensor3}") print(f"tensor4:{tensor4}") print(f"tensor5:{tensor5}") print(f"tensor6:{tensor6}")输出:
pythontensor1:tensor([1.]) tensor2:tensor([2.], dtype=torch.float64) tensor3:tensor([3.], dtype=torch.float16) tensor4:tensor([4], dtype=torch.int32) tensor5:tensor([5]) tensor6:tensor([False, True, False, True])构造全 0 全 1 张量并初始化类型:
python# 构造全0张量 并设置类型为torch.float32 zerotensor = torch.zeros([3, 6], dtype=torch.float32) # 构造全1张量 并设置类型为torch.int32 onetensor = torch.ones([3, 6], dtype=torch.int32) print(zerotensor) print(onetensor)输出:
pythontensor([[0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.]]) tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]], dtype=torch.int32)创建相同大小的全 1 或全 0 张量
python# 创建相同大小全1张量 tensor1 = torch.Tensor([[1, 2, 3], [4, 5, 6]]) likeone = torch.ones_like(tensor1) # 创建相同大小全0张量 likezero = torch.zeros_like(tensor1) print(tensor1) print(likeone) print(likezero)输出:
pythontensor([[1., 2., 3.], [4., 5., 6.]]) tensor([[1., 1., 1.], [1., 1., 1.]]) tensor([[0., 0., 0.], [0., 0., 0.]])生成同纬度随机张量
python# 生成同纬度随机张量 rantensor = torch.rand_like(tensor1) print(rantensor)输出:
pythontensor([[0.1340, 0.2402, 0.7677], [0.6867, 0.7134, 0.0426]])使用new_full填充张量
pythonnewfull = tensor1.new_full((3, 3), fill_value=8) print(newfull)输出:
pythontensor([[8., 8., 8.], [8., 8., 8.], [8., 8., 8.]])使用new_zeros构建全 0 张量
python# 使用new_zeros构建全0张量 newzeros = tensor1.new_zeros((3, 3)) print(newzeros)输出:
pythontensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])使用new_ones构建全 1 张量
python# 使用new_ones构建全0张量 newones = tensor1.new_ones((3, 3)) print(newones)输出:
pythontensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]])使用new_empty构建全 空 张量
python# 使用new_empty构建全0张量 newempty = tensor1.new_empty((3, 3)) print(newempty)输出:
pythontensor([[8., 8., 8.], [8., 8., 8.], [8., 8., 8.]])张量的操作:
张量的类型转换(两种方式):
pythontensor1 = torch.Tensor([1, 2, 3]) print(tensor1.dtype) # 转换成整型的第一种方法 print(tensor1.int().dtype) # 转换成整形的第二种方法 print(tensor1.to(dtype=torch.int32).dtyp输出:
pythontorch.float32 torch.int32 torch.int32获取张量的值(只能获取一个数):
pythontensor1[0].item()张量转换成数组:
pythonarray = tensor1.numpy()数组转换为张量:
pythonarray = np.ones((3, 3)) tensor1 = torch.as_tensor(array) tensor2 = torch.from_numpy(array) print(tensor1) print(tensor2)输出:
pythontensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64) tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64)
基于PyTorch深度学习实战入门系列-PyTorch基础上
神奇的布欧2024-03-25 23:16
相关推荐
莫非王土也非王臣4 分钟前
循环神经网络Java后端的Ai之路7 分钟前
【AI大模型开发】-基于 Word2Vec 的中文古典小说词向量分析实战Lips61111 分钟前
第五章 神经网络(含反向传播计算)gongfuyd11 分钟前
《强化学习的数学原理》中文版第2章-第4章总结愚公搬代码18 分钟前
【愚公系列】《AI+直播营销》032-主播的选拔和人设设计(主播人设打造的4个要素)love530love18 分钟前
告别环境崩溃:ONNX 与 Protobuf 版本兼容性指南gorgeous(๑><๑)21 分钟前
【电子科大-李晶晶组-AAAI26】利用专用提示引导泛化视觉 - 语言模型程序员果子28 分钟前
GraphRAG:让大模型在知识图谱中精准导航Code-world-142 分钟前
NVIDIA Isaac Sim 安装教程