Stats History#
history
#
| CLASS | DESCRIPTION |
|---|---|
RunningHistory |
Accumulates complete history of all data batches. |
Classes#
RunningHistory(data: torch.Tensor | None = None, state: dict | None = None)
#
Bases: Stats
Accumulates complete history of all data batches.
Concatenates all added batches into a single tensor. Unlike other running statistics, this stores all data in memory. Useful for collecting data across batches without manual concatenation logic.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Initial data tensor.
TYPE:
|
state
|
Previous state to restore from.
TYPE:
|
Properties
data : torch.Tensor | None Concatenated data from all batches.
Notes
Data is concatenated in chunks of 100 batches for efficiency.
Use history() to retrieve the complete concatenated tensor.
Warnings
This statistic stores ALL data in memory. For large datasets, consider using other running statistics that maintain only summary information.
Examples:
Basic usage:
>>> from spectre.stats import RunningHistory
>>> import torch
>>>
>>> stat = RunningHistory()
>>> for i in range(5):
... batch = torch.randn(10, 3)
... stat.add(batch)
>>>
>>> all_data = stat.history()
>>> all_data.shape
torch.Size([50, 3])
Collecting predictions across batches:
>>> stat = RunningHistory()
>>> model = MyModel()
>>> for batch in dataloader:
... predictions = model(batch)
... stat.add(predictions)
>>>
>>> all_predictions = stat.history()
| METHOD | DESCRIPTION |
|---|---|
add |
Add batch to history. |
history |
Get complete concatenated history. |
load_state_dict |
Load state from dictionary. |
state_dict |
Save state to dictionary. |
to_ |
Move internal tensors to specified device. |
Source code in spectre/stats/history.py
Functions#
add(X: torch.Tensor, weights: torch.Tensor | None = None, *args, **kwargs) -> None
#
Add batch to history.
Batches are buffered and concatenated periodically for efficiency.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Data batch to add to history.
TYPE:
|
Source code in spectre/stats/history.py
history() -> torch.Tensor
#
Get complete concatenated history.
Triggers final concatenation of any buffered batches.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
All data concatenated along first dimension. |
Source code in spectre/stats/history.py
load_state_dict(state: dict) -> None
#
Load state from dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
State dictionary from
TYPE:
|
Source code in spectre/stats/history.py
state_dict() -> dict
#
Save state to dictionary.
Triggers final concatenation before saving.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
State dictionary containing concatenated history. |
Source code in spectre/stats/history.py
to_(device: str) -> None
#
Move internal tensors to specified device.
Triggers final concatenation before moving.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
Target device ("cpu", "cuda", "cuda:0", etc.).
TYPE:
|