gpt4 book ai didi

python - 神经网络 : Mysterious ReLu

转载 作者:太空狗 更新时间:2023-10-30 02:07:40 31 4
gpt4 key购买 nike

作为一个更大项目的一部分,我一直在构建一个编程语言检测器,即代码片段的分类器。我的基线模型非常简单:将输入标记化并将片段编码为词袋,或者,在这种情况下,bag-of-tokens,并在这些特征之上制作一个简单的神经网络。

boxes

NN 的输入是一个固定长度的计数器数组,其中包含最独特的标记,例如 "def" , "self" , "function" , "->" , "const" , "#include"等,是从语料库中自动提取的。这个想法是这些标记对于编程语言来说是非常独特的,所以即使是这种天真的方法也应该得到准确率高。

Input:
def 1
for 2
in 2
True 1
): 3
,: 1

...

Output: python

设置

我很快就获得了 99% 的准确率,并认为这是它按预期工作的标志。这是模型(完整的可运行脚本是 here ):

# Placeholders
x = tf.placeholder(shape=[None, vocab_size], dtype=tf.float32, name='x')
y = tf.placeholder(shape=[None], dtype=tf.int32, name='y')
training = tf.placeholder_with_default(False, shape=[], name='training')

# One hidden layer with dropout
reg = tf.contrib.layers.l2_regularizer(0.01)
hidden1 = tf.layers.dense(x, units=96, kernel_regularizer=reg,
activation=tf.nn.elu, name='hidden1')
dropout1 = tf.layers.dropout(hidden1, rate=0.2, training=training, name='dropout1')

# Output layer
logits = tf.layers.dense(dropout1, units=classes, kernel_regularizer=reg,
activation=tf.nn.relu, name='logits')

# Cross-entropy loss
loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, abels=y))

# Misc reports: accuracy, correct/misclassified samples, etc.
correct_predicted = tf.nn.in_top_k(logits, y, 1, name='in-top-k')
prediction = tf.argmax(logits, axis=1)
wrong_predicted = tf.logical_not(correct_predicted, name='not-in-top-k')
x_misclassified = tf.boolean_mask(x, wrong_predicted, name='misclassified')
accuracy = tf.reduce_mean(tf.cast(correct_predicted, tf.float32), name='accuracy')

输出非常令人鼓舞:

iteration=5  loss=2.580  train-acc=0.34277
iteration=10 loss=2.029 train-acc=0.69434
iteration=15 loss=2.054 train-acc=0.92383
iteration=20 loss=1.934 train-acc=0.98926
iteration=25 loss=1.942 train-acc=0.99609
Files.VAL mean accuracy = 0.99121 <-- After just 1 epoch!

iteration=30 loss=1.943 train-acc=0.99414
iteration=35 loss=1.947 train-acc=0.99512
iteration=40 loss=1.946 train-acc=0.99707
iteration=45 loss=1.946 train-acc=0.99609
iteration=50 loss=1.944 train-acc=0.99902
iteration=55 loss=1.946 train-acc=0.99902
Files.VAL mean accuracy = 0.99414

测试准确率也在 1.0 左右。一切看起来都很完美。

神秘的ReLu

但后来我注意到我把 activation=tf.nn.relu进入最后的密集层 ( logits ),这显然是一个错误:无需丢弃 softmax 之前的负分数,因为它们表示概率较低的类别。零阈值只会人为地增加这些类别的可能性,这将是一个错误。摆脱它只会让模型在正确的类别中更加健壮和自信。

我是这么想的。所以我用 activation=None 代替了它,再次运行模型,然后令人惊讶的事情发生了:性能没有提高。完全没有。事实上,它明显退化:

iteration=5  loss=5.236  train-acc=0.16602
iteration=10 loss=4.068 train-acc=0.18750
iteration=15 loss=3.110 train-acc=0.37402
iteration=20 loss=5.149 train-acc=0.14844
iteration=25 loss=2.880 train-acc=0.18262
Files.VAL mean accuracy = 0.28711

iteration=30 loss=3.136 train-acc=0.25781
iteration=35 loss=2.916 train-acc=0.22852
iteration=40 loss=2.156 train-acc=0.39062
iteration=45 loss=1.777 train-acc=0.45312
iteration=50 loss=2.726 train-acc=0.33105
Files.VAL mean accuracy = 0.29362

