pytorch的基本操作

一. p4 pytorch基本操作

  1. x = torch.empty(5, 3);

5行3列的一个张量(矩阵),特需格式是tensor

import torch
x = torch.empty(5, 3)
x
输出:
tensor([[-4.7960e+35,  3.0794e-41,  0.0000e+00],
        [ 0.0000e+00, -4.8290e+35,  3.0794e-41],
        [-4.8290e+35,  3.0794e-41, -4.8290e+35],
        [ 3.0794e-41,  0.0000e+00,  0.0000e+00],
        [-2.7661e+13,  4.5565e-41, -2.6833e+13]])
  1. x = torch.rand(5, 3)

5行3列的随机数

#随机值,tensors是他的一个特需格式,所有计算都是在tensor的基础上运算
x = torch.rand(5, 3)
x
输出:
tensor([[0.5898, 0.6413, 0.3710],
        [0.8552, 0.1343, 0.6362],
        [0.0649, 0.6644, 0.0517],
        [0.4938, 0.4127, 0.6358],
        [0.1633, 0.9749, 0.7178]])
  1. x = torch.zeros(5, 3, dtype=torch.long)

5行3列的零矩阵

x = torch.zeros(5, 3, dtype=torch.long)
x
输出:
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
  • x = torch.tensor([5.5, 3])
    写入一个矩阵(传入数据)
  • x = torch.randn_like(x, dtype=torch.float)
    构建的大小一样的矩阵
x = torch.tensor([5.5, 3])
x
输出:
tensor([5.5000, 3.0000])
# 构建的矩阵大小一样
x= x.new_ones(5, 3, dtype=torch.double)

x = torch.randn_like(x, dtype=torch.float)
x
输出:
tensor([[ 0.5455,  0.3819, -0.4844],
        [-2.3630,  1.4743,  0.2780],
        [ 0.5868,  0.3403, -0.2346],
        [ 1.5565,  1.4095, -0.7017],
        [ 0.2720,  1.6439, -1.3772]])
    • 0
      点赞
    • 2
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

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

    余额充值