python - Tensorboard 权重直方图仅最后一层可见变化

标签 python tensorflow neural-network tensorboard

我在我的网络中添加了一个 TensorBoard 可视化,并注意到只有异常值发生了很大变化。为什么网络的权重变化不大?这在叠加直方图中尤为明显。

直方图 weight histogram 相同但叠加 View overlay

我的模型

def neural_network_model(inputdata):
    """The blueprint of the network and the tensorboard information
        :param inputdata: the placeholder for the inputdata
        :returns: the output of the network?
            """
    W1 = tf.get_variable("W1", shape=[set.input, nodes_h1],
                         initializer=tf.contrib.layers.xavier_initializer())
    B1 = tf.get_variable("B1", shape=[nodes_h1],
                         initializer=tf.random_normal_initializer())
    layer1 = tf.matmul(inputdata, W1)
    layer1_bias = tf.add(layer1, B1)
    layer1_act = tf.nn.relu(layer1)

    W2 = tf.get_variable("W2", shape=[nodes_h1, nodes_h2],
                         initializer=tf.contrib.layers.xavier_initializer())
    B2 = tf.get_variable("B2", shape=[nodes_h2],
                         initializer=tf.random_normal_initializer())
    layer2 = tf.matmul(layer1_act, W2)
    layer2_bias = tf.add(layer2, B2)
    layer2_act = tf.nn.relu(layer2)

    W3 = tf.get_variable("W3", shape=[nodes_h2, nodes_h3],
                         initializer=tf.contrib.layers.xavier_initializer())
    B3 = tf.get_variable("B3", shape=[nodes_h3],
                         initializer=tf.random_normal_initializer())

    layer3 = tf.matmul(layer2_act, W3)
    layer3_bias = tf.add(layer3, B3)
    layer3_act = tf.nn.relu(layer3)
    WO = tf.get_variable("WO", shape=[nodes_h3, set.output],
                         initializer=tf.contrib.layers.xavier_initializer())
    layerO = tf.matmul(layer3_act, WO)

    with tf.name_scope('Layer1'):
        tf.summary.histogram("weights", W1)
        tf.summary.histogram("layer", layer1)
        tf.summary.histogram("bias", layer1_bias)
        tf.summary.histogram("activations", layer1_act)
    with tf.name_scope('Layer2'):
        tf.summary.histogram("weights", W2)
        tf.summary.histogram("layer", layer2)
        tf.summary.histogram("bias", layer2_bias)
        tf.summary.histogram("activations", layer2_act)
    with tf.name_scope('Layer3'):
        tf.summary.histogram("weights", W3)
        tf.summary.histogram("layer", layer3)
        tf.summary.histogram("bias", layer3_bias)
        tf.summary.histogram("activations", layer3_act)
    with tf.name_scope('Output'):
        tf.summary.histogram("weights", WO)
        tf.summary.histogram("layer", layerO)
    return layerO

我对训练过程的理解是应该调整权重,这在图像中几乎没有发生。然而,损失已经完成,我已经对网络进行了 10000 次训练,所以我预计总体上会有更多变化。尤其是我不明白的权重缺乏变化。

img

最佳答案

我的神经网络中的权重直方图也遇到过类似的问题。尽管 Relu 确实处理了隐藏层的消失梯度问题,但您应该检查您的学习率并确保每个变量的更新不会太小。这很可能会导致接近于零的更新,从而导致随着时间的推移发生微不足道的变化。您可以使用以下代码段简单地检查每一层的渐变:

def replace_none_with_zero(tensor):
   return[0 if i==None else i for i in tensor]

with tf.name_scope('Gradients'):
   gradient_for_variable_of_interest=replace_none_with_zero(
                              tf.gradients(loss,[variable_of_interest]))

然后通过在梯度上调用 tf.summary.histogram 来检查 tensorboard 中的梯度。

关于python - Tensorboard 权重直方图仅最后一层可见变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45059249/

相关文章:

python - 如何自定义 Repl.it 以不使用诗歌?

python - 为什么 python 调试器在使用 Pycharm 时总是会超时等待 113 上的响应?

python - Tensorflow- 属性错误 : 'KeepAspectRatioResizer' object has no attribute 'per_channel_pad_value'

python - 尝试在 Tensorflow 中使用未初始化的值变量(使用了 sess.run(tf.global_variables_initializer()) !)

Python/Numpy - 矩阵乘以一个二维数组和另一个二维数组的每一行

python - 如何从 dtype 为字符串的 tf.tensor 中获取字符串值

python - 为什么 Mean Average Percentage Error(mape) 非常高?

R:nrow[w] * ncol[w] 中的错误:二元运算符的非数字参数,同时使用神经网络包

neural-network - 为什么我们在计算机中使用神经网络?

python - 如何混合不平衡的数据集以达到每个标签所需的分布?