brainx-skill 1.0.6 → 1.0.9
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 +10 -8
- package/adapters/gemini.js +7 -0
- package/adapters/opencode.js +7 -0
- package/adapters/windsurf.js +7 -0
- package/installation/lib/installer.js +4 -1
- package/manifest.json +6 -1
- package/package.json +8 -2
- package/skills/braincell/SKILL.md +182 -0
- package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
- package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +273 -0
- package/skills/braincell/references/channel-library.md +85 -0
- package/skills/braincell/references/ion-library.md +87 -0
- package/skills/braincell/references/mixions-for-adaptation.md +81 -0
- package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
- package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
- package/skills/braincell/references/multicompartment/filter-function-library.md +154 -0
- package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
- package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
- package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
- package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +260 -0
- package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
- package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
- package/skills/braincell/references/scripts/channel_ablation.py +61 -0
- package/skills/braincell/references/scripts/fi_curve.py +68 -0
- package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
- package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
- package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
- package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
- package/skills/braincell/references/solver-library-with-effects.md +188 -0
- package/skills/brainevent/SKILL.md +268 -0
- package/skills/brainevent/references/connectivity-variants.md +312 -0
- package/skills/brainevent/references/custom-operators-cpu.md +304 -0
- package/skills/brainevent/references/custom-operators-gpu.md +468 -0
- package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
- package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
- package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
- package/skills/brainevent/references/sparse-formats.md +182 -0
- package/skills/brainevent/references/synaptic-plasticity.md +285 -0
- package/skills/brainmass/SKILL.md +290 -0
- package/skills/brainmass/references/batch-transform-acceleration.md +136 -0
- package/skills/brainmass/references/brainstate/parameter-constraints-regularization.md +214 -0
- package/skills/brainmass/references/brainstate/parameter-transforms-regularizers-catalog.md +171 -0
- package/skills/brainmass/references/braintools/cogtask.md +190 -0
- package/skills/brainmass/references/braintools/data-preprocessing.md +83 -0
- package/skills/brainmass/references/braintools/metric.md +157 -0
- package/skills/brainmass/references/braintools/optimizer.md +187 -0
- package/skills/brainmass/references/braintools/parameter-initializer.md +187 -0
- package/skills/brainmass/references/braintools/surrogate.md +151 -0
- package/skills/brainmass/references/coupling-network-api.md +148 -0
- package/skills/brainmass/references/datasets-api.md +72 -0
- package/skills/brainmass/references/fitting-with-objectives-api.md +146 -0
- package/skills/brainmass/references/forward-observation-api.md +131 -0
- package/skills/brainmass/references/horn-task-training.md +199 -0
- package/skills/brainmass/references/modellibrary.md +104 -0
- package/skills/brainmass/references/noiseprocesses.md +121 -0
- package/skills/brainmass/references/parameter-sweeps-and-regime-analysis.md +130 -0
- package/skills/brainmass/references/scripts/eeg-fitting-with-gradients.py +230 -0
- package/skills/brainmass/references/scripts/gradient-free-fitting.py +106 -0
- package/skills/brainmass/references/scripts/hopf-bifurcation-single-node.py +78 -0
- package/skills/brainmass/references/scripts/horn-cognitive-task-training.py +211 -0
- package/skills/brainmass/references/scripts/jansen-rit-eeg-proxy.py +76 -0
- package/skills/brainmass/references/scripts/kuramoto-synchronization.py +81 -0
- package/skills/brainmass/references/scripts/linear-baseline-node.py +81 -0
- package/skills/brainmass/references/scripts/resting-state-meg-whole-brain-pipeline.py +249 -0
- package/skills/brainmass/references/scripts/seizure-epileptor-case-study.py +126 -0
- package/skills/brainmass/references/scripts/wilson-cowan-ei-dynamics.py +86 -0
- package/skills/brainmass/references/scripts/wong-wang-decision-making.py +167 -0
- package/skills/brainmass/references/scripts/wong-wang-dmf-resting-state.py +72 -0
- package/skills/brainmass/references/visualization-analysis-api.md +130 -0
- package/skills/brainpy-state/SKILL.md +232 -0
- package/skills/brainpy-state/references/array-creation.md +158 -0
- package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
- package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
- package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
- package/skills/brainpy-state/references/braintools/metric.md +152 -0
- package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
- package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
- package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
- package/skills/brainpy-state/references/component-selection.md +273 -0
- package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
- package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
- package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
- package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
- package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
- package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
- package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
- package/skills/brainpy-state/references/projection-patterns.md +125 -0
- package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
- package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
- package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
- package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
- package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
- package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
- package/skills/brainstate/SKILL.md +1 -1
- package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +250 -399
- package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +6 -2
- package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +16 -2
- package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +8 -3
- package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +64 -143
- package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +13 -1
- package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +11 -1
- package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +35 -7
- package/skills/brainstate/references/collective_model_operations.md +9 -9
- package/skills/brainstate/references/extension_mechanisms.md +12 -1
- package/skills/brainstate/references/libraries/prebuilt-activation-library.md +106 -97
- package/skills/brainstate/references/libraries/prebuilt-layer-library.md +102 -176
- package/skills/brainstate/references/model-interop-and-migration.md +11 -1
- package/skills/brainstate/references/size-inference-variations.md +11 -0
- package/skills/brainstate/references/state-graph-operations.md +13 -15
- package/skills/brainstate/references/state_collections_and_utilities.md +12 -1
- package/skills/brainx-general-guard/SKILL.md +68 -0
- package/skills/brainstate/references/braintools-optimizer-reference.md +0 -133
- package/skills/brainstate/references/libraries/solver-library-with-effects.md +0 -85
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# BrainEvent sparse formats
|
|
2
|
+
|
|
3
|
+
Use this reference when explicit sparse connectivity must be stored or reoriented as CSR or CSC, or when coordinate edge-list input must be converted. Keep `CSR` as the single representative code path.
|
|
4
|
+
|
|
5
|
+
## Storage model
|
|
6
|
+
|
|
7
|
+
`CSR` and `CSC` are BrainEvent's public explicit sparse connectivity types; choose between them by the access and contraction orientation.
|
|
8
|
+
|
|
9
|
+
| API | Description |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `CSR(data, indices=None, indptr=None, *, shape, ...)` | Use for explicit row-oriented sparse connectivity and common forward `BinaryArray @ connectivity`; it stores values, column indices, and row pointers. |
|
|
12
|
+
| `CSC(data, indices=None, indptr=None, *, shape, ...)` | Use for explicit column-oriented connectivity, transpose-centered products, or column access; it stores values, row indices, and column pointers. |
|
|
13
|
+
| `csr_to_csc_index(csr_indptr, csr_indices, *, shape, include_perm=True, ...)` | Use to convert an existing CSR topology to CSC; it returns CSC structure and, by default, the permutation for the stored values. |
|
|
14
|
+
|
|
15
|
+
## Import coordinate edge lists
|
|
16
|
+
|
|
17
|
+
BrainEvent does not expose a public `brainevent.COO` connectivity type. Treat COO only as an input or interchange format for coordinate edge lists; convert it to `CSR` or `CSC` before event-driven multiplication.
|
|
18
|
+
|
|
19
|
+
| API | Description |
|
|
20
|
+
|---|---|
|
|
21
|
+
| `coo2csr(row_ids, col_ids, *, shape)` | Use only to import coordinate row and column arrays into CSR; it returns `(indptr, indices, order)`, where `data[order]` aligns values with the row-compressed structure. |
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
import brainevent
|
|
25
|
+
import jax.numpy as jnp
|
|
26
|
+
|
|
27
|
+
# Import a 4 x 5 coordinate edge list as CSR:
|
|
28
|
+
# (0, 1) = 1.5, (1, 3) = 2.0, (2, 0) = 0.5, (3, 4) = 3.0.
|
|
29
|
+
shape = (4, 5)
|
|
30
|
+
row = jnp.array([0, 1, 2, 3])
|
|
31
|
+
col = jnp.array([1, 3, 0, 4])
|
|
32
|
+
data = jnp.array([1.5, 2.0, 0.5, 3.0])
|
|
33
|
+
|
|
34
|
+
indptr, indices, order = brainevent.coo2csr(row, col, shape=shape)
|
|
35
|
+
connectivity = brainevent.CSR(
|
|
36
|
+
(data[order], indices, indptr),
|
|
37
|
+
shape=shape,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
print("shape:", connectivity.shape)
|
|
41
|
+
print("stored values:", connectivity.nse)
|
|
42
|
+
print("dense form:\n", connectivity.todense())
|
|
43
|
+
|
|
44
|
+
spikes = brainevent.BinaryArray([1, 0, 1, 0])
|
|
45
|
+
postsynaptic_input = spikes @ connectivity
|
|
46
|
+
|
|
47
|
+
assert postsynaptic_input.shape == (5,)
|
|
48
|
+
assert jnp.allclose(
|
|
49
|
+
postsynaptic_input,
|
|
50
|
+
jnp.array([0.5, 1.5, 0.0, 0.0, 0.0]),
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
This example constructs a public `CSR` matrix from coordinate input. Keep `data[order]`: `coo2csr()` sorts the index structure and returns the value permutation separately. Do not retain or present the coordinate arrays as BrainEvent connectivity.
|
|
55
|
+
|
|
56
|
+
## Selection rules
|
|
57
|
+
|
|
58
|
+
| Format | Use when | Critical invariant |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `CSR` | The contraction is row-oriented, especially presynaptic `BinaryArray @ connectivity` forward propagation. | `indptr` has one boundary per row plus the terminal boundary; `indices` are column indices. |
|
|
61
|
+
| `CSC` | The contraction or access is column-oriented or transpose-centered. | `indptr` has one boundary per column plus the terminal boundary; `indices` are row indices. |
|
|
62
|
+
|
|
63
|
+
Prefer CSR with `BinaryArray` for the common forward spiking-network path. Benchmark CSR and CSC when the actual contraction pattern makes orientation performance uncertain.
|
|
64
|
+
|
|
65
|
+
## Application: two-layer sparse spiking network
|
|
66
|
+
|
|
67
|
+
The official practice network constructs two reusable CSR layers, thresholds each weighted event product into the next `BinaryArray`, and evaluates firing statistics across multiple sparse inputs.
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
import brainevent
|
|
71
|
+
import brainstate
|
|
72
|
+
import jax.numpy as jnp
|
|
73
|
+
import numpy as np
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def dense_to_csr(weights):
|
|
77
|
+
row, col = jnp.where(weights != 0)
|
|
78
|
+
data = weights[row, col]
|
|
79
|
+
indptr, indices, order = brainevent.coo2csr(
|
|
80
|
+
row,
|
|
81
|
+
col,
|
|
82
|
+
shape=weights.shape,
|
|
83
|
+
)
|
|
84
|
+
return brainevent.CSR(
|
|
85
|
+
(data[order], indices, indptr),
|
|
86
|
+
shape=weights.shape,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class SparseSpikingNetwork:
|
|
91
|
+
def __init__(
|
|
92
|
+
self,
|
|
93
|
+
n_input,
|
|
94
|
+
n_hidden,
|
|
95
|
+
n_output,
|
|
96
|
+
connection_probability=0.1,
|
|
97
|
+
seed=0,
|
|
98
|
+
):
|
|
99
|
+
self.n_input = n_input
|
|
100
|
+
self.n_hidden = n_hidden
|
|
101
|
+
self.n_output = n_output
|
|
102
|
+
|
|
103
|
+
brainstate.random.seed(seed)
|
|
104
|
+
mask1 = brainstate.random.bernoulli(
|
|
105
|
+
connection_probability,
|
|
106
|
+
size=(n_input, n_hidden),
|
|
107
|
+
)
|
|
108
|
+
weights1 = (
|
|
109
|
+
brainstate.random.normal(size=(n_input, n_hidden))
|
|
110
|
+
* 0.1
|
|
111
|
+
* mask1
|
|
112
|
+
)
|
|
113
|
+
self.w1 = dense_to_csr(weights1)
|
|
114
|
+
|
|
115
|
+
mask2 = brainstate.random.bernoulli(
|
|
116
|
+
connection_probability,
|
|
117
|
+
size=(n_hidden, n_output),
|
|
118
|
+
)
|
|
119
|
+
weights2 = (
|
|
120
|
+
brainstate.random.normal(size=(n_hidden, n_output))
|
|
121
|
+
* 0.1
|
|
122
|
+
* mask2
|
|
123
|
+
)
|
|
124
|
+
self.w2 = dense_to_csr(weights2)
|
|
125
|
+
|
|
126
|
+
def forward(
|
|
127
|
+
self,
|
|
128
|
+
input_spikes,
|
|
129
|
+
hidden_threshold=0.3,
|
|
130
|
+
output_threshold=0.5,
|
|
131
|
+
):
|
|
132
|
+
hidden_input = input_spikes @ self.w1
|
|
133
|
+
hidden_spikes = brainevent.BinaryArray(
|
|
134
|
+
hidden_input > hidden_threshold
|
|
135
|
+
)
|
|
136
|
+
output_input = hidden_spikes @ self.w2
|
|
137
|
+
output_spikes = brainevent.BinaryArray(
|
|
138
|
+
output_input > output_threshold
|
|
139
|
+
)
|
|
140
|
+
return output_input, output_spikes, hidden_spikes
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
network = SparseSpikingNetwork(
|
|
144
|
+
n_input=500,
|
|
145
|
+
n_hidden=200,
|
|
146
|
+
n_output=10,
|
|
147
|
+
connection_probability=0.15,
|
|
148
|
+
seed=42,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
input_counts = []
|
|
152
|
+
hidden_counts = []
|
|
153
|
+
output_counts = []
|
|
154
|
+
|
|
155
|
+
brainstate.random.seed(999)
|
|
156
|
+
for _ in range(100):
|
|
157
|
+
input_spikes = brainevent.BinaryArray(
|
|
158
|
+
brainstate.random.bernoulli(0.1, size=(500,))
|
|
159
|
+
)
|
|
160
|
+
output, output_spikes, hidden_spikes = network.forward(input_spikes)
|
|
161
|
+
|
|
162
|
+
input_counts.append(int(jnp.sum(input_spikes.value)))
|
|
163
|
+
hidden_counts.append(int(jnp.sum(hidden_spikes.value)))
|
|
164
|
+
output_counts.append(int(jnp.sum(output_spikes.value)))
|
|
165
|
+
|
|
166
|
+
assert output.shape == (10,)
|
|
167
|
+
print("layer connections:", network.w1.nse, network.w2.nse)
|
|
168
|
+
print(
|
|
169
|
+
"average active neurons:",
|
|
170
|
+
np.mean(input_counts),
|
|
171
|
+
np.mean(hidden_counts),
|
|
172
|
+
np.mean(output_counts),
|
|
173
|
+
)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
This adapts section 6, “Practice: Building a sparse connection neural network.” Construct CSR weights once and reuse them across samples; only event vectors change during forward propagation.
|
|
177
|
+
|
|
178
|
+
## Sources
|
|
179
|
+
|
|
180
|
+
- Tutorial 2, Sparse Data Structures - CSR and CSC: https://brainx.chaobrain.com/brainevent/tutorials/data-structures/02_sparse_matrices.html
|
|
181
|
+
- Sparse Matrix Data Structures API: https://brainx.chaobrain.com/brainevent/reference/apis/sparsedata.html
|
|
182
|
+
- Utility Functions API: https://brainx.chaobrain.com/brainevent/reference/apis/utilities.html
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# BrainEvent synaptic plasticity
|
|
2
|
+
|
|
3
|
+
Use this reference when binary pre- or postsynaptic events must modify stored synaptic weights. Plasticity is an overlay on the core `BinaryArray @ connectivity` workflow, not a prerequisite for ordinary event-driven communication.
|
|
4
|
+
|
|
5
|
+
BrainEvent's event-driven plasticity operators touch only weights connected to neurons that fired, following the same event-sparsity principle as its matrix products.
|
|
6
|
+
|
|
7
|
+
## Choose the update operator
|
|
8
|
+
|
|
9
|
+
| Weight storage | Presynaptic event triggers update | Postsynaptic event triggers update | Use when |
|
|
10
|
+
|---|---|---|---|
|
|
11
|
+
| CSR | `update_csr_on_binary_pre()` | `update_csr_on_binary_post()` | Connectivity is sparse and fixed; only stored synapses should be visited |
|
|
12
|
+
| Dense | `update_dense_on_binary_pre()` | `update_dense_on_binary_post()` | A small fully connected layer already uses a dense weight matrix |
|
|
13
|
+
|
|
14
|
+
Use the `*_on_binary_pre` direction when the source neuron firing triggers the rule. Use the `*_on_binary_post` direction when the target neuron firing triggers it.
|
|
15
|
+
|
|
16
|
+
## Canonical CSR STDP overlay
|
|
17
|
+
|
|
18
|
+
The official tutorial maintains exponentially decaying pre/post traces, applies a spike-triggered update to the CSR `data`, and rebuilds the CSR object while preserving its structural arrays.
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import brainevent
|
|
22
|
+
import brainstate
|
|
23
|
+
import jax.numpy as jnp
|
|
24
|
+
import numpy as np
|
|
25
|
+
|
|
26
|
+
n_pre = 100
|
|
27
|
+
n_post = 50
|
|
28
|
+
connection_probability = 0.1
|
|
29
|
+
|
|
30
|
+
# Create explicit sparse weights and convert their COO indices to CSR.
|
|
31
|
+
brainstate.random.seed(42)
|
|
32
|
+
mask = brainstate.random.bernoulli(
|
|
33
|
+
connection_probability,
|
|
34
|
+
size=(n_pre, n_post),
|
|
35
|
+
)
|
|
36
|
+
weights_dense = (
|
|
37
|
+
brainstate.random.uniform(0.0, 0.5, size=(n_pre, n_post))
|
|
38
|
+
* mask
|
|
39
|
+
)
|
|
40
|
+
row, col = jnp.where(weights_dense != 0)
|
|
41
|
+
data = weights_dense[row, col]
|
|
42
|
+
indptr, indices, order = brainevent.coo2csr(
|
|
43
|
+
row,
|
|
44
|
+
col,
|
|
45
|
+
shape=(n_pre, n_post),
|
|
46
|
+
)
|
|
47
|
+
csr_weights = brainevent.CSR(
|
|
48
|
+
(data[order], indices, indptr),
|
|
49
|
+
shape=(n_pre, n_post),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# Initialize traces and the official presynaptic-triggered STDP parameters.
|
|
53
|
+
pre_trace = jnp.zeros(n_pre)
|
|
54
|
+
post_trace = jnp.zeros(n_post)
|
|
55
|
+
decay_pre = np.exp(-1.0 / 20.0)
|
|
56
|
+
decay_post = np.exp(-1.0 / 20.0)
|
|
57
|
+
A_plus = 0.005
|
|
58
|
+
initial_mean = csr_weights.data.mean()
|
|
59
|
+
|
|
60
|
+
brainstate.random.seed(100)
|
|
61
|
+
for _ in range(500):
|
|
62
|
+
pre_spike = brainstate.random.bernoulli(0.05, size=(n_pre,))
|
|
63
|
+
post_spike = brainstate.random.bernoulli(0.05, size=(n_post,))
|
|
64
|
+
|
|
65
|
+
pre_trace = (
|
|
66
|
+
pre_trace * decay_pre
|
|
67
|
+
+ pre_spike.astype(jnp.float32)
|
|
68
|
+
)
|
|
69
|
+
post_trace = (
|
|
70
|
+
post_trace * decay_post
|
|
71
|
+
+ post_spike.astype(jnp.float32)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
new_data = brainevent.update_csr_on_binary_pre(
|
|
75
|
+
weight=csr_weights.data,
|
|
76
|
+
indices=csr_weights.indices,
|
|
77
|
+
indptr=csr_weights.indptr,
|
|
78
|
+
pre_spike=pre_spike,
|
|
79
|
+
post_trace=post_trace * A_plus,
|
|
80
|
+
w_min=0.0,
|
|
81
|
+
w_max=1.0,
|
|
82
|
+
shape=csr_weights.shape,
|
|
83
|
+
)
|
|
84
|
+
csr_weights = brainevent.CSR(
|
|
85
|
+
(new_data, csr_weights.indices, csr_weights.indptr),
|
|
86
|
+
shape=csr_weights.shape,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
print("connections:", csr_weights.nse)
|
|
90
|
+
print("initial mean weight:", initial_mean)
|
|
91
|
+
print("final mean weight:", csr_weights.data.mean())
|
|
92
|
+
print(
|
|
93
|
+
"final range:",
|
|
94
|
+
csr_weights.data.min(),
|
|
95
|
+
csr_weights.data.max(),
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This mirrors the official sparse-network construction and learning loop while using only the presynaptic CSR update API. Preserve `indices`, `indptr`, and `shape`; the operator updates weight values, not the sparse topology.
|
|
100
|
+
|
|
101
|
+
For a bidirectional STDP rule, combine the corresponding pre- and post-triggered operators with the appropriate traces and signs from the learning rule. Do not silently substitute the pre-triggered operator for a post-triggered update.
|
|
102
|
+
|
|
103
|
+
## Application: adaptive two-layer spiking network
|
|
104
|
+
|
|
105
|
+
The official self-learning practice stores two CSR layers, propagates binary events through both, maintains decaying traces, and applies presynaptic-triggered updates after each sample.
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
import brainevent
|
|
109
|
+
import brainstate
|
|
110
|
+
import jax.numpy as jnp
|
|
111
|
+
import numpy as np
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def dense_to_csr(weights):
|
|
115
|
+
row, col = jnp.where(weights != 0)
|
|
116
|
+
data = weights[row, col]
|
|
117
|
+
indptr, indices, order = brainevent.coo2csr(
|
|
118
|
+
row,
|
|
119
|
+
col,
|
|
120
|
+
shape=weights.shape,
|
|
121
|
+
)
|
|
122
|
+
return brainevent.CSR(
|
|
123
|
+
(data[order], indices, indptr),
|
|
124
|
+
shape=weights.shape,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class AdaptiveSpikingNetwork:
|
|
129
|
+
def __init__(
|
|
130
|
+
self,
|
|
131
|
+
n_input,
|
|
132
|
+
n_hidden,
|
|
133
|
+
n_output,
|
|
134
|
+
connection_probability=0.15,
|
|
135
|
+
seed=0,
|
|
136
|
+
):
|
|
137
|
+
self.n_input = n_input
|
|
138
|
+
self.n_hidden = n_hidden
|
|
139
|
+
self.n_output = n_output
|
|
140
|
+
|
|
141
|
+
brainstate.random.seed(seed)
|
|
142
|
+
mask1 = brainstate.random.bernoulli(
|
|
143
|
+
connection_probability,
|
|
144
|
+
size=(n_input, n_hidden),
|
|
145
|
+
)
|
|
146
|
+
weights1 = (
|
|
147
|
+
brainstate.random.uniform(
|
|
148
|
+
0.0,
|
|
149
|
+
0.3,
|
|
150
|
+
size=(n_input, n_hidden),
|
|
151
|
+
)
|
|
152
|
+
* mask1
|
|
153
|
+
)
|
|
154
|
+
self.w1 = dense_to_csr(weights1)
|
|
155
|
+
|
|
156
|
+
mask2 = brainstate.random.bernoulli(
|
|
157
|
+
connection_probability,
|
|
158
|
+
size=(n_hidden, n_output),
|
|
159
|
+
)
|
|
160
|
+
weights2 = (
|
|
161
|
+
brainstate.random.uniform(
|
|
162
|
+
0.0,
|
|
163
|
+
0.3,
|
|
164
|
+
size=(n_hidden, n_output),
|
|
165
|
+
)
|
|
166
|
+
* mask2
|
|
167
|
+
)
|
|
168
|
+
self.w2 = dense_to_csr(weights2)
|
|
169
|
+
|
|
170
|
+
self.input_trace = jnp.zeros(n_input)
|
|
171
|
+
self.hidden_trace = jnp.zeros(n_hidden)
|
|
172
|
+
self.output_trace = jnp.zeros(n_output)
|
|
173
|
+
self.decay = np.exp(-1.0 / 20.0)
|
|
174
|
+
self.learning_rate = 0.008
|
|
175
|
+
|
|
176
|
+
def forward(self, input_spikes, learning=True):
|
|
177
|
+
hidden_input = input_spikes @ self.w1
|
|
178
|
+
hidden_spikes = brainevent.BinaryArray(hidden_input > 0.5)
|
|
179
|
+
output_input = hidden_spikes @ self.w2
|
|
180
|
+
output_spikes = brainevent.BinaryArray(output_input > 0.8)
|
|
181
|
+
|
|
182
|
+
input_events = input_spikes.value
|
|
183
|
+
hidden_events = hidden_spikes.value
|
|
184
|
+
output_events = output_spikes.value
|
|
185
|
+
self.input_trace = (
|
|
186
|
+
self.input_trace * self.decay
|
|
187
|
+
+ input_events.astype(jnp.float32)
|
|
188
|
+
)
|
|
189
|
+
self.hidden_trace = (
|
|
190
|
+
self.hidden_trace * self.decay
|
|
191
|
+
+ hidden_events.astype(jnp.float32)
|
|
192
|
+
)
|
|
193
|
+
self.output_trace = (
|
|
194
|
+
self.output_trace * self.decay
|
|
195
|
+
+ output_events.astype(jnp.float32)
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
if learning:
|
|
199
|
+
new_w1 = brainevent.update_csr_on_binary_pre(
|
|
200
|
+
weight=self.w1.data,
|
|
201
|
+
indices=self.w1.indices,
|
|
202
|
+
indptr=self.w1.indptr,
|
|
203
|
+
pre_spike=input_events,
|
|
204
|
+
post_trace=self.hidden_trace * self.learning_rate,
|
|
205
|
+
w_min=0.0,
|
|
206
|
+
w_max=1.0,
|
|
207
|
+
shape=self.w1.shape,
|
|
208
|
+
)
|
|
209
|
+
self.w1 = brainevent.CSR(
|
|
210
|
+
(new_w1, self.w1.indices, self.w1.indptr),
|
|
211
|
+
shape=self.w1.shape,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
new_w2 = brainevent.update_csr_on_binary_pre(
|
|
215
|
+
weight=self.w2.data,
|
|
216
|
+
indices=self.w2.indices,
|
|
217
|
+
indptr=self.w2.indptr,
|
|
218
|
+
pre_spike=hidden_events,
|
|
219
|
+
post_trace=self.output_trace * self.learning_rate,
|
|
220
|
+
w_min=0.0,
|
|
221
|
+
w_max=1.0,
|
|
222
|
+
shape=self.w2.shape,
|
|
223
|
+
)
|
|
224
|
+
self.w2 = brainevent.CSR(
|
|
225
|
+
(new_w2, self.w2.indices, self.w2.indptr),
|
|
226
|
+
shape=self.w2.shape,
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
return output_input, hidden_spikes, output_spikes
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
network = AdaptiveSpikingNetwork(
|
|
233
|
+
n_input=200,
|
|
234
|
+
n_hidden=100,
|
|
235
|
+
n_output=10,
|
|
236
|
+
connection_probability=0.12,
|
|
237
|
+
seed=2024,
|
|
238
|
+
)
|
|
239
|
+
initial_w1 = network.w1.data.mean()
|
|
240
|
+
initial_w2 = network.w2.data.mean()
|
|
241
|
+
activity = []
|
|
242
|
+
|
|
243
|
+
brainstate.random.seed(0)
|
|
244
|
+
for _ in range(50):
|
|
245
|
+
epoch_output_count = 0
|
|
246
|
+
for _ in range(20):
|
|
247
|
+
input_spikes = brainevent.BinaryArray(
|
|
248
|
+
brainstate.random.bernoulli(0.1, size=(200,))
|
|
249
|
+
)
|
|
250
|
+
output, _, output_spikes = network.forward(
|
|
251
|
+
input_spikes,
|
|
252
|
+
learning=True,
|
|
253
|
+
)
|
|
254
|
+
epoch_output_count += int(jnp.sum(output_spikes.value))
|
|
255
|
+
activity.append(epoch_output_count / 20)
|
|
256
|
+
|
|
257
|
+
assert output.shape == (10,)
|
|
258
|
+
assert network.w1.data.mean() >= initial_w1
|
|
259
|
+
assert network.w2.data.mean() >= initial_w2
|
|
260
|
+
print("W1 mean:", initial_w1, "->", network.w1.data.mean())
|
|
261
|
+
print("W2 mean:", initial_w2, "->", network.w2.data.mean())
|
|
262
|
+
print("final output activity:", activity[-1])
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
This adapts “Practice: Building a Self-Learning Neural Network,” replaces manual CSR row assembly with `coo2csr()`, and removes visualization-only history. It demonstrates the tutorial’s potentiation path; add the post-triggered operator when the learning rule also requires depression.
|
|
266
|
+
|
|
267
|
+
## Storage boundary
|
|
268
|
+
|
|
269
|
+
- Prefer CSR plasticity for large sparse networks with fixed connectivity.
|
|
270
|
+
- Prefer dense plasticity for small fully connected layers already represented densely.
|
|
271
|
+
- Do not choose JITC when individual connection weights must be persistently updated; use stored CSR or dense weights.
|
|
272
|
+
|
|
273
|
+
## Exact API routing
|
|
274
|
+
|
|
275
|
+
Open the Matrix Operations API for exact signatures and associated primitives:
|
|
276
|
+
|
|
277
|
+
- CSR: `update_csr_on_binary_pre`, `update_csr_on_binary_post`, and their `*_p` primitives.
|
|
278
|
+
- Dense: `update_dense_on_binary_pre`, `update_dense_on_binary_post`, and their `*_p` primitives.
|
|
279
|
+
|
|
280
|
+
API: https://brainx.chaobrain.com/brainevent/reference/apis/operations.html
|
|
281
|
+
|
|
282
|
+
## Sources
|
|
283
|
+
|
|
284
|
+
- Tutorial 5: Synaptic Plasticity Modeling - Foundation of Learning and Memory: https://brainx.chaobrain.com/brainevent/tutorials/data-structures/05_synaptic_plasticity.html
|
|
285
|
+
- Apply event-driven synaptic plasticity: https://brainx.chaobrain.com/brainevent/how-to/data-structures/synaptic-plasticity.html
|