How to view TensorFlow model parameters?

In TensorFlow, you can use the model.summary() method to view the parameters of the model. This method will print out the structure of the model and the number of parameters in each layer. An example is shown below:

import tensorflow as tf

# 创建模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 打印模型参数
model.summary()

When the above code is run, it will output the structure of the model and the number of parameters in each layer. For example:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 64)                50240     
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650       
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________

In the above output, the number of parameters for each layer will be displayed in the “Param #” column.

bannerAds