准确率随着训练的提高而提高,但从未超过 91-92%。我来回改了几次激活,改变不同的参数(层大小、dropout、正则化器、额外层,任何东西)并且总是有相同的结果:“错误”模型立即达到 99%,而“正确”模型在 50 个 epoch 后勉强达到 90%。根据tensorboard,权重分布没有太大差异:梯度没有消失,两个模型都学习了通常。

这怎么可能?最终的 ReLu 怎么能让一个模型如此优越?特别是如果这个 ReLu 是一个错误?

最佳答案

预测分布

在玩了一会儿之后,我决定可视化这两个模型的实际预测分布:

predicted_distribution = tf.nn.softmax(logits, name='distribution')

下面是分布的直方图以及它们如何随时间演变。

使用 ReLu(错误模型)

predicted-distribution

没有 ReLu(正确模型)

predicted-distribution

第一个直方图很有意义,大部分概率都接近于0。但 ReLu 模型的直方图可疑:经过几次迭代后,值似乎集中在 0.15 附近。打印实际预测证实了这个想法:

[0.14286 0.14286 0.14286 0.14286 0.14286 0.14286 0.14286]
[0.14286 0.14286 0.14286 0.14286 0.14286 0.14286 0.14286]

我有 7 个类(class)(当时针对 7 种不同的语言),0.142861/7。事实证明,“完美”模型学会了输出0 logits,这反过来转化为统一的预测。

但是这个分布如何被报告为 99% 准确?

tf.nn.in_top_k

在深入了解 tf.nn.in_top_k 之前我检查了另一种计算精度的方法:

true_correct = tf.equal(tf.argmax(logits, 1), tf.cast(y, tf.int64))
alternative_accuracy = tf.reduce_mean(tf.cast(true_correct, tf.float32))

... 对最高预测类别和基本事实进行诚实比较。结果是这样的:

iteration=2  loss=3.992  train-acc=0.13086  train-alt-acc=0.13086
iteration=4 loss=3.590 train-acc=0.13086 train-alt-acc=0.12207
iteration=6 loss=2.871 train-acc=0.21777 train-alt-acc=0.13672
iteration=8 loss=2.466 train-acc=0.37695 train-alt-acc=0.16211
iteration=10 loss=2.099 train-acc=0.62305 train-alt-acc=0.10742
iteration=12 loss=2.066 train-acc=0.79980 train-alt-acc=0.17090
iteration=14 loss=2.016 train-acc=0.84277 train-alt-acc=0.17285
iteration=16 loss=1.954 train-acc=0.91309 train-alt-acc=0.13574
iteration=18 loss=1.956 train-acc=0.95508 train-alt-acc=0.06445
iteration=20 loss=1.923 train-acc=0.97754 train-alt-acc=0.11328

确实,k=1tf.nn.in_top_k 很快偏离了正确的准确度,并开始报告幻想的 99% 值。那么它实际上做了什么?这是the documentation说:

Says whether the targets are in the top K predictions.

This outputs a batch_size bool array, an entry out[i] is true if the prediction for the target class is among the top k predictions among all predictions for example i. Note that the behavior of InTopK differs from the TopK op in its handling of ties; if multiple classes have the same prediction value and straddle the top-k boundary, all of those classes are considered to be in the top k.

就是它是什么。如果概率是统一的(这实际上意味着“我不知道”),那么它们都是正确的。情况更糟,因为如果 logits 分布几乎均匀,softmax 可能会将其转换为完全均匀分布,如这个简单示例所示:

x = tf.constant([0, 1e-8, 1e-8, 1e-9])
tf.nn.softmax(x).eval()
# >>> array([0.25, 0.25, 0.25, 0.25], dtype=float32)

... 这意味着根据 tf.nn.in_top_k 规范,每个几乎一致的预测都可以被认为是“正确的”。

结论

tf.nn.in_top_k 是 tensorflow 中精度测量的危险选择,因为它可能会默默地吞下错误的预测并将它们报告为“正确”。相反,您应该始终使用这个冗长但值得信赖的表达方式:

accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1), tf.cast(y, tf.int64)), tf.float32))

关于python - 神经网络 : Mysterious ReLu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993004/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com