pytorch张量创建和运算常用操作(1)

import torch


if __name__ =="__main__":
   #******创建张量******#
     #1、创建从0-11的12个张量
        print("\n1、创建从0-11的12个张量\n")
        x1=torch.arange(12)
        print("x1:",x1)
     #2、创建3*4 均值0方差1的高斯分布
        print("\n2、创建3*4 均值0方差1的高斯分布\n")
        x2 = torch.randn((3,4))
        print("x2:", x2)
     #3、创建矩阵2*3*4
        print("\n3、创建2*3*4 0矩阵\n")
        x3 = torch.zeros((2,3,4))   #1矩阵zeros换成ones
        print("x3:", x3)
     #4、自定义矩阵
        print("\n4、自定义矩阵\n")
        x4 = torch.tensor([[1,2],[3,4],[5,6]])
        print("x4:", x4)

   #张量的相关参数
      #5、张量的shape
        print("\n5、张量的shape\n")
        print("x4的shape:",x4.shape)
      #6、张量的元素数量
        print("\n6、张量的元素数量\n")
        print("x4的numel:", x4.numel())

   #张量的形状改变操作
       #6、reshape   改变形状
        print("\n6、reshape   改变形状\n")#它会先按一行一行排列成一行,再按顺序重塑成2行三列
        X4_0= x4.reshape(2,3)
        print("X4_0:",X4_0)
       #7、 view     和reshape一样用
        print("\n7、view   改变形状\n")
        X4_1= x4.view(2,3)
        print("X4_1:", X4_1)

       #8、 usqueeze squeeze  维度增加删除操作  只能增加维度多1,也只能删除维度是1的
        print("\n8、squeeze 维度删除操作\n")
        X4_2= X4_1.unsqueeze(dim=1)  #0是在最前面加1 (1,2,3) ,1是得到(2,1,3)
        print("X4_2shape:", X4_2.shape)
        X4_3= X4_2.squeeze(dim=1)
        print("X4_3shape:", X4_3.shape)\
       #9、  expand  扩展维度 (2,1,3) ->(2,2,3),只能对1进行扩展,多的维度也是对原来进行复制
        print("\nexpand  扩展维度 (2,1,3) ->(2,3,3)")
        X4_4=X4_2.expand(2,3,3)
        print("X4_4:",X4_4)
        print("X4_4shape:", X4_4.shape)

       #10、 repeat 将某一维度的进行乘倍复制    ,感觉算是expand的拓展,expand能做repeat也能做,repeat能做expand不能做
        print("\nrepeat  复制维度 (2,1,3) ->(2,3,3)")
        X4_5 = X4_2.repeat(1, 3, 1)
        print("X4_5:", X4_5)
        print("X4_5shape:", X4_5.shape)

       #11、 .t() 转置  只能用于矩阵
        print("\n11、t() 转置")
        print("x4:", x4)
        X4_6 = x4.t()
        print("X4_6:", X4_6)
       #12、transpose交换维度顺序,对于矩阵交换维度顺序,0,1不分先后顺序,就将行边列列边行
        print("\n12、transpose交换维度顺序")
        print("x4:", x4)
        X4_7 = x4.transpose(0,1)
        print("X4_7:", X4_7)
       #13、 permute  可随意调整维度顺序(1,2,0)就表示将第0个维度放到最后一个,permute最通用些
        print("\n13、permute交换维度顺序")
        print("x4:", x4)
        X4_8 = x4.permute(1, 0)
        print("X4_8:", X4_8)

      """
      注意对于numpy变量没有permute,对于numpy变量transpose的用法和这里permute一样
      这里是对于tensor变量的

      """









import torch



if __name__ == "__main__":
    # + - * / 都表示矩阵元素对应位置进行运算,**为求幂运算x^y# log对数运算


    #1、指数运算
    print("\n1、指数运算\n")
    x1=torch.tensor([[1,2],[3,4],[5,6]])
    y=torch.exp(x1)
    print("y:",y)

    #2、cat拼接操作  在某一维度上将两个张量拼接起来,除进行拼接的维度之外的维度大小应该相等。
    print("\n2、cat拼接操作\n")
    x2 =torch.tensor([[1,2],[3,4],[5,6]])
    print("x1shape:",x1.shape)
    print("x2shape:",x2.shape)
    y=torch.cat([x1,x2],dim=0)
    print("y shape:", y.shape)
    print("y:", y)

    #3、stack(增加一个维度,并将两个张量进行拼接)
    print("\n3、stack(增加一个维度,并将两个张量进行拼接)\n")
    y=torch.stack([x1,x2],dim=0)
    print("y shape:", y.shape)
    print("y:", y)

    #4、split(拆分某个维度,与CAT对应)
    print("\n4、split(拆分某个维度,与CAT对应)\n")  #这里0维度是2拆成[1,1]  ,也可3拆成[2,1]
    y1,y2=y.split([1,1],dim=0)
    print("y1 shape:", y1.shape)
    print("y2 shape:", y2.shape)
    print("y1 :", y1)
    print("y2 :", y2)

    #5、Chunk(某个维度上,拆成m份)
    print("\n5、Chunk(某个维度上,拆成m份)\n")
    y3,y4=y.chunk(2,dim=0)
    print("y3 shape:", y3.shape)
    print("y4 shape:", y4.shape)
    print("y3 :", y3)
    print("y4 :", y4)

    #6、矩阵运算mm(只能算二维矩阵)和matmul(能算多维矩阵,如[4,3,28,32]和[4,3,64,32]的维度[28,32] [64,32]对应矩阵进行运算)
    print("\n6、矩阵运算mm(只能算二维矩阵)和matmul\n")
    a=torch.rand(4,3,28,64)
    b=torch.rand(4,3,64,32)
    y=torch.matmul(a,b)
    print("y:shape",y.shape)

    #7、近似运算: 向下取整a.floor() 向上取整ceil()  四舍五入round  取整数:trunc  取小数:frac


    #8、取中数:median() 取最大max  ,取最小min,取范围clamp(min,min)


    #9、broadcast机制
    print("\n9、broadcast机制\n")
    a=torch.arange(3).reshape((3,1))
    b=torch.arange(2).reshape((1,2))
    print("a:",a)
    print("b:",b)
    y = a + b
    print("a+b=",y)

    #10、根据索引取值  同样能够根据取值的办法进行赋值
    print("\n10、根据索引取值\n")
    c=torch.arange(9).reshape(3,3)
    print("c:",c)
    print("c[-1]=",c[-1])  #取最后一行
    print("c[-1,-1]=", c[-1,-1])#取最后一行最后一列
    print("c[0,0:3]=", c[0,0:3])  # 取最后一行最后一列

    #11、我节省内存代码中避免出现Y=X+Y,因为这样Y的内存地址会在新的地址,建议是Y+=X,或者Y[:]=Y+X

    #12、numpy转tensor tensor转张量

    A=c.numpy()
    B=torch.tensor(A)
    print("A type",type(A))
    print("B type", type(B))
















  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值