- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
作为一个更大项目的一部分,我一直在构建一个编程语言检测器,即代码片段的分类器。我的基线模型非常简单:将输入标记化并将片段编码为词袋,或者,在这种情况下,bag-of-tokens,并在这些特征之上制作一个简单的神经网络。
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 左右。一切看起来都很完美。
但后来我注意到我把 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(错误模型)
没有 ReLu(正确模型)
第一个直方图很有意义,大部分概率都接近于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.14286
是 1/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=1
的 tf.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 entryout[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 ofInTopK
differs from theTopK
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/
在其 API documentation ,它说“计算整流线性”。 是 Re(ctified) L(inear)... 那么 U 是什么? 最佳答案 Re(ctified) L(inear) (U)n
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
我看到这里记录了这个“tf.nn.relu”:https://www.tensorflow.org/api_docs/python/tf/nn/relu 但后来我在“model_fn”的这个页面上也看
我只是想知道 的用途和专业之间是否有任何显着差异 Dense(activation='relu') 和 keras.layers.ReLu 后一个如何以及在哪里可以使用?我最好的猜测是在功能 API
我最近在做一个基于 TensorFlow 的 Udacity 深度学习类(class)。 .我有一个简单的 MNIST大约 92% 准确的程序: from tensorflow.examples.tu
我在使用 relu 激活函数时无法实现反向传播。我的模型有两个隐藏层,两个隐藏层都有 10 个节点,输出层有一个节点(因此有 3 个权重,3 个偏差)。除了这个损坏的 backward_prop 函数
由于neuralnet包没有ReLU功能,所以我尝试写ReLU功能的代码。但是有一个错误我不明白。请在下面查看我的代码和错误信息。 relu=0,x,0)} nn =0)} relu <- funct
由于neuralnet包没有ReLU功能,所以我尝试写ReLU功能的代码。但是有一个错误我不明白。请在下面查看我的代码和错误信息。 relu=0,x,0)} nn =0)} relu <- funct
我已经用 relu 作为激活函数在 tensorflow 中编写了一个卷积网络,但是它不是学习(对于评估和训练数据集,损失都是恒定的)。对于不同的激活函数,一切都按其应有的方式工作。 这是创建 nn
我正在使用 python 2.7 并试图更好地了解 tensorflow 。 我正在使用以下代码尝试在 mnist 数据上训练自动编码器,当我使用 sigmoid 激活时,它可以正常(90%),但是当
所以我开始使用 Pytorch,并在 FashionMNIST 数据集上构建一个非常基本的 CNN。我在使用神经网络时注意到一些奇怪的行为,我不知道为什么会发生这种情况,在前向函数中,当我在每个线性层
我正在根据负值和正值的数据训练神经网络。 有没有什么方法可以将数据输入 ReLU 网络,而不将其全部转换为正数,并且有一个单独的输入来表示数据是负数还是正数? 我看到的问题是,输入层的负输入意味着除非
ReLU 函数,在这种情况下,我使用了 leaky ReLU 给我错误的输出。但是使用 sigmoid 函数它给了我一个可接受的输出 这是我的代码: import numpy as np def re
在 keras 文档中,函数 keras.activations.relu(x, alpha=0.0, max_value=None, Threshold=0.0) 定义为: f(x) = max_v
我正在尝试使用神经网络 (Keras) 逼近正弦函数。 是的,我阅读了相关的帖子:) Link 1 Link 2 Link 3 使用四个带 sigmoid 的隐藏神经元和一个带线性激活的输出层效果很好
作为一个更大项目的一部分,我一直在构建一个编程语言检测器,即代码片段的分类器。我的基线模型非常简单:将输入标记化并将片段编码为词袋,或者,在这种情况下,bag-of-tokens,并在这些特征之上制作
我正在实现一个神经网络,并希望使用 ReLU 作为神经元的激活函数。此外,我正在使用 SDG 和反向传播训练网络。我正在用范例 XOR 问题测试神经网络,到目前为止,如果我使用逻辑函数或双曲正切作为激
测试代码: ? 1
最近我观察到,在定义神经网络时,我们多次为每一层定义单独的 ReLU 对象。为什么我们不能在需要的地方使用相同的 ReLU 对象。 例如而不是像这样写- def __init__(self):
我们使用 ReLu 而不是 Sigmoid 激活函数,因为它没有像激活函数一样存在于 Sigmoid 中的梯度消失和爆炸问题, Leaky-ReLU 是 rely 的改进之一。大家都在谈论 Leaky
我是一名优秀的程序员,十分优秀!