kl_divergence.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Union
  15. import torch
  16. from torch import Tensor
  17. from typing_extensions import Literal
  18. from torchmetrics.utilities.checks import _check_same_shape
  19. from torchmetrics.utilities.compute import _safe_xlogy
  20. def _kld_update(p: Tensor, q: Tensor, log_prob: bool) -> tuple[Tensor, int]:
  21. """Update and returns KL divergence scores for each observation and the total number of observations.
  22. Args:
  23. p: data distribution with shape ``[N, d]``
  24. q: prior or approximate distribution with shape ``[N, d]``
  25. log_prob: bool indicating if input is log-probabilities or probabilities. If given as probabilities,
  26. will normalize to make sure the distributes sum to 1
  27. """
  28. _check_same_shape(p, q)
  29. if p.ndim != 2 or q.ndim != 2:
  30. raise ValueError(f"Expected both p and q distribution to be 2D but got {p.ndim} and {q.ndim} respectively")
  31. total = p.shape[0]
  32. if log_prob:
  33. measures = torch.sum(p.exp() * (p - q), axis=-1) # type: ignore[call-overload]
  34. else:
  35. p = p / p.sum(axis=-1, keepdim=True) # type: ignore[call-overload]
  36. q = q / q.sum(axis=-1, keepdim=True) # type: ignore[call-overload]
  37. measures = _safe_xlogy(p, p / q).sum(axis=-1) # type: ignore[call-overload]
  38. return measures, total
  39. def _kld_compute(
  40. measures: Tensor, total: Union[int, Tensor], reduction: Literal["mean", "sum", "none", None] = "mean"
  41. ) -> Tensor:
  42. """Compute the KL divergenece based on the type of reduction.
  43. Args:
  44. measures: Tensor of KL divergence scores for each observation
  45. total: Number of observations
  46. reduction:
  47. Determines how to reduce over the ``N``/batch dimension:
  48. - ``'mean'`` [default]: Averages score across samples
  49. - ``'sum'``: Sum score across samples
  50. - ``'none'`` or ``None``: Returns score per sample
  51. Example:
  52. >>> p = torch.tensor([[0.36, 0.48, 0.16]])
  53. >>> q = torch.tensor([[1/3, 1/3, 1/3]])
  54. >>> measures, total = _kld_update(p, q, log_prob=False)
  55. >>> _kld_compute(measures, total)
  56. tensor(0.0853)
  57. """
  58. if reduction == "sum":
  59. return measures.sum()
  60. if reduction == "mean":
  61. return measures.sum() / total
  62. if reduction is None or reduction == "none":
  63. return measures
  64. return measures / total
  65. def kl_divergence(
  66. p: Tensor, q: Tensor, log_prob: bool = False, reduction: Literal["mean", "sum", "none", None] = "mean"
  67. ) -> Tensor:
  68. r"""Compute `KL divergence`_.
  69. .. math::
  70. D_{KL}(P||Q) = \sum_{x\in\mathcal{X}} P(x) \log\frac{P(x)}{Q{x}}
  71. Where :math:`P` and :math:`Q` are probability distributions where :math:`P` usually represents a distribution
  72. over data and :math:`Q` is often a prior or approximation of :math:`P`. It should be noted that the KL divergence
  73. is a non-symmetrical metric i.e. :math:`D_{KL}(P||Q) \neq D_{KL}(Q||P)`.
  74. Args:
  75. p: data distribution with shape ``[N, d]``
  76. q: prior or approximate distribution with shape ``[N, d]``
  77. log_prob: bool indicating if input is log-probabilities or probabilities. If given as probabilities,
  78. will normalize to make sure the distributes sum to 1
  79. reduction:
  80. Determines how to reduce over the ``N``/batch dimension:
  81. - ``'mean'`` [default]: Averages score across samples
  82. - ``'sum'``: Sum score across samples
  83. - ``'none'`` or ``None``: Returns score per sample
  84. Example:
  85. >>> from torch import tensor
  86. >>> p = tensor([[0.36, 0.48, 0.16]])
  87. >>> q = tensor([[1/3, 1/3, 1/3]])
  88. >>> kl_divergence(p, q)
  89. tensor(0.0853)
  90. """
  91. measures, total = _kld_update(p, q, log_prob)
  92. return _kld_compute(measures, total, reduction)