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

Function binary_crossentropy

tensorflow/python/keras/backend.py:4455–4484  ·  view source on GitHub ↗

Binary crossentropy between an output tensor and a target tensor. Arguments: target: A tensor with the same shape as `output`. output: A tensor. from_logits: Whether `output` is expected to be a logits tensor. By default, we consider that `output` encodes a p

(target, output, from_logits=False)

Source from the content-addressed store, hash-verified

4453
4454@keras_export('keras.backend.binary_crossentropy')
4455def binary_crossentropy(target, output, from_logits=False):
4456 """Binary crossentropy between an output tensor and a target tensor.
4457
4458 Arguments:
4459 target: A tensor with the same shape as `output`.
4460 output: A tensor.
4461 from_logits: Whether `output` is expected to be a logits tensor.
4462 By default, we consider that `output`
4463 encodes a probability distribution.
4464
4465 Returns:
4466 A tensor.
4467 """
4468 if not from_logits:
4469 if (isinstance(output, (ops.EagerTensor, variables_module.Variable)) or
4470 output.op.type != 'Sigmoid'):
4471 epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
4472 output = clip_ops.clip_by_value(output, epsilon_, 1. - epsilon_)
4473
4474 # Compute cross entropy from probabilities.
4475 bce = target * math_ops.log(output + epsilon())
4476 bce += (1 - target) * math_ops.log(1 - output + epsilon())
4477 return -bce
4478 else:
4479 # When sigmoid activation function is used for output operation, we
4480 # use logits from the sigmoid function directly to compute loss in order
4481 # to prevent collapsing zero when training.
4482 assert len(output.op.inputs) == 1
4483 output = output.op.inputs[0]
4484 return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
4485
4486
4487@keras_export('keras.backend.sigmoid')

Callers

nothing calls this directly

Calls 3

_constant_to_tensorFunction · 0.85
epsilonFunction · 0.85
logMethod · 0.45

Tested by

no test coverage detected