Count Model Parameters
Problem Statement
Count the total number of trainable parameters in a neural network.
Background
Understanding model size is important for deployment and memory planning. Each layer's parameters include weights and biases, and the total determines the model's capacity and memory footprint.
Your Task
Write a function count_parameters(in_features, hidden_features, out_features) that builds a two-layer network (with ReLU) and returns a breakdown of its trainable parameters.
Output Format
Return a dictionary with keys: "total_params" (int), "layer1_params" (int), "layer2_params" (int).
Example
in_features=10, hidden_features=20, out_features=5
{'total_params': 325, 'layer1_params': 220, 'layer2_params': 105}- Inputs:
in_features=10is the input vector size,hidden_features=20is the width of the hidden layer, andout_features=5is the number of outputs. The network maps , with ReLU in between. - Layer 1: a linear map from 10 inputs to 20 outputs holds a weight matrix plus one bias per output, so parameters.
- Layer 2: the same rule from 20 inputs to 5 outputs gives a weight matrix plus 5 biases: .
- ReLU is a fixed elementwise function with nothing to learn, so it contributes 0.
- Output: the total is , giving
{'total_params': 325, 'layer1_params': 220, 'layer2_params': 105}. All three values are plain integers. - Notice that the hidden width appears in both layers, so the count grows roughly as : for the first layer alone is .
Constraints
- Linear layer params = in_features * out_features + out_features (bias)
- Use param.numel() to count elements