• 1-Pytorch初始化张量和张量的类型


    1-Pytorch初始化张量和张量的类型

    1 导入必备库

    import torch
    import numpy as np
    
    • 1
    • 2

    2 初始化张量

    # 初始化张量
    t = torch.tensor([1,2])#.type(torch.FloatTensor)
    print(t)
    print(t.dtype)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    tensor([1, 2])
    torch.int64
    
    • 1
    • 2

    3 创建float型张量

    # 创建float型张量
    t = torch.FloatTensor([1,2])
    print(t)
    print(t.dtype)
    
    t = torch.LongTensor([1,2])#int型
    print(t)
    print(t.dtype)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    输出:

    tensor([1., 2.])
    torch.float32
    tensor([1, 2])
    torch.int64
    
    • 1
    • 2
    • 3
    • 4

    4 从Numpy数组ndarray创建张量

    # 从Numpy数组ndarray创建张量
    np_array = np.array([[1,2],[3,4]])
    t_np = torch.from_numpy(np_array)#.type(torch.int32)
    print(t_np)
    
    '''张量的ndarray类型主要包含:
        32位浮点型:torch.float32/torh.float(常用),相当于torch.FloatTensor
        64位浮点型:torch.float64
        16位浮点型:torch.float16
        64位整型:torch.in64/torch.long(常用),相当于torch.LongTensor
        32位整型:torch.int32
        16位整型:torch.int16
        8位整型:torch.int8
    '''
    print(torch.float == torch.float32)
    print(torch.long == torch.int64)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    输出:

    tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    True
    True
    
    • 1
    • 2
    • 3
    • 4

    5 构建张量时用dtype明确其类型,或者用type

    # 构建张量时用dtype明确其类型,或者用type
    t = torch.tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    print(t)
    print(t.dtype)
    
    t = torch.tensor([[1, 2],
            [3, 4]]).type(torch.int32)
    print(t)
    print(t.dtype)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出:

    tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    torch.int32
    tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    torch.int32
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6 等价转换int64和float32

    t = torch.tensor([[1, 2],
            [3, 4]]).type(torch.int32)
    print(t)
    print(t.dtype)
    
    t = t.long()    #等同int64
    print(t)
    print(t.dtype)
    
    t = t.float()   #等同float32
    print(t)
    print(t.dtype)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出:

    tensor([[1, 2],
            [3, 4]], dtype=torch.int32)
    torch.int32
    tensor([[1, 2],
            [3, 4]])
    torch.int64
    tensor([[1., 2.],
            [3., 4.]])
    torch.float32
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    centos中安装的goland配置sdk报错:所选的目录不是Go SDK的有效主路经
    11.27学术报告听讲笔记
    开发JSP应用的基础知识
    进制A+B [牛客网]
    C语言宏定义提供了一些进阶操作
    OpenGL之着色器
    C++11智能指针shared_ptr介绍和辅助函数
    电脑操作系统你真的搞懂了分类?(第五十三课)
    很搞笑的片段
    已知中序遍历数组和先序遍历数组,返回后序遗历数组
  • 原文地址:https://blog.csdn.net/m0_46256255/article/details/132778631