reshaping a tensor with padding in pytorch

前端 未结 4 756
不思量自难忘°
不思量自难忘° 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:01

    The simplest solution is to allocate a tensor with your padding value and the target dimensions and assign the portion for which you have data:

    target = torch.zeros(30, 35, 512)
    source = torch.ones(30, 35, 49)
    target[:, :, :49] = source
    

    Note that there is no guarantee that padding your tensor with zeros and then multiplying it with another tensor makes sense in the end, that is up to you.

提交回复
热议问题