我试图通过向cifar10.py添加一些代码来编辑我自己的模型,这就是问题所在 .
在cifar10.py中,[tutorial] [1]说:
练习:推断的输出是非规范化的logits . 尝试使用tf.nn.softmax()编辑网络体系结构以返回规范化预测 .
所以我直接将"local4"的输出输入到 tf.nn.softmax()
. 这给了我缩放的logits,这意味着所有logits的总和是1 .
但是在loss函数中,cifar10.py代码使用:
tf.nn.sparse_softmax_cross_entropy_with_logits()
和这个功能的描述说
警告:此操作需要未缩放的日志,因为它在内部执行logmax以提高效率 . 不要使用softmax的输出调用此op,因为它会产生不正确的结果 .
此外,根据描述,作为上述函数的输入的logits必须具有[batch_size,num_classes]的形状,并且它意味着logits应该是未缩放的softmax,如示例代码计算unnormalized softmaxlogit,如下所示 .
# softmax, i.e. softmax(WX + b)
with tf.variable_scope('softmax_linear') as scope:
weights = _variable_with_weight_decay('weights', [192, NUM_CLASSES],
stddev=1/192.0, wd=0.0)
biases = _variable_on_cpu('biases', [NUM_CLASSES],
tf.constant_initializer(0.0))
softmax_linear = tf.add(tf.matmul(local4, weights), biases, name=scope.name)
_activation_summary(softmax_linear)
这是否意味着我不必在代码中使用 tf.nn.softmax
?
1 回答
如果需要,可以在代码中使用
tf.nn.softmax
,但是您必须自己计算损失:实际上,您不使用
tf.nn.softmax
来计算损失 . 但是,如果您想要计算算法的预测并将它们与真实标签进行比较(计算准确性),则需要使用tf.nn.softmax
.