forked from Lightning-AI/pytorch-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix incomplete RunningMean (Lightning-AI#1309)
* fix RunningMean * changelog * fix none * Update supporters.py just needed to multiply by zero for init * Revert "Update supporters.py" This reverts commit 7e0da6c * fix NaN * formatting Co-authored-by: William Falcon <waf2107@columbia.edu>
- Loading branch information
1 parent
09e5a87
commit 48b4b62
Showing
7 changed files
with
65 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import torch | ||
|
||
|
||
class TensorRunningMean(object): | ||
""" | ||
Tracks a running mean without graph references. | ||
Round robbin for the mean | ||
Examples: | ||
>>> accum = TensorRunningMean(5) | ||
>>> accum.last(), accum.mean() | ||
(None, None) | ||
>>> accum.append(torch.tensor(1.5)) | ||
>>> accum.last(), accum.mean() | ||
(tensor(1.5000), tensor(1.5000)) | ||
>>> accum.append(torch.tensor(2.5)) | ||
>>> accum.last(), accum.mean() | ||
(tensor(2.5000), tensor(2.)) | ||
>>> accum.reset() | ||
>>> _= [accum.append(torch.tensor(i)) for i in range(13)] | ||
>>> accum.last(), accum.mean() | ||
(tensor(12.), tensor(10.)) | ||
""" | ||
def __init__(self, window_length: int): | ||
self.window_length = window_length | ||
self.memory = torch.Tensor(self.window_length) | ||
self.current_idx: int = 0 | ||
self.last_idx: int = None | ||
self.rotated: bool = False | ||
|
||
def reset(self) -> None: | ||
self = TensorRunningMean(self.window_length) | ||
|
||
def last(self): | ||
if self.last_idx is not None: | ||
return self.memory[self.last_idx] | ||
|
||
def append(self, x): | ||
# map proper type for memory if they don't match | ||
if self.memory.type() != x.type(): | ||
self.memory.type_as(x) | ||
|
||
# store without grads | ||
with torch.no_grad(): | ||
self.memory[self.current_idx] = x | ||
self.last_idx = self.current_idx | ||
|
||
# increase index | ||
self.current_idx += 1 | ||
|
||
# reset index when hit limit of tensor | ||
self.current_idx = self.current_idx % self.window_length | ||
if self.current_idx == 0: | ||
self.rotated = True | ||
|
||
def mean(self): | ||
if self.last_idx is not None: | ||
return self.memory.mean() if self.rotated else self.memory[:self.current_idx].mean() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters