An autoencoder is a type of neural network that learns to copy its input to its output. It does this by first compressing the input into a smaller representation, then reconstructing the original input from that compressed form.
Click to reveal answer
beginner
What are the two main parts of an autoencoder?
The two main parts are the encoder and the decoder. The encoder compresses the input into a smaller code, and the decoder tries to rebuild the original input from that code.
Click to reveal answer
intermediate
Why do autoencoders learn a compressed representation of data?
Because the middle layer (called the bottleneck) has fewer neurons than the input, the network must learn the most important features to represent the data efficiently.
Click to reveal answer
beginner
What loss function is commonly used to train an autoencoder?
Mean Squared Error (MSE) loss is commonly used because it measures how close the reconstructed output is to the original input.
Click to reveal answer
intermediate
Show a simple PyTorch autoencoder architecture code snippet.
2. Which of the following is the correct way to define a simple encoder layer in PyTorch?
easy
A. self.encoder = nn.Linear(input_dim, hidden_dim)
B. self.encoder = nn.Conv2d(hidden_dim, input_dim)
C. self.encoder = nn.ReLU(input_dim, hidden_dim)
D. self.encoder = nn.MaxPool2d(input_dim, hidden_dim)
Solution
Step 1: Identify correct PyTorch layer for encoding
Encoding usually uses a linear layer to reduce dimensions: nn.Linear(input_dim, hidden_dim).
Step 2: Check other options for correctness
Conv2d requires 4D input and different parameters; ReLU is an activation, not a layer; MaxPool2d is for downsampling images, not linear encoding.
Final Answer:
self.encoder = nn.Linear(input_dim, hidden_dim) -> Option A
Quick Check:
Linear layer for encoding = B [OK]
Hint: Use nn.Linear for encoder layers in simple autoencoders [OK]
Common Mistakes:
Using activation functions as layers
Confusing convolution with linear layers
Wrong parameter order in layers
3. Given this PyTorch autoencoder snippet, what is the shape of the output tensor after encoding if input shape is (batch_size=10, features=784)?
class Autoencoder(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Linear(784, 32)
def forward(self, x):
x = self.encoder(x)
return x
model = Autoencoder()
input_tensor = torch.randn(10, 784)
output = model(input_tensor)
medium
A. (784, 32)
B. (10, 32)
C. (10, 784)
D. (32, 784)
Solution
Step 1: Understand input and encoder layer
Input tensor shape is (10, 784). Encoder is nn.Linear(784, 32), so it maps 784 features to 32 features per sample.
Step 2: Calculate output shape after encoder
Output shape keeps batch size 10, features become 32, so output shape is (10, 32).
Final Answer:
(10, 32) -> Option B
Quick Check:
Batch size unchanged, features to 32 = D [OK]
Hint: Linear layer output shape = (batch_size, output_features) [OK]
Common Mistakes:
Swapping batch size and feature dimensions
Confusing input and output feature sizes
Assuming shape changes batch size
4. Identify the error in this autoencoder decoder definition:
class Decoder(nn.Module):
def __init__(self):
super().__init__()
self.decoder = nn.Linear(32, 784)
def forward(self, x):
x = self.decoder(x)
x = nn.ReLU(x)
return x
medium
A. Missing activation function after linear layer
B. The decoder input and output dimensions are swapped
C. nn.ReLU is used incorrectly as a function instead of a layer
D. The forward method should return x before applying ReLU
Solution
Step 1: Check usage of nn.ReLU
nn.ReLU is a class and must be instantiated before use, e.g. nn.ReLU()(x), not nn.ReLU(x).
Step 2: Verify decoder dimensions and flow
Decoder maps 32 to 784 correctly, and activation after linear is common, so no issue there.
Final Answer:
nn.ReLU is used incorrectly as a function instead of a layer -> Option C
Quick Check:
Activation must be instantiated = C [OK]
Hint: Instantiate nn.ReLU() before calling it on tensor [OK]
Common Mistakes:
Calling nn.ReLU as a function directly
Swapping input/output dims in Linear
Forgetting to apply activation
5. You want to build an autoencoder that compresses 1000-dimensional input to 10 dimensions and then reconstructs it. Which architecture below correctly defines encoder and decoder layers in PyTorch?
Decoder in A expands 10 -> 100 -> 1000, matching encoder compression steps. ""class Autoencoder(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Linear(1000, 500),
nn.ReLU(),
nn.Linear(500, 50)
)
self.decoder = nn.Sequential(
nn.Linear(50, 500),
nn.ReLU(),
nn.Linear(500, 1000),
nn.Sigmoid()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x""" has wrong final layer input size (50 to 1000 is correct, but encoder compresses to 50, not 10). Options B and C have reversed or incorrect sizes.
Final Answer:
Option A correctly defines encoder and decoder layers for 1000 to 10 compression and reconstruction -> Option A
Quick Check:
Encoder compresses 1000 to 10, decoder reverses = A [OK]