Autoencoder_RuntimeError: shape '[-1, 784]' is invalid for input of size 20

username: Solaris001
cooked: <p>Hi everyone, just recently started to implement an autoencoder model using pytorch. My data is cleaned and ready to get worked with. For the start, I followed a tutorial on autoencoders in pytorch and could not find out where the error is coming from. It’d be great if anyone could help!</p>
<p>data_loader = torch.utils.data.DataLoader(dataset = training_data, batch_size = 10, shuffle = False)</p>
<p>class Autoencoder_Linear(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.encoder = nn.Sequential( # we use sequential model, where we repeatedly apply linear layers<br>
nn.Linear(28 * 28, 128), # (N, 784) → (N, 128)<br>
nn.ReLU(),<br>
nn.Linear(128, 64),<br>
nn.ReLU(),<br>
nn.Linear(64, 36),<br>
nn.ReLU(),<br>
nn.Linear(36, 18),<br>
nn.ReLU(),<br>
nn.Linear(18, 9)<br>
)</p>
<pre><code>    self.decoder = nn.Sequential(
        nn.Linear(9, 18),
        nn.ReLU(),
        nn.Linear(18, 36),
        nn.ReLU(),
        nn.Linear(36, 64),
        nn.ReLU(),
        nn.Linear(64, 128),
        nn.ReLU(),
        nn.Linear(128, 28 * 28),
        nn.Softmax() 
       # Softmax is used as the final layer for multi-class classification problems. 
       # It is used to predict the probability of each output class.
    )

def forward(self, x): # implement forward
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded
</code></pre>
<p>`model = Autoencoder_Linear()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(),<br>
lr=1e-1, # learning rate, here: .001<br>
weight_decay=1e-8)</p>
<p>num_epochs = 10<br>
outputs = []<br>
losses = []<br>
for epoch in range(num_epochs):<br>
for (city, _) in data_loader:<br>
# reshaping the city<br>
city = city.reshape(-1, 28*28)<br>
reconstructed = model(city) # reconstructed model<br>
loss = criterion(reconstructed, city) # criterion with reconstructted cities and original cities</p>
<pre><code>    optimizer.zero_grad() # zero gradients
    loss.backward()
    optimizer.step()

print(f'Epoch:{epoch+1}, Loss:{loss.item():.4f}') # print epoch and loss after epoch
losses.append(loss)
outputs.append(epoch, city, reconstructed) # store in outputs list
</code></pre>
<h2>
<a name="output-1" class="anchor" href="#output-1"></a>`<br>
Output:</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
5     for (city, _) in data_loader:<br>
6         # reshaping the city<br>
----&gt; 7         city = city.reshape(-1, 28*28)<br>
8         reconstructed = model(city) # reconstructed model<br>
9         loss = criterion(reconstructed, city) # criterion with reconstructted cities and original cities</p>
<p>RuntimeError: shape ‘[-1, 784]’ is invalid for input of size 20</p>

username: ptrblck
cooked: <p>Based on the error message it seems your input data has 20 elements while you are expecting samples with <code>784</code> features. If your inputs are expected to have <code>20</code> features only, change the <code>in_features</code> value of your first linear layer to <code>20</code> too.</p>