brainx-skill 1.0.14 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -1
- package/manifest.json +1 -1
- package/mcp-servers/codex/skills.json +1 -1
- package/package.json +1 -1
- package/skills/bio-neuro-lit/SKILL.md +4 -2
- package/skills/bio-neuro-lit/references/required-supplements.md +27 -0
- package/skills/brainx-acceleration/SKILL.md +30 -437
- package/skills/brainx-acceleration/references/audit-and-rewrite-patterns.md +326 -0
- package/skills/brainx-general-guard/SKILL.md +24 -8
- package/skills/brainx-install/SKILL.md +3 -3
- package/skills/brainx-install/references/compatibility-and-release-matching.md +28 -20
- package/skills/brainx-install/references/uninstall-and-cleanup.md +11 -4
- package/skills/brainx-modeling-loop/SKILL.md +143 -36
- package/skills/brainx-modeling-loop/agents/openai.yaml +2 -2
- package/skills/brainx-modeling-loop/references/mechanism-comparison.md +62 -0
- package/skills/brainx-modeling-loop/references/multiscale-coupling.md +16 -0
- package/skills/brainx-modeling-loop/references/paper-reproduction.md +42 -0
- package/skills/brainx-modeling-loop/references/parameter-fitting-workflow.md +14 -14
- package/skills/brainx-modeling-loop/references/run-experiment.md +1 -1
- package/skills/brainx-modeling-loop/references/training-workflow.md +10 -8
- package/skills/brainx-visualization/SKILL.md +6 -2
- package/skills/brainx-visualization/references/neural-data-visualization.md +1 -1
- package/skills/brainx-visualization/references/statistical-and-model-visualization.md +1 -1
- package/skills/package-skills/braincell/SKILL.md +23 -14
- package/skills/package-skills/braincell/references/braintools/input-current.md +1 -1
- package/skills/package-skills/braincell/references/braintools/metric.md +72 -46
- package/skills/package-skills/braincell/references/mixions-for-adaptation.md +1 -1
- package/skills/package-skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +1 -1
- package/skills/package-skills/braincell/references/multicompartment/cv-policy-reference.md +1 -1
- package/skills/package-skills/braincell/references/multicompartment/filter-function-library.md +3 -3
- package/skills/package-skills/braincell/references/multicompartment/morphology-io-loading-validation.md +1 -1
- package/skills/package-skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +31 -43
- package/skills/package-skills/braincell/references/multicompartment/network-workflow.md +49 -0
- package/skills/package-skills/braincell/references/multicompartment/probe-reference.md +26 -114
- package/skills/package-skills/braincell/references/multicompartment/topology-building-and-visualization.md +40 -28
- package/skills/package-skills/braincell/references/scripts/cell_multicompartment_reference.py +13 -17
- package/skills/package-skills/brainevent/SKILL.md +0 -3
- package/skills/package-skills/brainevent/references/synaptic-plasticity.md +1 -1
- package/skills/package-skills/brainmass/SKILL.md +5 -3
- package/skills/package-skills/brainmass/references/brainstate/parameter-constraints-regularization.md +9 -7
- package/skills/package-skills/brainmass/references/brainstate/parameter-transforms-regularizers-catalog.md +4 -4
- package/skills/package-skills/brainmass/references/braintools/data-preprocessing.md +15 -7
- package/skills/package-skills/brainmass/references/braintools/metric.md +72 -46
- package/skills/package-skills/brainmass/references/coupling-network-api.md +3 -1
- package/skills/package-skills/brainmass/references/fitting-with-objectives-api.md +6 -6
- package/skills/package-skills/brainmass/references/horn-task-training.md +9 -9
- package/skills/package-skills/brainmass/references/modellibrary.md +12 -10
- package/skills/package-skills/brainmass/references/scripts/resting-state-meg-whole-brain-pipeline.py +5 -3
- package/skills/package-skills/brainmass/references/scripts/wilson-cowan-ei-dynamics.py +10 -6
- package/skills/package-skills/brainmass/references/simulator-input-monitor-api.md +3 -1
- package/skills/package-skills/brainmass/references/visualization-analysis-api.md +17 -9
- package/skills/package-skills/brainspike/SKILL.md +201 -0
- package/skills/package-skills/brainspike/references/braintools/connectivity.md +40 -0
- package/skills/package-skills/brainspike/references/braintools/data-preprocessing.md +39 -0
- package/skills/package-skills/brainspike/references/braintools/input-current.md +55 -0
- package/skills/package-skills/brainspike/references/braintools/metric.md +36 -0
- package/skills/package-skills/brainspike/references/braintools/optimizer.md +50 -0
- package/skills/package-skills/brainspike/references/braintools/parameter-initializer.md +43 -0
- package/skills/package-skills/brainspike/references/braintools/surrogate.md +40 -0
- package/skills/package-skills/brainspike/references/migration-boundary.md +38 -0
- package/skills/package-skills/brainspike/references/model-and-readout-selection.md +41 -0
- package/skills/package-skills/brainspike/references/multirate-coupling.md +31 -0
- package/skills/package-skills/brainspike/references/multiscale-composition.md +28 -0
- package/skills/package-skills/brainspike/references/native-composition.md +52 -0
- package/skills/package-skills/brainspike/references/native-delays.md +34 -0
- package/skills/package-skills/brainspike/references/native-projections.md +43 -0
- package/skills/package-skills/brainspike/references/native-training.md +36 -0
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/devices.md +33 -33
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/divergence-and-parity.md +15 -15
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/integration-categories.md +1 -1
- package/skills/package-skills/brainspike/references/nest-compatible/model-library.md +200 -0
- package/skills/package-skills/brainspike/references/nest-compatible/network-building.md +276 -0
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/brette_et_al_2007.py +23 -26
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/brunel_alpha.py +18 -21
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/brunel_delta.py +16 -19
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +11 -12
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +10 -11
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/spatial_gaussex.py +18 -18
- package/skills/package-skills/{brainpy-state → brainspike}/references/nest-compatible/scripts/synapsecollection.py +26 -29
- package/skills/package-skills/brainspike/references/nest-compatible/synapse-and-connectivity.md +166 -0
- package/skills/package-skills/brainspike/references/nest-delay-boundaries.md +31 -0
- package/skills/package-skills/brainspike/references/nonlinear-state-sharing.md +26 -0
- package/skills/package-skills/{brainpy-state → brainspike}/references/perturbation-experiment-validity.md +2 -2
- package/skills/package-skills/brainspike/references/simulator-training.md +61 -0
- package/skills/package-skills/brainspike/references/temporal-task-training.md +36 -0
- package/skills/package-skills/brainspike/scripts/advanced_native_patterns.py +76 -0
- package/skills/package-skills/brainspike/scripts/delay_workflow.py +44 -0
- package/skills/package-skills/brainspike/scripts/delayed_cue_training.py +78 -0
- package/skills/package-skills/brainspike/scripts/lif_smoke.py +31 -0
- package/skills/package-skills/brainspike/scripts/multiscale_workflow.py +35 -0
- package/skills/package-skills/brainspike/scripts/native_ei_network.py +47 -0
- package/skills/package-skills/brainspike/scripts/native_workflows.py +77 -0
- package/skills/package-skills/brainspike/scripts/nest_boundary_workflows.py +108 -0
- package/skills/package-skills/brainspike/scripts/train_native_snn.py +65 -0
- package/skills/package-skills/brainspike/scripts/train_nest_simulator.py +69 -0
- package/skills/package-skills/brainspike/scripts/train_nest_simulator_two_weights.py +220 -0
- package/skills/package-skills/brainstate/SKILL.md +16 -21
- package/skills/package-skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +1 -138
- package/skills/package-skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +1 -1
- package/skills/package-skills/brainstate/references/brainstate/delay-buffers.md +45 -0
- package/skills/package-skills/brainstate/references/brainstate/parameter-constraints-regularization.md +9 -7
- package/skills/package-skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +4 -4
- package/skills/package-skills/brainstate/references/brainstate/randomness-and-reproducibility.md +6 -4
- package/skills/package-skills/brainstate/references/brainstate/transformation-grad-expansion.md +4 -4
- package/skills/package-skills/brainstate/references/brainstate/transformation-vmap-expansion.md +1 -1
- package/skills/package-skills/brainstate/references/braintools-optimizer-reference.md +16 -0
- package/skills/package-skills/brainstate/references/libraries/prebuilt-layer-library.md +1 -1
- package/skills/package-skills/brainstate/references/simulation-environment.md +5 -5
- package/skills/package-skills/braintrace/SKILL.md +17 -38
- package/skills/package-skills/braintrace/references/Drtrl.md +9 -26
- package/skills/package-skills/braintrace/references/algorithm selection.md +7 -12
- package/skills/package-skills/braintrace/references/batching.md +15 -35
- package/skills/package-skills/braintrace/references/compiler_internal.md +1 -1
- package/skills/package-skills/braintrace/references/custom algorithms.md +1 -2
- package/skills/package-skills/braintrace/references/customizing_primitive_transforms.md +26 -15
- package/skills/package-skills/braintrace/references/pp_pprop workflow.md +5 -139
- package/skills/package-skills/brainunit/SKILL.md +7 -2
- package/skills/package-skills/brainunit/references/array-mechanics/functional-structural-api.md +1 -1
- package/skills/package-skills/brainunit/references/math-function-library.md +2 -2
- package/skills/package-skills/brainunit/references/typing-and-runtime-validation.md +5 -2
- package/skills/package-skills/brainevent/references/scripts/102_EI_net_1996.py +0 -107
- package/skills/package-skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +0 -168
- package/skills/package-skills/brainevent/references/scripts/coba_ei_teaching.py +0 -356
- package/skills/package-skills/brainpy-state/SKILL.md +0 -247
- package/skills/package-skills/brainpy-state/references/array-creation.md +0 -158
- package/skills/package-skills/brainpy-state/references/brain-dynamics-delay-protocol.md +0 -147
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/analysis.md +0 -150
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/brainpy legacy workflow.md +0 -373
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/built-in dynamic neuron model.md +0 -150
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/connecting neurons.md +0 -175
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/customize neuron and synpase.md +0 -286
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/Input generation.md +0 -111
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/More about simulation.md +0 -163
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/Multi-device array sharding.md +0 -113
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/Parallel experiment execution.md +0 -136
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/array creation and mechanics.md +0 -222
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/brainpy math environment setting.md +0 -123
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/delays.md +0 -107
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/infrastructure/object oriented transformations and control flows.md +0 -185
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/integrators.md +0 -181
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/route activity through connectivity.md +0 -102
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/synaptic projections.md +0 -147
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/synpase properties.md +0 -159
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/loss library.md +0 -127
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/optimizers.md +0 -167
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/parameter initializers.md +0 -107
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/prebuilt neural network layers.md +0 -186
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/surrogate gradients.md +0 -104
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/trainer library.md +0 -89
- package/skills/package-skills/brainpy-state/references/brainPy(legacy)/training/trainingworkflows.md +0 -253
- package/skills/package-skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +0 -334
- package/skills/package-skills/brainpy-state/references/braintools/connectivity.md +0 -206
- package/skills/package-skills/brainpy-state/references/braintools/data-preprocessing.md +0 -79
- package/skills/package-skills/brainpy-state/references/braintools/input-current.md +0 -199
- package/skills/package-skills/brainpy-state/references/braintools/metric.md +0 -152
- package/skills/package-skills/brainpy-state/references/braintools/optimizer.md +0 -179
- package/skills/package-skills/brainpy-state/references/braintools/parameter-initializer.md +0 -183
- package/skills/package-skills/brainpy-state/references/braintools/surrogate.md +0 -147
- package/skills/package-skills/brainpy-state/references/component-selection.md +0 -288
- package/skills/package-skills/brainpy-state/references/nest-compatible/model-library.md +0 -200
- package/skills/package-skills/brainpy-state/references/nest-compatible/nest-workflow.md +0 -156
- package/skills/package-skills/brainpy-state/references/nest-compatible/network-building.md +0 -264
- package/skills/package-skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +0 -153
- package/skills/package-skills/brainpy-state/references/projection-patterns.md +0 -127
- package/skills/package-skills/brainpy-state/references/scripts/103_COBA_2005.py +0 -86
- package/skills/package-skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +0 -181
- package/skills/package-skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +0 -156
- package/skills/package-skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +0 -111
- package/skills/package-skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +0 -221
- package/skills/package-skills/brainpy-state/references/scripts/sound_localization.py +0 -240
- package/skills/package-skills/brainpy-state/references/scripts/training-snn.py +0 -119
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Algorithm selection
|
|
2
2
|
|
|
3
|
-
Use this reference when choosing a BrainTrace
|
|
3
|
+
Use this reference when choosing a BrainTrace estimator beyond the default `D_RTRL`. Use `Drtrl.md` or `pp_pprop workflow.md` for the two canonical training workflows; open `custom algorithms.md` only when no built-in class expresses the method.
|
|
4
4
|
|
|
5
5
|
## Choose an estimator
|
|
6
6
|
|
|
@@ -17,8 +17,6 @@ Choose by trace representation, recurrence approximation, learning signal, and s
|
|
|
17
17
|
| `OTTT` | Use for very large SNNs using the implemented presynaptic trace rule. | Requires `leak`; report the selected mode. |
|
|
18
18
|
| `OSTTP` | Use for target-projection learning with supplied per-layer feedback matrices. | Requires `B_list`; declare target timing. |
|
|
19
19
|
|
|
20
|
-
`SnAp`, `UORO`, `ThreeFactor`, `DNI`, `SyntheticGradient`, and `ETraceConfig` are not public BrainTrace `0.2.4` APIs. Do not select them from newer documentation when targeting BrainX `v2026.7.9`.
|
|
21
|
-
|
|
22
20
|
## Compile the chosen algorithm
|
|
23
21
|
|
|
24
22
|
`braintrace.compile` is the shared entry point; options are forwarded to the selected algorithm constructor.
|
|
@@ -45,20 +43,17 @@ learner = braintrace.compile(
|
|
|
45
43
|
learner.module.report.show(2)
|
|
46
44
|
```
|
|
47
45
|
|
|
48
|
-
## Drive the sequence
|
|
49
|
-
|
|
50
|
-
BrainTrace `0.2.4` advances hidden and eligibility State on every learner call; BrainState owns the explicit temporal carry and gradient accumulation.
|
|
46
|
+
## Drive the sequence
|
|
51
47
|
|
|
52
48
|
| API | Description |
|
|
53
49
|
|---|---|
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
56
|
-
| `braintrace.SingleStepData(data)` | Use to mark one current input
|
|
50
|
+
| `learner.etrace_grad(..., step_fn=...)` | Use for online sequence gradients. Call the learner once per step loss. |
|
|
51
|
+
| `learner.etrace_evolve(inputs, ...)` | Use for warm-up or evaluation that advances State without accumulating gradients. |
|
|
52
|
+
| `braintrace.SingleStepData(data)` | Use to mark one current input. |
|
|
57
53
|
| `braintrace.MultiStepData(sequence)` | Use when one learner call intentionally consumes a multi-step window. |
|
|
58
54
|
|
|
59
|
-
|
|
55
|
+
Open `batching.md` for mapped and native-batch reset patterns.
|
|
60
56
|
|
|
61
57
|
## Sources
|
|
62
58
|
|
|
63
|
-
- [
|
|
64
|
-
- [BrainTrace `v0.2.4` public API](https://github.com/chaobrain/braintrace/blob/v0.2.4/braintrace/__init__.py)
|
|
59
|
+
- [Algorithms API](https://brainx.chaobrain.com/braintrace/apis/algorithms.html)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Batching
|
|
2
2
|
|
|
3
|
-
Use this reference when adding a batch axis
|
|
3
|
+
Use this reference when adding a batch axis, selecting native batched execution or independent mapped lanes, resetting mapped State, or scanning time-major data.
|
|
4
4
|
|
|
5
5
|
## Choose one batching owner
|
|
6
6
|
|
|
@@ -25,7 +25,6 @@ Compile-owned vmap creates per-lane State and returns a `brainstate.nn.Vmap`; it
|
|
|
25
25
|
| `brainstate.transform.vmap(in_states=mapped_states)` | Use to map reset over exactly the State created by compile-owned vmap. |
|
|
26
26
|
|
|
27
27
|
```python
|
|
28
|
-
import jax
|
|
29
28
|
import jax.numpy as jnp
|
|
30
29
|
import brainstate
|
|
31
30
|
import braintrace
|
|
@@ -34,7 +33,7 @@ import braintrace
|
|
|
34
33
|
class SimpleGRU(brainstate.nn.Module):
|
|
35
34
|
def __init__(self, n_in, n_rec, n_out):
|
|
36
35
|
super().__init__()
|
|
37
|
-
self.rnn = braintrace.nn.
|
|
36
|
+
self.rnn = braintrace.nn.MiniGRU(in_size=n_in, out_size=n_rec)
|
|
38
37
|
self.out = braintrace.nn.Linear(n_rec, n_out)
|
|
39
38
|
|
|
40
39
|
def update(self, x):
|
|
@@ -82,41 +81,23 @@ Reset native batched execution with `brainstate.nn.reset_all_states(model, batch
|
|
|
82
81
|
|
|
83
82
|
## Accumulate time-major gradients
|
|
84
83
|
|
|
85
|
-
Keep time on axis 0 and batch on axis 1. The
|
|
84
|
+
Keep time on axis 0 and batch on axis 1. The sequence driver owns time. Compilation owns the batch axis.
|
|
86
85
|
|
|
87
86
|
```python
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def step_loss(inp, target, mask):
|
|
87
|
+
def step_loss(inp, target):
|
|
92
88
|
prediction = mapped_learner(inp)
|
|
93
|
-
|
|
94
|
-
return loss * mask, prediction
|
|
89
|
+
return jnp.mean((prediction - target) ** 2), prediction
|
|
95
90
|
|
|
96
91
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
92
|
+
grads, losses, outputs = mapped_learner.etrace_grad(
|
|
93
|
+
inputs,
|
|
94
|
+
targets,
|
|
95
|
+
step_fn=step_loss,
|
|
100
96
|
has_aux=True,
|
|
97
|
+
mask=loss_mask,
|
|
98
|
+
reduction='sum',
|
|
101
99
|
return_value=True,
|
|
102
100
|
)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def accumulate(grads, sample):
|
|
106
|
-
current, loss, prediction = step_grad(*sample)
|
|
107
|
-
grads = jax.tree.map(lambda total, value: total + value, grads, current)
|
|
108
|
-
return grads, (loss, prediction)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
initial_grads = jax.tree.map(
|
|
112
|
-
jnp.zeros_like,
|
|
113
|
-
{key: state.value for key, state in weights.items()},
|
|
114
|
-
)
|
|
115
|
-
grads, (losses, outputs) = brainstate.transform.scan(
|
|
116
|
-
accumulate,
|
|
117
|
-
initial_grads,
|
|
118
|
-
(inputs, targets, loss_mask),
|
|
119
|
-
)
|
|
120
101
|
optimizer.update(grads)
|
|
121
102
|
```
|
|
122
103
|
|
|
@@ -129,12 +110,11 @@ Every step must call the learner exactly once, including masked cue and delay st
|
|
|
129
110
|
| `braintrace.SingleStepData(data)` | Use to mark `data` as one step; a plain array has the same interpretation. |
|
|
130
111
|
| `braintrace.MultiStepData(sequence)` | Use when one learner call should consume axis 0 as a multi-step window. |
|
|
131
112
|
|
|
132
|
-
|
|
113
|
+
Use `etrace_grad` for a sequence with per-step masks, outputs, or a single accumulated gradient update.
|
|
133
114
|
|
|
134
|
-
Open `Drtrl.md` or `pp_pprop workflow.md` for estimator-specific training. Open `
|
|
115
|
+
Open `Drtrl.md` or `pp_pprop workflow.md` for estimator-specific training. Open `brainstate` → `references/collective_model_operations.md` when an explicit mapped lifecycle is unavoidable.
|
|
135
116
|
|
|
136
117
|
## Sources
|
|
137
118
|
|
|
138
|
-
- [
|
|
139
|
-
- [
|
|
140
|
-
- [BrainTrace `v0.2.4` native batching example](https://github.com/chaobrain/braintrace/blob/v0.2.4/examples/pp_prop/06-batching-batched.py)
|
|
119
|
+
- [Batching strategies](https://brainx.chaobrain.com/braintrace/advanced/batching.html)
|
|
120
|
+
- [Algorithms API](https://brainx.chaobrain.com/braintrace/apis/algorithms.html)
|
|
@@ -114,7 +114,7 @@ The graph executor runs the compiled forward graph and produces the hidden-to-we
|
|
|
114
114
|
| `ETraceGraphExecutor` | Use as the base executor when developing a custom algorithm that consumes an `ETraceGraph`. It runs the forward graph and exposes its Jacobian calculations. |
|
|
115
115
|
| `ETraceVjpGraphExecutor` | Use for algorithms in the `ETraceVjpAlgorithm` family. It provides the VJP-based executor used by the built-in estimators. |
|
|
116
116
|
|
|
117
|
-
Keep executor calls inside the algorithm lifecycle. Application code should
|
|
117
|
+
Keep executor calls inside the algorithm lifecycle. Application code should use the compiled learner’s sequence drivers. Use `etrace_grad` to accumulate online gradients and `etrace_evolve` to advance State without accumulating gradients.
|
|
118
118
|
|
|
119
119
|
## Control-flow policy
|
|
120
120
|
|
|
@@ -191,12 +191,11 @@ assert int(learner.running_index.value) == 0
|
|
|
191
191
|
## Execution and validation rules
|
|
192
192
|
|
|
193
193
|
- Compile the custom algorithm once for a stable model structure; do not rebuild it per step.
|
|
194
|
-
-
|
|
194
|
+
- Call the learner exactly once per step and accumulate online gradients with `learner.etrace_grad`. Apply the optimizer once after the sequence.
|
|
195
195
|
- Compare an unmodified subclass against its parent before testing the new rule.
|
|
196
196
|
- Verify trace shapes, finite values, reset-to-zero behavior, parameter-gradient keys, and parameter updates.
|
|
197
197
|
- Use a reduced BPTT oracle only in a regime where the parent estimator documents equivalence. `D_RTRL` uses a diagonal recurrent-Jacobian approximation and is not generally equal to BPTT.
|
|
198
198
|
- Do not reimplement graph tracing, hidden grouping, Jacobian calculation, or State write-back unless the research method genuinely requires a new base engine.
|
|
199
|
-
- This reference targets BrainTrace `0.2.4`; do not use sequence-driver methods introduced in `0.2.5`.
|
|
200
199
|
|
|
201
200
|
## Sources
|
|
202
201
|
|
|
@@ -154,27 +154,38 @@ assert not fast_path.applicable(
|
|
|
154
154
|
|
|
155
155
|
## Exactness validation
|
|
156
156
|
|
|
157
|
-
Validate both the forward transform and its online gradient.
|
|
157
|
+
Validate both the forward transform and its online gradient. Compute the reference gradient with BrainState BPTT on a fresh model from the same factory: initialize State, sum one loss per step, and differentiate the sequence total once with `brainstate.transform.grad`. Compare it with `learner.etrace_grad(..., reduction="sum")` from an identically initialized model and input.
|
|
158
158
|
|
|
159
159
|
```python
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
)
|
|
160
|
+
import jax
|
|
161
|
+
import jax.numpy as jnp
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def bptt_grads(model_factory, inputs, targets, batch_size=1):
|
|
165
|
+
model = model_factory()
|
|
166
|
+
weights = model.states(brainstate.ParamState)
|
|
167
|
+
brainstate.nn.init_all_states(model, batch_size=batch_size)
|
|
165
168
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
)
|
|
169
|
+
def total_loss():
|
|
170
|
+
return sum(
|
|
171
|
+
jnp.mean((model(x) - target) ** 2)
|
|
172
|
+
for x, target in zip(inputs, targets)
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
return brainstate.transform.grad(total_loss, weights)()
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
bptt = bptt_grads(Model, inputs, targets)
|
|
179
|
+
online = learner.etrace_grad(
|
|
180
|
+
inputs, targets, step_fn=step_loss, reduction="sum"
|
|
181
|
+
)
|
|
182
|
+
gap = max(
|
|
183
|
+
float(jnp.max(jnp.abs(a - b) / (jnp.abs(b) + 1e-8)))
|
|
184
|
+
for a, b in zip(jax.tree.leaves(online), jax.tree.leaves(bptt))
|
|
173
185
|
)
|
|
174
|
-
assert_param_gradients_close(online, bptt, atol=1e-4)
|
|
175
186
|
```
|
|
176
187
|
|
|
177
|
-
Use
|
|
188
|
+
Use this comparison only in development tests, not as an application API. Choose a reduced model whose recurrent Jacobian lies in the estimator's documented exactness regime; a general D-RTRL model is not automatically BPTT-equivalent. A nonzero `gap` on an approximate estimator indicates regime mismatch, not a failed forward transform; validate the forward transform separately before attributing the difference to the gradient rule.
|
|
178
189
|
|
|
179
190
|
Check these failures explicitly:
|
|
180
191
|
|
|
@@ -15,142 +15,9 @@ Use this reference when input/output-factorized `pp_prop` is preferable to D-RTR
|
|
|
15
15
|
|
|
16
16
|
Factorization discards correlations present in an unfactorized trace. Report `decay_or_rank` with results and validate the estimator against BPTT on a reduced matched model when gradient fidelity matters.
|
|
17
17
|
|
|
18
|
-
##
|
|
18
|
+
## Check the model integration before training
|
|
19
19
|
|
|
20
|
-
Use ETP-aware projections for
|
|
21
|
-
|
|
22
|
-
| API | Description |
|
|
23
|
-
|---|---|
|
|
24
|
-
| `braintrace.nn.Linear(...)` | Use for an input-plus-recurrent projection whose weights participate through ETP primitives. |
|
|
25
|
-
| `braintrace.sparse_matmul(x, weight_data, sparse_mat=...)` | Use for fixed sparse recurrent structure; pass raw numeric/bool spikes as `x` and a BrainEvent `DataRepresentation` as `sparse_mat`. |
|
|
26
|
-
| `brainpy.state.LIF(...)` | Use for leaky integrate-and-fire dynamics with a differentiable surrogate spike function. |
|
|
27
|
-
| `braintrace.nn.LeakyRateReadout(...)` | Use to integrate recurrent spikes into a continuous task output. |
|
|
28
|
-
| `brainstate.environ.context(dt=...)` | Use to scope the physical simulation step around construction and execution. |
|
|
29
|
-
|
|
30
|
-
```python
|
|
31
|
-
import brainpy.state
|
|
32
|
-
import brainstate
|
|
33
|
-
import braintrace
|
|
34
|
-
import braintools
|
|
35
|
-
import brainunit as u
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
class LIF_SNN(brainstate.nn.Module):
|
|
39
|
-
def __init__(self, n_in, n_rec, n_out):
|
|
40
|
-
super().__init__()
|
|
41
|
-
self.linear = braintrace.nn.Linear(
|
|
42
|
-
n_in + n_rec,
|
|
43
|
-
n_rec,
|
|
44
|
-
w_init=braintools.init.KaimingNormal(scale=50., unit=u.mA),
|
|
45
|
-
b_init=braintools.init.ZeroInit(unit=u.mA),
|
|
46
|
-
)
|
|
47
|
-
self.neuron = brainpy.state.LIF(
|
|
48
|
-
n_rec,
|
|
49
|
-
tau=20. * u.ms,
|
|
50
|
-
R=1. * u.ohm,
|
|
51
|
-
V_th=0.1 * u.mV,
|
|
52
|
-
V_reset=0. * u.mV,
|
|
53
|
-
V_rest=0. * u.mV,
|
|
54
|
-
spk_fun=braintools.surrogate.ReluGrad(),
|
|
55
|
-
spk_reset='soft',
|
|
56
|
-
)
|
|
57
|
-
self.readout = braintrace.nn.LeakyRateReadout(
|
|
58
|
-
n_rec,
|
|
59
|
-
n_out,
|
|
60
|
-
tau=20. * u.ms,
|
|
61
|
-
w_init=braintools.init.KaimingNormal(),
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
def update(self, x):
|
|
65
|
-
previous = self.neuron.get_spike()
|
|
66
|
-
current = self.linear(u.math.concatenate([x, previous], axis=-1))
|
|
67
|
-
return self.readout(self.neuron(current))
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
The projection uses current units so `I * R` is voltage-compatible with membrane State and threshold.
|
|
71
|
-
|
|
72
|
-
## Train a batched spike sequence
|
|
73
|
-
|
|
74
|
-
Compile one-step behavior with per-sample mapped State, reset both State domains, accumulate masked step gradients through a scan, and update once per sequence.
|
|
75
|
-
|
|
76
|
-
| API | Description |
|
|
77
|
-
|---|---|
|
|
78
|
-
| `braintrace.compile(..., braintrace.pp_prop, batch_size=B, vmap=True, decay_or_rank=...)` | Use to create independent recurrent and trace State for each batch lane. |
|
|
79
|
-
| `brainstate.transform.grad(step_loss, weights, has_aux=True, return_value=True)` | Use around one learner call to return the online gradient, masked loss, and output. |
|
|
80
|
-
| `brainstate.transform.scan(...)` | Use to advance every time step and carry the summed gradient. |
|
|
81
|
-
| `brainstate.nn.clip_grad_norm(...)` | Use after accumulation when gradient clipping is required. |
|
|
82
|
-
|
|
83
|
-
```python
|
|
84
|
-
import jax
|
|
85
|
-
import jax.numpy as jnp
|
|
86
|
-
|
|
87
|
-
n_steps, batch_size = 60, 16
|
|
88
|
-
n_in, n_rec, n_out = 2, 48, 2
|
|
89
|
-
inputs = jnp.zeros((n_steps, batch_size, n_in))
|
|
90
|
-
labels = jnp.arange(batch_size, dtype=jnp.int32) % n_out
|
|
91
|
-
loss_mask = jnp.concatenate([jnp.zeros(50), jnp.ones(10)])
|
|
92
|
-
|
|
93
|
-
with brainstate.environ.context(dt=1. * u.ms):
|
|
94
|
-
model = LIF_SNN(n_in, n_rec, n_out)
|
|
95
|
-
learner = braintrace.compile(
|
|
96
|
-
model,
|
|
97
|
-
braintrace.pp_prop,
|
|
98
|
-
inputs[0],
|
|
99
|
-
batch_size=batch_size,
|
|
100
|
-
vmap=True,
|
|
101
|
-
decay_or_rank=0.9,
|
|
102
|
-
)
|
|
103
|
-
algorithm = learner.module
|
|
104
|
-
algorithm.report.show(2)
|
|
105
|
-
assert algorithm.report.counts['errors'] == 0
|
|
106
|
-
|
|
107
|
-
weights = model.states(brainstate.ParamState)
|
|
108
|
-
optimizer = braintools.optim.Adam(lr=2e-3)
|
|
109
|
-
optimizer.register_trainable_weights(weights)
|
|
110
|
-
|
|
111
|
-
mapped_states = learner.states('new')
|
|
112
|
-
|
|
113
|
-
@brainstate.transform.vmap(in_states=mapped_states)
|
|
114
|
-
def reset_sequence():
|
|
115
|
-
brainstate.nn.reset_all_states(learner)
|
|
116
|
-
|
|
117
|
-
def step_loss(cue, mask):
|
|
118
|
-
output = learner(cue)
|
|
119
|
-
loss = braintools.metric.softmax_cross_entropy_with_integer_labels(
|
|
120
|
-
output,
|
|
121
|
-
labels,
|
|
122
|
-
).mean()
|
|
123
|
-
return loss * mask, output
|
|
124
|
-
|
|
125
|
-
step_grad = brainstate.transform.grad(
|
|
126
|
-
step_loss,
|
|
127
|
-
weights,
|
|
128
|
-
has_aux=True,
|
|
129
|
-
return_value=True,
|
|
130
|
-
)
|
|
131
|
-
|
|
132
|
-
def train_sequence():
|
|
133
|
-
reset_sequence()
|
|
134
|
-
initial_grads = jax.tree.map(
|
|
135
|
-
jnp.zeros_like,
|
|
136
|
-
{key: state.value for key, state in weights.items()},
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
def accumulate(grads, sample):
|
|
140
|
-
current, loss, output = step_grad(*sample)
|
|
141
|
-
grads = jax.tree.map(lambda x, y: x + y, grads, current)
|
|
142
|
-
return grads, (loss, output)
|
|
143
|
-
|
|
144
|
-
grads, (losses, outputs) = brainstate.transform.scan(
|
|
145
|
-
accumulate,
|
|
146
|
-
initial_grads,
|
|
147
|
-
(inputs, loss_mask),
|
|
148
|
-
)
|
|
149
|
-
optimizer.update(brainstate.nn.clip_grad_norm(grads, 1.0))
|
|
150
|
-
return losses.sum() / loss_mask.sum(), outputs
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
`step_loss` calls the learner on every cue, delay, and report step. The zero mask removes the loss signal before the report window without freezing hidden or eligibility State. Do not put `optimizer.update()` inside the scan unless the intended experiment explicitly uses per-timestep parameter changes and reports that schedule.
|
|
20
|
+
Use ETP-aware native projections with a public `brainspike.Neuron` subclass. Open `brainspike` → `references/native-training.md` and `brainspike` → `scripts/train_native_snn.py` for the verified recurrent SNN, compiled pp-prop learner, State resets, and sequence update. The reduced Docker check verifies compiler participation, finite nonzero recurrent/readout gradients, decreasing loss, and a matched BPTT gradient direction. Do not transfer that result to an arbitrary NEST Simulator graph or assume estimator equality.
|
|
154
21
|
|
|
155
22
|
## Validate silent-delay memory
|
|
156
23
|
|
|
@@ -162,7 +29,7 @@ A lower training loss does not prove that the first cue survived the delay; use
|
|
|
162
29
|
| Frozen evaluation | Fix evaluation trials, seeds, masks, and metrics before viewing the trained result. |
|
|
163
30
|
| First-cue ablation | Replace or permute only the first cue and rerun the same mapped rollout; accuracy should fall materially relative to intact trials. |
|
|
164
31
|
| Robustness | Prefer held-out stochastic cue realizations or a nearby longer delay when the task permits them. |
|
|
165
|
-
| Schedule | Report sequences and optimizer updates separately; one
|
|
32
|
+
| Schedule | Report sequences and optimizer updates separately; one sequence-driver call followed by one update means one optimizer update per sequence. |
|
|
166
33
|
|
|
167
34
|
For a deterministic two-cue task with only four combinations, first-cue ablation is the minimum discriminating control. Perfect accuracy on the same four training combinations is insufficient by itself.
|
|
168
35
|
|
|
@@ -174,6 +41,5 @@ Open `compiler_internal.md` only when structured report records cannot explain t
|
|
|
174
41
|
|
|
175
42
|
## Sources
|
|
176
43
|
|
|
177
|
-
- [
|
|
178
|
-
- [
|
|
179
|
-
- [BrainTrace `v0.2.4` working-memory example](https://github.com/chaobrain/braintrace/blob/v0.2.4/examples/pp_prop/03-neurons-gif-working-memory.py)
|
|
44
|
+
- [pp-prop tutorial](https://brainx.chaobrain.com/braintrace/tutorials/pp_prop.html)
|
|
45
|
+
- [SNN online learning](https://brainx.chaobrain.com/braintrace/tutorials/snn_online_learning.html)
|
|
@@ -186,7 +186,7 @@ elementary_charge = u.constants.elementary_charge
|
|
|
186
186
|
|
|
187
187
|
## Normalize inputs and create ranges
|
|
188
188
|
|
|
189
|
-
Use `u.math.asarray()` to normalize plain data and quantities. Without `unit`, it returns
|
|
189
|
+
Use `u.math.asarray()` to normalize plain data and quantities. Without `unit`, it returns a plain array for plain data or preserves the input unit. With `unit=target`, it converts an existing quantity to `target`; a plain or dimensionless input raises `UnitMismatchError`, because `unit` selects a conversion rather than attaching a unit. `u.math.array` is an alias.
|
|
190
190
|
|
|
191
191
|
Use `u.math.arange()` to create half-open intervals `[start, stop)`, returning an array for plain arguments or a `Quantity` for unit-bearing arguments. Unit-bearing `start`, `stop`, and `step` must share one unit. At least one of `start` or `stop` is required.
|
|
192
192
|
|
|
@@ -204,6 +204,8 @@ times = u.math.arange(0.0 * u.ms, 10.0 * u.ms, 0.1 * u.ms)
|
|
|
204
204
|
# Expected: 100 values from 0.0 through 9.9 ms; array precision follows JAX.
|
|
205
205
|
```
|
|
206
206
|
|
|
207
|
+
Attach a unit to plain data by multiplication (`u.math.asarray(values) * u.mm`) or with a constructor that accepts `unit=` for new values (`u.math.ones`, `u.math.zeros`, `u.math.from_numpy`).
|
|
208
|
+
|
|
207
209
|
Do not strip units before normalization or attach a unit to an unchecked plain range afterward.
|
|
208
210
|
|
|
209
211
|
## Type and validate quantity boundaries
|
|
@@ -214,7 +216,10 @@ Annotations alone describe the contract. Apply `@u.typing.validate_units` when c
|
|
|
214
216
|
|
|
215
217
|
```python
|
|
216
218
|
def normalize(values: u.typing.QuantityLike, unit: u.typing.UnitLike = None):
|
|
217
|
-
|
|
219
|
+
data = u.math.asarray(values)
|
|
220
|
+
if unit is not None and isinstance(data, u.Quantity):
|
|
221
|
+
data = u.math.asarray(data, unit=unit)
|
|
222
|
+
return data
|
|
218
223
|
|
|
219
224
|
|
|
220
225
|
@u.typing.validate_units
|
package/skills/package-skills/brainunit/references/array-mechanics/functional-structural-api.md
CHANGED
|
@@ -87,4 +87,4 @@ Examples use concise `Quantity(..., "unit")` results; the exact displayed repres
|
|
|
87
87
|
## Sources mirrored
|
|
88
88
|
|
|
89
89
|
- https://brainunit.readthedocs.io/apis/brainunit.math.html
|
|
90
|
-
-
|
|
90
|
+
- Use the module index above to open the generated page for a specific function.
|
|
@@ -332,7 +332,7 @@ The original worked example, extended to cover all eight choices:
|
|
|
332
332
|
```python
|
|
333
333
|
import brainunit as u
|
|
334
334
|
|
|
335
|
-
x = u.math.asarray([1.0, 2.0, 3.0]
|
|
335
|
+
x = u.math.asarray([1.0, 2.0, 3.0]) * u.meter
|
|
336
336
|
|
|
337
337
|
total = u.math.sum(x)
|
|
338
338
|
# Expected: 6 m.
|
|
@@ -428,6 +428,6 @@ Use `einrearrange()` and `einrepeat()` through `array-mechanics.md` when element
|
|
|
428
428
|
|
|
429
429
|
- https://brainunit.readthedocs.io/apis/brainunit.math.html
|
|
430
430
|
- https://brainunit.readthedocs.io/unit_operations/einstein_operations.html
|
|
431
|
-
-
|
|
431
|
+
- Use the module index above to open the generated page for a specific function.
|
|
432
432
|
- https://brainunit.readthedocs.io/apis/generated/brainunit.math.einreduce.html
|
|
433
433
|
- https://brainunit.readthedocs.io/apis/generated/brainunit.math.einsum.html
|
|
@@ -14,7 +14,7 @@ Use this reference when a BrainUnit interface needs precise annotations, reusabl
|
|
|
14
14
|
| `HAS_UNIT` | Any `Quantity`, regardless of dimension | Only the presence of a unit-aware value matters |
|
|
15
15
|
| `LENGTH`, `TIME`, `SPEED`, and other pre-built aliases | A reusable named physical dimension | Repeated signatures should share a short domain term |
|
|
16
16
|
|
|
17
|
-
`QuantityLike` is intentionally broad and does not require a unit. Normalize it with `u.math.asarray()` before relying on array or unit properties.
|
|
17
|
+
`QuantityLike` is intentionally broad and does not require a unit. Normalize it with `u.math.asarray()` before relying on array or unit properties. Passing `unit=target` converts an existing quantity and raises `UnitMismatchError` for plain data, so attach a unit to plain values first when one is required.
|
|
18
18
|
|
|
19
19
|
```python
|
|
20
20
|
import brainunit as u
|
|
@@ -22,7 +22,10 @@ from brainunit.typing import QuantityLike, UnitLike
|
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
def normalize(values: QuantityLike, unit: UnitLike = None):
|
|
25
|
-
|
|
25
|
+
data = u.math.asarray(values)
|
|
26
|
+
if unit is not None and isinstance(data, u.Quantity):
|
|
27
|
+
data = u.math.asarray(data, unit=unit)
|
|
28
|
+
return data
|
|
26
29
|
```
|
|
27
30
|
|
|
28
31
|
## Array and structural aliases
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 BrainX Ecosystem Limited. All Rights Reserved.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
# ==============================================================================
|
|
15
|
-
|
|
16
|
-
#
|
|
17
|
-
# Implementation of the EI network from Brunel (1996) with the brainstate package.
|
|
18
|
-
#
|
|
19
|
-
# - Van Vreeswijk, Carl, and Haim Sompolinsky. “Chaos in neuronal networks with balanced
|
|
20
|
-
# excitatory and inhibitory activity.” Science 274.5293 (1996): 1724-1726.
|
|
21
|
-
#
|
|
22
|
-
# Dynamic of membrane potential is given as:
|
|
23
|
-
#
|
|
24
|
-
# $$ \tau \frac {dV_i}{dt} = -(V_i - V_{rest}) + I_i^{ext} + I_i^{net} (t) $$
|
|
25
|
-
#
|
|
26
|
-
# where $I_i^{net}(t)$ represents the synaptic current, which describes the sum of excitatory and inhibitory neurons.
|
|
27
|
-
#
|
|
28
|
-
# $$ I_i^{net} (t) = J_E \sum_{j=1}^{pN_e} \sum_{t_j^\alpha < t} f(t-t_j^\alpha ) - J_I \sum_{j=1}^{pN_i} \sum_{t_j^\alpha < t} f(t-t_j^\alpha )$$
|
|
29
|
-
#
|
|
30
|
-
# where
|
|
31
|
-
#
|
|
32
|
-
# $$ f(t) = \begin{cases} {\rm exp} (-\frac t {\tau_s} ), \quad t \geq 0 \\
|
|
33
|
-
# 0, \quad t < 0 \end{cases} $$
|
|
34
|
-
#
|
|
35
|
-
# Parameters: $J_E = \frac 1 {\sqrt {pN_e}}, J_I = \frac 1 {\sqrt {pN_i}}$
|
|
36
|
-
#
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
import brainunit as u
|
|
40
|
-
import matplotlib.pyplot as plt
|
|
41
|
-
|
|
42
|
-
import brainpy
|
|
43
|
-
import brainstate
|
|
44
|
-
import braintools
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
class EINet(brainstate.nn.Module):
|
|
48
|
-
def __init__(self, n_exc, n_inh, prob, JE, JI):
|
|
49
|
-
super().__init__()
|
|
50
|
-
self.n_exc = n_exc
|
|
51
|
-
self.n_inh = n_inh
|
|
52
|
-
self.num = n_exc + n_inh
|
|
53
|
-
|
|
54
|
-
# neurons
|
|
55
|
-
self.N = brainpy.state.LIF(
|
|
56
|
-
n_exc + n_inh,
|
|
57
|
-
V_rest=-52. * u.mV, V_th=-50. * u.mV, V_reset=-60. * u.mV, tau=10. * u.ms,
|
|
58
|
-
V_initializer=braintools.init.Normal(-60., 10., unit=u.mV), spk_reset='soft'
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
# synapses
|
|
62
|
-
self.E = brainpy.state.AlignPostProj(
|
|
63
|
-
comm=brainstate.nn.EventFixedProb(n_exc, self.num, prob, JE),
|
|
64
|
-
syn=brainpy.state.Expon.desc(self.num, tau=2. * u.ms),
|
|
65
|
-
out=brainpy.state.CUBA.desc(),
|
|
66
|
-
post=self.N,
|
|
67
|
-
)
|
|
68
|
-
self.I = brainpy.state.AlignPostProj(
|
|
69
|
-
comm=brainstate.nn.EventFixedProb(n_inh, self.num, prob, JI),
|
|
70
|
-
syn=brainpy.state.Expon.desc(self.num, tau=2. * u.ms),
|
|
71
|
-
out=brainpy.state.CUBA.desc(),
|
|
72
|
-
post=self.N,
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
def update(self, inp):
|
|
76
|
-
spks = self.N.get_spike() != 0.
|
|
77
|
-
self.E(spks[:self.n_exc])
|
|
78
|
-
self.I(spks[self.n_exc:])
|
|
79
|
-
self.N(inp)
|
|
80
|
-
return self.N.get_spike()
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
# connectivity
|
|
84
|
-
num_exc = 500
|
|
85
|
-
num_inh = 500
|
|
86
|
-
prob = 0.1
|
|
87
|
-
# external current
|
|
88
|
-
Ib = 3. * u.mA
|
|
89
|
-
# excitatory and inhibitory synaptic weights
|
|
90
|
-
JE = 1 / u.math.sqrt(prob * num_exc) * u.mS
|
|
91
|
-
JI = -1 / u.math.sqrt(prob * num_inh) * u.mS
|
|
92
|
-
|
|
93
|
-
# network
|
|
94
|
-
brainstate.environ.set(dt=0.1 * u.ms)
|
|
95
|
-
net = EINet(num_exc, num_inh, prob=prob, JE=JE, JI=JI)
|
|
96
|
-
brainstate.nn.init_all_states(net)
|
|
97
|
-
|
|
98
|
-
# simulation
|
|
99
|
-
times = u.math.arange(0. * u.ms, 1000. * u.ms, brainstate.environ.get_dt())
|
|
100
|
-
spikes = brainstate.transform.for_loop(lambda t: net.update(Ib), times, pbar=brainstate.transform.ProgressBar(10))
|
|
101
|
-
|
|
102
|
-
# visualization
|
|
103
|
-
t_indices, n_indices = u.math.where(spikes)
|
|
104
|
-
plt.scatter(times[t_indices], n_indices, s=1)
|
|
105
|
-
plt.xlabel('Time (ms)')
|
|
106
|
-
plt.ylabel('Neuron index')
|
|
107
|
-
plt.show()
|