python - Tensorflow ReLu 不起作用?

标签 python tensorflow neural-network conv-neural-network relu

我已经用 relu 作为激活函数在 tensorflow 中编写了一个卷积网络,但是它不是学习(对于评估和训练数据集,损失都是恒定的)。 对于不同的激活函数,一切都按其应有的方式工作。

这是创建 nn 的代码:

def _create_nn(self):
    current = tf.layers.conv2d(self.input, 20, 3, activation=self.activation)
    current = tf.layers.max_pooling2d(current, 2, 2)
    current = tf.layers.conv2d(current, 24, 3, activation=self.activation)
    current = tf.layers.conv2d(current, 24, 3, activation=self.activation)
    current = tf.layers.max_pooling2d(current, 2, 2)
    self.descriptor = current = tf.layers.conv2d(current, 32, 5, activation=self.activation)
    if not self.drop_conv:
        current = tf.layers.conv2d(current, self.layer_7_filters_n, 3, activation=self.activation)
    if self.add_conv:
        current = tf.layers.conv2d(current, 48, 2, activation=self.activation)

    self.descriptor = current

    last_conv_output_shape = current.get_shape().as_list()
    self.descr_size = last_conv_output_shape[1] * last_conv_output_shape[2] * last_conv_output_shape[3]

    current = tf.layers.dense(tf.reshape(current, [-1, self.descr_size]), 100, activation=self.activation)
    current = tf.layers.dense(current, 50, activation=self.last_activation)

    return current

self.activiation 设置为 tf.nn.relu,self.last_activiation 设置为 tf.nn.softmax

损失函数和优化器在这里创建:

    self._nn = self._create_nn()

    self._loss_function = tf.reduce_sum(tf.squared_difference(self._nn, self.Y), 1)

    optimizer = tf.train.AdamOptimizer()
    self._train_op = optimizer.minimize(self._loss_function)

我尝试通过传递 tf.random_normal_initializer(0.1, 0.1) 作为初始化器来更改变量初始化,但它并没有导致损失函数发生任何变化。

如果您帮助我让这个神经网络与 ReLu 配合使用,我将不胜感激。

编辑:Leaky ReLu 也有同样的问题

编辑:我设法重复相同错误的小例子:

x = tf.constant([[3., 211., 123., 78.]])
v = tf.Variable([0.5, 0.5, 0.5, 0.5])
h_d = tf.layers.Dense(4, activation=tf.nn.leaky_relu)
h = h_d(x)
y_d = tf.layers.Dense(4, activation=tf.nn.softmax)
y = y_d(h)
d = tf.constant([[.5, .5, 0, 0]])

h_d 和 y_d 内核的梯度(使用 tf.gradients 计算)和偏差等于或接近 0

最佳答案

在极不可能的情况下,某个层中的所有激活对于所有样本都可能为负。它们被 ReLU 设置为零,并且没有学习进度,因为 ReLU 负部分的梯度为零。

导致这种情况的可能性更大的是数据集较小、输入特征的奇怪缩放、不适当的权重初始化和/或中间层中的 channel 较少。

这里您使用 random_normal_initializermean=0.1,所以也许您的输入都是负数,因此会映射到负值。尝试mean=0,或重新调整输入特征。

您还可以尝试 Leaky ReLU。也可能是学习率太小或太大。

关于python - Tensorflow ReLu 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50838376/

相关文章:

python - 如何使用 matplotlib/seaborn 和 pandas 数据框创建具有共享 x 轴的倒置条形图

python - Matplotlib imshow 缩放功能?

python - 在 colab 中使用 keras_to_tpu_model 时,TPU 运行速度与 CPU 一样慢

javascript - 使用 Brainjs 训练输出不在特定范围内

machine-learning - 如何将未知大小的时间步输入到 TimeDistributed Keras 层

python - 如何在 Python 中打印返回请求的函数?

python - 如何完全关闭sqlalchemy注销

python - 如何不在 Tensorflow 中重新初始化预训练加载的模型?

python - Tensorflow translate.py导入错误: No module named translate

python - "TypeError: ' 执行 sess.run() 时键入 ' object is not subscriptable"