WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

PyTorch Β· Intermediate Β· question 39 of 100

How do you implement a simple autoencoder in PyTorch? Provide an example.?

πŸ“• Buy this interview preparation book: 100 PyTorch questions & answers β€” PDF + EPUB for $5

An autoencoder is a type of neural network that is used to learn a compressed representation of an input data. It consists of two main parts: an encoder that maps the input data to a compressed representation and a decoder that maps the compressed representation back to the original data. The main objective of an autoencoder is to learn the compressed representation that preserves as much information as possible about the input data.

In PyTorch, we can implement a simple autoencoder using the nn.Module class. Here is an example of how to do it:

import torch
import torch.nn as nn

class Autoencoder(nn.Module):
    def __init__(self, input_size, hidden_size):
        super().__init__()
        self.encoder = nn.Linear(input_size, hidden_size)
        self.decoder = nn.Linear(hidden_size, input_size)
        
    def forward(self, x):
        # Encoding
        x = self.encoder(x)
        # Decoding
        x = self.decoder(x)
        return x

In the above code, we define the Autoencoder class that inherits from the nn.Module class. The constructor method β€˜__init__β€˜ takes two arguments: input_size and hidden_size, which represent the size of the input data and the size of the compressed representation respectively. We define the encoder and decoder using the nn.Linear module, which is a fully connected layer in PyTorch. The forward method performs the encoding and decoding steps.

To train the autoencoder, we need to define a loss function and an optimizer. In this case, we can use the mean squared error (MSE) loss as the reconstruction loss, since we want the output of the decoder to be as close as possible to the input data. Here is an example of how to train the autoencoder on a sample dataset:

# Create a sample dataset
data = torch.randn(100, 20)

# Initialize the autoencoder
autoencoder = Autoencoder(input_size=20, hidden_size=10)

# Define the loss function and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(autoencoder.parameters(), lr=0.1)

# Train the autoencoder
for epoch in range(100):
    # Forward pass
    output = autoencoder(data)
    loss = criterion(output, data)
    
    # Backward pass
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    # Print the loss after every 10 epochs
    if (epoch+1) % 10 == 0:
        print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, 100, loss.item()))

In the above code, we create a sample dataset of size (100, 20) and initialize the autoencoder with an input size of 20 and a hidden size of 10. We define the MSE loss as the reconstruction loss and use the Adam optimizer to optimize the parameters of the autoencoder. We train the autoencoder for 100 epochs and print the loss after every 10 epochs.

Finally, we can use the trained autoencoder to encode and decode new data. Here is an example of how to do it:

# Encode new data
new_data = torch.randn(1, 20)
compressed_data = autoencoder.encoder(new_data)

# Decode the compressed data
reconstructed_data = autoencoder.decoder(compressed_data)

print('Input data:', new_data)
print('Compressed data:', compressed_data)
print('Reconstructed data:', reconstructed_data)

In the above code, we create a new data point of size (1, 20), encode it using the encoder of the trained autoencoder, and then decode the compressed representation using the decoder of the autoencoder. We print the original input data, the compressed data, and the reconstructed data.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview β€” then scores it.
πŸ“ž Practice PyTorch β€” free 15 min
πŸ“• Buy this interview preparation book: 100 PyTorch questions & answers β€” PDF + EPUB for $5

All 100 PyTorch questions Β· All topics