zoukankan      html  css  js  c++  java
  • Tensor的创建与维度查看-01

    Tensor的创建与维度查看

     1 import torch 
     2 import numpy as np
     3 
     4 # 最基础的Tensor()函数创建方法, 参数为Tensor的每一维大小
     5 a =  torch.Tensor(2,2)
     6 print(a)
     7 >> tensor([[1.0965e-38, 4.5670e-41],
     8         [4.6761e+17, 4.5670e-41]])
     9 
    10 b=torch.DoubleTensor(2,2)
    11 print(b)
    12 >> tensor([[0., 0.],
    13         [0., 0.]], dtype=torch.float64)
    14 
    15 # 使用Python的list序列进行创建
    16 c=torch.Tensor([[1,2],[3,4]])
    17 print(c)
    18 >> tensor([[1., 2.],
    19         [3., 4.]])
    20 
    21 # 使用zeros()函数, 所有元素均为0
    22 d=torch.zeros(2,2)
    23 print(d)
    24 >> tensor([[0., 0.],
    25         [0., 0.]])
    26 
    27 # 使用ones()函数, 所有元素均为1
    28 e=torch.ones(5,5)
    29 print(e)
    30 >>  tensor([[1., 1., 1., 1., 1.],
    31         [1., 1., 1., 1., 1.],
    32         [1., 1., 1., 1., 1.],
    33         [1., 1., 1., 1., 1.],
    34         [1., 1., 1., 1., 1.]])
    35 
    36 # 使用eye()函数, 对角线元素为1, 不要求行列数相同, 生成二维矩阵
    37 f=torch.eye(3,4)
    38 print(f)
    39 >> tensor([[1., 0., 0., 0.],
    40         [0., 1., 0., 0.],
    41         [0., 0., 1., 0.]])
    42 
    43 # 使用randn()函数, 生成随机数矩阵
    44 g=torch.randn(2,2)
    45 print(g)
    46 >> tensor([[ 1.4531, -1.5791],
    47         [ 0.7568, -0.7648]])
    48 
    49 # 使用arange(start, end, step)函数, 表示从start到end, 间距为step, 一维向量
    50 h=torch.arange(1,20,1)
    51 print(h)
    52 >> tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
    53         19])
    54 
    55 # 使用linspace(start, end, steps)函数, 表示从start到end, 一共steps份, 一维向量
    56 i=torch.linspace(1,6,6)
    57 print(i)
    58 >> tensor([1., 2., 3., 4., 5., 6.])
    59 
    60 # 使用randperm(num)函数, 生成长度为num的随机排列向量
    61 j=torch.randperm(10)
    62 print(j)
    63 >> tensor([0, 9, 4, 1, 5, 3, 7, 8, 2, 6])
    64 
    65 # PyTorch 0.4中增加了torch.tensor()方法, 参数可以为Python的list、 NumPy的ndarray等
    66 k=torch.tensor([1,2,3])
    67 print(k)
    68 >> tensor([1, 2, 3])
    View Code
     1 import torch 
     2 
     3 a=torch.randn(2,2)
     4 print(a.shape)
     5 print(a.size())
     6 print(a.numel())
     7 print(a.nelement())
     8 
     9 >> torch.Size([2, 2])
    10 >> torch.Size([2, 2])
    11 >> 4
    12 >> 4
    View Code
  • 相关阅读:
    设备坐标与逻辑坐标
    4个设备上下文DC
    VC6.0智能提示消失恢复
    VC
    JavaWeb_设置Cookie的作用路径
    JavaWeb_Cookie显示最近浏览的商品
    JavaWeb_Cookie
    MVC案例——通过配置切换底层存储源
    MVC案例——修改用户
    MVC案例——删除操作
  • 原文地址:https://www.cnblogs.com/zhaopengpeng/p/13597840.html
Copyright © 2011-2022 走看看