pytorch - Pytorch Conv2d 自动编码器输出形状
问题描述
我构建了波纹管卷积自动编码器并尝试对其进行调整以获得 [NxHxW] = 1024 的编码器输出形状 (x_encoder) 而不会增加损失。目前我的输出形状是 [4, 64, 64] 有什么想法吗?
# define the NN architecture
class ConvAutoencoder(nn.Module):
def __init__(self):
super(ConvAutoencoder, self).__init__()
## encoder layers ##
# conv layer (depth from in --> 16), 3x3 kernels
self.conv1 = nn.Conv2d(1, 16, 3, padding=1)
# conv layer (depth from 16 --> 4), 3x3 kernels
self.conv2 = nn.Conv2d(16, 4, 3, padding=1)
# pooling layer to reduce x-y dims by two; kernel and stride of 2
self.pool = nn.MaxPool2d(2, 2)
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.t_conv1 = nn.ConvTranspose2d(4, 16, 2, stride=2)
self.t_conv2 = nn.ConvTranspose2d(16, 1, 2, stride=2)
def forward(self, x):
## encode ##
# add hidden layers with relu activation function
# and maxpooling after
x = F.relu(self.conv1(x))
x = self.pool(x)
# add second hidden layer
x = F.relu(self.conv2(x))
x = self.pool(x) # compressed representation
x_encoder = x
## decode ##
# add transpose conv layers, with relu activation function
x = F.relu(self.t_conv1(x))
# output layer (with sigmoid for scaling from 0 to 1)
x = F.sigmoid(self.t_conv2(x))
return x, x_encoder
解决方案
假设 x_encoder 的形状为 ,这应该可以工作(torch.Size([1, 4, 64, 64])
。您可以添加一个转换。步幅设置为 2 或 Conv 的层。层后跟一个池化层。检查下面的代码:
# conv layer (depth from in --> 16), 3x3 kernels
self.conv1 = nn.Conv2d(1, 16, 3, padding=1)
# conv layer (depth from 16 --> 4), 3x3 kernels
self.conv2 = nn.Conv2d(16, 4, 3, padding=1)
# pooling layer to reduce x-y dims by two; kernel and stride of 2
self.pool = nn.MaxPool2d(2, 2)
# The changes
self.conv3 = nn.Conv2d(4, 1, 1, 2)
# or
self.conv3 = nn.Conv2d(4, 1, 1)
self.maxpool2d = nn.MaxPool2d((2, 2))
推荐阅读
- javascript - 如何从文本呈现 react-router Link 组件?
- excel - Excel VBA - 用联合擦除行?
- javascript - 如何通过 webpack 和 file-loader 在两个捆绑 (scss) 文件中使用一个图像?最后一个返回 undefined
- parallel-processing - 为什么当 map(fib,array) 类型稳定时 pmap(fib,array) 会导致类型不稳定?
- firefox - 使用 Firefox 编写网站交互脚本
- python - 如何使用 Python 创建一个函数,该函数将在字典中为数据表中的每一行返回一个值?
- ruby-on-rails - Rails:在不同的控制器中调用模型中的方法
- .net - 尽管使用了 Overridable/Overrides,但实现 ToString 并不会覆盖原始 ToString 方法。我想念什么?
- javascript - 如何向 Vue 导入其组件块的路径添加前缀?
- javascript - UI 焦点错误,选项卡后焦点跳回上次编辑的文本框