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!
data_loader = torch.utils.data.DataLoader(dataset = training_data, batch_size = 10, shuffle = False)
class Autoencoder_Linear(nn.Module):
def init(self):
super().init()
self.encoder = nn.Sequential( # we use sequential model, where we repeatedly apply linear layers
nn.Linear(28 * 28, 128), # (N, 784) → (N, 128)
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 36),
nn.ReLU(),
nn.Linear(36, 18),
nn.ReLU(),
nn.Linear(18, 9)
)
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
`model = Autoencoder_Linear()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),
lr=1e-1, # learning rate, here: .001
weight_decay=1e-8)
num_epochs = 10
outputs = []
losses = []
for epoch in range(num_epochs):
for (city, _) in data_loader:
# reshaping the city
city = city.reshape(-1, 28*28)
reconstructed = model(city) # reconstructed model
loss = criterion(reconstructed, city) # criterion with reconstructted cities and original cities
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
`
Output:
RuntimeError Traceback (most recent call last)
in
5 for (city, _) in data_loader:
6 # reshaping the city
----> 7 city = city.reshape(-1, 28*28)
8 reconstructed = model(city) # reconstructed model
9 loss = criterion(reconstructed, city) # criterion with reconstructted cities and original cities
RuntimeError: shape ‘[-1, 784]’ is invalid for input of size 20