码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    Class Activation Mapping(CAM)介绍
    魔改了一下bootstrap-treeview组件,发布个NPM包体验一下
    【产品运营】如何做好B端产品规划
    主流的第三方直播SDK对比(腾讯云、即构、阿里云、声网、网易云信、网宿)
    使用Jedis监听Redis Stream 实现消息队列功能
    JSON Web Tokens攻击漏洞手法
    【数据分析】基于Matlab检测多元数据集中的异常值
    Spark - 第13章 高级RDD
    C++ 学习(20)STL - map容器
    基于PHP+MySQL学院信息发布系统的设计与实现
  • 原文地址:https://blog.csdn.net/m0_46256255/article/details/132778631
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号