索引超出 MaxUnpool2d 的范围
Indices out of range for MaxUnpool2d
我想了解 Pytorch 中的 unpooling,因为我想构建一个卷积自动编码器。
我有以下代码
from torch.autograd import Variable
data = Variable(torch.rand(1, 73, 480))
pool_t = nn.MaxPool2d(2, 2, return_indices=True)
unpool_t = nn.MaxUnpool2d(2, 2)
out, indices1 = pool_t(data)
out = unpool_t(out, indices1)
但我经常在最后一行(unpooling)收到这个错误。
IndexError: tuple index out of range
尽管在此示例中模拟了数据,但由于必须进行预处理,因此输入必须具有该形状。
我对卷积网络还很陌生,但我什至尝试过在池化之前使用 ReLU 和卷积 2D 层,但是,在对这种形状进行解池化时,索引似乎总是不正确。
您的数据是一维的,您正在使用二维池化和反池化操作。
PyTorch 将张量的前两个维度解释为 "batch dimension" 和 "channel"/"feature space" 维度。其余维度被视为空间维度。
因此,在您的示例中,data
是大小为 (1, 73, 480)
的 3D 张量,pytorch 将其解释为单个批次 ("batch dimension" = 1),每个样本有 73 个通道和 480 个样本。
出于某种原因 MaxPool2d
适合您并将通道维度视为空间维度并对其进行采样 - 我不确定这是错误还是功能。
如果您确实想沿第二个维度进行采样,您可以添加一个额外的维度,使 data
成为 4D 张量:
out, indices1 = pool_t(data[None,...])
In [11]: out = unpool_t(out, indices1, data[None,...].size())
我想了解 Pytorch 中的 unpooling,因为我想构建一个卷积自动编码器。
我有以下代码
from torch.autograd import Variable
data = Variable(torch.rand(1, 73, 480))
pool_t = nn.MaxPool2d(2, 2, return_indices=True)
unpool_t = nn.MaxUnpool2d(2, 2)
out, indices1 = pool_t(data)
out = unpool_t(out, indices1)
但我经常在最后一行(unpooling)收到这个错误。
IndexError: tuple index out of range
尽管在此示例中模拟了数据,但由于必须进行预处理,因此输入必须具有该形状。
我对卷积网络还很陌生,但我什至尝试过在池化之前使用 ReLU 和卷积 2D 层,但是,在对这种形状进行解池化时,索引似乎总是不正确。
您的数据是一维的,您正在使用二维池化和反池化操作。
PyTorch 将张量的前两个维度解释为 "batch dimension" 和 "channel"/"feature space" 维度。其余维度被视为空间维度。
因此,在您的示例中,data
是大小为 (1, 73, 480)
的 3D 张量,pytorch 将其解释为单个批次 ("batch dimension" = 1),每个样本有 73 个通道和 480 个样本。
出于某种原因 MaxPool2d
适合您并将通道维度视为空间维度并对其进行采样 - 我不确定这是错误还是功能。
如果您确实想沿第二个维度进行采样,您可以添加一个额外的维度,使 data
成为 4D 张量:
out, indices1 = pool_t(data[None,...])
In [11]: out = unpool_t(out, indices1, data[None,...].size())