|
| 1 | +API Reference |
| 2 | +============= |
| 3 | + |
| 4 | +Complete API reference for BrainPy 3.0. |
| 5 | + |
| 6 | +.. note:: |
| 7 | + BrainPy 3.0 is built on top of `brainstate <https://brainstate.readthedocs.io/>`_, |
| 8 | + `brainunit <https://brainunit.readthedocs.io/>`_, and `braintools <https://braintools.readthedocs.io/>`_. |
| 9 | + |
| 10 | +Organization |
| 11 | +------------ |
| 12 | + |
| 13 | +The API is organized into the following categories: |
| 14 | + |
| 15 | +.. grid:: 1 2 2 2 |
| 16 | + |
| 17 | + .. grid-item-card:: :material-regular:`psychology;2em` Neurons |
| 18 | + :link: neurons.html |
| 19 | + |
| 20 | + Spiking neuron models (LIF, ALIF, Izhikevich, etc.) |
| 21 | + |
| 22 | + .. grid-item-card:: :material-regular:`timeline;2em` Synapses |
| 23 | + :link: synapses.html |
| 24 | + |
| 25 | + Synaptic dynamics (Expon, Alpha, AMPA, GABA, NMDA) |
| 26 | + |
| 27 | + .. grid-item-card:: :material-regular:`account_tree;2em` Projections |
| 28 | + :link: projections.html |
| 29 | + |
| 30 | + Connect neural populations (AlignPostProj, AlignPreProj) |
| 31 | + |
| 32 | + .. grid-item-card:: :material-regular:`hub;2em` Networks |
| 33 | + :link: networks.html |
| 34 | + |
| 35 | + Network building blocks and utilities |
| 36 | + |
| 37 | + .. grid-item-card:: :material-regular:`school;2em` Training |
| 38 | + :link: training.html |
| 39 | + |
| 40 | + Gradient-based learning utilities |
| 41 | + |
| 42 | + .. grid-item-card:: :material-regular:`input;2em` Input/Output |
| 43 | + :link: input-output.html |
| 44 | + |
| 45 | + Input generation and output processing |
| 46 | + |
| 47 | +Quick Reference |
| 48 | +--------------- |
| 49 | + |
| 50 | +**Most commonly used classes:** |
| 51 | + |
| 52 | +Neurons |
| 53 | +~~~~~~~ |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + import brainpy as bp |
| 58 | +
|
| 59 | + # Leaky Integrate-and-Fire |
| 60 | + bp.LIF(size, V_rest, V_th, V_reset, tau, R, ...) |
| 61 | +
|
| 62 | + # Adaptive LIF |
| 63 | + bp.ALIF(size, V_rest, V_th, V_reset, tau, tau_w, a, b, ...) |
| 64 | +
|
| 65 | + # Izhikevich |
| 66 | + bp.Izhikevich(size, a, b, c, d, ...) |
| 67 | +
|
| 68 | +Synapses |
| 69 | +~~~~~~~~ |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + # Exponential |
| 74 | + bp.Expon.desc(size, tau) |
| 75 | +
|
| 76 | + # Alpha |
| 77 | + bp.Alpha.desc(size, tau) |
| 78 | +
|
| 79 | + # AMPA receptor |
| 80 | + bp.AMPA.desc(size, tau) |
| 81 | +
|
| 82 | + # GABA_a receptor |
| 83 | + bp.GABAa.desc(size, tau) |
| 84 | +
|
| 85 | +Projections |
| 86 | +~~~~~~~~~~~ |
| 87 | + |
| 88 | +.. code-block:: python |
| 89 | +
|
| 90 | + # Standard projection |
| 91 | + bp.AlignPostProj( |
| 92 | + comm=brainstate.nn.EventFixedProb(n_pre, n_post, prob, weight), |
| 93 | + syn=bp.Expon.desc(n_post, tau), |
| 94 | + out=bp.COBA.desc(E), |
| 95 | + post=post_neurons |
| 96 | + ) |
| 97 | +
|
| 98 | +Networks |
| 99 | +~~~~~~~~ |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + # Module base class |
| 104 | + class MyNetwork(brainstate.nn.Module): |
| 105 | + def __init__(self): |
| 106 | + super().__init__() |
| 107 | + # ... define components |
| 108 | +
|
| 109 | + def update(self, x): |
| 110 | + # ... network dynamics |
| 111 | + return output |
| 112 | +
|
| 113 | +Training |
| 114 | +~~~~~~~~ |
| 115 | + |
| 116 | +.. code-block:: python |
| 117 | +
|
| 118 | + import braintools |
| 119 | +
|
| 120 | + # Optimizer |
| 121 | + optimizer = braintools.optim.Adam(lr=1e-3) |
| 122 | +
|
| 123 | + # Gradients |
| 124 | + grads = brainstate.transform.grad(loss_fn, params)(...) |
| 125 | +
|
| 126 | + # Update |
| 127 | + optimizer.update(grads) |
| 128 | +
|
| 129 | +Documentation Sections |
| 130 | +---------------------- |
| 131 | + |
| 132 | +.. toctree:: |
| 133 | + :maxdepth: 2 |
| 134 | + |
| 135 | + neurons |
| 136 | + synapses |
| 137 | + projections |
| 138 | + networks |
| 139 | + training |
| 140 | + input-output |
| 141 | + |
| 142 | +Import Structure |
| 143 | +---------------- |
| 144 | + |
| 145 | +BrainPy uses a clear import hierarchy: |
| 146 | + |
| 147 | +.. code-block:: python |
| 148 | +
|
| 149 | + import brainpy as bp # Core BrainPy |
| 150 | + import brainstate # State management and modules |
| 151 | + import brainunit as u # Physical units |
| 152 | + import braintools # Training utilities |
| 153 | +
|
| 154 | + # Neurons and synapses |
| 155 | + neuron = bp.LIF(100, ...) |
| 156 | + synapse = bp.Expon.desc(100, tau=5*u.ms) |
| 157 | +
|
| 158 | + # State management |
| 159 | + state = brainstate.ShortTermState(...) |
| 160 | + brainstate.nn.init_all_states(net) |
| 161 | +
|
| 162 | + # Units |
| 163 | + current = 2.0 * u.nA |
| 164 | + voltage = -65 * u.mV |
| 165 | + time = 10 * u.ms |
| 166 | +
|
| 167 | + # Training |
| 168 | + optimizer = braintools.optim.Adam(lr=1e-3) |
| 169 | + loss = braintools.metric.softmax_cross_entropy(...) |
| 170 | +
|
| 171 | +Type Conventions |
| 172 | +---------------- |
| 173 | + |
| 174 | +**States:** |
| 175 | + |
| 176 | +- ``ShortTermState`` - Temporary dynamics (V, g, spikes) |
| 177 | +- ``ParamState`` - Learnable parameters (weights, biases) |
| 178 | +- ``LongTermState`` - Persistent statistics |
| 179 | + |
| 180 | +**Units:** |
| 181 | + |
| 182 | +All physical quantities use ``brainunit``: |
| 183 | + |
| 184 | +- Voltage: ``u.mV`` |
| 185 | +- Current: ``u.nA``, ``u.pA`` |
| 186 | +- Time: ``u.ms``, ``u.second`` |
| 187 | +- Conductance: ``u.mS``, ``u.nS`` |
| 188 | +- Concentration: ``u.mM`` |
| 189 | + |
| 190 | +**Shapes:** |
| 191 | + |
| 192 | +- Single trial: ``(n_neurons,)`` |
| 193 | +- Batched: ``(batch_size, n_neurons)`` |
| 194 | +- Connectivity: ``(n_pre, n_post)`` |
| 195 | + |
| 196 | +See Also |
| 197 | +-------- |
| 198 | + |
| 199 | +**External Documentation:** |
| 200 | + |
| 201 | +- `BrainState Documentation <https://brainstate.readthedocs.io/>`_ - State management |
| 202 | +- `BrainUnit Documentation <https://brainunit.readthedocs.io/>`_ - Physical units |
| 203 | +- `BrainTools Documentation <https://braintools.readthedocs.io/>`_ - Training utilities |
| 204 | +- `JAX Documentation <https://jax.readthedocs.io/>`_ - Underlying computation |
0 commit comments