Creates a criterion that measures the triplet loss given input
tensors
nn_triplet_margin_with_distance_loss(
distance_function = NULL,
margin = 1,
swap = FALSE,
reduction = "mean"
)
(callable, optional): A nonnegative, real-valued function that
quantifies the closeness of two tensors. If not specified,
nn_pairwise_distance()
will be used. Default: None
(float, optional): A non-negative margin representing the minimum difference
between the positive and negative distances required for the loss to be 0. Larger
margins penalize cases where the negative examples are not distant enough from the
anchors, relative to the positives. Default:
(bool, optional): Whether to use the distance swap described in the paper
Learning shallow convolutional feature descriptors with triplet losses by
V. Balntas, E. Riba et al. If TRUE, and if the positive example is closer to the
negative example than the anchor is, swaps the positive example and the anchor in
the loss computation. Default: FALSE
.
(string, optional): Specifies the (optional) reduction to apply to the output:
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied,
'mean'
: the sum of the output will be divided by the number of
elements in the output, 'sum'
: the output will be summed. Default: 'mean'
Input:
Output: A Tensor of shape reduction
is 'none'
, or a scalar
otherwise.
The unreduced loss (i.e., with reduction
set to 'none'
)
can be described as:
where distance_function
;
and reduction
is not 'none'
(default 'mean'
), then:
See also nn_triplet_margin_loss()
, which computes the triplet
loss for input tensors using the
if (torch_is_installed()) {
# Initialize embeddings
embedding <- nn_embedding(1000, 128)
anchor_ids <- torch_randint(1, 1000, 1, dtype = torch_long())
positive_ids <- torch_randint(1, 1000, 1, dtype = torch_long())
negative_ids <- torch_randint(1, 1000, 1, dtype = torch_long())
anchor <- embedding(anchor_ids)
positive <- embedding(positive_ids)
negative <- embedding(negative_ids)
# Built-in Distance Function
triplet_loss <- nn_triplet_margin_with_distance_loss(
distance_function = nn_pairwise_distance()
)
output <- triplet_loss(anchor, positive, negative)
# Custom Distance Function
l_infinity <- function(x1, x2) {
torch_max(torch_abs(x1 - x2), dim = 1)[[1]]
}
triplet_loss <- nn_triplet_margin_with_distance_loss(
distance_function = l_infinity, margin = 1.5
)
output <- triplet_loss(anchor, positive, negative)
# Custom Distance Function (Lambda)
triplet_loss <- nn_triplet_margin_with_distance_loss(
distance_function = function(x, y) {
1 - nnf_cosine_similarity(x, y)
}
)
output <- triplet_loss(anchor, positive, negative)
}
Run the code above in your browser using DataLab