【问题标题】:padding zeroes to a tensor on both dimensions在两个维度上将零填充到张量
【发布时间】:2020-01-20 06:46:25
【问题描述】:

我有一个张量 t

1 2
3 4
5 6
7 8

我也想成功

0 0 0 0
0 1 2 0
0 3 4 0
0 5 6 0
0 7 8 0
0 0 0 0 

我尝试使用 new=torch.tensor([0. 0. 0. 0.]) 张量堆叠四次,但没有奏效。

t = torch.arange(8).reshape(1,4,2).float()
print(t)
new=torch.tensor([[0., 0., 0.,0.]])
print(new)
r = torch.stack([t,new])  # invalid argument 0: Tensors must have same number of dimensions: got 4 and 3
new=torch.tensor([[[0., 0., 0.,0.]]])
print(new)
r = torch.stack([t,new])  # invalid argument 0: Sizes of tensors must match except in dimension 0.

我也试过猫,也没用。

【问题讨论】:

    标签: python pytorch tensor zero-padding


    【解决方案1】:

    最好先初始化所需形状的数组,然后在适当的索引处添加数据。

    import torch
    
    t = torch.arange(8).reshape(1,4,2).float()
    x = torch.zeros((1, t.shape[1]+2, t.shape[2]+2))
    x[:, 1:-1, 1:-1] = t
    
    print(x)
    

    另一方面,如果你只是想用零填充你的张量(而不仅仅是在某处添加额外的零),你可以使用torch.nn.functional.pad

    import torch
    
    t = torch.arange(8).reshape(1, 4, 2).float()
    x = torch.nn.functional.pad(t, (1, 1, 1, 1))
    
    print(x)
    

    【讨论】:

    • 这比栈操作好很多。无论如何你知道我在堆栈操作上哪里出错了吗?
    • 是的,你的张量是(1, 4, 2)-形的,所以你不能仅仅将它与一个本质上是(1, 4)-形的张量叠加。首先,您需要使用cat,并删除t 的第一个维度。你可以用一系列复杂的cat来解决它:x = torch.cat([torch.zeros(1, 4), torch.cat([torch.zeros(4, 1), t[0], torch.zeros(4, 1)], 1), torch.zeros(1, 4)])[None, :, :]
    • 你成功了!太棒了。
    • 第二种方法(F.pad)在渐变必须流过时更好。 Torch 给出了一个错误,说明第一种方法的张量就地修改。
    【解决方案2】:

    可以使用torch.cat 连接张量,得到想要的张量。这是一个完整的例子:

    # input tensor
    In [98]: at = torch.arange(1, 9).reshape(-1, 2).float()
    In [99]: at 
    Out[99]: 
    tensor([[1., 2.],
            [3., 4.],
            [5., 6.],
            [7., 8.]])
    
    # columns to be padded
    In [100]: col_zeros = torch.zeros(at.shape[0]).reshape(-1, 1)      
    In [101]: col_zeros   
    Out[101]: 
    tensor([[0.],
            [0.],
            [0.],
            [0.]])
    
    # rows to be padded
    In [102]: row_zeros = torch.zeros(at.shape[1]+2).reshape(1, -1)   
    In [103]: row_zeros 
    Out[103]: tensor([[0., 0., 0., 0.]])
    

    让我们先填充列:

    # the order in the list of tensors matter.
    # since we want a zero column on both sides, we place the input tensor in the middle
    # and pad the `col_zeros` on both sides (i.e. along dimension=1)
    In [104]: col_padded = torch.cat([col_zeros, at, col_zeros], dim=1) 
    In [105]: col_padded     
    Out[105]: 
    tensor([[0., 1., 2., 0.],
            [0., 3., 4., 0.],
            [0., 5., 6., 0.],
            [0., 7., 8., 0.]])
    

    接下来,让我们填充行:

    # here we pad the `row_zeros` on the upper and lower sides (i.e. along dimension=0)
    # placing the already `col_padded` tensor in the middle of the list of tensors
    In [106]: final_padded = torch.cat([row_zeros, col_padded, row_zeros], dim=0)  
    In [107]: final_padded      
    Out[107]: 
    tensor([[0., 0., 0., 0.],
            [0., 1., 2., 0.],
            [0., 3., 4., 0.],
            [0., 5., 6., 0.],
            [0., 7., 8., 0.],
            [0., 0., 0., 0.]])
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2016-03-12
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多