PIXELBANKv9.1.0
Menu

Use nn.Flatten Layer

Problem Statement

Use nn.Flatten to convert multi-dimensional tensors to 1D (per batch).

Background

When connecting convolutional layers to fully connected layers, you need to flatten the spatial dimensions while keeping the batch dimension intact. nn.Flatten does this automatically.

Your Task

Write a function apply_flatten(batch_size, channels, height, width) that creates a random tensor with the given shape, flattens it, and returns information about the shapes before and after.

Output Format

Return a dictionary with keys: "input_shape" (list), "output_shape" (list), "flattened_features" (int).

Example:

Input:
batch_size=2, channels=3, height=4, width=4
Output:
{'input_shape': [2, 3, 4, 4], 'output_shape': [2, 48], 'flattened_features': 48}
Reasoning:

344 = 48 features, batch size 2 preserved

Constraints:

  • Use nn.Flatten()
  • Batch dimension should be preserved
  • Use torch.randn for random tensor
solution.py

Test Results

0/0
Run code to see test results.
Use nn.Flatten Layer - Easy | PixelBank