Layers¶
Pre-built layer compositions combining neurons, synapses, encoders, and recorders into reusable building blocks.
| Class | Architecture | Backend |
|---|---|---|
SCDenseLayer |
Fully-connected LIF | NumPy (loop-based) |
VectorizedSCLayer |
Fully-connected, packed bitwise | NumPy / CuPy GPU |
SCConv2DLayer |
2D convolution | NumPy |
SCRecurrentLayer |
Echo state / reservoir | NumPy |
SCLearningLayer |
Dense + online STDP | NumPy |
SCFusionLayer |
Multi-modal MUX fusion | NumPy |
StochasticAttention |
SC attention mechanism | NumPy |
MemristiveDenseLayer |
Memristive device model | NumPy |
JaxSCDenseLayer |
Fully-connected LIF | JAX (JIT, GPU/TPU) |
HardwareAwareSCLayer |
Dense + memristive defects | NumPy |
PredictiveCodingSCLayer |
XOR error, zero-multiplication | NumPy |
RallDendrite |
Compartmental dendritic tree | NumPy |
LateralInhibition |
Gaussian surround suppression | NumPy |
WinnerTakeAll |
k-WTA competitive layer | NumPy |
Dense Layer¶
sc_neurocore.layers.sc_dense_layer.SCDenseLayer
dataclass
¶
Stochastic-computing dense layer of LIF neurons.
Each neuron receives shared SC dot-product input current and produces independent spike trains. Software-only but fully SC-driven at the input/synapse level.
Example¶
layer = SCDenseLayer( ... n_neurons=4, x_inputs=[0.5, 0.3], weight_values=[0.8, 0.6], ... x_min=0.0, x_max=1.0, w_min=0.0, w_max=1.0, length=256, ... ) layer.run(T=100) trains = layer.get_spike_trains() trains.shape (4, 100)
Source code in src/sc_neurocore/layers/sc_dense_layer.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
run(T)
¶
Run the layer for T time steps, updating all neurons.
The current I_t is shared across all neurons (common input processed through SC dot-product). Neurons differ by their internal noise and parameters.
Source code in src/sc_neurocore/layers/sc_dense_layer.py
117 118 119 120 121 122 123 124 125 126 127 128 129 | |
get_spike_trains()
¶
Return spike matrix of shape (n_neurons, T).
Source code in src/sc_neurocore/layers/sc_dense_layer.py
131 132 133 134 135 136 137 138 139 140 141 142 | |
summary()
¶
Return firing statistics for each neuron.
Source code in src/sc_neurocore/layers/sc_dense_layer.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
Vectorized Layer¶
sc_neurocore.layers.vectorized_layer.VectorizedSCLayer
dataclass
¶
High-performance SC layer using packed bitwise operations.
Uses GPU (CuPy) when available, otherwise pure NumPy.
Optional sparse connectivity via scipy.sparse.
Example¶
import numpy as np layer = VectorizedSCLayer(n_inputs=8, n_neurons=4, length=512) out = layer.forward(np.random.rand(8)) out.shape (4,) (out >= 0).all() and (out <= 1).all() True
Source code in src/sc_neurocore/layers/vectorized_layer.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
forward(input_values)
¶
Compute output firing rates for the layer.
Source code in src/sc_neurocore/layers/vectorized_layer.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
Convolutional Layer¶
sc_neurocore.layers.sc_conv_layer.SCConv2DLayer
dataclass
¶
SC 2D convolutional layer using unipolar probability multiplication.
Example¶
import numpy as np conv = SCConv2DLayer(in_channels=1, out_channels=2, kernel_size=3, padding=1) img = np.random.rand(1, 8, 8) out = conv.forward(img) out.shape (2, 8, 8)
Source code in src/sc_neurocore/layers/sc_conv_layer.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
forward(input_image)
¶
input_image: (in_channels, H, W) Returns: (out_channels, H_out, W_out) as probabilities (or firing rates).
Source code in src/sc_neurocore/layers/sc_conv_layer.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
Recurrent / Reservoir Layer¶
sc_neurocore.layers.recurrent.SCRecurrentLayer
dataclass
¶
SC recurrent / reservoir layer (echo state network).
Spectral radius bound follows Jaeger, GMD Report 148, 2001.
Example¶
import numpy as np res = SCRecurrentLayer(n_inputs=3, n_neurons=10, seed=0) state = res.step(np.array([0.5, 0.3, 0.8])) state.shape (10,)
Source code in src/sc_neurocore/layers/recurrent.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
step(input_vector)
¶
Process one time step (e.g., one frame of audio). Input: (n_inputs,) Output: (n_neurons,) - New State
Source code in src/sc_neurocore/layers/recurrent.py
70 71 72 73 74 75 76 77 78 79 80 | |
Learning Layer¶
sc_neurocore.layers.sc_learning_layer.SCLearningLayer
dataclass
¶
SC dense layer with integrated STDP learning.
Each neuron has per-input STDP synapses. Plasticity follows Bi & Poo 1998 asymmetry convention.
Source code in src/sc_neurocore/layers/sc_learning_layer.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
run_epoch(input_values)
¶
Run one bitstream epoch (length 'length').
Source code in src/sc_neurocore/layers/sc_learning_layer.py
77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
Fusion Layer¶
sc_neurocore.layers.fusion.SCFusionLayer
dataclass
¶
Fuses multiple data modalities using stochastic multiplexing (MUX).
Example¶
import numpy as np layer = SCFusionLayer( ... input_dims={"audio": 4, "visual": 4}, ... fusion_weights={"audio": 0.7, "visual": 0.3}, ... ) out = layer.forward({"audio": np.ones(4), "visual": np.zeros(4)}) out.shape (4,)
Source code in src/sc_neurocore/layers/fusion.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
forward(inputs)
¶
inputs: {'modality': np.array([values])}
Source code in src/sc_neurocore/layers/fusion.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
Attention Layer¶
sc_neurocore.layers.attention.StochasticAttention
dataclass
¶
Stochastic Computing Attention Block.
Two modes:
forward()— row-sum normalised (SC-native, no exp). Matches Rust engineforward().forward_softmax()— proper softmax with temperature scaling.
Example¶
Q = np.random.default_rng(0).uniform(0, 1, (4, 8)) K = np.random.default_rng(1).uniform(0, 1, (6, 8)) V = np.random.default_rng(2).uniform(0, 1, (6, 5)) attn = StochasticAttention(dim_k=8) attn.forward(Q, K, V).shape (4, 5) attn.forward_softmax(Q, K, V).shape (4, 5)
Source code in src/sc_neurocore/layers/attention.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
forward(Q, K, V)
¶
Row-sum normalised attention (SC-native, no exp).
Parameters¶
Q : (N, dim_k) K : (M, dim_k) V : (M, dim_v)
Returns¶
(N, dim_v)
Source code in src/sc_neurocore/layers/attention.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
forward_softmax(Q, K, V)
¶
Proper softmax attention with temperature scaling.
softmax(Q @ K^T / temperature) @ V
Numerically stable via max-subtraction before exp.
Parameters¶
Q : (N, dim_k) K : (M, dim_k) V : (M, dim_v)
Returns¶
(N, dim_v)
Source code in src/sc_neurocore/layers/attention.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
forward_bitstream(Q, K, V, length=1024, use_sobol=False)
¶
SC-native attention via bitstream AND gates.
Each element is encoded as a bitstream, inner products computed via AND (bit-level multiply), results decoded by popcount.
When use_sobol=True, Sobol low-discrepancy sequences replace Bernoulli random streams, reducing variance from O(1/√L) to O(1/L).
Parameters¶
Q : (N, dim_k) — query probabilities in [0, 1] K : (M, dim_k) — key probabilities in [0, 1] V : (M, dim_v) — value probabilities in [0, 1] length : int — bitstream length use_sobol : bool — use Sobol sequences for variance reduction
Returns¶
(N, dim_v) — attention output probabilities
Source code in src/sc_neurocore/layers/attention.py
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 | |
Memristive Layer¶
sc_neurocore.layers.memristive.MemristiveDenseLayer
dataclass
¶
Bases: VectorizedSCLayer
Dense layer mapped to a memristor crossbar with hardware non-idealities.
Defect parameters from Prezioso et al., Nature 521:61-64, 2015.
Source code in src/sc_neurocore/layers/memristive.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
apply_hardware_defects()
¶
Corrupt weights based on physical properties.
Source code in src/sc_neurocore/layers/memristive.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
JAX Dense Layer¶
sc_neurocore.layers.jax_dense_layer.JaxSCDenseLayer
dataclass
¶
JAX-accelerated stochastic dense layer of LIF neurons.
Example¶
layer = JaxSCDenseLayer(n_neurons=10, n_inputs=5, seed=0) # doctest: +SKIP import jax.numpy as jnp # doctest: +SKIP spikes = layer.step(jnp.ones(10) * 0.5) # doctest: +SKIP spikes.shape # doctest: +SKIP (10,)
Source code in src/sc_neurocore/layers/jax_dense_layer.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
step(I_t)
¶
Advance the entire layer by one time step.
I_t: (n_neurons,) input current for each neuron. Returns: spikes: (n_neurons,) uint8 array.
Source code in src/sc_neurocore/layers/jax_dense_layer.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
run(currents)
¶
Run for multiple steps.
currents: (T, n_neurons) Returns: spikes: (T, n_neurons)
Source code in src/sc_neurocore/layers/jax_dense_layer.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
Hardware-Aware SC Layer¶
Trains around memristive defects (stuck-at faults) by masking gradients on defective synapses.
sc_neurocore.layers.hardware_aware.HardwareAwareSCLayer
dataclass
¶
SC layer with memristive hardware defect injection.
Parameters¶
n_inputs : int Number of input channels. n_neurons : int Number of output neurons. length : int Bitstream length. stuck_rate : float Fraction of synapses with stuck-at faults (0 or 1). Default 0.05. variability : float Additive weight noise std. Default 0.02. seed : int Random seed for defect generation.
Source code in src/sc_neurocore/layers/hardware_aware.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
update_weights(gradient, lr=0.01)
¶
Update weights with gradient, respecting stuck-at mask.
Stuck synapses receive zero gradient — the network learns around the defects.
Source code in src/sc_neurocore/layers/hardware_aware.py
85 86 87 88 89 90 91 92 93 94 95 | |
Predictive Coding SC Layer (Conjecture C9)¶
Zero-multiplication predictive coding: XOR = error, popcount = magnitude, STDP = precision. First SC implementation of Bayesian prediction error minimization.
sc_neurocore.layers.predictive_coding.PredictiveCodingSCLayer
dataclass
¶
Zero-multiplication predictive coding in SC.
Parameters¶
n_inputs : int Number of input channels. n_neurons : int Number of predictive neurons. length : int Bitstream length. lr : float STDP-like learning rate for prediction weights. seed : int or None Random seed.
Source code in src/sc_neurocore/layers/predictive_coding.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
forward(inputs)
¶
Process one timestep.
Parameters¶
inputs : array-like Input probabilities, shape (n_inputs,).
Returns¶
dict with keys: 'prediction_error': float — mean Hamming distance across neurons 'surprises': ndarray shape (n_neurons,) — per-neuron surprise 'predictions': ndarray shape (n_neurons, n_inputs) — predicted probs
Source code in src/sc_neurocore/layers/predictive_coding.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 | |
Rall Branching Dendrite¶
Compartmental dendritic tree with Rall's 3/2 power rule for impedance matching. Distal-to-proximal propagation with inter-compartment coupling.
sc_neurocore.layers.rall_dendrite.RallDendrite
dataclass
¶
Dendritic tree with Rall branching and compartmental dynamics.
Parameters¶
n_branches : int Number of dendritic branches. branch_length : int Number of compartments per branch. tau : float Membrane time constant (ms). coupling : float Inter-compartment coupling strength (0 to 1). dt : float Timestep (ms).
Source code in src/sc_neurocore/layers/rall_dendrite.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
branch_voltages
property
¶
Current compartment voltages, shape (n_branches, branch_length).
step(branch_inputs)
¶
Advance one timestep.
Parameters¶
branch_inputs : np.ndarray Shape (n_branches,) — synaptic current injected at distal tip of each branch.
Returns¶
float Somatic voltage.
Source code in src/sc_neurocore/layers/rall_dendrite.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
Lateral Inhibition¶
sc_neurocore.layers.circuit_primitives.LateralInhibition
dataclass
¶
Lateral inhibition: each neuron inhibits its neighbors.
Models the surround suppression found in retinal ganglion cells, cortical simple cells, and throughout sensory processing.
The inhibition kernel is a Gaussian centered on each neuron with
width radius, producing a Mexican-hat (center-surround) response
when combined with the neuron's own excitation.
Source code in src/sc_neurocore/layers/circuit_primitives.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
apply(rates)
¶
Apply lateral inhibition to firing rates.
Parameters¶
rates : np.ndarray, shape (n_neurons,) Input firing rates or probabilities.
Returns¶
np.ndarray, shape (n_neurons,) Inhibited firing rates, clipped to [0, inf).
Source code in src/sc_neurocore/layers/circuit_primitives.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Winner-Take-All¶
sc_neurocore.layers.circuit_primitives.WinnerTakeAll
dataclass
¶
k-Winner-Take-All circuit.
Only the top-k neurons remain active; all others are suppressed to zero. Models competitive dynamics in cortical columns and basal ganglia action selection.
With k=1, this is a hard argmax over the population.
Source code in src/sc_neurocore/layers/circuit_primitives.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
apply(rates)
¶
Apply k-WTA to firing rates.
Parameters¶
rates : np.ndarray, shape (n_neurons,) Input firing rates.
Returns¶
np.ndarray, shape (n_neurons,) Only top-k values survive; rest are zero.
Source code in src/sc_neurocore/layers/circuit_primitives.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
winners(rates)
¶
Return indices of the k winning neurons.
Source code in src/sc_neurocore/layers/circuit_primitives.py
107 108 109 | |