Datasets¶
btorch.datasets
¶
Dataset utilities and noise generation for neuromorphic simulations.
This module provides noise generators (functional and layer-based) commonly used for simulating background activity, synaptic noise, and input currents in spiking neural networks.
Noise Types¶
Ornstein-Uhlenbeck (OU):
Temporally correlated Gaussian noise with configurable time constant
(tau) and standard deviation (sigma). Useful for modeling
synaptic noise and membrane potential fluctuations.
Poisson: Discrete event noise with configurable rate. Suitable for spike train generation and stochastic synaptic inputs.
Pink (1/f): Colored noise with power spectral density proportional to 1/frequency. Generated via causal FIR filtering of white noise. Useful for modeling naturalistic temporal correlations.
Functional API¶
- [`ou_noise`](btorch/datasets/noise.py:25): Generate OU noise sequence
- [`ou_noise_like`](btorch/datasets/noise.py:123): OU noise with
reference tensor
- [`poisson_noise`](btorch/datasets/noise.py:156): Generate Poisson
events
- [`poisson_noise_like`](btorch/datasets/noise.py:192): Poisson with
reference tensor
- [`pink_noise`](btorch/datasets/noise.py:284): Generate pink noise
- [`pink_noise_like`](btorch/datasets/noise.py:344): Pink noise with
reference tensor
Layer API¶
- [`OUNoiseLayer`](btorch/datasets/noise.py:367): Stateful OU noise
module with single/multi-step modes
- [`PoissonNoiseLayer`](btorch/datasets/noise.py:510): Stateless Poisson
encoder/generator module
- [`PinkNoiseLayer`](btorch/datasets/noise.py:603): Stateful pink noise
module with FIR history
All noise functions support
- Per-neuron or scalar parameters (broadcastable)
- Deterministic sampling via
torch.Generator - GPU/CPU device placement
- Compatible with
torch.compile
Attributes¶
__all__ = ['OUNoiseLayer', 'PinkNoiseLayer', 'PoissonNoiseLayer', 'ou_noise', 'ou_noise_like', 'pink_noise', 'pink_noise_like', 'poisson_noise', 'poisson_noise_like']
module-attribute
¶
Classes¶
OUNoiseLayer
¶
Bases: _BaseNoiseLayer
Ornstein-Uhlenbeck (OU) noise layer for temporally correlated noise.
Implements exact discretization of the OU process where sigma is the
stationary standard deviation:
n_{t+1} = alpha * n_t + beta * eps_t
alpha = exp(-dt/tau)
beta = sigma * sqrt(1 - exp(-2*dt/tau))
eps_t ~ N(0, 1)
The layer supports both single-step (stateful) and multi-step (vectorized) modes. In stateful mode, the noise state persists across forward calls.
Scale and bias are handled internally by
:class:btorch.models.linear.LearnableScale.
Learnable parameters
scale: Multiplicative scaling (default: 1.0)bias: Additive offset (default: 0.0)sigma: Stationary standard deviationtau: Time constant
Tensor Conventions
- Multi-step input: Output shape is
(T, *batch_dims, *n_neuron)where neuron dims are trailing. - Single-step input: Output shape is
(*batch_dims, *n_neuron). - Internal state
self.noisestores the state BEFORE the current step/sequence (i.e., the initial condition).
Multi-step Backend
- Scalar tau/sigma: Uses single conv1d (fast)
- Per-neuron tau/sigma: Uses grouped conv1d, O(T^2 * D) complexity
Determinism Note
Multi-step uses vectorized RNG and convolution, so exact equality with repeated single-step updates is not guaranteed even with a generator. Use single-step loops if you need step-by-step equivalence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons or shape of trailing neuron dims. |
required |
sigma
|
float | Tensor
|
Stationary standard deviation (scalar or per-neuron). |
0.5
|
tau
|
float | Tensor
|
Time constant in same units as dt (scalar or per-neuron). |
10.0
|
step_mode
|
Literal['s', 'm']
|
|
'm'
|
trainable_param
|
bool | set[str]
|
Set of parameter names to make trainable, or True/False for all/none. Options: {"scale", "bias", "sigma", "tau"}. |
False
|
trainable_shape
|
str
|
Shape policy for trainable values:
- |
'scalar'
|
stateful
|
bool
|
If True, maintain noise state between calls (required for single-step mode). |
False
|
tau_min
|
float
|
Minimum value for tau (clamped for numerical stability). |
1e-06
|
scale
|
float | Tensor
|
Initial multiplicative scaling. |
1.0
|
bias
|
float | Tensor
|
Initial additive offset. |
0.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
noise |
Tensor
|
Current noise state tensor (only if |
scale |
Tensor
|
Output scaling (via LearnableScale). |
bias |
Tensor
|
Output offset (via LearnableScale). |
sigma |
Tensor
|
Stationary std dev (Parameter if trainable, else buffer). |
tau |
Tensor
|
Time constant (Parameter if trainable, else buffer). |
Source code in btorch/datasets/noise.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | |
Functions¶
multi_step_forward(T, dt=None, *, generator=None)
¶
Generate a multi-step OU noise sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
T
|
int
|
Number of timesteps. |
required |
dt
|
float | None
|
Timestep (defaults to |
None
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Noise sequence of shape |
Tensor
|
matches |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If stateful but noise buffer not initialized. |
Source code in btorch/datasets/noise.py
single_step_forward(dt=None, *, generator=None)
¶
Single-step update of OU noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
float | None
|
Timestep (defaults to |
None
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Updated noise tensor with same shape as |
Source code in btorch/datasets/noise.py
PinkNoiseLayer
¶
Bases: _BaseNoiseLayer
Pink (1/f) noise layer using causal FIR filtering.
Generates colored noise with PSD ~ 1/frequency by filtering white noise through a fractional integration FIR kernel. Supports both single-step (stateful, with history preservation) and multi-step (vectorized) modes.
In stateful mode, the FIR history is preserved across calls for seamless continuation of noise sequences.
Scale and bias are handled internally by
:class:btorch.models.linear.LearnableScale.
Learnable parameters
scale: Multiplicative scaling (default: 1.0)bias: Additive offset (default: 0.0)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons or shape of trailing neuron dims. |
required |
fir_order
|
int
|
Length of the FIR filter kernel (default 64). |
64
|
step_mode
|
Literal['s', 'm']
|
|
'm'
|
trainable_param
|
bool | set[str]
|
Set of parameter names to make trainable, or True/False for all/none. Options: {"scale", "bias"}. |
False
|
trainable_shape
|
str
|
Shape policy for trainable values:
- |
'scalar'
|
stateful
|
bool
|
If True, maintain FIR history between calls (required for single-step mode). |
False
|
scale
|
float | Tensor
|
Initial multiplicative scaling. |
1.0
|
bias
|
float | Tensor
|
Initial additive offset. |
0.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
noise |
Tensor
|
Current noise value (only if |
white_history |
Tensor
|
Previous white noise samples for FIR continuity
(shape |
fir_order |
Length of the FIR kernel. |
|
scale |
Tensor
|
Output scaling (via LearnableScale). |
bias |
Tensor
|
Output offset (via LearnableScale). |
Source code in btorch/datasets/noise.py
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 | |
Functions¶
multi_step_forward(T, *, generator=None)
¶
Vectorized multi-step pink-noise generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
T
|
int
|
Number of timesteps. |
required |
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Noise sequence of shape |
Source code in btorch/datasets/noise.py
single_step_forward(*, generator=None)
¶
Single-step pink-noise update using FIR history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Single noise sample with shape |
Source code in btorch/datasets/noise.py
PoissonNoiseLayer
¶
Bases: _BaseNoiseLayer
Poisson noise layer serving as both generator and encoder.
Generates Poisson-distributed event counts with rate scaled by dt:
lambda = rate * dt per timestep. Because Poisson processes are
memoryless, this layer does not maintain internal state.
The layer can operate in two modes:
- Generator mode: uses the rate provided at construction.
- Encoder mode: accepts an external rate tensor via forward()
or multi_step_forward(), similar to SpikingJelly's encoder pattern.
Scale and bias are handled internally by
:class:btorch.models.linear.LearnableScale.
Learnable parameters
scale: Multiplicative scaling (default: 1.0)bias: Additive offset (default: 0.0)rate: Events per unit time (default: 1.0)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons or shape of trailing neuron dims. |
required |
rate
|
float | Tensor
|
Default events per unit time (scalar or per-neuron,
broadcastable). Used when |
1.0
|
step_mode
|
Literal['s', 'm']
|
|
'm'
|
trainable_param
|
bool | set[str]
|
Set of parameter names to make trainable, or True/False for all/none. Options: {"scale", "bias", "rate"}. |
False
|
trainable_shape
|
str
|
Shape policy for trainable values:
- |
'scalar'
|
stateful
|
bool
|
Kept for API compatibility but ignored (Poisson is memoryless). |
False
|
scale
|
float | Tensor
|
Initial multiplicative scaling. |
1.0
|
bias
|
float | Tensor
|
Initial additive offset. |
0.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
scale |
Tensor
|
Output scaling (via LearnableScale). |
bias |
Tensor
|
Output offset (via LearnableScale). |
rate |
Tensor
|
Event rate (Parameter if trainable, else buffer). |
Source code in btorch/datasets/noise.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 | |
Functions¶
forward(rate=None, dt=None, *, generator=None)
¶
Single-step Poisson sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
Tensor | None
|
Optional rate tensor with shape |
None
|
dt
|
float | None
|
Timestep (defaults to |
None
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Event counts tensor broadcastable to the rate shape. |
Source code in btorch/datasets/noise.py
multi_step_forward(T, rate=None, dt=None, *, generator=None)
¶
Vectorized multi-step Poisson sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
T
|
int
|
Number of timesteps. |
required |
rate
|
Tensor | None
|
Optional rate tensor. If None, uses |
None
|
dt
|
float | None
|
Timestep (defaults to |
None
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Event counts of shape |
Source code in btorch/datasets/noise.py
Functions¶
ou_noise(*size, sigma, tau, T, dt, device=None, dtype=None, noise0=None, generator=None)
¶
Generate Ornstein-Uhlenbeck (OU) noise sequence.
OU noise follows the stochastic differential equation
dx = -x/tau * dt + sigma * sqrt(2/tau) * dW
The exact discretization used is
n_{t+1} = alpha * n_t + beta * eps_t alpha = exp(-dt/tau) beta = sigma * sqrt(1 - exp(-2*dt/tau)) eps_t ~ N(0, 1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int
|
Shape of the noise per timestep (e.g., |
()
|
sigma
|
Tensor
|
Standard deviation of the stationary distribution. Can be
scalar or per-element (broadcastable to |
required |
tau
|
Tensor
|
Time constant controlling correlation length. Can be scalar or
per-element (broadcastable to |
required |
T
|
int
|
Number of timesteps to generate. |
required |
dt
|
float
|
Simulation timestep (same units as |
required |
device
|
device | None
|
Device for the output tensor (if |
None
|
dtype
|
dtype | None
|
Dtype for the output tensor (if |
None
|
noise0
|
Tensor | None
|
Initial noise state with shape |
None
|
generator
|
Generator | None
|
Optional RNG generator for deterministic sampling. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
RuntimeError
|
If |
Source code in btorch/datasets/noise.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
ou_noise_like(like, sigma, tau, *, T, dt, noise0=None, generator=None)
¶
Generate OU noise matching a reference tensor's shape and device.
Convenience wrapper around ou_noise that infers size, device,
and dtype from a reference tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
like
|
Tensor
|
Reference tensor with shape |
required |
sigma
|
Tensor
|
Standard deviation (scalar or broadcastable to |
required |
tau
|
Tensor
|
Time constant (scalar or broadcastable to |
required |
T
|
int
|
Number of timesteps. |
required |
dt
|
float
|
Simulation timestep. |
required |
noise0
|
Tensor | None
|
Optional initial state with shape |
None
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
OU noise tensor of shape |
Source code in btorch/datasets/noise.py
pink_noise(*size, T, fir_order=64, device=None, dtype=None, white_history=None, generator=None, return_white_history=False)
¶
Generate pink (1/f) noise using a causal FIR filter.
Pink noise has power spectral density proportional to 1/frequency, creating naturalistic temporal correlations. Generated by filtering white noise through a fractional integration FIR kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int
|
Shape per timestep. Output will be |
()
|
T
|
int
|
Number of timesteps to generate. |
required |
fir_order
|
int
|
Length of the FIR filter kernel (default 64). Higher values give better low-frequency approximation but more state. |
64
|
device
|
device | None
|
Device for the output tensor (if |
None
|
dtype
|
dtype | None
|
Dtype for the output (must be floating point). |
None
|
white_history
|
Tensor | None
|
Optional previous white noise history with shape
|
None
|
generator
|
Generator | None
|
Optional RNG generator for deterministic sampling. |
None
|
return_white_history
|
bool
|
If True, also return the updated history tensor for stateful usage. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor | tuple[Tensor, Tensor]
|
Pink noise tensor of shape |
Tensor | tuple[Tensor, Tensor]
|
is True, returns |
Tensor | tuple[Tensor, Tensor]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Source code in btorch/datasets/noise.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
pink_noise_like(like, *, T, fir_order=64, white_history=None, generator=None, return_white_history=False)
¶
Generate pink noise matching a reference tensor's metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
like
|
Tensor
|
Reference tensor with shape |
required |
T
|
int
|
Number of timesteps. |
required |
fir_order
|
int
|
FIR filter length. |
64
|
white_history
|
Tensor | None
|
Optional history tensor with shape
|
None
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
return_white_history
|
bool
|
If True, also return updated history. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor | tuple[Tensor, Tensor]
|
Pink noise of shape |
Tensor | tuple[Tensor, Tensor]
|
tuple if |
Source code in btorch/datasets/noise.py
poisson_noise(*size, rate, T, dt=1.0, device=None, dtype=None, generator=None)
¶
Generate Poisson noise (discrete event counts).
Samples are drawn from Poisson distribution with lambda = rate * dt for each timestep and element. The output represents event counts per timestep (0, 1, 2, ...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int
|
Shape per timestep (e.g., |
()
|
rate
|
float | Tensor
|
Event rate per unit time. Can be scalar or per-element
(broadcastable to |
required |
T
|
int
|
Number of timesteps. |
required |
dt
|
float
|
Simulation timestep (scales the rate: lambda = rate * dt). |
1.0
|
device
|
device | None
|
Device for the output tensor. |
None
|
dtype
|
dtype | None
|
Dtype for the output (must be floating point). |
None
|
generator
|
Generator | None
|
Optional RNG generator for deterministic sampling. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Event count tensor of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Source code in btorch/datasets/noise.py
poisson_noise_like(like, rate, *, T, dt=1.0, generator=None)
¶
Generate Poisson noise matching a reference tensor's metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
like
|
Tensor
|
Reference tensor defining per-timestep shape |
required |
rate
|
float | Tensor
|
Event rate per unit time (scalar or broadcastable). |
required |
T
|
int
|
Number of timesteps. |
required |
dt
|
float
|
Simulation timestep. |
1.0
|
generator
|
Generator | None
|
Optional RNG generator. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Poisson event counts of shape |