0

I have the following Convolutional Autoencoder setup:

class autoencoder(nn.Module):
def  __init__(self):
    super(autoencoder, self).__init__()
    self.encoder = nn.Sequential(
        nn.Conv2d(1, 16, 3, stride=3, padding=1),  # b, 16, 10, 10
        nn.ReLU(True),
        nn.MaxPool2d(2, stride=2),  # b, 16, 5, 5
        nn.Conv2d(16, 8, 3, stride=2, padding=1),  # b, 8, 3, 3
        nn.ReLU(True),
        nn.MaxPool2d(2, stride=1)  # b, 8, 2, 2                     
                                )

    self.decoder = nn.Sequential(
        nn.ConvTranspose2d(8, 16, 3, stride=2),  # b, 16, 5, 5
        nn.ReLU(True),
        nn.ConvTranspose2d(16, 8, 5, stride=3, padding=1),  # b, 8, 15, 15
        nn.ReLU(True),
        nn.ConvTranspose2d(8, 1, 2, stride=2, padding=1),  # b, 1, 28, 28
        nn.Tanh()          
                                )

This is the main Loop:

for epoch in range(epochs):
running_loss = 0
for data in (train_loader):
    image,_=data
    inputs = image.view(image.size(0),-1)
    optimizer.zero_grad()
    #image = np.expand_dims(img, axis=0)

    outputs = net(inputs)
    loss = criterion(outputs,inputs)
    loss.backward()
    optimizer.step()
    running_loss += loss.data[0]
print('At Iteration : %d   ;  Mean-Squared Error : %f'%(epoch + 1,running_loss/(train_set.train_data.size(0)/batch_size)))

This is the Error:

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [16, 1, 3, 3], but got input of size [1000, 784] instead

This has something to do with the flattening of the image but Im not exactly sure how to deflatten it.

1 Answer 1

1

Why are you "flattening" your input image (2nd line of main loop):

inputs = image.view(image.size(0),-1)

This line turns your 4 dimensional image (batch - channels - height - width) to a two dimensional "flat" vector (batch - c * h * w).
You autoencoder expects its inputs to be 4D and not "flat". just remove this line and you should be okay.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.