devto 2026-08-07 원문 보기 ↗
When building neural networks, one of the first questions you should ask is:
How many trainable parameters does my model have?
The number of parameters determines:
In this article, we'll learn how to calculate the number of parameters manually and verify the results using TensorFlow 2.x (Keras).
Parameters are the values that the neural network learns during training.
There are two types:
Every neuron has:
Therefore, for a layer with:
Suppose we have:
Architecture:
Input(4)
│
Hidden(5)
│
Output(1)
Each of the 5 neurons receives 4 inputs.
Weights:
4 × 5 = 20
Biases:
5
Total:
20 + 5 = 25
Input = 5
Output neurons = 1
Weights:
5 × 1 = 5
Biases:
1
Total:
5 + 1 = 6
25 + 6 = 31
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(4,)),
tf.keras.layers.Dense(5, activation="relu"),
tf.keras.layers.Dense(1)
])
model.summary()
Output:
Layer (type) Output Shape Param #
dense (None, 5) 25
dense_1 (None, 1) 6
Total params: 31
Perfect match.
Architecture:
Input(8)
│
Hidden(16)
│
Hidden(10)
│
Output(3)
Input = 8
Neurons = 16
Weights = 8 × 16 = 128
Biases = 16
Total = 144
Input = 16
Neurons = 10
Weights = 16 × 10 = 160
Biases = 10
Total = 170
Input = 10
Output neurons = 3
Weights = 10 × 3 = 30
Biases = 3
Total = 33
144 + 170 + 33 = 347
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(8,)),
tf.keras.layers.Dense(16, activation="relu"),
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dense(3, activation="softmax")
])
model.summary()
Output:
Layer (type) Param #
dense 144
dense_1 170
dense_2 33
Total params: 347
Again, the manual calculation matches TensorFlow exactly.
Architecture:
Input(20)
│
Hidden(64)
│
Hidden(32)
│
Hidden(16)
│
Output(5)
(20 × 64) + 64
= 1280 + 64
= 1344
(64 × 32) + 32
= 2048 + 32
= 2080
(32 × 16) + 16
= 512 + 16
= 528
(16 × 5) + 5
= 80 + 5
= 85
1344
+2080
+528
+85
------
4037
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(20,)),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(32, activation="relu"),
tf.keras.layers.Dense(16, activation="relu"),
tf.keras.layers.Dense(5, activation="softmax")
])
model.summary()
Expected output:
Layer (type) Param #
dense 1344
dense_1 2080
dense_2 528
dense_3 85
Total params: 4037
Layers such as:
Dense(32, activation="relu")
or
Dense(10, activation="sigmoid")
have exactly the same number of parameters.
Activation functions like:
perform mathematical operations but do not learn any weights or biases, so they contribute zero trainable parameters.
| Layer | Formula |
|---|---|
| Dense | (inputs × neurons) + neurons |
| Dense (alternative form) | (inputs + 1) × neurons |
| Biases | One per neuron |
| Total Model Parameters | Sum of all layer parameters |
Every Dense layer learns weights and biases.
The parameter count for a Dense layer is:
Parameters = (Input Units × Output Units) + Output Units
Parameters = (Input Units + 1) × Output Units
The output size of one layer becomes the input size of the next layer.
The total number of trainable parameters is the sum of the parameters across all trainable layers.
You can always verify your manual calculations using model.summary() in TensorFlow 2.x with Keras.