loss_binary_crossentropy {keras3} | R Documentation |
Computes the cross-entropy loss between true labels and predicted labels.
Description
Use this cross-entropy loss for binary (0 or 1) classification applications. The loss function requires the following inputs:
-
y_true
(true label): This is either 0 or 1. -
y_pred
(predicted value): This is the model's prediction, i.e, a single floating-point value which either represents a logit, (i.e, value in[-inf, inf]
whenfrom_logits=TRUE
) or a probability (i.e, value in[0., 1.]
whenfrom_logits=FALSE
).
Usage
loss_binary_crossentropy(
y_true,
y_pred,
from_logits = FALSE,
label_smoothing = 0,
axis = -1L,
...,
reduction = "sum_over_batch_size",
name = "binary_crossentropy",
dtype = NULL
)
Arguments
y_true |
Ground truth values. shape = |
y_pred |
The predicted values. shape = |
from_logits |
Whether to interpret |
label_smoothing |
Float in range |
axis |
The axis along which to compute crossentropy (the features axis).
Defaults to |
... |
For forward/backward compatability. |
reduction |
Type of reduction to apply to the loss. In almost all cases
this should be |
name |
Optional name for the loss instance. |
dtype |
The dtype of the loss's computations. Defaults to |
Value
Binary crossentropy loss value. shape = [batch_size, d0, .. dN-1]
.
Examples
y_true <- rbind(c(0, 1), c(0, 0)) y_pred <- rbind(c(0.6, 0.4), c(0.4, 0.6)) loss <- loss_binary_crossentropy(y_true, y_pred) loss
## tf.Tensor([0.91629073 0.71355818], shape=(2), dtype=float64)
Recommended Usage: (set from_logits=TRUE
)
With compile()
API:
model %>% compile( loss = loss_binary_crossentropy(from_logits=TRUE), ... )
As a standalone function:
# Example 1: (batch_size = 1, number of samples = 4) y_true <- op_array(c(0, 1, 0, 0)) y_pred <- op_array(c(-18.6, 0.51, 2.94, -12.8)) bce <- loss_binary_crossentropy(from_logits = TRUE) bce(y_true, y_pred)
## tf.Tensor(0.865458, shape=(), dtype=float32)
# Example 2: (batch_size = 2, number of samples = 4) y_true <- rbind(c(0, 1), c(0, 0)) y_pred <- rbind(c(-18.6, 0.51), c(2.94, -12.8)) # Using default 'auto'/'sum_over_batch_size' reduction type. bce <- loss_binary_crossentropy(from_logits = TRUE) bce(y_true, y_pred)
## tf.Tensor(0.865458, shape=(), dtype=float32)
# Using 'sample_weight' attribute bce(y_true, y_pred, sample_weight = c(0.8, 0.2))
## tf.Tensor(0.2436386, shape=(), dtype=float32)
# 0.243 # Using 'sum' reduction` type. bce <- loss_binary_crossentropy(from_logits = TRUE, reduction = "sum") bce(y_true, y_pred)
## tf.Tensor(1.730916, shape=(), dtype=float32)
# Using 'none' reduction type. bce <- loss_binary_crossentropy(from_logits = TRUE, reduction = NULL) bce(y_true, y_pred)
## tf.Tensor([0.23515666 1.4957594 ], shape=(2), dtype=float32)
Default Usage: (set from_logits=FALSE
)
# Make the following updates to the above "Recommended Usage" section # 1. Set `from_logits=FALSE` loss_binary_crossentropy() # OR ...('from_logits=FALSE')
## <keras.src.losses.losses.BinaryCrossentropy object> ## signature: (y_true, y_pred, sample_weight=None)
# 2. Update `y_pred` to use probabilities instead of logits y_pred <- c(0.6, 0.3, 0.2, 0.8) # OR [[0.6, 0.3], [0.2, 0.8]]
See Also
Other losses:
Loss()
loss_binary_focal_crossentropy()
loss_categorical_crossentropy()
loss_categorical_focal_crossentropy()
loss_categorical_hinge()
loss_cosine_similarity()
loss_ctc()
loss_dice()
loss_hinge()
loss_huber()
loss_kl_divergence()
loss_log_cosh()
loss_mean_absolute_error()
loss_mean_absolute_percentage_error()
loss_mean_squared_error()
loss_mean_squared_logarithmic_error()
loss_poisson()
loss_sparse_categorical_crossentropy()
loss_squared_hinge()
loss_tversky()
metric_binary_crossentropy()
metric_binary_focal_crossentropy()
metric_categorical_crossentropy()
metric_categorical_focal_crossentropy()
metric_categorical_hinge()
metric_hinge()
metric_huber()
metric_kl_divergence()
metric_log_cosh()
metric_mean_absolute_error()
metric_mean_absolute_percentage_error()
metric_mean_squared_error()
metric_mean_squared_logarithmic_error()
metric_poisson()
metric_sparse_categorical_crossentropy()
metric_squared_hinge()