展开操作如何在具有扩张和步幅的 pytorch 中工作?
How unfold operation works in pytorch with dilation and stride?
在我的例子中,我在 A 的张量上应用这个展开操作,如下所示:
A.shape=torch.Size([16, 309,128])
A = A.unsqueeze(1) # that's I guess for making it 4 dim for unfold operation
A_out= F.unfold(A, (7, 128), stride=(1,128),dilation=(3,1))
A_out.shape=torch.Size([16, 896,291])
我没有得到这个 291。如果没有膨胀因子,它应该是 [16,896,303] 对吗?
但是如果 dialtion=3 那么它是 291 怎么办?这里也没有提到步幅,所以deafualt是1,但是如果也提到4怎么办。请指导。
Also here stride is not mentioned so default is 1 but what if it is
also mentioned like 4.
您的代码已经有 stride=(1,128)
。如果 stride 仅设置为 4
,在这种情况下它将像 (4,4)
一样使用。这可以通过下面的公式轻松验证。
If the dilation factor is not there, it would be [16,896,303] right?
是的。示例如下。
But if dialtion=3 then it's 291 how?
按照pytorch文档中给出的公式得出291
。完成 A.unsqueeze(1)
后形状变为 [16, 1, 309, 128]
。这里,N=16
、C=1
、H=309
、W=128.
输出维度为 (N, C * product(kernel_size), L)
。使用 kernel_size=(7,128)
所以这就变成了 (16, 1 * 7 * 128, L)
= (16, 896, L)
.
L
可以使用下面的公式对每个维度进行乘法计算。
L = d3 * d4
超高尺寸 spatial_size[3] = 309
、padding[3] = 0
默认、dilation[3] = 3
、kernel_size[3] = 7
、stride[3] = 1
.
d3 = (309 + 2 * 0 - 3 * (7 - 1) - 1) / 1 + 1
= 291
超宽尺寸 spatial_size[4] = 128
,padding[4] = 0
默认,dilation[4] = 1
,kernel_size[4] = 128
,stride[4] = 128
。
d4 = (128 + 2 * 0 - 1 * (128 - 1) - 1) / 128 + 1
= 1
所以,使用上面的公式 L
变成 291
.
代码
import torch
from torch.nn import functional as F
A = torch.randn([16, 309,128])
print(A.shape)
A = A.unsqueeze(1)
print(A.shape)
A_out= F.unfold(A, kernel_size=(7, 128), stride=(1,128),dilation=(3,1))
print(A_out.shape)
输出
torch.Size([16, 309, 128])
torch.Size([16, 1, 309, 128])
torch.Size([16, 896, 291])
链接
在我的例子中,我在 A 的张量上应用这个展开操作,如下所示:
A.shape=torch.Size([16, 309,128])
A = A.unsqueeze(1) # that's I guess for making it 4 dim for unfold operation
A_out= F.unfold(A, (7, 128), stride=(1,128),dilation=(3,1))
A_out.shape=torch.Size([16, 896,291])
我没有得到这个 291。如果没有膨胀因子,它应该是 [16,896,303] 对吗? 但是如果 dialtion=3 那么它是 291 怎么办?这里也没有提到步幅,所以deafualt是1,但是如果也提到4怎么办。请指导。
Also here stride is not mentioned so default is 1 but what if it is also mentioned like 4.
您的代码已经有 stride=(1,128)
。如果 stride 仅设置为 4
,在这种情况下它将像 (4,4)
一样使用。这可以通过下面的公式轻松验证。
If the dilation factor is not there, it would be [16,896,303] right?
是的。示例如下。
But if dialtion=3 then it's 291 how?
按照pytorch文档中给出的公式得出291
。完成 A.unsqueeze(1)
后形状变为 [16, 1, 309, 128]
。这里,N=16
、C=1
、H=309
、W=128.
输出维度为 (N, C * product(kernel_size), L)
。使用 kernel_size=(7,128)
所以这就变成了 (16, 1 * 7 * 128, L)
= (16, 896, L)
.
L
可以使用下面的公式对每个维度进行乘法计算。
L = d3 * d4
超高尺寸 spatial_size[3] = 309
、padding[3] = 0
默认、dilation[3] = 3
、kernel_size[3] = 7
、stride[3] = 1
.
d3 = (309 + 2 * 0 - 3 * (7 - 1) - 1) / 1 + 1
= 291
超宽尺寸 spatial_size[4] = 128
,padding[4] = 0
默认,dilation[4] = 1
,kernel_size[4] = 128
,stride[4] = 128
。
d4 = (128 + 2 * 0 - 1 * (128 - 1) - 1) / 128 + 1
= 1
所以,使用上面的公式 L
变成 291
.
代码
import torch
from torch.nn import functional as F
A = torch.randn([16, 309,128])
print(A.shape)
A = A.unsqueeze(1)
print(A.shape)
A_out= F.unfold(A, kernel_size=(7, 128), stride=(1,128),dilation=(3,1))
print(A_out.shape)
输出
torch.Size([16, 309, 128])
torch.Size([16, 1, 309, 128])
torch.Size([16, 896, 291])