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 Dec 23, 2020
1 parent bdae8bb commit ca886e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pytorch_lightning/trainer/supporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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
38 changes: 38 additions & 0 deletions tests/trainer/test_supporters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import torch

from pytorch_lightning.trainer.supporters import TensorRunningAccum


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

0 comments on commit ca886e7

Please sign in to comment.