No account yet ?
Linear Auto Encoder consists only of linear layers.
Let's build a simple auto-encoder for MNIST in PyTorch where the encoder and decoder consist of a linear layer.
First, the data is passed to an encoder which creates a compressed representation of the input.
Then this representation is passed through a decoder to reconstruct the input data.
Tested in Anaconda and Python 3.7
import torch.nn as nn import torch.nn.functional as F class Autoencoder(nn.Module): def __init__(self, encoding_dim): super(Autoencoder, self).__init__() ## Encoder ## # linear layer (784 -> encoding_dim) self.fc1 = nn.Linear(28 * 28, encoding_dim) ## Decoder ## # linear layer (encoding_dim -> input size) self.fc2 = nn.Linear(encoding_dim, 28*28) def forward(self, x): x = F.relu(self.fc1(x)) # output layer (sigmoid for scaling from 0 to 1) x = F.sigmoid(self.fc2(x)) return x encoding_dim = 32 model = Autoencoder(encoding_dim) import torch import numpy as np from torchvision import datasets import torchvision.transforms as transforms from matplotlib import pyplot as plt # load the training and test datasets train_data = datasets.MNIST(root='data', train=True, download=True, transform=transforms.ToTensor()) test_data = datasets.MNIST(root='data', train=False, download=True, transform=transforms.ToTensor()) batch_size = 20 train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size) test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size) # loss function criterion = nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) n_epochs = 20 for epoch in range(1, n_epochs+1): train_loss = 0.0 for data in train_loader: images, labels = data # flatten images images = images.view(images.size(0), -1) optimizer.zero_grad() # forward pass outputs = model(images) # calculate the loss loss = criterion(outputs, images) # backward pass + optimization loss.backward() optimizer.step() # update running training loss train_loss += loss.item()*images.size(0) # print avg training statistics train_loss = train_loss/len(train_loader) print('Epoch: {} \tTraining Loss: {:.6f}'.format( epoch, train_loss)) dataiter = iter(test_loader) images, labels = dataiter.next() images_flatten = images.view(images.size(0), -1) # get sample outputs output = model(images_flatten) images = images.numpy() output = output.view(batch_size, 1, 28, 28) output = output.detach().numpy() # plot the first ten input images and then reconstructed images fig, axes = plt.subplots(nrows=2, ncols=10, sharex=True, sharey=True, figsize=(25,4)) for images, row in zip([images, output], axes): for img, ax in zip(images, row): ax.imshow(np.squeeze(img), cmap='gray') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False)
Source : https://iq.opengenus.org/autoencoder/
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License