• pytorch张量维度操作(拼接、维度扩展、压缩、转置、重复……)


    涉及的方法有下面几种:

    torch.cat()
    
    torch.Tensor.expand()
    
    torch.squeeze()
    
    torch.Tensor.repeat()
    
    torch.Tensor.narrow()
    
    torch.Tensor.view()
    
    torch.Tensor.resize_()
    
    torch.Tensor.permute()

    拼接张量

    torch.cat(seq, dim=0, out=None) → Tensor

    在指定的维度dim上对序列seq进行连接操作。

    参数:

    • seq (sequence of Tensors) - Python序列或相同类型的张量序列
    • dim (int, optional) - 沿着此维度连接张量
    • out (Tensor, optional) - 输出参数
    x = torch.randn(2, 3)
    x
    -0.5866 -0.3784 -0.1705
    -1.0125 0.7406 -1.2073
    [torch.FloatTensor of size 2x3]
    torch.cat((x, x, x), 0)
    -0.5866 -0.3784 -0.1705
    -1.0125 0.7406 -1.2073
    -0.5866 -0.3784 -0.1705
    -1.0125 0.7406 -1.2073
    -0.5866 -0.3784 -0.1705
    -1.0125 0.7406 -1.2073
    [torch.FloatTensor of size 6x3]
    torch.cat((x, x, x), 1)
    -0.5866 -0.3784 -0.1705 -0.5866 -0.3784 -0.1705 -0.5866 -0.3784 -0.1705
    -1.0125 0.7406 -1.2073 -1.0125 0.7406 -1.2073 -1.0125 0.7406 -1.2073
    [torch.FloatTensor of size 2x9]

    拼接张量2

    torch.stack((Tensor), dim)

    a = torch.IntTensor([[1,2,3],[11,22,33]])
    b= torch.IntTensor([[4,5,6],[44,55,66]])
    c=torch.stack([a,b],0)
    d=torch.stack([a,b],1)
    e=torch.stack([a,b],2)
    print(c)
    print(d)
    print(e)
    >>> print(c)
    tensor([[[ 1,  2,  3],
             [11, 22, 33]],
    
            [[ 4,  5,  6],
             [44, 55, 66]]], dtype=torch.int32)
    >>> print(d)
    tensor([[[ 1,  2,  3],
             [ 4,  5,  6]],
    
            [[11, 22, 33],
             [44, 55, 66]]], dtype=torch.int32)
    >>> print(e)
    tensor([[[ 1,  4],
             [ 2,  5],
             [ 3,  6]],
    
            [[11, 44],
             [22, 55],
             [33, 66]]], dtype=torch.int32)
    

    c, dim = 0时, c = [ a,  b]

    d, dim =1 时, d = [ [a[0] ,  b[0] ] ,  [a[1], b[1] ] ]

    e, dim = 2 时, e=[ [ [ a[0][0], b[0][0] ], [ a[0][1], b[0][1]], [a[0][2], b[0][2]] ], [ [a[1][0], b[1][0] ], [a[1][1], b[0][1] ], [a[1][2], b[1][2] ] ] ]

    扩大张量

    torch.Tensor.expand(*sizes) → Tensor

    返回张量的一个新视图,可以将张量的单个维度扩大为更大的尺寸。

    张量也可以扩大为更高维,新增加的维度将附在前面。 扩大张量不需要分配新内存,仅仅是新建一个张量的视图。任意一个一维张量在不分配新内存情况下都可以扩展为任意的维度。

    传入-1则意味着维度扩大不涉及这个维度。

    参数:

    sizes (torch.Size or int…) – 想要扩展的目标维度

    1 x = torch.Tensor([[1], [2], [3]])
    2 x.size()
    3 torch.Size([3, 1])
    4 x.expand(3, 4)
    5 1 1 1 1
    6 2 2 2 2
    7 3 3 3 3
    8 [torch.FloatTensor of size 3x4]

    压缩张量

    torch.squeeze(input, dim=None, out=None) → Tensor

    除去输入张量input中数值为1的维度,并返回新的张量。如果输入张量的形状为(A×1×B×C×1×D),那么输出张量的形状为

    (A×B×C×D)。

    当通过dim参数指定维度时,维度压缩操作只会在指定的维度上进行。如果输入向量的形状为(A×1×B),squeeze(input, 0)会保持张量的维度不变,只有在执行

    squeeze(input, 1)时,输入张量的形状会被压缩至(A×B)。

    如果一个张量只有1个维度,那么它不会受到上述方法的影响。

    输出的张量与原张量共享内存,如果改变其中的一个,另一个也会改变。

    参数:

    input (Tensor) – 输入张量

    dim (int, optional) – 如果给定,则只会在给定维度压缩

    out (Tensor, optional) – 输出张量

     1 x = torch.zeros(2, 1, 2, 1, 2)
     2 x.size()
     3 torch.Size([2, 1, 2, 1, 2])
     4 y = torch.squeeze(x)
     5 y.size()
     6 torch.Size([2, 2, 2])
     7 y = torch.squeeze(x, 0)
     8 y.size()
     9 torch.Size([2, 1, 2, 1, 2])
    10 y = torch.squeeze(x, 1)
    11 y.size()
    12 torch.Size([2, 2, 1, 2])

    重复张量

    torch.Tensor.repeat(*sizes)

    沿着指定的维度重复张量。不同于expand()方法,本函数复制的是张量中的数据。

    参数:

    • size (torch.size or int…) - 沿着每一维重复的次数
    1 x = torch.Tensor([1, 2, 3])
    2 x.repeat(4, 2)
    3 1 2 3 1 2 3
    4 1 2 3 1 2 3
    5 1 2 3 1 2 3
    6 1 2 3 1 2 3
    7 [torch.FloatTensor of size 4x6]

    torch.Tensor.unfold(dim, size, step) → Tensor

    返回一个新的张量,其中元素复制于有原张量在dim维度上的数据,复制重复size次,复制时的步进值为step。

    参数:

    dim (int) - 目标维度

    size (int) - 复制重复的次数(展开维度)

    step (int) - 步长

    例子:

     1 x = torch.arange(1, 8)
     2 x
     3 1
     4 2
     5 3
     6 4
     7 5
     8 6
     9 7
    10 [torch.FloatTensor of size 7]
    11 x.unfold(0, 2, 1)
    12 1 2
    13 2 3
    14 3 4
    15 4 5
    16 5 6
    17 6 7
    18 [torch.FloatTensor of size 6x2]
    19 x.unfold(0, 2, 2)
    20 1 2
    21 3 4
    22 5 6
    23 [torch.FloatTensor of size 3x2]

    缩小张量

    torch.Tensor.narrow(dimension, start, length) → Tensor

    返回一个经过缩小后的张量。操作的维度由dimension指定。缩小范围是从start开始到start+length。执行本方法的张量与返回的张量共享相同的底层内存。

    参数:

    dimension (int) – 要进行缩小的维度

    start (int) – 开始维度索引

    length (int) – 缩小持续的长度

    例子:

     1 x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
     2 x.narrow(0, 0, 2)
     3 1 2 3
     4 4 5 6
     5 [torch.FloatTensor of size 2x3]
     6 x.narrow(1, 1, 2)
     7 2 3
     8 5 6
     9 8 9
    10 [torch.FloatTensor of size 3x2]

    张量变形

    torch.Tensor.view(*args) → Tensor

    返回一个有相同数据但是不同形状的新的向量。

    返回的装两必须与原张量有相同的数据和相同的元素个数,但是可以有不同的尺寸。

    参数:

    • args (torch.Size or int…) - 理想的指定尺寸
    1 x = torch.randn(4, 4)
    2 x.size()
    3 torch.Size([4, 4])
    4 y = x.view(16)
    5 y.size()
    6 torch.Size([16])

    重设张量尺寸

    torch.Tensor.resize_(*sizes)

    将张量的尺寸调整为指定的大小。如果元素个数比当前的内存大小大,就将底层存储大小调整为与新元素数目一致的大小。

    如果元素个数比当前内存小,则底层存储不会被改变。原来张量中被保存下来的元素将保持不变,但新内存将不会被初始化。

    参数:

    sizes (torch.Size or int…) - 需要调整的大小

    1 x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
    2 x.resize_(2, 2)
    3 x
    4 1 2
    5 3 4
    6 [torch.FloatTensor of size 2x2]

    置换张量维度

    torch.Tensor.permute(*dims)

    将执行本方法的张量的维度换位。

    参数:

    • dim (int) - 指定换位顺序

    例子:

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

    查看张量单个元素的字节数

    torch.Tensor.element_size() → int

    查看某类型张量单个元素的字节数。

    1 torch.FloatTensor().element_size()
    2 4

    ---------------------
    作者:蓝鲸123
    来源:CSDN
    原文:https://blog.csdn.net/TH_NUM/article/details/83088915
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    String StringBuffer StringBuilder 之间的区别
    StringBuffer和String的相互转换
    StringBuffer的替换和反转和截取功能
    StringBuffer的添加与删除功能
    后端——框架——容器框架——spring_core——容器
    后端——框架——容器框架——spring_core——bean以及xml配置
    后端——框架——容器框架——spring_core——注解
    后端——框架——容器框架——spring_core——国际化 & Properties
    后端——框架——容器框架——spring_core——Resource
    后端——框架——容器框架——spring_core——校验器
  • 原文地址:https://www.cnblogs.com/jiangkejie/p/10683531.html
Copyright © 2020-2023  润新知