添加层 def add_layer()

神经层里常见的参数通常有weightsbiases激励函数

weights为一个in_size行, out_size列的随机变量矩阵

biases的推荐值不为0,在0向量的基础上加0.1

Wx_plus_b, 即神经网络未激活的值

import tensorflow as tf

def add_layer(inputs, in_size, out_size, activation_function=None): 

    Weights = tf.Variable(tf.random_normal([in_size, out_size])) 
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

更新为:

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,
    kernel_constraint=None,
    bias_constraint=None,
    trainable=True,
    name=None,
    reuse=None
)

Last updated