For brevity, let P = labels and Q = predictions. The
Kullback-Leibler divergence KL(P||Q) is:
KL(P||Q)=P*log(P)-P*log(Q)
For the usage of weights and reduction, please refer to tf.losses.
Args
labels
Tensor of type float32 or float64, with shape [d1, ..., dN,
num_classes], represents the target distribution.
predictions
Tensor of the same type and shape as labels, represents
the predicted distribution.
axis
The dimension along which the KL divergence is computed. The values
of labels and predictions along axis should meet the requirements
of a multinomial distribution.
weights
(optional) Tensor whose rank is either 0, or the same as that of
labels, and must be broadcastable to labels (i.e., all dimensions must
be either 1, or the same as the corresponding losses dimension).
scope
The scope for the operations performed in computing the loss.
loss_collection
Collection to which the loss will be added.
reduction
Type of reduction to apply to the loss.
Returns
Weighted loss floatTensor. If reduction is NONE, this has the same
shape as labels, otherwise, it is a scalar.
Raises
InvalidArgumentError
If labels or predictions don't meet the
requirements of a multinomial distribution.
ValueError
If axis is None, if the shape of predictions doesn't
match that of labels, or if the shape of weights is invalid.
[null,null,["Last updated 2024-01-12 UTC."],[],[],null,["# nsl.lib.kl_divergence\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/neural-structured-learning/blob/v1.4.0/neural_structured_learning/lib/distances.py#L50-L112) |\n\nAdds a KL-divergence to the training procedure. \n\n nsl.lib.kl_divergence(\n labels,\n predictions,\n axis=None,\n weights=1.0,\n scope=None,\n loss_collection=tf.compat.v1.GraphKeys.LOSSES,\n reduction=tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS\n )\n\nFor brevity, let `P = labels` and `Q = predictions`. The\nKullback-Leibler divergence `KL(P||Q)` is: \n\n KL(P||Q) = P * log(P) - P * log(Q)\n\n| **Note:** the function assumes that `predictions` and `labels` are the values of a multinomial distribution, i.e., each value is the probability of the corresponding class.\n\nFor the usage of `weights` and `reduction`, please refer to `tf.losses`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `labels` | `Tensor` of type `float32` or `float64`, with shape `[d1, ..., dN, num_classes]`, represents the target distribution. |\n| `predictions` | `Tensor` of the same type and shape as `labels`, represents the predicted distribution. |\n| `axis` | The dimension along which the KL divergence is computed. The values of `labels` and `predictions` along `axis` should meet the requirements of a multinomial distribution. |\n| `weights` | (optional) `Tensor` whose rank is either 0, or the same as that of `labels`, and must be broadcastable to `labels` (i.e., all dimensions must be either `1`, or the same as the corresponding `losses` dimension). |\n| `scope` | The scope for the operations performed in computing the loss. |\n| `loss_collection` | Collection to which the loss will be added. |\n| `reduction` | Type of reduction to apply to the loss. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Weighted loss `float` `Tensor`. If `reduction` is `NONE`, this has the same shape as `labels`, otherwise, it is a scalar. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|------------------------|-----------------------------------------------------------------------------------------------------------------------------|\n| `InvalidArgumentError` | If `labels` or `predictions` don't meet the requirements of a multinomial distribution. |\n| `ValueError` | If `axis` is `None`, if the shape of `predictions` doesn't match that of `labels`, or if the shape of `weights` is invalid. |\n\n\u003cbr /\u003e"]]