错误:张量a(892)的大小必须与非单例维度3的张量b(400)的大小匹配



我正在pytorch使用的形状为(3347400(的图像数据集中构建自动编码器模型,当我试图训练我的模型时,我面临着上述错误这是我的编码器和解码器型号

class Autoencoder(nn.Module):
def __init__(self):
super().__init__()        
self.encoder = nn.Sequential(
nn.Conv2d(3, 16, 3, stride=2, padding=1),
nn.ReLU(True),
nn.Conv2d(16, 32, 3, stride=2, padding=1),
nn.ReLU(True),
nn.Conv2d(32, 64, 7) 
)


self.decoder = nn.Sequential(
nn.ConvTranspose2d(64, 32, 7), 
nn.ReLU(True),
nn.ConvTranspose2d(32, 16, 3, stride=3,padding=1), 
nn.ReLU(True),
nn.ConvTranspose2d(16, 3, 3, stride=3,padding=1), 
nn.Sigmoid()
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded

对于我使用MSE((的损失,有人能帮我吗?

制作自动编码器时需要注意的一点是,输入形状和输出形状应该相同。

自动编码器由压缩输入信息的部分(encoder(和将压缩信息转换为原始输入(decoder(的部分组成。因此,输入的形状和输出的形状必须相同。

我修改了你的自动编码器一点,以匹配输入形状和输出形状。此代码将帮助您理解。

此代码使用torch.nn.functional.pad来匹配输入形状和输出形状。

代码:

import torch
import torch.nn as nn
import torch.nn.functional as F
class Autoencoder(nn.Module):
def __init__(self):
super().__init__()        
self.encoder = nn.Sequential(
nn.Conv2d(3, 16, 3, stride=2, padding=1),
nn.ReLU(True),
nn.Conv2d(16, 32, 3, stride=2, padding=1),
nn.ReLU(True),
nn.Conv2d(32, 64, 7) 
)

self.decoder = nn.Sequential(
nn.ConvTranspose2d(64, 32, 7), 
nn.ReLU(True),
nn.ConvTranspose2d(32, 16, 3, stride=2, padding=1), 
nn.ReLU(True),
nn.ConvTranspose2d(16, 3, 3, stride=2, padding=1), 
nn.Sigmoid()
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
# use torch.nn.functional.pad
output = F.pad(decoded, (1,2,1,1), "constant", 0)
return output
x = torch.randn(3,347,400)
model = Autoencoder()
# When you modify the model, the output should be the same as the shape of x.
print(model(x).shape)

或者,可以通过如下调整步幅或填充值,使输入形状和输出形状相同。

代码:

import torch
import torch.nn as nn
class Autoencoder(nn.Module):
def __init__(self):
super().__init__()        
self.encoder = nn.Sequential(
nn.Conv2d(3, 16, 3, stride=1, padding=1),
nn.ReLU(True),
nn.Conv2d(16, 32, 3, stride=1, padding=1),
nn.ReLU(True),
nn.Conv2d(32, 64, 7) 
)

self.decoder = nn.Sequential(
nn.ConvTranspose2d(64, 32, 7), 
nn.ReLU(True),
nn.ConvTranspose2d(32, 16, 3, stride=1, padding=1), 
nn.ReLU(True),
nn.ConvTranspose2d(16, 3, 3, stride=1, padding=1), 
nn.Sigmoid()
)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
model = Autoencoder()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
x = torch.randn(3,347,400)
# When you modify the model, the output should be the same as the shape of x.
print(model(x).shape)

两个结果都是相同的

结果:

torch.Size([3, 347, 400])

相关内容

  • 没有找到相关文章

最新更新