【发布时间】:2018-01-26 06:26:26
【问题描述】:
在 Keras 文档中的训练示例中,
https://keras.io/getting-started/sequential-model-guide/#training
使用binary_crossentropy,在网络的最后一层添加sigmoid激活,但有必要在最后一层添加sigmoid吗?正如我在源代码中发现的:
def binary_crossentropy(output, target, from_logits=False):
"""Binary crossentropy between an output tensor and a target tensor.
Arguments:
output: A tensor.
target: A tensor with the same shape as `output`.
from_logits: Whether `output` is expected to be a logits tensor.
By default, we consider that `output`
encodes a probability distribution.
Returns:
A tensor.
"""
# Note: nn.softmax_cross_entropy_with_logits
# expects logits, Keras expects probabilities.
if not from_logits:
# transform back to logits
epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype)
output = clip_ops.clip_by_value(output, epsilon, 1 - epsilon)
output = math_ops.log(output / (1 - output))
return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
Keras 在 Tensorflow 中调用 sigmoid_cross_entropy_with_logits,但在 sigmoid_cross_entropy_with_logits 函数中,又重新计算了 sigmoid(logits)。
https://www.tensorflow.org/versions/master/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits
所以我认为最后添加 sigmoid 是没有意义的,但似乎我在网上找到的所有 Keras 中的二进制/多标签分类示例和教程都添加了 sigmoid 终于。另外我不明白是什么意思
# Note: nn.softmax_cross_entropy_with_logits
# expects logits, Keras expects probabilities.
为什么 Keras 期望概率?它不使用 nn.softmax_cross_entropy_with_logits 函数吗?有意义吗?
谢谢。
【问题讨论】:
标签: tensorflow keras