函数原型
tf.keras.layers.Dense(units, 
					  activation=None, 
					  use_bias=True,
					  kernel_initializer='glorot_uniform',
					  bias_initializer='zeros', 
					  kernel_regularizer=None,
					  bias_regularizer=None,
					  activity_regularizer=None, 
					  kernel_constraint=None,
					  bias_constraint=None, 
					  **kwargs
)
函数说明

全连接层用于将一个特征空间线性变化到另一个特征空间,在整个神经网络中起到分类器的作用。
在这里插入图片描述
全连接层的每一个节点都与上一层每个节点连接,把前一层的输出特征都综合起来,因为用到了所有的局部特征,所有叫全连接。全连接层一般用于模型的最后一层,通过将样本的隐藏特征空间映射到样本标记空间,最终实现样本分类的目的。

全连接层在tensorflow里面用Dense函数来定义,实现的运算为output = activation(dot(input, kernel) + bias)。其中input为输入数据,kernel为核矩阵,由参数kernel_initializer定义,dot为两个矩阵的点积运算, bias为偏置矩阵,由参数bias_initializer定义,activation为激活函数。

参数use_bias表示是否使用偏置矩阵,默认为True。如果use_bias=False,则输出矩阵的运算结果output = activation(dot(input, kernel))

还有第一个参数units,定义了输出空间的维度。怎么理解呢?如果上一层输出形状为(None, 32),通过Dense(units=16)层后,输出形状为(None, 16);如果输出形状为(None, 32, 32),通过Dense(units=16)层后,输出形状为(None, 32, 16)。更准确的说,这个参数改变了输出空间最后一维的大小。

如果用Dense层作为第一层,需要提供一个input_shape参数来描述输入张量的形状。

函数用法

第一个例子

model = tf.keras.Sequential([
    # 输入层,输入形状为(None, 32, 64)
    tf.keras.layers.InputLayer(input_shape=(32, 64)),
    # 全连接层,输出最后一维维度为32,激活函数为relu,输出形状为(None, 32, 32)
    tf.keras.layers.Dense(32, activation="relu")
])
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 32, 32)            2080      
                                                                 
=================================================================
Total params: 2,080
Trainable params: 2,080
Non-trainable params: 0
_________________________________________________________________

第二个例子

model = tf.keras.Sequential([
    # 输入层,输入形状为(None, 32, 64)
    tf.keras.layers.InputLayer(input_shape=(32, 64)),
    # 全连接层,输出最后一维维度为32,激活函数为relu,输出形状为(None, 32, 32)
    tf.keras.layers.Dense(32, activation="relu"),
    # 展平层,降低输出维度
    tf.keras.layers.Flatten(),
    # 全连接层,激活函数为softmax,用于多分类的情形,输出形状为(None, 4)
    tf.keras.layers.Dense(4, activation="softmax")
])
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 32, 32)            2080      
                                                                 
 flatten (Flatten)           (None, 1024)              0         
                                                                 
 dense_1 (Dense)             (None, 4)                 4100      
                                                                 
=================================================================
Total params: 6,180
Trainable params: 6,180
Non-trainable params: 0
_________________________________________________________________

第三个例子

model = tf.keras.Sequential([
    # 输入层,输入形状为(None, 32, 64)
    tf.keras.Input(shape=(32, 64)),
    # 全连接层,输出最后一维维度为32,激活函数为relu,输出形状为(None, 32, 32)
    tf.keras.layers.Dense(32, activation="relu"),
    # 展平层,降低输出维度
    tf.keras.layers.Flatten(),
    # 全连接层,激活函数为relu, 输出形状为(None, 16)
    tf.keras.layers.Dense(16, activation="relu"),
    # 全连接层,激活函数为sigmoid,用于二分类的情形,输出形状为(None, 1)
    tf.keras.layers.Dense(1, activation="sigmoid")
])
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 32, 32)            2080      
                                                                 
 flatten (Flatten)           (None, 1024)              0         
                                                                 
 dense_1 (Dense)             (None, 16)                16400     
                                                                 
 dense_2 (Dense)             (None, 2)                 34        
                                                                 
=================================================================
Total params: 18,514
Trainable params: 18,514
Non-trainable params: 0
_________________________________________________________________
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