要查看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
列显示了总的参数数量。