要查看TensorFlow模型的參數,可以使用model.summary()
方法來打印出模型的結構和參數數量。示例代碼如下:
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)
])
# 打印模型的參數
model.summary()
運行以上代碼,會輸出類似如下的結果:
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
_________________________________________________________________
以上結果中,Param #
列顯示了每個層的參數數量,Total params
列顯示了總的參數數量。