Skip to content

Commit 1a73cd4

Browse files
author
Jeff Yang
authored
Merge branch 'master' into docs/fix-auto_collect_arguments
2 parents 91067f7 + 01a925d commit 1a73cd4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/source/metrics.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ If ``on_epoch`` is True, the logger automatically logs the end of epoch metric v
7878
self.valid_acc(logits, y)
7979
self.log('valid_acc', self.valid_acc, on_step=True, on_epoch=True)
8080
81+
.. note::
82+
If using metrics in data parallel mode (dp), the metric update/logging should be done
83+
in the ``<mode>_step_end`` method (where ``<mode>`` is either ``training``, ``validation``
84+
or ``test``). This is due to metric states else being destroyed after each forward pass,
85+
leading to wrong accumulation. In practice do the following:
86+
87+
.. code-block:: python
88+
89+
def training_step(self, batch, batch_idx):
90+
data, target = batch
91+
pred = self(data)
92+
...
93+
return {'loss' : loss, 'preds' : preds, 'target' : target}
94+
95+
def training_step_end(self, outputs):
96+
#update and log
97+
self.metric(outputs['preds'], outputs['target'])
98+
self.log('metric', self.metric)
99+
100+
81101
This metrics API is independent of PyTorch Lightning. Metrics can directly be used in PyTorch as shown in the example:
82102

83103
.. code-block:: python

0 commit comments

Comments
 (0)