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). fr
(target, output, from_logits=False, axis=-1)
| 4320 | |
| 4321 | @keras_export('keras.backend.categorical_crossentropy') |
| 4322 | def categorical_crossentropy(target, output, from_logits=False, axis=-1): |
| 4323 | """Categorical crossentropy between an output tensor and a target tensor. |
| 4324 | |
| 4325 | Arguments: |
| 4326 | target: A tensor of the same shape as `output`. |
| 4327 | output: A tensor resulting from a softmax |
| 4328 | (unless `from_logits` is True, in which |
| 4329 | case `output` is expected to be the logits). |
| 4330 | from_logits: Boolean, whether `output` is the |
| 4331 | result of a softmax, or is a tensor of logits. |
| 4332 | axis: Int specifying the channels axis. `axis=-1` corresponds to data |
| 4333 | format `channels_last', and `axis=1` corresponds to data format |
| 4334 | `channels_first`. |
| 4335 | |
| 4336 | Returns: |
| 4337 | Output tensor. |
| 4338 | |
| 4339 | Raises: |
| 4340 | ValueError: if `axis` is neither -1 nor one of the axes of `output`. |
| 4341 | |
| 4342 | Example: |
| 4343 | ```python: |
| 4344 | import tensorflow as tf |
| 4345 | from tensorflow.keras import backend as K |
| 4346 | a = tf.constant([1., 0., 0., 0., 1., 0., 0., 0., 1.], shape=[3,3]) |
| 4347 | print("a: ", a) |
| 4348 | b = tf.constant([.9, .05, .05, .5, .89, .6, .05, .01, .94], shape=[3,3]) |
| 4349 | print("b: ", b) |
| 4350 | loss = K.categorical_crossentropy(a, b) |
| 4351 | print('Loss: ', loss) #Loss: tf.Tensor([0.10536055 0.8046684 0.06187541], shape=(3,), dtype=float32) |
| 4352 | loss = K.categorical_crossentropy(a, a) |
| 4353 | print('Loss: ', loss) #Loss: tf.Tensor([1.1920929e-07 1.1920929e-07 1.1920929e-07], shape=(3,), dtype=float32) |
| 4354 | ``` |
| 4355 | """ |
| 4356 | if not from_logits: |
| 4357 | if (isinstance(output, (ops.EagerTensor, variables_module.Variable)) or |
| 4358 | output.op.type != 'Softmax'): |
| 4359 | # scale preds so that the class probas of each sample sum to 1 |
| 4360 | output = output / math_ops.reduce_sum(output, axis, True) |
| 4361 | # Compute cross entropy from probabilities. |
| 4362 | epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype) |
| 4363 | output = clip_ops.clip_by_value(output, epsilon_, 1. - epsilon_) |
| 4364 | return -math_ops.reduce_sum(target * math_ops.log(output), axis) |
| 4365 | else: |
| 4366 | # When softmax activation function is used for output operation, we |
| 4367 | # use logits from the softmax function directly to compute loss in order |
| 4368 | # to prevent collapsing zero when training. |
| 4369 | # See b/117284466 |
| 4370 | assert len(output.op.inputs) == 1 |
| 4371 | output = output.op.inputs[0] |
| 4372 | return nn.softmax_cross_entropy_with_logits_v2( |
| 4373 | labels=target, logits=output, axis=axis) |
| 4374 | |
| 4375 | |
| 4376 | @keras_export('keras.backend.sparse_categorical_crossentropy') |
nothing calls this directly
no test coverage detected