美文网首页
Pytorch中[:,None]的用法解析

Pytorch中[:,None]的用法解析

作者: 逍遥_yjz | 来源:发表于2022-10-29 10:02 被阅读0次

    一.Pytorch中[:,None]的用法解析

    1. [:,None]的用法解析

    Tensor中利用None来增加维度,可以简单的理解为在None的位置上增加一维,新增维度大小为1,同时有几个None就会增加几个维度。

    2. 代码举例

    2.1 [None,:,:]和[None,:]输出张量一样,输入数据为二维张量,输出为三维张量。

    x = torch.randn(3,4)
    y = x[None,:,:]
    x,x.shape,y,y.shape
    
    
    输出结果如下:
    (tensor([[-1.2004,  0.0202, -0.4225, -0.0444],
             [ 0.4218, -1.0867,  1.4224,  0.5967],
             [ 0.4703,  0.8158, -0.9460, -0.7291]]),
     torch.Size([3, 4]),
     tensor([[[-1.2004,  0.0202, -0.4225, -0.0444],
              [ 0.4218, -1.0867,  1.4224,  0.5967],
              [ 0.4703,  0.8158, -0.9460, -0.7291]]]),
     torch.Size([1, 3, 4]))
    
    

    2.2 [None,None:,:]和[None,None]输出张量一样,输入数据为二维张量,输出为四维张量。

    x = torch.randn(3,4)
    y = x[None,None,:,:]
    x,x.shape,y,y.shape
    
    
    输出结果如下:
    (tensor([[-0.7301, -0.2588,  0.2528, -0.7637],
             [-1.5438,  0.6894,  0.5747,  0.0481],
             [ 0.5045, -0.3611, -1.2757,  1.0789]]),
     torch.Size([3, 4]),
     tensor([[[[-0.7301, -0.2588,  0.2528, -0.7637],
               [-1.5438,  0.6894,  0.5747,  0.0481],
               [ 0.5045, -0.3611, -1.2757,  1.0789]]]]),
     torch.Size([1, 1, 3, 4]))
    
    

    2.3 [:,None,:]和[:,None]输出张量一样,输入数据为二维张量,输出为三维张量。

    x = torch.randn(3,4)
    y = x[:,None,:]
    x,x.shape,y,y.shape
    
    
    输出结果如下:
    (tensor([[ 1.0753, -1.5525,  0.8249, -0.2986],
             [-0.7956, -0.0708,  1.8574, -1.0563],
             [ 0.5642,  0.9701,  1.0636, -1.2102]]),
     torch.Size([3, 4]),
     tensor([[[ 1.0753, -1.5525,  0.8249, -0.2986]],
    
             [[-0.7956, -0.0708,  1.8574, -1.0563]],
    
             [[ 0.5642,  0.9701,  1.0636, -1.2102]]]),
     torch.Size([3, 1, 4]))
    
    

    2.4 [:,None,:]和[:,None]输出张量一样,输入数据为二维张量,输出为三维张量。

    x = torch.randn(3,4)
    y = x[:,None]
    x,x.shape,y,y.shape
    
    
    输出结果如下:
    (tensor([[-2.7815, -0.8274,  1.1110,  0.9889],
             [-0.6636, -1.5992,  0.7225,  0.3466],
             [-1.4326,  2.0451, -1.6679,  0.0902]]),
     torch.Size([3, 4]),
     tensor([[[-2.7815, -0.8274,  1.1110,  0.9889]],
    
             [[-0.6636, -1.5992,  0.7225,  0.3466]],
    
             [[-1.4326,  2.0451, -1.6679,  0.0902]]]),
     torch.Size([3, 1, 4]))
    
    

    2.5 [:,:,None],输入数据为二维张量,输出为三维张量。

    x = torch.randn(3,4)
    y = x[:,:,None]
    x,x.shape,y,y.shape
    
    
    输出结果如下:
    (tensor([[ 0.0735,  0.1196,  1.7420, -0.2371],
             [-0.2613, -1.3396, -0.0262, -0.3695],
             [-1.2122, -1.1700,  2.3281, -0.8234]]),
     torch.Size([3, 4]),
     tensor([[[ 0.0735],
              [ 0.1196],
              [ 1.7420],
              [-0.2371]],
    
             [[-0.2613],
              [-1.3396],
              [-0.0262],
              [-0.3695]],
    
             [[-1.2122],
              [-1.1700],
              [ 2.3281],
              [-0.8234]]]),
     torch.Size([3, 4, 1]))
    
    

    2.6 [:,:,None,None],输入数据为二维张量,输出为四维张量。

    x = torch.randn(3,4)
    y = x[:,:,None,None]
    x,x.shape,y,y.shape
    
    
    输出结果如下:
    (tensor([[ 0.5205,  0.7751, -0.9279,  0.6369],
             [ 1.0077, -0.2766,  0.5953, -1.1734],
             [ 1.9789,  0.1456, -1.9392, -0.4931]]),
     torch.Size([3, 4]),
     tensor([[[[ 0.5205]],
    
              [[ 0.7751]],
    
              [[-0.9279]],
    
              [[ 0.6369]]],
    
             [[[ 1.0077]],
    
              [[-0.2766]],
    
              [[ 0.5953]],
    
              [[-1.1734]]],
    
             [[[ 1.9789]],
    
              [[ 0.1456]],
    
              [[-1.9392]],
    
              [[-0.4931]]]]),
     torch.Size([3, 4, 1, 1]))
    
    

    参考知识文章

    相关文章

      网友评论

          本文标题:Pytorch中[:,None]的用法解析

          本文链接:https://www.haomeiwen.com/subject/oclzzrtx.html