tf.keras.layers.Dense相当于在全连接层中添加一个层
Dense实现以下操作:output = activation(dot(input,kernel)+ bias) 其中,activation是用activation参数传递的逐元素激活函数,kernel是该层创建的权重矩阵,bias是由图层创建的偏差向量(仅在use_bias为True时适用)。
用法:
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
)
部分参数说明:
- units:输出的维度大小,改变inputs的最后一维
- activation:激活函数,即神经网络的非线性变化,‘relu’,'selu’等。
- use_bias:使用,bias为True(默认使用),不用,bias改成False即可,是否使用偏置项
示例:
import tensorflow as tf
x = tf.random.normal([4, 784])
net = tf.keras.layers.Dense(512)
out = net(x)
out.shape
Out[9]: TensorShape([4, 512])
net.kernel.shape, net.bias.shape
Out[10]: (TensorShape([784, 512]), TensorShape([512]))
build(input_shape):定义权重的方法
net = tf.keras.layers.Dense(10)
net.bias
# AttributeError: 'Dense' object has no attribute 'bias'
net.get_weights() # w
Out[6]: []
net.weights # b
Out[7]: []
net.build(input_shape = (None, 4))
net.kernel.shape, net.bias.shape
Out[9]: (TensorShape([4, 10]), TensorShape([10]))
net.build(input_shape = (None, 20))
net.kernel.shape, net.bias.shape
Out[12]: (TensorShape([20, 10]), TensorShape([10]))
net.build(input_shape = (2, 4))
net.kernel
Out[14]:
<tf.Variable 'kernel:0' shape=(4, 10) dtype=float32, numpy=
array([[ 0.46936738, 0.61390924, -0.048132 , -0.14290583, 0.06667054,
0.20691943, 0.45904446, -0.1339497 , -0.04281723, -0.62330157],
[-0.49840224, 0.05999857, -0.19198364, -0.21388018, -0.5439534 ,
-0.47478867, -0.6437938 , 0.17085022, 0.3079493 , -0.5329439 ],
[-0.63211656, -0.5696165 , 0.2264719 , 0.15284914, -0.57099473,
0.50631917, 0.47583818, -0.16816309, -0.23766968, -0.3798188 ],
[-0.40847504, -0.18635285, -0.3801926 , 0.4729545 , 0.12844968,
-0.6282965 , 0.5639292 , 0.47344756, 0.5877563 , -0.12006855]],
dtype=float32)>
当input_shape和具体创建时的shape不一致时,会报错
net.build(input_shape = (None, 20))
net.kernel.shape, net.bias.shape
Out[16]: (TensorShape([20, 10]), TensorShape([10]))
# 20 != 12时,会报错
out = net(tf.random.randn((4, 12)))
# AttributeError: module 'tensorflow_core._api.v2.random' has no attribute 'randn'
out = net(tf.random.normal((4, 20)))
out.shape
Out[19]: TensorShape([4, 10])