zoukankan      html  css  js  c++  java
  • Pytorch之permute函数

    1、主要作用:变换tensor维度

    example:

    import torch
    x = torch.randn(2, 3, 5)
    print(x.size())
    print(x.permute(2, 0, 1).size())
    
    >>>torch.Size([2, 3, 5])
    >>>torch.Size([5, 2, 3])

    2、介绍一下transpose与permute的异同:

    同:都是对tensor维度进行转置;

    异:permute函数可以对任意高维矩阵进行转置,但没有torch.permute()这个调用方式

    torch.randn(2,3,4,5).permute(3,2,0,1).shape
    
    >>>torch.Size([5, 4, 2, 3])

    transpose只能操作2D矩阵的转置,无法操作超过2个维度,所以要想实现多个维度的转置,既可以用一次性的

    permute,也可以多次使用transpose;

    torch.randn(2,3,4,5).transpose(3,0).transpose(2,1).transpose(3,2).shape
    
    >>>torch.Size([5, 4, 2, 3])

    3、permute函数与contiguous、view函数的关联

    contiguous: view只能作用在contiguous的variable上,如果在view之前调用了transpose、permute等,就需要调用

    contiguous()来返回一个contiguous的copy;

    也就是说transpose、permute等操作会让tensor变得在内存上不连续,因此要想view,就得让tensor先连续;

    解释如下:有些tensor并不是占用一整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行contiguous()这个函数,把tensor变成在内存中连续分布的形式;

    判断ternsor是否为contiguous,可以调用torch.Tensor.is_contiguous()函数:

    import torch 
    x = torch.ones(10, 10) 
    x.is_contiguous()                                 # True 
    x.transpose(0, 1).is_contiguous()                 # False
    x.transpose(0, 1).contiguous().is_contiguous()    # True

    另:在pytorch的最新版本0.4版本中,增加了torch.reshape(),与 numpy.reshape() 的功能类似,大致相当于 tensor.contiguous().view(),这样就省去了对tensor做view()变换前,调用contiguous()的麻烦;

    3、permute与view函数功能

    import torch
    import numpy as np
    
    a=np.array([[[1,2,3],[4,5,6]]])
    unpermuted=torch.tensor(a)
    print(unpermuted.size())              #  ——>  torch.Size([1, 2, 3])
    
    permuted=unpermuted.permute(2,0,1)
    print(permuted.size())                #  ——>  torch.Size([3, 1, 2])
    
    view_test = unpermuted.view(1,3,2)
    print(view_test.size())   
    
    >>>torch.Size([1, 2, 3])
    torch.Size([3, 1, 2])
    torch.Size([1, 3, 2])

     参考:https://zhuanlan.zhihu.com/p/76583143

  • 相关阅读:
    Androidproject文件下assets目录与res目录的差别
    tomcat 实现域名crm.test.com訪问
    全文索引--自己定义chinese_lexer词典
    Elasticsearch 之 慘痛部署(分片移位)
    iOS开发核心语言Objective C —— 全部知识点总结
    [LeedCode OJ]#28 Implement strStr()
    【Django】 gunicorn部署纪要
    PKI(公钥基础设施)基础知识笔记
    Cocoapods Undefined symbols for architecture armv7sarm64
    Android开发Tips(3)
  • 原文地址:https://www.cnblogs.com/yqpy/p/12459519.html
Copyright © 2011-2022 走看看