问题描述
我有一个尺寸为 (30, 35, 49)
的张量.我想将它重塑为 (30, 35, 512)
以便能够与另一个形状为 (30, 35, 512)
的张量相乘.
我想用 (30, 35, 49)
维度对张量进行填充,以使其成为 (30, 35, 512)
维度.>
如何做到这一点?
虽然@nemo 的解决方案工作正常,但有一个 pytorch 内部例程,torch.nn.functional.pad
,做同样的事情 - 并且它有几个属性,一个 torch.ones(*sizes)*pad_value
解决方案没有(即其他形式的填充,如反射填充或复制填充......它还检查一些与梯度相关的属性):
import torch.nn.functional as F源 = 火炬.rand((5,10))# 现在我们通过在 pos 0 和 pos 6 处附加一行 0 来扩展到大小 (7, 11),# 以及位置 10 处的一列 0结果 = F.pad(input=source, pad=(0, 1, 1, 1), mode='constant', value=0)
参数的语义是:
input
:源张量,pad
:长度2 * len(source.shape)
形式的列表(开始最后一个轴,结束最后一个轴,开始第二个到最后一个轴,结束第 2 到最后一个轴,从第 3 个到最后一个轴开始,等等),说明应该在每个轴的开头和结尾添加多少维,mode
:'constant'
、'reflect'
或'replicate'
.默认值:'constant'
用于不同类型的填充value
用于常量填充.
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 the shape (30, 35, 512)
.
I want to do padding on the tensor with (30, 35, 49)
dimension in order to make it (30, 35, 512)
dimensional.
How can this be done?
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 length2 * 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 paddingvalue
for constant padding.
这篇关于在pytorch中用填充重塑张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!