如何解决由于PyTorch中的大小不匹配导致的运行时错误?

Shr*_*yas 4 python autoencoder pytorch

我正在尝试使用简单的自动编码器PyTorch.我的数据集由256 x 256 x 3图像组成.我已经构建了一个torch.utils.data.dataloader.DataLoader将图像存储为张量的对象.当我运行autoencoder时,我收到运行时错误:

大小不匹配,m1:[76800 x 256],m2:[784 x 128] atUsers/soumith/minicondabuild3/conda-bld/pytorch_1518371252923/work/torch/lib/TH/generic/THTensorMath.c:1434

这些是我的超参数:

batch_size=100,
learning_rate = 1e-3,
num_epochs = 100
Run Code Online (Sandbox Code Playgroud)

以下是我的自动编码器的架构:

class autoencoder(nn.Module):
    def __init__(self):
        super(autoencoder, self).__init__()
        self.encoder = nn.Sequential(
            nn.Linear(3*256*256, 128),
            nn.ReLU(),
            nn.Linear(128, 64),
            nn.ReLU(True),
            nn.Linear(64, 12),
            nn.ReLU(True),
            nn.Linear(12, 3))

        self.decoder = nn.Sequential(
            nn.Linear(3, 12),
            nn.ReLU(True),
            nn.Linear(12, 64),
            nn.ReLU(True),
            nn.Linear(64, 128),
            nn.Linear(128, 3*256*256),
            nn.ReLU())

def forward(self, x):
    x = self.encoder(x)
    #x = self.decoder(x)
    return x
Run Code Online (Sandbox Code Playgroud)

这是我用来运行模型的代码:

for epoch in range(num_epochs):
for data in dataloader:
    img = data['image']
    img = Variable(img)
    # ===================forward=====================
    output = model(img)
    loss = criterion(output, img)
    # ===================backward====================
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
# ===================log========================
print('epoch [{}/{}], loss:{:.4f}'
      .format(epoch+1, num_epochs, loss.data[0]))
if epoch % 10 == 0:
    pic = show_img(output.cpu().data)
    save_image(pic, './dc_img/image_{}.jpg'.format(epoch))
Run Code Online (Sandbox Code Playgroud)

pro*_*sti 27

每当你有:

RuntimeError: size mismatch, m1: [a x b], m2: [c x d]
Run Code Online (Sandbox Code Playgroud)

你只需要关心b=c你就完成了:

m1[a x b]哪个是[batch size x in features]

m2[c x d]哪个是[in features x out features]


Was*_*mad 7

如果输入是3 x 256 x 256,则需要将其转换为B x N通过线性层传递:nn.Linear(3*256*256, 128)其中Bbatch_sizeN线性图层输入大小.如果你是在一个时间给予一个形象,你可以塑造你的输入张量转换3 x 256 x 2561 x (3*256*256)如下.

img = img.view(1, -1) # converts [3 x 256 x 256] to 1 x 196608
output = model(img)
Run Code Online (Sandbox Code Playgroud)