基于PyTorch深度学习实战入门系列-PyTorch基础上

  1. 判断GPU是否可用

    torch.cuda.is_available()
    
  2. 张量

    Torch 定义了 10 种张量类型,包括 CPU 和 GPU 形式,如下表所示:

    数据类型dtypeCPU张量GPU张量
    32位浮点数torch.float32、torch.floattorch.FloatTensortorch.cuda.FloatTensor
    64位浮点数torch.float64、torch.doubletorch. DoubleTensortorch.cuda.DoubleTensor
    16位浮点数torch.float16、torch.halftorch.HalfTensortorch.cuda.HalfTensor
    16位浮点数torch.bfloat16torch.BFloat16Tensortorch.cuda.BFloat16Tensor
    32位复数torch.complex32、torch.chalf//
    64位复数torch.complex64、torch.cfloattorch.complex/
    128位复数torch.complex128、torch.cdouble//
    8位整型(无符号)torch.uint8torch.ByteTensortorch.cuda.ByteTensor
    8位整型(有符号)torch.int8torch.CharTensortorch.cuda.CharTensor
    16位整型(有符号)torch.int16、torch.shorttorch.ShortTensortorch.cuda.ShortTensor
    32位整型(有符号)torch.int32、torch.inttorch.IntTensortorch.cuda.IntTensor
    64位整型(有符号)torch.int64、torch.longtorch.LongTensortorch.cuda.LongTensor
    布尔torch.booltorch.BoolTensortorch.cuda.BoolTensor
    量化的8位整型(无符号)torch.quint8torch.ByteTensor/
    量化8位整型(有符号)torch.qint8torch.CharTensor/
    量化的32位整型(有符号)torch.qint32torch.IntTensor/
    量化4位整型(无符号)torch.quint4x2torch.ByteTensor/
    torch.tensor()和torch.Tensor()的区别:

    torch.tensor 是一个工厂函数,它接收多种类型的输入,包括原始数据(如列表、元组或张量)、数据类型和计算设备的指定以及是否需要开启自动求导功能的指示。它的返回值总是新创建的一个张量,即使输入本身就是张量。

    torch.Tensor 是一个类,它用于创建空的张量。它的参数可以是张量的形状(shape)或者是另一个张量。该类的实例化不会改变输入张量的内容,而是返回一个新的张量对象。

    虽然 torch.tensor 和 torch.Tensor 在功能上是相似的,都用于创建PyTorch中的张量,但 torch.Tensor 作为类,提供了更多灵活性,并且在使用时可以避免一些复杂的类型推断问题。

    torch中默认的数据类型为32位浮点型
    tensor1 = torch.Tensor(5)
    tensor1.dtype
    

    输出:

    torch.float32
    
    更改默认数据类型:
    torch.set_default_tensor_type(torch.FloatTensor)
    
    获取默认数据类型:
    torch.get_default_dtype()
    

    输出:

    torch.float32
    
    初始化张量
    通过传入List初始化:
    tensor2 = torch.Tensor([[1, 2], [3, 4]])
    tensor2
    

    输出:

    tensor([[1., 2.],
            [3., 4.]])
    
    通过传入数组初始化:
    array = np.array([[5, 6], [7, 8]])
    tensor3 = torch.Tensor(array)
    tensor3
    

    输出:

    tensor([[5., 6.],
            [7., 8.]])
    
    按类型初始化:
    # 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}")
    

    输出:

    tensor1: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 张量并初始化类型:
    # 构造全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)
    

    输出:

    tensor([[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 张量
    # 创建相同大小全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)
    

    输出:

    tensor([[1., 2., 3.],
            [4., 5., 6.]])
    tensor([[1., 1., 1.],
            [1., 1., 1.]])
    tensor([[0., 0., 0.],
            [0., 0., 0.]])
    
    生成同纬度随机张量
    # 生成同纬度随机张量
    rantensor = torch.rand_like(tensor1)
    print(rantensor)
    

    输出:

    tensor([[0.1340, 0.2402, 0.7677],
            [0.6867, 0.7134, 0.0426]])
    
    使用new_full填充张量
    newfull = tensor1.new_full((3, 3), fill_value=8)
    print(newfull)
    

    输出:

    tensor([[8., 8., 8.],
            [8., 8., 8.],
            [8., 8., 8.]])
    
    使用new_zeros构建全 0 张量
    # 使用new_zeros构建全0张量
    newzeros = tensor1.new_zeros((3, 3))
    print(newzeros)
    

    输出:

    tensor([[0., 0., 0.],
            [0., 0., 0.],
            [0., 0., 0.]])
    
    使用new_ones构建全 1 张量
    # 使用new_ones构建全0张量
    newones = tensor1.new_ones((3, 3))
    print(newones)
    

    输出:

    tensor([[1., 1., 1.],
            [1., 1., 1.],
            [1., 1., 1.]])
    
    使用new_empty构建全 空 张量
    # 使用new_empty构建全0张量
    newempty = tensor1.new_empty((3, 3))
    print(newempty)
    

    输出:

    tensor([[8., 8., 8.],
            [8., 8., 8.],
            [8., 8., 8.]])
    
    张量的操作:
    张量的类型转换(两种方式):
    tensor1 = torch.Tensor([1, 2, 3])
    print(tensor1.dtype)
    # 转换成整型的第一种方法
    print(tensor1.int().dtype)
    # 转换成整形的第二种方法
    print(tensor1.to(dtype=torch.int32).dtyp
    

    输出:

    torch.float32
    torch.int32
    torch.int32
    
    获取张量的值(只能获取一个数):
    tensor1[0].item()
    
    张量转换成数组:
    array = tensor1.numpy()
    
    数组转换为张量:
    array = np.ones((3, 3))
    tensor1 = torch.as_tensor(array)
    tensor2 = torch.from_numpy(array)
    print(tensor1)
    print(tensor2)
    

    输出:

    tensor([[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)
    
  • 28
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇的布欧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值