首先,TensorFlow中封装了全连接层函数 tf.layers.dense(),方便了开发者自己手动构造权重矩阵 W W W和偏移矩阵 b b b,利用矩阵乘法实现全连接层。
tf.layers.dense( input, units=k )会在内部自动生成一个权矩阵:kernel 和偏移项:bias,
例如:
tf.layers.dense()省略了下述过程(省略部分代码),拿简单神经网络手写数字识别案例举例
# 获取真实的数据
mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True)
# 1、建立数据的占位符 x [None, 784] y_true [None, 10]
with tf.compat.v1.variable_scope("data"):
x = tf.compat.v1.placeholder(tf.compat.v1.float32, [None, 784])
y_true = tf.compat.v1.placeholder(tf.compat.v1.int32, [None, 10])
# 2、建立一个全连接层的神经网络 w [784, 10] b [10]
with tf.compat.v1.variable_scope("fc_model"):
# 随机初始化权重和偏置
weight = tf.compat.v1.Variable(tf.compat.v1.random.normal([784, 10], mean=0.0, stddev=1.0), name="w")
bias = tf.compat.v1.Variable(tf.compat.v1.constant(0.0, shape=[10]))
# 预测None个样本的输出结果matrix [None, 784]* [784, 10] + [10] = [None, 10]
y_predict = tf.compat.v1.matmul(x, weight) + bias
tf.layers.dense(inputs, units, activation=None, use_bias=True, kernel_initializer=None,
bias_initializer=tf.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None,
activity_regularizer=None, trainable=True, name=None, reuse=None
)
# 1. 调用tf.compat.v1.layers.dense计算
input = tf.compat.v1.reshape(tf.constant([[1., 2.], [2., 3.]]), shape=[4, 1])
b1 = tf.compat.v1.layers.dense(input,
units=2,
kernel_initializer=tf.constant_initializer(value=2), # shape: [1,2]
bias_initializer=tf.constant_initializer(value=1)) # shape: [4,2]
# 2. 采用矩阵相乘的方式计算
kernel = tf.compat.v1.reshape(tf.constant([2., 2.]), shape=[1, 2])
bias = tf.compat.v1.reshape(tf.constant([1., 1., 1., 1., 1., 1., 1., 1.]), shape=[4, 2])
b2 = tf.compat.v1.add(tf.matmul(input, kernel), bias)
with tf.compat.v1.Session()as sess:
sess.run(tf.compat.v1.global_variables_initializer())
print(sess.run(b1))
print(sess.run(b2))
输出:
[[3. 3.]
[5. 5.]
[5. 5.]
[7. 7.]]
[[3. 3.]
[5. 5.]
[5. 5.]
[7. 7.]]
inputs = tf.compat.v1.ones([4, 10])
inputs2 = tf.compat.v1.ones([3, 30])
a = tf.compat.v1.layers.dense(inputs, 6)
b = tf.compat.v1.layers.dense(inputs2, 7)
print(inputs)
print(inputs2)
print("*"*10)
print(a)
print(b)
输出:
Tensor("ones:0", shape=(4, 10), dtype=float32)
Tensor("ones_1:0", shape=(3, 30), dtype=float32)
**********
Tensor("dense/BiasAdd:0", shape=(4, 6), dtype=float32)
Tensor("dense_1/BiasAdd:0", shape=(3, 7), dtype=float32)