Tensorflow学习之tf.layers.dense()

1. tf.layers.dense()

首先,TensorFlow中封装了全连接层函数 tf.layers.dense(),方便了开发者自己手动构造权重矩阵W和偏移矩阵b,利用矩阵乘法实现全连接层。

1.1 原理

tf.layers.dense( input, units=k )会在内部自动生成一个权矩阵:kernel 和偏移项:bias,
例如:

  • 对于尺寸为[m, n]的二维张量input, tf.layers.dense()会生成:尺寸为[n, k]的权矩阵:kernel,和尺寸为[m, k] 的偏移项:bias。内部的计算过程为y = input * kernel + bias,输出值y的维度为[m, k]。

tf.layers.dense()省略了下述过程(省略部分代码),拿简单神经网络手写数字识别案例举例

  • 每张照片为28%5Ctimes28像素,即特征值为28%5Ctimes28%3D784%E4%B8%AA
  • 假设我们有[None,]个样本。那么input:[None,784]
  • bias可以确定为一个 = [1,10]的数据。
  • 那么,kernel = [784, 10]的二维张量。
# 获取真实的数据
    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

1.2 相关参数

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
                )

  • inputs:tf.layers.dense 的Tensor输入
    . 【样本数、特征值】
  • units:神经元的个数,【输出空间的维数,改变inputs的最后一维】
    。【手写数字识别中 lable的个数为:10】
  • activation:激活函数,例如:relu函数
    。None:以默认为保持线性激活。
  • use_bias:Boolean,表示该层是否使用偏差。
  • kernel_initializer:权重矩阵的初始化函数;默认None,则使用tf.get_variable使用的默认初始化程序初始化权重。
  • bias_initializer:偏置的初始化函数。
  • kernel_regularizer:权重矩阵的正则化函数。
  • bias_regularizer:正规函数的偏差。
  • activity_regularizer:输出的正则化函数。
  • kernel_constraint:默认:None
  • bias_constraint:默认:None
  • trainable:Boolean,如果为True,还将变量添加到图集合GraphKeys.TRAINABLE_VARIABLES中
    (请参阅参考资料tf.Variable)。
  • name:String,图层的名称
    ;具有相同名称的图层将共享权重,但为了避免错误,在这种情况下,我们需要reuse=True。
  • reuse:是否以同一名称重用前一层的权重。

1.3对比:

# 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)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2022年3月18日 下午1:59
下一篇 2022年3月18日 下午2:18

相关推荐