Skip to content

Commit bc382b4

Browse files
committed
Add details to parameter count
1 parent 7c12777 commit bc382b4

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

pytorch_tutorial/convolutional_neural_network/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ print(model)
197197

198198
### Parameter count
199199

200+
The total number of parameters for this model is obtained by summing the parameter counts for each of its layers:
201+
202+
- [Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) and [LazyLinear](https://pytorch.org/docs/stable/generated/torch.nn.LazyLinear.html) layers have `(in_features + 1) * out_features` parameters (the `+1` accounts for bias).
203+
- For [Conv2d](https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html) layers, each kernel has `in_channels * kernel_size * kernel_size + 1` parameters.
204+
- [MaxPool2d](https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html) and [Flatten](https://pytorch.org/docs/stable/generated/torch.nn.Flatten.html) layers have no parameters.
205+
206+
> [!NOTE]
207+
> The `get_parameter_count()` utility function was defined in a [previous example](../linear_regression/README.md#parameter-count).
208+
209+
---
210+
200211
```python
201212
# Compute and print parameter count
202213
n_params = get_parameter_count(model)

pytorch_tutorial/multilayer_perceptron/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ The total number of parameters for this model is obtained by summing the paramet
140140
# Compute and print parameter count
141141
n_params = get_parameter_count(model)
142142
print(f"Model has {n_params} trainable parameters")
143+
143144
# Linear layers have (in_features + 1) * out_features parameters.
144145
# Hidden layer has (2 + 1) * hidden_layer_dim parameters.
145146
# Output layer has (hidden_layer_dim + 1) * 1 parameters

pytorch_tutorial/multilayer_perceptron/test_multilayer_perceptron.py

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def test_multilayer_perceptron(show_plots=False):
7878
# Compute and print parameter count
7979
n_params = get_parameter_count(model)
8080
print(f"Model has {n_params} trainable parameters")
81+
8182
# Linear layers have (in_features + 1) * out_features parameters.
8283
# Hidden layer has (2 + 1) * hidden_layer_dim parameters.
8384
# Output layer has (hidden_layer_dim + 1) * 1 parameters

0 commit comments

Comments
 (0)