Hi I have a project where I need to create a convolutional autoencoder trained on the MNIST database, but my constraint is that I must not use pooling. My embedding dim is 16 and I need to have a 256 * 16 * 1 * 1 tensor as output of my encoder.
I have written the following class to define my encoder :
class AutoEncoderCNN(nn.Module):
def __init__(self,nb_channels, embedding_dim):
super(AutoEncoderCNN, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(16, 32, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(128, 256, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(128, 256, kernel_size=5, stride=1),
nn.ReLU()
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(256, 128, kernel_size=5, stride=1),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=5, stride=1),
nn.ReLU(),
nn.ConvTranspose2d(64, 32, kernel_size=5, stride=1),
nn.ReLU(),
nn.ConvTranspose2d(32, 16, kernel_size=5, stride=1),
nn.ReLU(),
nn.ConvTranspose2d(16, 1, kernel_size=5, stride=1),
nn.Sigmoid()
)
def encode(self, x):
x = self.encoder(x)# A COMPLETER
return x
def decode(self, x):
x = self.decoder(x)# A COMPLETER
return x
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
But I have this dimension error when I try to train my network :
RuntimeError: Given groups=1, weight of size [32, 1, 5, 5], expected input[1, 256, 28, 28] to have 1 channels, but got 256 channels instead
My loss function :
loss_function = nn.MSELoss(size_average=None, reduce=None, reduction='mean')
My optimize :
optimizer = optim.Adam(modelcnn.parameters(), lr=learning_rate)
My dataloader :
mnistTrainLoader = DataLoader(mnistTrainSet_clean, batch_size=batch_size,shuffle=True, num_workers=0)
My train loop :
# Procédure d'entrainement du model, en utilisant un dataloader, un optimiseur et le nombre d'époques
def train(model, data_loader, opt, n_epochs):
losses = []
i=0
for epoch in range(n_epochs): # Boucle sur les époques
running_loss = 0.0
for features, labels in data_loader:
# A COMPLETER
#Propagation en avant
labels_pred = model(features) # Equivalent à model.forward(features)
#Calcul du coût
loss = loss_function(labels_pred,labels)
#on sauvegarde la loss pour affichage futur
losses.append(loss.item())
#Effacer les gradients précédents
optimizer.zero_grad()
#Calcul des gradients (rétro-propagation)
loss.backward()
# Mise à jour des poids : un pas de l'optimiseur
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 10 == 9:
print('[Epoque : %d, iteration: %5d] loss: %.3f'%
(epoch + 1, i + 1, running_loss / 10))
running_loss = 0.0
i+=1
print('Entrainement terminé')
return losses
I have tried many things to solve it but nothing work. Anyone can help me please ?