繁体   English   中英

如果我使用许多关联的网络,为什么我的交叉熵损失函数会变得很大?

[英]Why does my cross-entropy loss function get huge if I use a network of many relus?

我有这个损失功能:

            loss_main = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(train_logits, train['labels']),
                name='loss_main',
            )

train_logits是通过如下构建的管道定义的:

    def build_logit_pipeline(data, include_dropout):
        # X --> *W1 --> +b1 --> relu --> *W2 --> +b2 ... --> softmax etc...
        pipeline = data

        for i in xrange(len(layer_sizes) - 1):
            last = i == len(layer_sizes) - 2
            with tf.name_scope("linear%d" % i):
                pipeline = tf.matmul(pipeline, weights[i])
                pipeline = tf.add(pipeline, biases[i])

            if not last:
                # insert relu after every one before the last
                with tf.name_scope("relu%d" % i):
                    pipeline = getattr(tf.nn, arg('act-func'))(pipeline)
                    if include_dropout and not arg('no-dropout'):
                        pipeline = tf.nn.dropout(pipeline, 0.5, name='dropout')

        return pipeline

layer_sizesweightsbiases构造像这样:

    def make_weight(from_, to, name=None):
        return tf.Variable(tf.truncated_normal([from_, to], stddev=0.5), name=name)

    def make_bias(to, name=None):
        return tf.Variable(tf.truncated_normal([to], stddev=0.5), name=name)

    layer_sizes = [dataset.image_size**2] + arg('layers') + [dataset.num_classes]
    with tf.name_scope("parameters"):
        with tf.name_scope("weights"):
            weights = [make_weight(layer_sizes[i], layer_sizes[i+1], name="weights_%d" % i)
                       for i in xrange(len(layer_sizes) - 1)]

        with tf.name_scope("biases"):
            biases = [make_bias(layer_sizes[i + 1], name="biases_%d" % i)
                      for i in xrange(len(layer_sizes) - 1)]

如果arg('act-func')是relu,则如果我建立了较长的relus-例如arg('layers')[750, 750, 750, 750, 750, 750] 750,750,750,750,750,750 [750, 750, 750, 750, 750, 750] -那么我的损失函数为巨大:

Global step: 0
Batch loss function: 28593700.000000

如果我的链条较短,例如arg('layers')[750] ,则损失函数较小:

Global step: 0
Batch loss function: 96.377831

我的问题是: 为什么损失函数如此显着不同? 据我了解,logits的输出是softmax'd以导致概率分布。 然后根据该概率分布确定交叉熵。 为什么要更改我具有的功能的数量? 我认为每个网络在开始时都应该同样错误-大约是随机的-因此损失永远不会变得太大。

请注意,这个损失函数包含任何L2损失,因此重量和偏见的数量增加也不会考虑到这一点。

相反,使用arg('act-func')作为tanh ,不会发生这种损失增加-与我期望的差不多。

首先检查softmax的输出。 如果输出是这样的:

[[0., 1.],
 [0., 1.],
 [0., 1.],
 ...
 [0., 1.]]

但是基本事实是这样的:

[[1., 0.],
 [1., 0.],
 [1., 0.],
 ...
 [1., 0.]]

则交叉熵损失将非常大。 根据交叉熵的公式:

-[ylog(a) + (1-y)log(1-a)]

其中y是基本事实, a是softmax的输出。

有时,它是一些“巨大的”特征值,没有进行规范化,从而使这些“错误”的softmax输出。 根据softmax的定义:

exp(z_j)/sum(exp(z_i)) for i=1 to D

其中D是向量z的维,如果存在一些“巨大”分量,则softmax的输出将几乎为0或1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM