|
| 1 | +import jax |
| 2 | +import jax.numpy as jnp |
| 3 | +import flax.linen as nn |
| 4 | +import optax |
| 5 | +from typing import List, Tuple, Callable, Optional |
| 6 | +import logging |
| 7 | + |
| 8 | +def spiking_neuron(x, membrane_potential, threshold=1.0, reset_potential=0.0, leak_factor=0.9): |
| 9 | + new_membrane_potential = jnp.add(leak_factor * membrane_potential, x) |
| 10 | + spike = jnp.where(new_membrane_potential >= threshold, 1.0, 0.0) |
| 11 | + new_membrane_potential = jnp.where(spike == 1.0, reset_potential, new_membrane_potential) |
| 12 | + return spike, new_membrane_potential |
| 13 | + |
| 14 | +class SpikingNeuralNetwork(nn.Module): |
| 15 | + num_neurons: List[int] |
| 16 | + activation: Callable = nn.relu |
| 17 | + spike_function: Callable = lambda x: jnp.where(x > 0, 1.0, 0.0) |
| 18 | + threshold: float = 1.0 |
| 19 | + reset_potential: float = 0.0 |
| 20 | + leak_factor: float = 0.9 |
| 21 | + |
| 22 | + @nn.compact |
| 23 | + def __call__(self, inputs, membrane_potentials=None): |
| 24 | + logging.debug(f"Input shape: {inputs.shape}") |
| 25 | + x = inputs |
| 26 | + |
| 27 | + # Input validation and reshaping |
| 28 | + if len(inputs.shape) == 1: |
| 29 | + x = jnp.expand_dims(x, axis=0) |
| 30 | + elif len(inputs.shape) > 2: |
| 31 | + x = jnp.reshape(x, (-1, x.shape[-1])) |
| 32 | + |
| 33 | + if x.shape[1] != self.num_neurons[0]: |
| 34 | + raise ValueError(f"Input shape {x.shape} does not match first layer neurons {self.num_neurons[0]}") |
| 35 | + |
| 36 | + if membrane_potentials is None: |
| 37 | + membrane_potentials = [jnp.zeros((x.shape[0], num_neuron)) for num_neuron in self.num_neurons] |
| 38 | + else: |
| 39 | + if len(membrane_potentials) != len(self.num_neurons): |
| 40 | + raise ValueError(f"Expected {len(self.num_neurons)} membrane potentials, got {len(membrane_potentials)}") |
| 41 | + membrane_potentials = [jnp.broadcast_to(mp, (x.shape[0], mp.shape[-1])) for mp in membrane_potentials] |
| 42 | + |
| 43 | + logging.debug(f"Adjusted input shape: {x.shape}") |
| 44 | + logging.debug(f"Adjusted membrane potentials shapes: {[mp.shape for mp in membrane_potentials]}") |
| 45 | + |
| 46 | + new_membrane_potentials = [] |
| 47 | + for i, (num_neuron, membrane_potential) in enumerate(zip(self.num_neurons, membrane_potentials)): |
| 48 | + logging.debug(f"Layer {i} - Input shape: {x.shape}, Membrane potential shape: {membrane_potential.shape}") |
| 49 | + |
| 50 | + spiking_layer = jax.vmap(lambda x, mp: spiking_neuron(x, mp, self.threshold, self.reset_potential, self.leak_factor), |
| 51 | + in_axes=(0, 0), out_axes=0) |
| 52 | + spikes, new_membrane_potential = spiking_layer(x, membrane_potential) |
| 53 | + |
| 54 | + logging.debug(f"Layer {i} - Spikes shape: {spikes.shape}, New membrane potential shape: {new_membrane_potential.shape}") |
| 55 | + |
| 56 | + x = self.activation(spikes) |
| 57 | + new_membrane_potentials.append(new_membrane_potential) |
| 58 | + |
| 59 | + # Adjust x for the next layer |
| 60 | + if i < len(self.num_neurons) - 1: |
| 61 | + x = nn.Dense(self.num_neurons[i+1])(x) |
| 62 | + |
| 63 | + logging.debug(f"Final output shape: {x.shape}") |
| 64 | + return self.spike_function(x), new_membrane_potentials |
| 65 | + |
| 66 | +class NeuromorphicComputing(nn.Module): |
| 67 | + num_neurons: List[int] |
| 68 | + threshold: float = 1.0 |
| 69 | + reset_potential: float = 0.0 |
| 70 | + leak_factor: float = 0.9 |
| 71 | + |
| 72 | + def setup(self): |
| 73 | + self.model = SpikingNeuralNetwork(num_neurons=self.num_neurons, |
| 74 | + threshold=self.threshold, |
| 75 | + reset_potential=self.reset_potential, |
| 76 | + leak_factor=self.leak_factor) |
| 77 | + logging.info(f"Initialized NeuromorphicComputing with {len(self.num_neurons)} layers") |
| 78 | + |
| 79 | + def __call__(self, inputs, membrane_potentials=None): |
| 80 | + return self.model(inputs, membrane_potentials) |
| 81 | + |
| 82 | + def init_model(self, rng, input_shape): |
| 83 | + dummy_input = jnp.zeros(input_shape) |
| 84 | + membrane_potentials = [jnp.zeros(input_shape[:-1] + (n,)) for n in self.num_neurons] |
| 85 | + # Ensure consistent shapes between inputs and membrane potentials |
| 86 | + if dummy_input.shape[1] != membrane_potentials[0].shape[1]: |
| 87 | + dummy_input = jnp.reshape(dummy_input, (-1, membrane_potentials[0].shape[1])) |
| 88 | + return self.init(rng, dummy_input, membrane_potentials) |
| 89 | + |
| 90 | + @jax.jit |
| 91 | + def forward(self, params, inputs, membrane_potentials): |
| 92 | + return self.apply(params, inputs, membrane_potentials) |
| 93 | + |
| 94 | + def train_step(self, params, inputs, targets, membrane_potentials, optimizer): |
| 95 | + def loss_fn(params): |
| 96 | + outputs, new_membrane_potentials = self.forward(params, inputs, membrane_potentials) |
| 97 | + return jnp.mean((outputs - targets) ** 2), new_membrane_potentials |
| 98 | + |
| 99 | + (loss, new_membrane_potentials), grads = jax.value_and_grad(loss_fn, has_aux=True)(params) |
| 100 | + updates, optimizer_state = optimizer.update(grads, optimizer.state) |
| 101 | + params = optax.apply_updates(params, updates) |
| 102 | + optimizer = optimizer.replace(state=optimizer_state) |
| 103 | + return params, loss, new_membrane_potentials, optimizer |
| 104 | + |
| 105 | + @staticmethod |
| 106 | + def handle_error(e: Exception) -> None: |
| 107 | + logging.error(f"Error in NeuromorphicComputing: {str(e)}") |
| 108 | + if isinstance(e, jax.errors.JAXException): |
| 109 | + logging.error("JAX-specific error occurred. Check JAX configuration and input shapes.") |
| 110 | + elif isinstance(e, ValueError): |
| 111 | + logging.error("Value error occurred. Check input data and model parameters.") |
| 112 | + else: |
| 113 | + logging.error("Unexpected error occurred. Please review the stack trace for more information.") |
| 114 | + raise |
| 115 | + |
| 116 | +def create_neuromorphic_model(num_neurons: List[int]) -> NeuromorphicComputing: |
| 117 | + return NeuromorphicComputing(num_neurons=num_neurons) |
0 commit comments