Skip to content

Commit

Permalink
Fix reset TensorRunningAccum (#5106)
Browse files Browse the repository at this point in the history
* Fix reset TensorRunningAccum

* add test for TensorRunningAccum's reset method

* fix CI failed due to PEP8

Co-authored-by: Rohit Gupta <[email protected]>
  • Loading branch information
2 people authored and Borda committed Jan 5, 2021
1 parent 151d86e commit 1d13943
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pytorch_lightning/trainer/supporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, window_length: int):

def reset(self) -> None:
"""Empty the accumulator."""
self = TensorRunningAccum(self.window_length)
self.__init__(self.window_length)

def last(self):
"""Get the last added element."""
Expand Down
25 changes: 24 additions & 1 deletion tests/trainer/test_supporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,33 @@
import torch

from torch.utils.data import TensorDataset
from pytorch_lightning.trainer.supporters import CycleIterator, CombinedLoader, CombinedDataset, CombinedLoaderIterator
from pytorch_lightning.trainer.supporters import (
CycleIterator, CombinedLoader, CombinedDataset, CombinedLoaderIterator, TensorRunningAccum)
from pytorch_lightning.utilities.exceptions import MisconfigurationException



def test_tensor_running_accum_reset():
""" Test that reset would set all attributes to the initialization state """

window_length = 10

accum = TensorRunningAccum(window_length=window_length)
assert accum.last() is None
assert accum.mean() is None

accum.append(torch.tensor(1.5))
assert accum.last() == torch.tensor(1.5)
assert accum.mean() == torch.tensor(1.5)

accum.reset()
assert accum.window_length == window_length
assert accum.memory is None
assert accum.current_idx == 0
assert accum.last_idx is None
assert not accum.rotated


def test_cycle_iterator():
"""Test the cycling function of `CycleIterator`"""
iterator = CycleIterator(range(100), 1000)
Expand Down

0 comments on commit 1d13943

Please sign in to comment.