Build with nn.Sequential
Problem Statement
Build a neural network using the nn.Sequential container.
Background
nn.Sequential is a convenient container for building simple feed-forward networks. Modules are added in order, and each module's output is automatically passed as input to the next.
Your Task
Write a function build_sequential_net(input_size, hidden_size, output_size) that builds a two-layer network with a ReLU activation between them and returns a dictionary describing it.
Output Format
Return a dictionary with keys: "num_layers" (int), "output_shape" (list β shape for a single sample), "has_relu" (boolean).
Example:
input_size=784, hidden_size=128, output_size=10
{'num_layers': 3, 'output_shape': [1, 10], 'has_relu': True}3 modules: Linear, ReLU, Linear. Output is (1, 10)
Constraints:
- Use nn.Sequential
- Include nn.Linear and nn.ReLU
- Network: Linear β ReLU β Linear
1. Background Knowledge
PyTorch nn.Sequential is a container that chains modules sequentially, automatically passing each module's output as input to the next. This is ideal for feed-forward neural networks where data flows linearly without branches or loops.
Key Components:
- nn.Linear(in_features, out_features): Fully connected layer performing y=xWT+b, where W \in \mathbb{R}^{\text{out}Γ\text{in}} , b \in \mathbb{R}^{\text{out}} .
- nn.ReLU(): Rectified Linear Unit activation, f(x)=max(0,x), introducing non-linearity.
- Network Architecture: Input β Linear β ReLU β Linear β Output, forming a 2-layer MLP (Multi-Layer Perceptron).
Mathematical Forward Pass:
Input: x β β^(1, input_size)
Hidden: h = ReLU(x * Wβ^T + bβ), h β β^(1, hidden_size)
Output: y = h * Wβ^T + bβ, y β β^(1, output_size)
Prerequisites: Basic PyTorch tensor operations, torch.nn module imports, tensor shape manipulation.
2. Algorithm Approach
Direct Construction Pattern:
- Import torch.nn as nn
- Create nn.Sequential([nn.Linear(...), nn.ReLU(), nn.Linear(...)])
- Analyze properties: len(sequential) for layer count, torch.no_grad() inference for shape
Why Sequential? Enables modular, readable network definition vs. manual forward() method.
3. Step-by-Step Strategy
def build_sequential_net(input_size, hidden_size, output_size):
# Step 1: Build Sequential network
net = nn.Sequential(
nn.Linear(input_size, hidden_size), # Layer 1
nn.ReLU(), # Activation
nn.Linear(hidden_size, output_size) # Layer 2
)
# Step 2: Count layers (3 modules total)
num_layers = len(net)
# Step 3: Get output shape for input (1, input_size)
with torch.no_grad():
sample_input = torch.randn(1, input_size)
output = net(sample_input)
output_shape = list(output.shape)
# Step 4: Check for ReLU (inspect modules)
has_relu = any(isinstance(m, nn.ReLU) for m in net)
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.