Skip to content

Commit 5955475

Browse files
committed
add functionality to Monitor when time is none
1 parent cbd018a commit 5955475

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

bindsnet/network/monitors.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def __init__(
3535
Constructs a ``Monitor`` object.
3636
3737
:param obj: An object to record state variables from during network simulation.
38-
:param state_vars: Iterable of strings indicating names of state variables to
39-
record.
38+
:param state_vars: Iterable of strings indicating names of state variables to record.
4039
:param time: If not ``None``, pre-allocate memory for state variable recording.
4140
:param device: Allow the monitor to be on different device separate from Network device
4241
"""
@@ -48,6 +47,11 @@ def __init__(
4847
self.batch_size = batch_size
4948
self.device = device
5049

50+
# if time is not specified the monitor variable accumulate the logs
51+
if self.time is None:
52+
self.device = "cpu"
53+
54+
self.recording = []
5155
self.reset_state_variables()
5256

5357
def get(self, var: str) -> torch.Tensor:
@@ -56,10 +60,15 @@ def get(self, var: str) -> torch.Tensor:
5660
Return recording to user.
5761
5862
:param var: State variable recording to return.
59-
:return: Tensor of shape ``[time, n_1, ..., n_k]``, where ``[n_1, ..., n_k]`` is
60-
the shape of the recorded state variable.
63+
:return: Tensor of shape ``[time, n_1, ..., n_k]``, where ``[n_1, ..., n_k]`` is the shape of the recorded state
64+
variable.
65+
Note, if time == `None`, get return the logs and empty the monitor variable
66+
6167
"""
62-
return torch.cat(self.recording[var], 0)
68+
return_logs = torch.cat(self.recording[var], 0)
69+
if self.time is None:
70+
self.recording[var] = []
71+
return return_logs
6372

6473
def record(self) -> None:
6574
# language=rst
@@ -83,7 +92,12 @@ def reset_state_variables(self) -> None:
8392
"""
8493
Resets recordings to empty ``List``s.
8594
"""
86-
self.recording = {v: [[] for i in range(self.time)] for v in self.state_vars}
95+
if self.time is None:
96+
self.recording = {v: [] for v in self.state_vars}
97+
else:
98+
self.recording = {
99+
v: [[] for i in range(self.time)] for v in self.state_vars
100+
}
87101

88102

89103
class NetworkMonitor(AbstractMonitor):

0 commit comments

Comments
 (0)