title: How to resolve runtime error due to size mismatch in PyTorch?
body: <p>I am trying to implement a simple autoencoder using <code>PyTorch</code>. My dataset consists of 256 x 256 x 3 images. I have built a <code>torch.utils.data.dataloader.DataLoader</code> object which has the image stored as tensor. When I run the autoencoder, I get a runtime error:</p>
  
  <blockquote>
    <p>size mismatch, m1: [76800 x 256], m2: [784 x 128] at
    /Users/soumith/minicondabuild3/conda-bld/pytorch_1518371252923/work/torch/lib/TH/generic/THTensorMath.c:1434</p>
  </blockquote>
  
  <p>These are my hyperparameters:</p>
  
  <pre><code>batch_size=100,
  learning_rate = 1e-3,
  num_epochs = 100
  </code></pre>
  
  <p>Following is the architecture of my auto-encoder:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This is the code I used to run the model:</p>
  
  <pre><code>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))
  </code></pre>