MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / sparse_categorical_crossentropy

Function sparse_categorical_crossentropy

tensorflow/python/keras/backend.py:4377–4451  ·  view source on GitHub ↗

Categorical crossentropy with integer targets. Arguments: target: An integer tensor. 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

(target, output, from_logits=False, axis=-1)

Source from the content-addressed store, hash-verified

4375
4376@keras_export('keras.backend.sparse_categorical_crossentropy')
4377def sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1):
4378 """Categorical crossentropy with integer targets.
4379
4380 Arguments:
4381 target: An integer tensor.
4382 output: A tensor resulting from a softmax
4383 (unless `from_logits` is True, in which
4384 case `output` is expected to be the logits).
4385 from_logits: Boolean, whether `output` is the
4386 result of a softmax, or is a tensor of logits.
4387 axis: Int specifying the channels axis. `axis=-1` corresponds to data
4388 format `channels_last', and `axis=1` corresponds to data format
4389 `channels_first`.
4390
4391 Returns:
4392 Output tensor.
4393
4394 Raises:
4395 ValueError: if `axis` is neither -1 nor one of the axes of `output`.
4396 """
4397 if not from_logits:
4398 if (isinstance(output, (ops.EagerTensor, variables_module.Variable)) or
4399 output.op.type != 'Softmax'):
4400 epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
4401 output = clip_ops.clip_by_value(output, epsilon_, 1 - epsilon_)
4402 output = math_ops.log(output)
4403 else:
4404 # When softmax activation function is used for output operation, we
4405 # use logits from the softmax function directly to compute loss in order
4406 # to prevent collapsing zero when training.
4407 # See b/117284466
4408 assert len(output.op.inputs) == 1
4409 output = output.op.inputs[0]
4410
4411 if isinstance(output.shape, (tuple, list)):
4412 output_rank = len(output.shape)
4413 else:
4414 output_rank = output.shape.ndims
4415 if output_rank is not None:
4416 axis %= output_rank
4417 if axis != output_rank - 1:
4418 permutation = list(
4419 itertools.chain(range(axis), range(axis + 1, output_rank), [axis]))
4420 output = array_ops.transpose(output, perm=permutation)
4421 elif axis != -1:
4422 raise ValueError(
4423 'Cannot compute sparse categorical crossentropy with `axis={}` on an '
4424 'output tensor with unknown rank'.format(axis))
4425
4426 target = cast(target, 'int64')
4427
4428 # Try to adjust the shape so that rank of labels = 1 - rank of logits.
4429 output_shape = array_ops.shape_v2(output)
4430 target_rank = target.shape.ndims
4431
4432 update_shape = (
4433 target_rank is not None and output_rank is not None and
4434 target_rank != output_rank - 1)

Callers

nothing calls this directly

Calls 12

_constant_to_tensorFunction · 0.85
epsilonFunction · 0.85
get_graphFunction · 0.85
transposeMethod · 0.80
reshapeMethod · 0.80
castFunction · 0.70
flattenFunction · 0.70
_is_symbolic_tensorFunction · 0.70
rangeFunction · 0.50
logMethod · 0.45
formatMethod · 0.45
as_defaultMethod · 0.45

Tested by

no test coverage detected