Selected topic
Autoencoders
Prefer practical output? Use related tools below while reading.
=====================================
An autoencoder is a type of neural network that can learn to compress and reconstruct data. It's a self-supervised learning technique, meaning it doesn't require labeled data to train.
Suppose we have a dataset of images and want to compress them using an autoencoder. We can use a convolutional neural network (CNN) as the encoder and a transposed CNN as the decoder.
Here's a simple example in PyTorch:
python
import torch
import torch.nn as nnclass Autoencoder(nn.Module):
def __init__(self, num_channels=1, height=28, width=28):
super(Autoencoder, self).__init__()
self.encoder = nn.Sequential(
# Conv2d -> ReLU -> MaxPool2d
nn.Conv2d(num_channels, 10, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten()
)
self.decoder = nn.Sequential(
# Linear -> ReLU -> Reshape
nn.Linear(140, 196),
nn.ReLU(),
nn.Reshape((7, 7, num_channels))
)
def forward(self, x):
encoded = self.encoder(x)
reconstructed = self.decoder(encoded)
return reconstructed
# Initialize the autoencoder and a random input image
model = Autoencoder()
input_image = torch.randn(1, 1, 28, 28)
# Train the autoencoder
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for epoch in range(10):
reconstructed = model(input_image)
loss = criterion(reconstructed, input_image)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("Reconstructed image:", reconstructed.detach().numpy())