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

  1. 判断GPU是否可用

    torch.cuda.is_available()
    
  2. 张量

    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位浮点型
    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)
    

相关推荐

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

    2024-03-22 14:52:03       20 阅读
  2. 基于PyTorch深度学习实战入门系列-PyTorch基础

    2024-03-22 14:52:03       20 阅读
  3. 深度学习Pytorch基础

    2024-03-22 14:52:03       20 阅读
  4. PytorchPytorch入门基础

    2024-03-22 14:52:03       14 阅读
  5. PyTorch深度学习实战(18)——目标检测基础

    2024-03-22 14:52:03       37 阅读
  6. 探索PyTorch深度学习:从基础实践

    2024-03-22 14:52:03       13 阅读
  7. 深度学习(二):pytorch基础知识

    2024-03-22 14:52:03       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 14:52:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 14:52:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 14:52:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 14:52:03       20 阅读

热门阅读

  1. python基础学习第一天

    2024-03-22 14:52:03       19 阅读
  2. 在Hive中使用Python编写的UDF函数

    2024-03-22 14:52:03       18 阅读
  3. Linux shell 命令中nohup 、&、重定向的使用

    2024-03-22 14:52:03       20 阅读
  4. 【Python】Python中装饰器和魔法方法的区别

    2024-03-22 14:52:03       21 阅读
  5. harmonyos:Socket连接

    2024-03-22 14:52:03       19 阅读
  6. 计算机常见的知识点(1)

    2024-03-22 14:52:03       24 阅读