Keras 如何确定我的模型中的参数数量

数据挖掘 喀拉斯 张量流
2022-02-27 20:20:20

我有以下 keras 模型:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential()
layer_in = keras.Input(shape=(256))
layer1 = layers.Dense(2, activation="relu", name="layer1")
layer2 = layers.Dense(3, activation="relu", name="layer2")
layer3 = layers.Dense(4, name="layer3")
model.add(layer_in)
model.add(layer1)
model.add(layer2)
model.add(layer3)
model.build()

keras.summary()调用时会产生以下内容

Model: "sequential_8"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 layer1 (Dense)              (None, 2)                 514       
                                                                 
 layer2 (Dense)              (None, 3)                 9         
                                                                 
 layer3 (Dense)              (None, 4)                 16        
                                                                 
=================================================================
Total params: 539
Trainable params: 539
Non-trainable params: 0
_________________________________________________________________

Keras 是如何确定层应该分别有 514、9 和 16 个参数的?

我原以为第一层将有 256 个参数,因为输入层layer_in, 被实例化为shape=(256)

1个回答

当您使用密集层时,Keras 将模型中的参数确定为以下计算:

# of params = # of outputs * (# of inputs + 1)

因此,计算如下:

Number of params layer 1 = 2 * (256+1) = 2*257 = 514 Params

Number of params layer 2 = 3 * (2+1) = 3*3 = 9 Params

Number of params layer 3 = 4 * (3+1) = 4*4 = 16 Params