Масштабируется ли он дважды в коде keras categoryorical_crossentropy? - PullRequest
0 голосов
/ 01 июля 2019

Я вижу, categorical_crossentropy реализовано в Keras следующим образом:

def categorical_crossentropy(target, output, from_logits=False, axis=-1):
    """Categorical crossentropy between an output tensor and a target tensor.
    # Arguments
        target: A tensor of the same shape as `output`.
        output: A tensor resulting from a softmax
            (unless `from_logits` is True, in which
            case `output` is expected to be the logits).
        from_logits: Boolean, whether `output` is the
            result of a softmax, or is a tensor of logits.
        axis: Int specifying the channels axis. `axis=-1`
            corresponds to data format `channels_last`,
            and `axis=1` corresponds to data format
            `channels_first`.
    # Returns
        Output tensor.
    # Raises
        ValueError: if `axis` is neither -1 nor one of
            the axes of `output`.
    """
    output_dimensions = list(range(len(output.get_shape())))
    if axis != -1 and axis not in output_dimensions:
        raise ValueError(
            '{}{}{}'.format(
                'Unexpected channels axis {}. '.format(axis),
                'Expected to be -1 or one of the axes of `output`, ',
                'which has {} dimensions.'.format(len(output.get_shape()))))
    # Note: tf.nn.softmax_cross_entropy_with_logits
    # expects logits, Keras expects probabilities.
    if not from_logits:
        # scale preds so that the class probas of each sample sum to 1
        output /= tf.reduce_sum(output, axis, True)
        # manual computation of crossentropy
        _epsilon = _to_tensor(epsilon(), output.dtype.base_dtype)
        output = tf.clip_by_value(output, _epsilon, 1. - _epsilon)
        return - tf.reduce_sum(target * tf.log(output), axis)

Я не понимаю от

output_dimensions = list (range (len (output.get_shape ())))

до

output / = tf.reduce_sum (output, axis, True).

Я понимаюВыходные данные - это вероятности, тензор, полученный в результате softmax -> Это означает, что масштабируемые преды так, чтобы классы проб каждого образца были равны 1. Почему им нужно масштабировать преды так, чтобы класс проб каждого образца снова снова равнялся 1?Пожалуйста, объясните это.

1 Ответ

0 голосов
/ 01 июля 2019

Поскольку необходимо убедиться, что каждая вероятность находится в диапазоне от 0 до 1, в противном случае вычисление перекрестной энтропии будет неправильным. Это также способ предотвратить ошибки пользователей, когда они создают (ненормализованные) вероятности за пределами этого диапазона.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...