We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[docs] Add doctests for metrics
### 📚 Documentation

Following #2230 , I listed the metrics places (suggested by @sdesrozis) where doctests can be added.

#### Metrics

- [x] Average (#2332)
- [x] GeometricAverage (#2332)
- [x] VariableAccumulation (This class is an helper, no test needed)
- [x] Accuracy (#2345)
- [x] ConfusionMatrix (#2336)
- [x] ClassificationReport (#2346)
- [x] DiceCoefficient (#2336)
- [x] JaccardIndex (#2336)
- [x] IoU (#2336)
- [x] mIoU (#2336)
- [x] EpochMetric (#2351)
- [x] Fbeta (#2340)
- [x] Loss (#2335)
- [x] MeanAbsoluteError (#2280)
- [x] MeanPairwiseDistance (#2307)
- [x] MeanSquaredError (#2280)
- [x] MetricsLambda (#2352)
- [x] MultiLabelConfusionMatrix (#2347)
- [x] Precision (#2340)
- [x] PSNR (#2311)
- [x] Recall (#2340)
- [x] RootMeanSquaredError (#2307)
- [x] RunningAverage (#2348)
- [x] SSIM (#2241)
- [x] TopKCategoricalAccuracy (#2284)
- [x] Bleu (#2317)
- [x] Rouge (#2317)
- [x] RougeL (#2317)
- [x] RougeN (#2317)
- [x] InceptionScore (#2349)
- [x] FID (#2349)

#### Contrib Metrics

- [x] AveragePrecision (#2341)
- [x] CohenKappa (#2321)
- [x] PrecisionRecallCurve (#2341)
- [x] ROC_AUC (#2341)
- [x] RocCurve (#2341)
- [x] CanberraMetric (#2323)
- [x] FractionalAbsoluteError (#2323)
- [x] FractionalBias (#2323)
- [x] GeometricMeanAbsoluteError (#2323)
- [x] GeometricMeanRelativeAbsoluteError (#2324)
- [x] ManhattanDistance (#2324)
- [x] MaximumAbsoluteError (#2324)
- [x] MeanAbsoluteRelativeError (#2324)
- [x] MeanError (#2324)
- [x] MeanNormalizedBias (#2324)
- [x] MedianAbsoluteError (#2324)
- [x] MedianAbsolutePercentageError (#2324) 
- [x] MedianRelativeAbsoluteError (#2324)
- [x] R2Score (#2324)
- [x] WaveHedgesDistance (#2324) 

This is a great issue for first comers and those who want to learn more about Sphinx. This issue is the community effort.
If you want to contribute, please comment on this issue with the metric you want to add doctest (one by one).

Example on how to add doctest, see #2241 

Thanks!

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `ignite/metrics/loss.py`
Content:
```
1 from typing import Callable, Dict, Sequence, Tuple, Union, cast
2 
3 import torch
4 
5 from ignite.exceptions import NotComputableError
6 from ignite.metrics.metric import Metric, reinit__is_reduced, sync_all_reduce
7 
8 __all__ = ["Loss"]
9 
10 
11 class Loss(Metric):
12     """
13     Calculates the average loss according to the passed loss_fn.
14 
15     Args:
16         loss_fn: a callable taking a prediction tensor, a target
17             tensor, optionally other arguments, and returns the average loss
18             over all observations in the batch.
19         output_transform: a callable that is used to transform the
20             :class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the
21             form expected by the metric.
22             This can be useful if, for example, you have a multi-output model and
23             you want to compute the metric with respect to one of the outputs.
24             The output is expected to be a tuple `(prediction, target)` or
25             (prediction, target, kwargs) where kwargs is a dictionary of extra
26             keywords arguments. If extra keywords arguments are provided they are passed to `loss_fn`.
27         batch_size: a callable taking a target tensor that returns the
28             first dimension size (usually the batch size).
29         device: specifies which device updates are accumulated on. Setting the
30             metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
31             non-blocking. By default, CPU.
32 
33     Attributes:
34         required_output_keys: dictionary defines required keys to be found in ``engine.state.output`` if the
35             latter is a dictionary. Default, ``("y_pred", "y", "criterion_kwargs")``. This is useful when the
36             criterion function requires additional arguments, which can be passed using ``criterion_kwargs``.
37             See an example below.
38 
39     Examples:
40         Let's implement a Loss metric that requires ``x``, ``y_pred``, ``y`` and ``criterion_kwargs`` as input
41         for ``criterion`` function. In the example below we show how to setup standard metric like Accuracy
42         and the Loss metric using an ``evaluator`` created with
43         :meth:`~ignite.engine.create_supervised_evaluator` method.
44 
45         .. code-block:: python
46 
47             import torch
48             import torch.nn as nn
49             from torch.nn.functional import nll_loss
50 
51             from ignite.metrics import Accuracy, Loss
52             from ignite.engine import create_supervised_evaluator
53 
54             model = ...
55 
56             criterion = nll_loss
57 
58             metrics = {
59                 "Accuracy": Accuracy(),
60                 "Loss": Loss(criterion)
61             }
62 
63             # global criterion kwargs
64             criterion_kwargs = {...}
65 
66             evaluator = create_supervised_evaluator(
67                 model,
68                 metrics=metrics,
69                 output_transform=lambda x, y, y_pred: {
70                     "x": x, "y": y, "y_pred": y_pred, "criterion_kwargs": criterion_kwargs}
71             )
72 
73             res = evaluator.run(data)
74 
75     """
76 
77     required_output_keys = ("y_pred", "y", "criterion_kwargs")
78 
79     def __init__(
80         self,
81         loss_fn: Callable,
82         output_transform: Callable = lambda x: x,
83         batch_size: Callable = len,
84         device: Union[str, torch.device] = torch.device("cpu"),
85     ):
86         super(Loss, self).__init__(output_transform, device=device)
87         self._loss_fn = loss_fn
88         self._batch_size = batch_size
89 
90     @reinit__is_reduced
91     def reset(self) -> None:
92         self._sum = torch.tensor(0.0, device=self._device)
93         self._num_examples = 0
94 
95     @reinit__is_reduced
96     def update(self, output: Sequence[Union[torch.Tensor, Dict]]) -> None:
97         if len(output) == 2:
98             y_pred, y = cast(Tuple[torch.Tensor, torch.Tensor], output)
99             kwargs = {}  # type: Dict
100         else:
101             y_pred, y, kwargs = cast(Tuple[torch.Tensor, torch.Tensor, Dict], output)
102         average_loss = self._loss_fn(y_pred, y, **kwargs).detach()
103 
104         if len(average_loss.shape) != 0:
105             raise ValueError("loss_fn did not return the average loss.")
106 
107         n = self._batch_size(y)
108         self._sum += average_loss.to(self._device) * n
109         self._num_examples += n
110 
111     @sync_all_reduce("_sum", "_num_examples")
112     def compute(self) -> float:
113         if self._num_examples == 0:
114             raise NotComputableError("Loss must have at least one example before it can be computed.")
115         return self._sum.item() / self._num_examples
116
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/ignite/metrics/loss.py b/ignite/metrics/loss.py
--- a/ignite/metrics/loss.py
+++ b/ignite/metrics/loss.py
@@ -42,35 +42,24 @@
         and the Loss metric using an ``evaluator`` created with
         :meth:`~ignite.engine.create_supervised_evaluator` method.
 
-        .. code-block:: python
+        .. testsetup:: *
 
-            import torch
-            import torch.nn as nn
-            from torch.nn.functional import nll_loss
+            default_trainer = get_default_trainer()
 
-            from ignite.metrics import Accuracy, Loss
-            from ignite.engine import create_supervised_evaluator
+        .. testcode::
 
-            model = ...
+            model = default_model
+            criterion = nn.NLLLoss()
+            metric = Loss(criterion)
+            metric.attach(default_evaluator, 'loss')
+            y_pred = torch.tensor([[0.1, 0.4, 0.5], [0.1, 0.7, 0.2]])
+            y_true = torch.tensor([2, 2]).long()
+            state = default_evaluator.run([[y_pred, y_true]])
+            print(state.metrics['loss'])
 
-            criterion = nll_loss
+        .. testoutput::
 
-            metrics = {
-                "Accuracy": Accuracy(),
-                "Loss": Loss(criterion)
-            }
-
-            # global criterion kwargs
-            criterion_kwargs = {...}
-
-            evaluator = create_supervised_evaluator(
-                model,
-                metrics=metrics,
-                output_transform=lambda x, y, y_pred: {
-                    "x": x, "y": y, "y_pred": y_pred, "criterion_kwargs": criterion_kwargs}
-            )
-
-            res = evaluator.run(data)
+            -0.3499999...
 
     """

{"golden_diff": "diff --git a/ignite/metrics/loss.py b/ignite/metrics/loss.py\n--- a/ignite/metrics/loss.py\n+++ b/ignite/metrics/loss.py\n@@ -42,35 +42,24 @@\n         and the Loss metric using an ``evaluator`` created with\n         :meth:`~ignite.engine.create_supervised_evaluator` method.\n \n-        .. code-block:: python\n+        .. testsetup:: *\n \n-            import torch\n-            import torch.nn as nn\n-            from torch.nn.functional import nll_loss\n+            default_trainer = get_default_trainer()\n \n-            from ignite.metrics import Accuracy, Loss\n-            from ignite.engine import create_supervised_evaluator\n+        .. testcode::\n \n-            model = ...\n+            model = default_model\n+            criterion = nn.NLLLoss()\n+            metric = Loss(criterion)\n+            metric.attach(default_evaluator, 'loss')\n+            y_pred = torch.tensor([[0.1, 0.4, 0.5], [0.1, 0.7, 0.2]])\n+            y_true = torch.tensor([2, 2]).long()\n+            state = default_evaluator.run([[y_pred, y_true]])\n+            print(state.metrics['loss'])\n \n-            criterion = nll_loss\n+        .. testoutput::\n \n-            metrics = {\n-                \"Accuracy\": Accuracy(),\n-                \"Loss\": Loss(criterion)\n-            }\n-\n-            # global criterion kwargs\n-            criterion_kwargs = {...}\n-\n-            evaluator = create_supervised_evaluator(\n-                model,\n-                metrics=metrics,\n-                output_transform=lambda x, y, y_pred: {\n-                    \"x\": x, \"y\": y, \"y_pred\": y_pred, \"criterion_kwargs\": criterion_kwargs}\n-            )\n-\n-            res = evaluator.run(data)\n+            -0.3499999...\n \n     \"\"\"\n", "issue": "[docs] Add doctests for metrics\n### \ud83d\udcda Documentation\r\n\r\nFollowing #2230 , I listed the metrics places (suggested by @sdesrozis) where doctests can be added.\r\n\r\n#### Metrics\r\n\r\n- [x] Average (#2332)\r\n- [x] GeometricAverage (#2332)\r\n- [x] VariableAccumulation (This class is an helper, no test needed)\r\n- [x] Accuracy (#2345)\r\n- [x] ConfusionMatrix (#2336)\r\n- [x] ClassificationReport (#2346)\r\n- [x] DiceCoefficient (#2336)\r\n- [x] JaccardIndex (#2336)\r\n- [x] IoU (#2336)\r\n- [x] mIoU (#2336)\r\n- [x] EpochMetric (#2351)\r\n- [x] Fbeta (#2340)\r\n- [x] Loss (#2335)\r\n- [x] MeanAbsoluteError (#2280)\r\n- [x] MeanPairwiseDistance (#2307)\r\n- [x] MeanSquaredError (#2280)\r\n- [x] MetricsLambda (#2352)\r\n- [x] MultiLabelConfusionMatrix (#2347)\r\n- [x] Precision (#2340)\r\n- [x] PSNR (#2311)\r\n- [x] Recall (#2340)\r\n- [x] RootMeanSquaredError (#2307)\r\n- [x] RunningAverage (#2348)\r\n- [x] SSIM (#2241)\r\n- [x] TopKCategoricalAccuracy (#2284)\r\n- [x] Bleu (#2317)\r\n- [x] Rouge (#2317)\r\n- [x] RougeL (#2317)\r\n- [x] RougeN (#2317)\r\n- [x] InceptionScore (#2349)\r\n- [x] FID (#2349)\r\n\r\n#### Contrib Metrics\r\n\r\n- [x] AveragePrecision (#2341)\r\n- [x] CohenKappa (#2321)\r\n- [x] PrecisionRecallCurve (#2341)\r\n- [x] ROC_AUC (#2341)\r\n- [x] RocCurve (#2341)\r\n- [x] CanberraMetric (#2323)\r\n- [x] FractionalAbsoluteError (#2323)\r\n- [x] FractionalBias (#2323)\r\n- [x] GeometricMeanAbsoluteError (#2323)\r\n- [x] GeometricMeanRelativeAbsoluteError (#2324)\r\n- [x] ManhattanDistance (#2324)\r\n- [x] MaximumAbsoluteError (#2324)\r\n- [x] MeanAbsoluteRelativeError (#2324)\r\n- [x] MeanError (#2324)\r\n- [x] MeanNormalizedBias (#2324)\r\n- [x] MedianAbsoluteError (#2324)\r\n- [x] MedianAbsolutePercentageError (#2324) \r\n- [x] MedianRelativeAbsoluteError (#2324)\r\n- [x] R2Score (#2324)\r\n- [x] WaveHedgesDistance (#2324) \r\n\r\nThis is a great issue for first comers and those who want to learn more about Sphinx. This issue is the community effort.\r\nIf you want to contribute, please comment on this issue with the metric you want to add doctest (one by one).\r\n\r\nExample on how to add doctest, see #2241 \r\n\r\nThanks!\n", "before_files": [{"content": "from typing import Callable, Dict, Sequence, Tuple, Union, cast\n\nimport torch\n\nfrom ignite.exceptions import NotComputableError\nfrom ignite.metrics.metric import Metric, reinit__is_reduced, sync_all_reduce\n\n__all__ = [\"Loss\"]\n\n\nclass Loss(Metric):\n    \"\"\"\n    Calculates the average loss according to the passed loss_fn.\n\n    Args:\n        loss_fn: a callable taking a prediction tensor, a target\n            tensor, optionally other arguments, and returns the average loss\n            over all observations in the batch.\n        output_transform: a callable that is used to transform the\n            :class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the\n            form expected by the metric.\n            This can be useful if, for example, you have a multi-output model and\n            you want to compute the metric with respect to one of the outputs.\n            The output is expected to be a tuple `(prediction, target)` or\n            (prediction, target, kwargs) where kwargs is a dictionary of extra\n            keywords arguments. If extra keywords arguments are provided they are passed to `loss_fn`.\n        batch_size: a callable taking a target tensor that returns the\n            first dimension size (usually the batch size).\n        device: specifies which device updates are accumulated on. Setting the\n            metric's device to be the same as your ``update`` arguments ensures the ``update`` method is\n            non-blocking. By default, CPU.\n\n    Attributes:\n        required_output_keys: dictionary defines required keys to be found in ``engine.state.output`` if the\n            latter is a dictionary. Default, ``(\"y_pred\", \"y\", \"criterion_kwargs\")``. This is useful when the\n            criterion function requires additional arguments, which can be passed using ``criterion_kwargs``.\n            See an example below.\n\n    Examples:\n        Let's implement a Loss metric that requires ``x``, ``y_pred``, ``y`` and ``criterion_kwargs`` as input\n        for ``criterion`` function. In the example below we show how to setup standard metric like Accuracy\n        and the Loss metric using an ``evaluator`` created with\n        :meth:`~ignite.engine.create_supervised_evaluator` method.\n\n        .. code-block:: python\n\n            import torch\n            import torch.nn as nn\n            from torch.nn.functional import nll_loss\n\n            from ignite.metrics import Accuracy, Loss\n            from ignite.engine import create_supervised_evaluator\n\n            model = ...\n\n            criterion = nll_loss\n\n            metrics = {\n                \"Accuracy\": Accuracy(),\n                \"Loss\": Loss(criterion)\n            }\n\n            # global criterion kwargs\n            criterion_kwargs = {...}\n\n            evaluator = create_supervised_evaluator(\n                model,\n                metrics=metrics,\n                output_transform=lambda x, y, y_pred: {\n                    \"x\": x, \"y\": y, \"y_pred\": y_pred, \"criterion_kwargs\": criterion_kwargs}\n            )\n\n            res = evaluator.run(data)\n\n    \"\"\"\n\n    required_output_keys = (\"y_pred\", \"y\", \"criterion_kwargs\")\n\n    def __init__(\n        self,\n        loss_fn: Callable,\n        output_transform: Callable = lambda x: x,\n        batch_size: Callable = len,\n        device: Union[str, torch.device] = torch.device(\"cpu\"),\n    ):\n        super(Loss, self).__init__(output_transform, device=device)\n        self._loss_fn = loss_fn\n        self._batch_size = batch_size\n\n    @reinit__is_reduced\n    def reset(self) -> None:\n        self._sum = torch.tensor(0.0, device=self._device)\n        self._num_examples = 0\n\n    @reinit__is_reduced\n    def update(self, output: Sequence[Union[torch.Tensor, Dict]]) -> None:\n        if len(output) == 2:\n            y_pred, y = cast(Tuple[torch.Tensor, torch.Tensor], output)\n            kwargs = {}  # type: Dict\n        else:\n            y_pred, y, kwargs = cast(Tuple[torch.Tensor, torch.Tensor, Dict], output)\n        average_loss = self._loss_fn(y_pred, y, **kwargs).detach()\n\n        if len(average_loss.shape) != 0:\n            raise ValueError(\"loss_fn did not return the average loss.\")\n\n        n = self._batch_size(y)\n        self._sum += average_loss.to(self._device) * n\n        self._num_examples += n\n\n    @sync_all_reduce(\"_sum\", \"_num_examples\")\n    def compute(self) -> float:\n        if self._num_examples == 0:\n            raise NotComputableError(\"Loss must have at least one example before it can be computed.\")\n        return self._sum.item() / self._num_examples\n", "path": "ignite/metrics/loss.py"}], "after_files": [{"content": "from typing import Callable, Dict, Sequence, Tuple, Union, cast\n\nimport torch\n\nfrom ignite.exceptions import NotComputableError\nfrom ignite.metrics.metric import Metric, reinit__is_reduced, sync_all_reduce\n\n__all__ = [\"Loss\"]\n\n\nclass Loss(Metric):\n    \"\"\"\n    Calculates the average loss according to the passed loss_fn.\n\n    Args:\n        loss_fn: a callable taking a prediction tensor, a target\n            tensor, optionally other arguments, and returns the average loss\n            over all observations in the batch.\n        output_transform: a callable that is used to transform the\n            :class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the\n            form expected by the metric.\n            This can be useful if, for example, you have a multi-output model and\n            you want to compute the metric with respect to one of the outputs.\n            The output is expected to be a tuple `(prediction, target)` or\n            (prediction, target, kwargs) where kwargs is a dictionary of extra\n            keywords arguments. If extra keywords arguments are provided they are passed to `loss_fn`.\n        batch_size: a callable taking a target tensor that returns the\n            first dimension size (usually the batch size).\n        device: specifies which device updates are accumulated on. Setting the\n            metric's device to be the same as your ``update`` arguments ensures the ``update`` method is\n            non-blocking. By default, CPU.\n\n    Attributes:\n        required_output_keys: dictionary defines required keys to be found in ``engine.state.output`` if the\n            latter is a dictionary. Default, ``(\"y_pred\", \"y\", \"criterion_kwargs\")``. This is useful when the\n            criterion function requires additional arguments, which can be passed using ``criterion_kwargs``.\n            See an example below.\n\n    Examples:\n        Let's implement a Loss metric that requires ``x``, ``y_pred``, ``y`` and ``criterion_kwargs`` as input\n        for ``criterion`` function. In the example below we show how to setup standard metric like Accuracy\n        and the Loss metric using an ``evaluator`` created with\n        :meth:`~ignite.engine.create_supervised_evaluator` method.\n\n        .. testsetup:: *\n\n            default_trainer = get_default_trainer()\n\n        .. testcode::\n\n            model = default_model\n            criterion = nn.NLLLoss()\n            metric = Loss(criterion)\n            metric.attach(default_evaluator, 'loss')\n            y_pred = torch.tensor([[0.1, 0.4, 0.5], [0.1, 0.7, 0.2]])\n            y_true = torch.tensor([2, 2]).long()\n            state = default_evaluator.run([[y_pred, y_true]])\n            print(state.metrics['loss'])\n\n        .. testoutput::\n\n            -0.3499999...\n\n    \"\"\"\n\n    required_output_keys = (\"y_pred\", \"y\", \"criterion_kwargs\")\n\n    def __init__(\n        self,\n        loss_fn: Callable,\n        output_transform: Callable = lambda x: x,\n        batch_size: Callable = len,\n        device: Union[str, torch.device] = torch.device(\"cpu\"),\n    ):\n        super(Loss, self).__init__(output_transform, device=device)\n        self._loss_fn = loss_fn\n        self._batch_size = batch_size\n\n    @reinit__is_reduced\n    def reset(self) -> None:\n        self._sum = torch.tensor(0.0, device=self._device)\n        self._num_examples = 0\n\n    @reinit__is_reduced\n    def update(self, output: Sequence[Union[torch.Tensor, Dict]]) -> None:\n        if len(output) == 2:\n            y_pred, y = cast(Tuple[torch.Tensor, torch.Tensor], output)\n            kwargs = {}  # type: Dict\n        else:\n            y_pred, y, kwargs = cast(Tuple[torch.Tensor, torch.Tensor, Dict], output)\n        average_loss = self._loss_fn(y_pred, y, **kwargs).detach()\n\n        if len(average_loss.shape) != 0:\n            raise ValueError(\"loss_fn did not return the average loss.\")\n\n        n = self._batch_size(y)\n        self._sum += average_loss.to(self._device) * n\n        self._num_examples += n\n\n    @sync_all_reduce(\"_sum\", \"_num_examples\")\n    def compute(self) -> float:\n        if self._num_examples == 0:\n            raise NotComputableError(\"Loss must have at least one example before it can be computed.\")\n        return self._sum.item() / self._num_examples\n", "path": "ignite/metrics/loss.py"}]}