reshaping a tensor with padding in pytorch

前端 未结 4 757
不思量自难忘°
不思量自难忘° 2021-02-02 10:25

I have a tensor with dimensions (30, 35, 49). I want to reshape it to (30, 35, 512) in order to be able to multiply with another tensor which has also

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 11:03

    While @nemo's solution works fine, there is a pytorch internal routine, torch.nn.functional.pad, that does the same - and which has a couple of properties that a torch.ones(*sizes)*pad_value solution does not (namely other forms of padding, like reflection padding or replicate padding ... it also checks some gradient-related properties):

    import torch.nn.functional as F
    source = torch.rand((5,10))
    # now we expand to size (7, 11) by appending a row of 0s at pos 0 and pos 6, 
    # and a column of 0s at pos 10
    result = F.pad(input=source, pad=(0, 1, 1, 1), mode='constant', value=0)
    

    The semantics of the arguments are:

    • input: the source tensor,
    • pad: a list of length 2 * len(source.shape) of the form (begin last axis, end last axis, begin 2nd to last axis, end 2nd to last axis, begin 3rd to last axis, etc.) that states how many dimensions should be added to the beginning and end of each axis,
    • mode: 'constant', 'reflect' or 'replicate'. Default: 'constant' for the different kinds of padding
    • value for constant padding.

提交回复
热议问题