码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 2-Pytorch创建随机值张量


    2-Pytorch创建随机值张量

    1 导入必备库

    import torch
    import numpy as np
    
    • 1
    • 2

    2 使用torch.rand()创建0-1均匀分布的随机数

    t = torch.rand(2,3)
    print(t)
    
    • 1
    • 2

    输出:

    tensor([[0.1452, 0.1435, 0.2124],
            [0.6646, 0.5232, 0.1572]])
    
    • 1
    • 2

    3 使用torch.randn()创建正态分布的随机数

    t = torch.randn(2,3)
    print(t)
    
    • 1
    • 2

    输出:

    tensor([[-0.5325, -0.4938,  0.6301],
            [-0.6313, -0.1412,  2.2926]])
    
    • 1
    • 2

    4 使用torch.zeros()创建全0张量

    t = torch.zeros(3)
    print(t)
    print(t.dtype)
    
    • 1
    • 2
    • 3

    输出:

    tensor([0., 0., 0.])
    torch.float32
    
    • 1
    • 2

    5 使用torch.ones()创建全1张量

    t = torch.ones(3)
    print(t)
    print(t.dtype)
    
    • 1
    • 2
    • 3

    输出:

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

    6 从另一个张量创建新的张量

    x = torch.zeros_like(t)
    print(x)
    x = torch.rand_like(t)
    print(x)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    tensor([0., 0., 0.])
    tensor([0.6516, 0.5740, 0.6379])
    
    • 1
    • 2

    7 张量的属性:
    tensor.shape返回张量的形状,与tensor.size()方法等价,tensor.dtype返回当前张量的类型

    t = torch.ones(2,3,dtype= torch.float64)
    print(t.shape)
    print(t.size())
    print(t.size(1)) # 返回第二维度大小
    print(t.dtype)
    print(t.device)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出:

    torch.Size([2, 3])
    torch.Size([2, 3])
    3
    torch.float64
    cpu
    
    • 1
    • 2
    • 3
    • 4
    • 5

    8 将张量移动到显存,使用tensor.to()方法将张量移动到GPU上

    # 如果GPU可用,将张量移动到显存
    # 设置使用哪块芯片,多块GPU使用逗号隔开
    # import os
    # os.environ['CUDA_VISIBLE_DEVICES'] = '0'    # 多块GPU设置,如:'0,1,2'
    
    if torch.cuda.is_available():
        t = t.to('cuda')
    print(t.device)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    输出:

    cuda:0
    
    • 1

    9 更稳妥的移动显存方法,无论是否有GPU都能保证运行

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')   # 启动cuda
    print("Using {} device".format(device))  # 打印当前设备
    t = t.to(device)
    print(t.device)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    Using cuda device
    cuda:0
    
    • 1
    • 2
  • 相关阅读:
    【uniapp小程序下载】调用uni.uploadfile方法在调试工具里是没有问题的,但是线上版本和体验版就调用不成功,真机调试也没问题
    【44. 状态压缩DP(蒙德里安的梦想)】
    计算机组成原理之计算机系统概论、计算机的发展史、系统总线,三章开篇讲
    OpenFeign
    LeetCode 724. Find Pivot Index / 1991. Find the Middle Index in Array
    Window10运行onnxruntime报错的处理
    Springmvc中对请求的处理
    json字符串转map集合||发送短信阿里&&腾讯
    Nginx的index.html文件内容+格式语法
    【源码+文档+调试】springboot文化传承小程序的设计与实现小程序源码分享
  • 原文地址:https://blog.csdn.net/m0_46256255/article/details/132792993
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号