### Install Lava on Linux/MacOS using Poetry Source: https://github.com/lava-nc/lava/blob/main/README.md Installs Lava using Poetry. Clones the repository, checks out a specific version, installs hooks, and sets up the virtual environment. ```bash cd $HOME curl -sSL https://install.python-poetry.org | python3 - git clone git@github.com:lava-nc/lava.git cd lava git checkout v0.9.0 ./utils/githook/install-hook.sh poetry config virtualenvs.in-project true poetry install source .venv/bin/activate pytest ``` -------------------------------- ### Install and Launch Jupyter Notebook Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial01_installing_lava.ipynb Install the Jupyter Notebook package and launch it to access Lava tutorials. Open your browser to the URL provided in the Jupyter log. ```bash pip install jupyter ``` ```bash jupyter notebook ``` -------------------------------- ### Setup input data and weights Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial02-Fixed_point_elementwise_product.ipynb Initializes input data and weights for the network, scaling them appropriately for the simulation. ```python num_steps = 10 weights1 = np.zeros((5,1)) weights2 = np.zeros((5,1)) weights1[:,0] = [2, 6, 10, -2, -6] weights2[:,0] = [4, 8, 12, -4, 8] weights1 /= 16 weights2 /= 16 inp_shape = (weights1.shape[1],) out_shape = (weights1.shape[0],) inp_data = np.zeros((inp_shape[0], num_steps)) inp_data[:, 2] = 16 inp_data[:, 6] = 32 ``` -------------------------------- ### Install Lava on Windows using PowerShell Source: https://github.com/lava-nc/lava/blob/main/README.md Installs Lava on Windows using PowerShell. Clones the repository, checks out a specific version, creates a virtual environment, and installs dependencies. ```powershell # Commands using PowerShell cd $HOME git clone git@github.com:lava-nc/lava.git cd lava git checkout v0.9.0 python3 -m venv .venv .venv\Scripts\activate pip install -U pip curl -sSL https://install.python-poetry.org | python3 - poetry config virtualenvs.in-project true poetry install pytest ``` -------------------------------- ### Install Lava with Pip Source: https://github.com/lava-nc/lava/wiki/Frequently-Asked-Questions-(FAQ) Install the Lava library using pip. Ensure Lava is installed correctly or that Python paths are set up properly if encountering 'ModuleNotFoundError'. ```bash pip install . ``` -------------------------------- ### Configure and connect a simple LIF-Dense-LIF network Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial11_serialization.ipynb Sets up a basic neural network with two LIF neurons and a Dense layer. This network serves as an example for serialization. ```python pre_size = 2 post_size = 3 # Create network weights = np.ones((post_size, pre_size)) lif_in = LIF(shape=(pre_size,), bias_mant=100, vth=120, name="LIF_neuron input") dense = Dense(weights=weights * 10, name="Dense") lif_out = LIF(shape=(post_size,), bias_mant=0, vth=50000, name="LIF_neuron output") # Connect the processes lif_in.s_out.connect(dense.s_in) dense.a_out.connect(lif_out.a_in) ``` -------------------------------- ### Install Lava using Conda with Intel optimized libraries Source: https://github.com/lava-nc/lava/blob/main/README.md Creates a Conda environment with Python 3.9, activates it, and installs Intel-optimized NumPy and SciPy, followed by Lava. ```bash conda create -n lava python=3.9 -c intel conda activate lava conda install -n lava -c intel numpy scipy conda install -n lava -c conda-forge lava --freeze-installed ``` -------------------------------- ### Install Lava from PyPI Source: https://github.com/lava-nc/lava/blob/main/README.md Installs Lava from PyPI using pip. This method does not grant access to run tests. ```bash python -m venv .venv source .venv/bin/activate ## Or Windows: .venv\Scripts\activate pip install -U pip pip install lava-nc ``` -------------------------------- ### Pytest Output Example Source: https://github.com/lava-nc/lava/blob/main/README.md Example output from running pytest, showing test collection, progress, warnings, and coverage results. ```text $ pytest ============================================== test session starts ============================================== platform linux -- Python 3.8.10, pytest-7.0.1, pluggy-1.0.0 rootdir: /home/user/lava, configfile: pyproject.toml, testpaths: tests plugins: cov-3.0.0 collected 205 items tests/lava/magma/compiler/test_channel_builder.py . [ 0%] tests/lava/magma/compiler/test_compiler.py ........................ [ 12%] tests/lava/magma/compiler/test_node.py .. [ 13%] tests/lava/magma/compiler/builder/test_channel_builder.py . [ 13%] ...... pytest output ... tests/lava/proc/sdn/test_models.py ........ [ 98%] tests/lava/proc/sdn/test_process.py ... [100%] =============================================== warnings summary ================================================ ...... pytest output ... src/lava/proc/lif/process.py 38 0 100% src/lava/proc/monitor/models.py 27 0 100% src/lava/proc/monitor/process.py 79 0 100% src/lava/proc/sdn/models.py 159 9 94% 199-202, 225-231 src/lava/proc/sdn/process.py 59 0 100% ----------------------------------------------------------------------------------------------------------------- TOTAL 4048 453 89% Required test coverage of 85.0% reached. Total coverage: 88.81% ============================ 199 passed, 6 skipped, 2 warnings in 118.17s (0:01:58) ============================= ``` -------------------------------- ### Prepare Input Data for Second Network Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial01-Fixed_point_dot_product.ipynb Generates input data arrays for the second set of vectors and weights, similar to the first example, to be used in the addition operator overload demonstration. ```python num_steps=16 inp_data = np.zeros((vec.shape[0], num_steps)) inp_data[:, 1] = vec.ravel() inp_data[:, 3] = 4*vec.ravel() inp_data[:, 5] = 16*vec.ravel() inp_data[:, 7] = 64*vec.ravel() inp_data[:, 9] = 256*vec.ravel() inp_data2 = np.zeros((vec2.shape[0], num_steps)) inp_data2[:, 1] = vec2.ravel() inp_data2[:, 3] = 4*vec2.ravel() inp_data2[:, 5] = 16*vec2.ravel() inp_data2[:, 7] = 64*vec2.ravel() inp_data2[:, 9] = 256*vec2.ravel() ``` -------------------------------- ### Install Lava from GitHub Releases Source: https://github.com/lava-nc/lava/blob/main/README.md Installs Lava from a published release tarball. This method does not grant access to run tests. Replace the version number as needed. ```bash python -m venv .venv source .venv/bin/activate ## Or Windows: .venv\Scripts\activate pip install -U pip # Substitute lava version needed for lava-nc-.tar.gz below pip install lava-nc-0.9.0.tar.gz ``` -------------------------------- ### Define Second Input Vector and Weights for Addition Example Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial01-Fixed_point_dot_product.ipynb Initializes a second set of input vectors and weights for demonstrating the addition operator overload. This involves creating new NumPy arrays and scaling them. ```python # Defining two input streams vec = np.array([40, 30, 20, 10]) weights = np.zeros((3,4)) weights[:, 0] = [8, 9, -7] weights[:, 1] = [9, 8, -5] weights[:, 2] = [8, -10, -4] weights[:, 3] = [8, -10, -3] vec2 = np.array([50, -50, 20, -20]) weights2 = np.zeros((3,4)) weights2[:, 0] = [3, -5, 4] weights2[:, 1] = [0, -2, -10] weights2[:, 2] = [6, 8, -4] weights2[:, 3] = [-5, 7, -7] weights /= 10 weights2 /= 10 weight_exp = 7 ``` -------------------------------- ### Setup GradedVec for overloaded multiplication Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial02-Fixed_point_elementwise_product.ipynb Initializes GradedVec objects to demonstrate the overloaded multiplication operator. ```python dense1 = lv.GradedDense(weights=weights1) dense2 = lv.GradedDense(weights=weights2) vec1 = lv.GradedVec(shape=out_shape, vth=1, exp=0) vec2 = lv.GradedVec(shape=out_shape, vth=1, exp=0) outvec = lv.GradedVec(shape=out_shape, vth=1, exp=0) generator1 = lv.InputVec(inp_data, loihi2=use_loihi2) generator2 = lv.InputVec(inp_data, loihi2=use_loihi2) monitor = lv.OutputVec(shape=out_shape, buffer=num_steps, loihi2=True) ``` -------------------------------- ### Instantiate and Connect DenseLayers Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial06_hierarchical_processes.ipynb Initializes two DenseLayers with specified weights and biases, then connects the output of the first layer to the input of the second. This setup is useful for building multi-layered neural network architectures. ```python weights0 = np.zeros(shape=dim) weights0[1,1]=1 weights1 = weights0 # Instantiate two DenseLayers. layer0 = DenseLayer(shape=dim, weights=weights0, bias_mant=4, vth=10) layer1 = DenseLayer(shape=dim, weights=weights1, bias_mant=4, vth=10) # Connect the first DenseLayer to the second DenseLayer. layer0.s_out.connect(layer1.s_in) ``` -------------------------------- ### Configure and Run Simulation Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/three_factor_learning/tutorial01_Reward_Modulated_STDP.ipynb Sets up the simulation configuration using Loihi2SimCfg and runs the network for a specified number of steps. Ensure SELECT_TAG is defined. ```python from lava.magma.core.run_conditions import RunSteps from lava.magma.core.run_configs import Loihi2SimCfg # Running pattern_pre.run(condition=RunSteps(num_steps=num_steps), run_cfg=Loihi2SimCfg(select_tag=SELECT_TAG)) ``` -------------------------------- ### Install Lava using Conda Source: https://github.com/lava-nc/lava/blob/main/README.md Installs Lava using the Conda package manager from the conda-forge channel. ```bash conda install lava -c conda-forge ``` -------------------------------- ### Configure Loihi 2 or CPU backend Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial02-Fixed_point_elementwise_product.ipynb Sets up the runtime configuration, choosing between Loihi 2 hardware or a CPU backend simulation based on availability. ```python loihi.use_slurm_host(loihi_gen=loihi.ChipGeneration.N3B3) use_loihi2 = loihi.is_installed() if use_loihi2: run_cfg = lv.Loihi2HwCfg() print("Running on Loihi 2") else: run_cfg = lv.Loihi2SimCfg(select_tag='fixed_pt') print("Loihi2 compiler is not available in this system. " "This tutorial will execute on CPU backend.") ``` -------------------------------- ### Implement Process Models for CPU Execution Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial07_remote_memory_access.ipynb Implement PyProcModel1 and PyProcModel2 for P1 and P2, respectively. These models run on the CPU and utilize the Loihi Sync Protocol. They demonstrate reading P2's 'var' and writing to it by adding the current time step. ```python import numpy as np from lava.magma.core.sync.protocols.loihi_protocol import LoihiProtocol from lava.magma.core.model.py.ports import PyRefPort from lava.magma.core.model.py.type import LavaPyType from lava.magma.core.resources import CPU from lava.magma.core.decorator import implements, requires from lava.magma.core.model.py.model import PyLoihiProcessModel ``` -------------------------------- ### Set up Monitors for Data Recording Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial00_tour_through_lava.ipynb Instantiate Monitor objects and use the .probe() method to specify which variables (e.g., membrane potential 'v') from which processes should be recorded during execution. ```python # Create Monitors to record membrane potentials monitor_lif1 = Monitor() monitor_lif2 = Monitor() # Probe membrane potentials from the two LIF populations monitor_lif1.probe(lif1.v, num_steps) monitor_lif2.probe(lif2.v, num_steps) ``` -------------------------------- ### Bypass Keyring for Poetry Installs Source: https://github.com/lava-nc/lava/wiki/Frequently-Asked-Questions-(FAQ) Temporarily disable or bypass the Python keyring backend to resolve 'DBusErrorResponse' or 'ItemNotFoundException' during poetry installations. ```bash PYTHON_KEYRING_BACKEND="keyring.backends.null.Keyring" poetry ``` ```bash export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring ``` ```bash keyring --disable ``` ```bash pip uninstall keyring ``` -------------------------------- ### Configure and Initialize Network Parameters Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial02_excitatory_inhibitory_network.ipynb Sets up parameters for a neural network simulation, including adjusting the 'q_factor' to control recurrent connection strengths and generating Gaussian weights. This configuration is used for simulating a network at a critical state. ```python # Defining new, larger q_factor. q_factor = np.sqrt(dim / 6) # Changing the strenghts of the recurrent connections. network_params_critical = network_params_balanced.copy() network_params_critical['q_factor'] = q_factor network_params_critical['weights'] = generate_gaussian_weights(dim, num_neurons_exc, network_params_critical['q_factor'], network_params_critical['g_factor']) # Configurations for execution. num_steps = 1000 rcfg = Loihi1SimCfg(select_tag='rate_neurons') run_cond = RunSteps(num_steps=num_steps) # Instantiating network and IO processes. network_critical = EINetwork(**network_params_critical) state_monitor = Monitor() state_monitor.probe(target=network_critical.state, num_steps=num_steps) ``` -------------------------------- ### Create Monitors for Dynamics Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/three_factor_learning/tutorial01_Reward_Modulated_STDP.ipynb Initializes and connects Monitor processes to observe weight and trace dynamics during learning. Use this to probe specific process variables over simulation steps. ```python from lava.proc.monitor.process import Monitor # Create monitors mon_pre_trace = Monitor() mon_post_trace = Monitor() mon_reward_trace = Monitor() mon_pre_spikes = Monitor() mon_post_spikes = Monitor() mon_weight = Monitor() mon_tag = Monitor() mon_s_in_y2 = Monitor() mon_y2 = Monitor() # Connect monitors mon_pre_trace.probe(plast_conn.x1, num_steps) mon_post_trace.probe(plast_conn.y1, num_steps) mon_reward_trace.probe(lif_post.s_out_y2, num_steps) mon_pre_spikes.probe(lif_pre.s_out, num_steps) mon_post_spikes.probe(lif_post.s_out, num_steps) mon_weight.probe(plast_conn.weights, num_steps) mon_tag.probe(plast_conn.tag_1, num_steps) ``` -------------------------------- ### Configure Simulation Parameters Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial09_custom_learning_rules.ipynb Sets up simulation parameters, including the simulation tag (fixed-point or floating-point), neuron model parameters (LIF), and network connection weights. This prepares the environment for running the simulation. ```python import numpy as np # Set this tag to "fixed_pt" or "floating_pt" to choose the corresponding models. SELECT_TAG = "fixed_pt" # LIF parameters if SELECT_TAG == "fixed_pt": du = 4095 dv = 4095 elif SELECT_TAG == "floating_pt": du = 1 dv = 1 vth = 240 # Number of neurons per layer num_neurons = 1 shape_lif = (num_neurons, ) shape_conn = (num_neurons, num_neurons) # Connection parameters # SpikePattern -> LIF connection weight wgt_inp = np.eye(num_neurons) * 250 # LIF -> LIF connection initial weight (learning-enabled) wgt_plast_conn = np.full(shape_conn, 50) # Number of simulation time steps num_steps = 200 time = list(range(1, num_steps + 1)) # Spike times spike_prob = 0.03 ``` -------------------------------- ### Instantiate Network Components Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial09_custom_learning_rules.ipynb Creates input devices, static connections, LIF neurons (pre- and post-synaptic), a plastic connection with a learning rule, and connects them to form the network. ```python # Create input devices pattern_pre = RingBuffer(data=spike_raster_pre.astype(int)) pattern_post = RingBuffer(data=spike_raster_post.astype(int)) # Create input connectivity conn_inp_pre = Dense(weights=wgt_inp) conn_inp_post = Dense(weights=wgt_inp) # Create pre-synaptic neurons lif_pre = LIF(u=0, v=0, du=du, dv=du, bias_mant=0, bias_exp=0, vth=vth, shape=shape_lif, name='lif_pre') # Create plastic connection plast_conn = LearningDense(weights=wgt_plast_conn, learning_rule=stdp, name='plastic_dense') # Create post-synaptic neuron lif_post = LIF(u=0, v=0, du=du, dv=du, bias_mant=0, bias_exp=0, vth=vth, shape=shape_lif, name='lif_post') # Connect network pattern_pre.s_out.connect(conn_inp_pre.s_in) conn_inp_pre.a_out.connect(lif_pre.a_in) pattern_post.s_out.connect(conn_inp_post.s_in) conn_inp_post.a_out.connect(lif_post.a_in) lif_pre.s_out.connect(plast_conn.s_in) plast_conn.a_out.connect(lif_post.a_in) # Connect back-propagating actionpotential (BAP) lif_post.s_out.connect(plast_conn.s_in_bap) ``` -------------------------------- ### Instantiate Network Components Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial04-Creating_network_motifs.ipynb Creates instances of InputVec, GradedDense, the custom MemoryBuffer motif, and OutputVec for monitoring the simulation. ```python invec = lv.InputVec(inp_data, loihi2=use_loihi2) in_out_syn = lv.GradedDense(weights=in_weights) memvec = MemoryBuffer(shape=mem_buffer_size) out_monitor = lv.OutputVec(shape=mem_buffer_size, buffer=num_steps, loihi2=use_loihi2) ``` -------------------------------- ### Retrieve Recorded Data from Monitors Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial00_tour_through_lava.ipynb Get the recorded data from monitor objects using the get_data() method after the simulation has stopped. ```python data_lif1 = monitor_lif1.get_data() data_lif2 = monitor_lif2.get_data() ``` -------------------------------- ### Import necessary libraries Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial02-Fixed_point_elementwise_product.ipynb Imports required libraries for the tutorial, including pylab and Lava-VA components. ```python from pylab import * ``` ```python import lava.frameworks.loihi2 as lv ``` ```python from lava.utils import loihi ``` -------------------------------- ### Configure Lava Execution Backend Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial01-Fixed_point_dot_product.ipynb Sets up the execution configuration for Lava, choosing between Loihi 2 hardware or a CPU backend simulation based on availability. ```python from lava.utils import loihi loihi.use_slurm_host(loihi_gen=loihi.ChipGeneration.N3B3) use_loihi2 = loihi.is_installed() if use_loihi2: run_cfg = lv.Loihi2HwCfg() print("Running on Loihi 2") else: run_cfg = lv.Loihi2SimCfg(select_tag='fixed_pt') print("Loihi2 compiler is not available in this system. " "This tutorial will execute on CPU backend.") ``` -------------------------------- ### Retrieve Variable Values Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial00_tour_through_lava.ipynb Use the 'get' function to retrieve the current value of a process variable. The 'set' function can be used to modify variable values after execution. ```python dense.weights.get() ``` -------------------------------- ### Instantiate Lava Processes Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial04_execution.ipynb Basic instantiation of LIF and Dense processes in Lava. This is the first step in manual compilation and execution. ```python from lava.proc.lif.process import LIF from lava.proc.dense.process import Dense lif1 = LIF(shape=(1,)) dense = Dense(weights=np.eye(1)) lif2 = LIF(shape=(1,)) ``` -------------------------------- ### Configure Loihi 2 Execution Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial03-Normalization_network.ipynb Configures the execution environment for Loihi 2, either on hardware or simulation. It checks for Loihi 2 installation and sets the appropriate configuration. ```python import lava.utils.loihi as loihi loihi.use_slurm_host(loihi_gen=loihi.ChipGeneration.N3B3) use_loihi2 = loihi.is_installed() if use_loihi2: run_cfg = lv.Loihi2HwCfg() print("Running on Loihi 2") else: run_cfg = lv.Loihi2SimCfg(select_tag='fixed_pt') print("Loihi2 compiler is not available in this system. " "This tutorial will execute on CPU backend.") ``` -------------------------------- ### Initialize and Execute Lava Runtime Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial04_execution.ipynb Initializes the Lava runtime and starts the execution of a compiled process for a specified number of steps. This is the final stage of manual execution control. ```python from lava.magma.runtime.runtime import Runtime from lava.magma.core.run_conditions import RunSteps from lava.magma.core.process.message_interface_enum import ActorType # create and initialize a runtime mp = ActorType.MultiProcessing runtime = Runtime(exe=executable, message_infrastructure_type=mp) runtime.initialize() # start execution runtime.start(run_condition=RunSteps(num_steps=42)) # stop execution runtime.stop() ``` -------------------------------- ### Define LIF Process in Lava Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial03_process_models.ipynb Defines a Leaky Integrate-and-Fire (LIF) neural process, including its dynamics and parameters. This serves as a foundational example for creating custom processes in Lava. ```python from lava.magma.core.process.process import AbstractProcess from lava.magma.core.process.variable import Var from lava.magma.core.process.ports.ports import InPort, OutPort class LIF(AbstractProcess): """Leaky-Integrate-and-Fire (LIF) neural Process. LIF dynamics abstracts to: u[t] = u[t-1] * (1-du) + a_in # neuron current v[t] = v[t-1] * (1-dv) + u[t] + bias_mant * 2 ** bias_exp # neuron voltage s_out = v[t] > vth # spike if threshold is exceeded v[t] = 0 # reset at spike Parameters ---------- du: Inverse of decay time-constant for current decay. dv: Inverse of decay time-constant for voltage decay. bias_mant: Mantissa part of neuron bias. bias_exp: Exponent part of neuron bias, if needed. Mostly for fixed point implementations. Unnecessary for floating point implementations. If specified, bias = bias_mant * 2**bias_exp. vth: Neuron threshold voltage, exceeding which, the neuron will spike. """ def __init__(self, **kwargs): super().__init__() shape = kwargs.get("shape", (1,)) du = kwargs.pop("du", 0) dv = kwargs.pop("dv", 0) bias_mant = kwargs.pop("bias_mant", 0) bias_exp = kwargs.pop("bias_exp", 0) vth = kwargs.pop("vth", 10) self.shape = shape self.a_in = InPort(shape=shape) self.s_out = OutPort(shape=shape) self.u = Var(shape=shape, init=0) self.v = Var(shape=shape, init=0) self.du = Var(shape=(1,), init=du) self.dv = Var(shape=(1,), init=dv) self.bias_mant = Var(shape=shape, init=bias_mant) self.bias_exp = Var(shape=shape, init=bias_exp) self.vth = Var(shape=(1,), init=vth) ``` -------------------------------- ### Import Loihi 2 Framework Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial03-Normalization_network.ipynb Imports the Loihi 2 framework for hardware interaction. ```python import lava.frameworks.loihi2 as lv ``` -------------------------------- ### Instantiate and Run Network Simulation Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial02_excitatory_inhibitory_network.ipynb Instantiates an excitatory-inhibitory network, sets up a monitor to record neuron states, runs the simulation for a specified number of steps, and retrieves the recorded data. ```python from lava.magma.core.run_conditions import RunSteps from lava.magma.core.run_configs import Loihi1SimCfg # Import monitoring Process. from lava.proc.monitor.process import Monitor # Configurations for execution. num_steps = 1000 rcfg = Loihi1SimCfg(select_tag='rate_neurons') run_cond = RunSteps(num_steps=num_steps) # Instantiating network and IO processes. network_balanced = EINetwork(**network_params_balanced) state_monitor = Monitor() state_monitor.probe(target=network_balanced.state, num_steps=num_steps) # Run the network. network_balanced.run(run_cfg=rcfg, condition=run_cond) states_balanced = state_monitor.get_data()[network_balanced.name][network_balanced.state.name] network_balanced.stop() ``` -------------------------------- ### Import Lava Core and Resource Decorators Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial03_process_models.ipynb Imports necessary Lava classes for defining processes, resources, and synchronization protocols. This setup is required for creating custom process models. ```python import numpy as np from lava.magma.core.decorator import implements, requires from lava.magma.core.resources import CPU from lava.magma.core.sync.protocols.loihi_protocol import LoihiProtocol ``` -------------------------------- ### Configuring and Running a Spiking LIF Network Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial02_excitatory_inhibitory_network.ipynb Sets up and runs a spiking LIF E/I network with custom parameters and monitors. This is used to observe network dynamics under specific conditions, such as increased q_factor. ```python num_steps = 1000 rcfg = CustomRunConfigFloat(select_tag='lif_neurons', select_sub_proc_model=True) run_cond = RunSteps(num_steps=num_steps) # Creating new new network with changed weights. lif_network_critical = EINetwork(**network_params_critical, convert=True) outport_plug = io.sink.RingBuffer(shape=shape, buffer=num_steps) # Instantiate Monitors to record the voltage and the current of the LIF neurons. monitor_v = Monitor() monitor_u = Monitor() lif_network_critical.outport.connect(outport_plug.a_in) monitor_v.probe(target=lif_network_critical.state, num_steps=num_steps) monitor_u.probe(target=lif_network_critical.state_alt, num_steps=num_steps) lif_network_critical.run(condition=run_cond, run_cfg=rcfg) # Fetching spiking activity. spks_critical = outport_plug.data.get() data_v_critical = monitor_v.get_data()[lif_network_critical.name][lif_network_critical.state.name] data_u_critical = monitor_u.get_data()[lif_network_critical.name][lif_network_critical.state_alt.name] lif_network_critical.stop() ``` -------------------------------- ### Get LIF Process Variable 'v' Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial02_processes.ipynb Retrieves the current value of the 'v' variable (membrane voltage) for the LIF Process. Useful for inspecting the state of the neurons after initialization or execution. ```python print(lif.v.get()) ``` -------------------------------- ### Initialize Network Parameters and Weights Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/three_factor_learning/tutorial01_Reward_Modulated_STDP.ipynb Sets up the initial weights and shapes for pre-synaptic and post-synaptic neurons, as well as input connections and plastic synapses. ```python # Pre-synaptic neuron parameters num_neurons_pre = 1 shape_lif_pre = (num_neurons_pre, ) shape_conn_pre = (num_neurons_pre, num_neurons_pre) # SpikeIn -> pre-synaptic LIF connection weight wgt_inp_pre = np.eye(num_neurons_pre) * 250 # Post-synaptic neuron parameters num_neurons_post = 2 shape_lif_post = (num_neurons_post, ) shape_conn_post = (num_neurons_post, num_neurons_pre) # SpikeIn -> post-synaptic LIF connection weight wgt_inp_post = np.eye(num_neurons_post) * 250 # Third-factor input weights # Graded SpikeIn -> post-synaptic LIF connection weight wgt_inp_reward = np.eye(num_neurons_post) * 100 # pre-LIF -> post-LIF connection initial weight (learning-enabled) # Plastic synapse wgt_plast_conn = np.full(shape_conn_post, 50) ``` -------------------------------- ### Running the Simulation Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial08_stdp.ipynb Executes the network simulation using the specified run condition and configuration. The `pattern_pre` process is used as the entry point for running the entire network. ```python # Running pattern_pre.run(condition=RunSteps(num_steps=num_steps), run_cfg=Loihi2SimCfg(select_tag=SELECT_TAG)) ``` -------------------------------- ### Download Pretrained Weights Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial01_mnist_digit_classification.ipynb Use these bash commands if you encounter an `UnpicklingError` when instantiating `ImageClassifier` to download the necessary pretrained weights. ```bash git lfs fetch git lfs pull ``` -------------------------------- ### Filter Data for Specific Classes Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial02_clp_on_coil100.ipynb Filters the training and testing datasets to include only samples belonging to a specified range of object classes. This is useful for limiting the scope of the experiment, for example, to reduce computation time. ```python cls_rng = [0, 20] n_classes = cls_rng[1] X_train = X_train[(y_train>cls_rng[0]) & (y_train<=cls_rng[1]), :] X_test = X_test[(y_test>cls_rng[0]) & (y_test<=cls_rng[1]), :] y_train = y_train[(y_train>cls_rng[0]) & (y_train<=cls_rng[1])] y_test = y_test[(y_test>cls_rng[0]) & (y_test<=cls_rng[1])] print(X_train.shape) print(X_test.shape) ``` -------------------------------- ### Setting up Monitors for Network Data Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial08_stdp.ipynb Initializes various Monitor processes to track different aspects of the network, such as neuron traces, spikes, and synaptic weights. It then probes these monitors to specific process outputs for the duration of the simulation. ```python # Create monitors mon_pre_trace = Monitor() mon_post_trace = Monitor() mon_pre_spikes = Monitor() mon_post_spikes = Monitor() mon_weight = Monitor() # Connect monitors mon_pre_trace.probe(plast_conn.x1, num_steps) mon_post_trace.probe(plast_conn.y1, num_steps) mon_pre_spikes.probe(lif_pre.s_out, num_steps) mon_post_spikes.probe(lif_post.s_out, num_steps) mon_weight.probe(plast_conn.weights, num_steps) ``` -------------------------------- ### Run CLP Network and Get Results Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial02_clp_on_coil100.ipynb Executes the CLP network for a defined number of steps using the specified run configuration. It then retrieves the results, including novelty spikes, prototype spikes, error spikes, and predictions. ```python # Run run_cond = RunSteps(num_steps=num_steps) clp.prototypes.run(condition=run_cond, run_cfg=run_cfg) novelty_spikes, proto_spikes, error_spikes, preds, currs = clp.get_results() ``` -------------------------------- ### Calculate Binned Spikes and Auto-Covariance Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial02_excitatory_inhibitory_network.ipynb Processes raw spike data by applying a moving average (convolution with a window) to get binned spike counts, then calculates the auto-covariance function for the binned activity. This is used to analyze network dynamics. ```python window = np.ones(window_size) binned_sps_critical_fixed = np.asarray([ np.convolve(spks_critical_fixed[i], window) for i in range(dim)])[:,:-window_size +1] lags, ac_fct_lif_critical_fixed = auto_cov_fct(acts=binned_sps_critical_fixed.T) ``` -------------------------------- ### Initialize CLP Parameters Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial01_one-shot_learning_with_novelty_detection.ipynb Sets general parameters, neural dynamics parameters, and learning rule parameters for the CLP system. These include time constants, thresholds, and decay rates. ```python # General params t_wait = 4 # the waiting window for novelty detection after input injection n_protos = 3 # number of the prototypes n_features = 2 # the feature dimension of the input b_fraction = 8 # The fraction of the fixed point representation used to translate floating point input to graded spikes # The time difference between two consecutive inputs n_steps_per_sample = 12 # number of time steps that the processes will run num_steps = (n_samples + 1) * n_steps_per_sample # PrototypeLIF neural dynamics parameters du = 4095 dv = 4095 vth = 63500 # Learning rule parameters x1_tau = 65535 # Pre-trace decay constant. The high values means no decay t_epoch = 1 # Epoch length # Config for writing graded payload of the input spike to x1-trace graded_spike_cfg = GradedSpikeCfg.OVERWRITE ``` -------------------------------- ### Configure CPU Simulation with Floating Point Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial00_tour_through_lava.ipynb Use Loihi1SimCfg to configure the simulation to run on the CPU with floating-point precision. Import Loihi1SimCfg from lava.magma.core.run_configs. ```python from lava.magma.core.run_configs import Loihi1SimCfg run_cfg = Loihi1SimCfg(select_tag="floating_pt") ``` -------------------------------- ### Instantiate network objects Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial02-Fixed_point_elementwise_product.ipynb Creates instances of network layers including GradedDense, ProductVec, InputVec, and OutputVec. ```python dense1 = lv.GradedDense(weights=weights1) dense2 = lv.GradedDense(weights=weights2) vec = lv.ProductVec(shape=out_shape, vth=1, exp=0) generator1 = lv.InputVec(inp_data, loihi2=use_loihi2) generator2 = lv.InputVec(inp_data, loihi2=use_loihi2) monitor = lv.OutputVec(shape=out_shape, buffer=num_steps, loihi2=True) ``` -------------------------------- ### Import necessary libraries for CLP experiments Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial02_clp_on_coil100.ipynb Imports core libraries for running experiments, including simulation configurations, run conditions, and specific Lava processes like LearningDense. ```python import numpy as np import matplotlib.pylab as plt from lava.magma.core.run_configs import Loihi2SimCfg from lava.magma.core.run_conditions import RunSteps from lava.proc.dense.process import LearningDense from lava.proc.dense.models import PyLearningDenseModelBitApproximate ``` -------------------------------- ### Initialize CLP Network and Generate Input Spikes Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial02_clp_on_coil100.ipynb Initializes the CLP network with specified parameters for OCL. It then generates input spikes for training data and sets up the necessary processes and connections for the network. ```python from clp import CLP clp = CLP(supervised=True, learn_novels=True, n_protos=n_protos, n_features=n_features, n_steps_per_sample=n_steps_per_sample, b_fraction=b_fraction, du=du, dv=dv, vth=vth, t_wait=t_wait, debug=False) s_pattern_inp, s_user_label = clp.generate_input_spikes(X_train, y_train) clp = clp.setup_procs_and_conns() num_steps = clp.num_steps ``` -------------------------------- ### Few-Shot Supervised OCL Training and Testing Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial02_clp_on_coil100.ipynb This code implements a few-shot learning regime for OCL. It iterates through different k-shot values, prepares data, initializes and trains a supervised CLP model, and then tests its performance on both old and new classes. Accuracy metrics are printed for each k-shot value. ```python from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score old_class_accs = [] inc_class_accs = [] total_accs = [] k_shot_list = [1, 5, 10] for k_shot in k_shot_list: X_train_k_shot = np.reshape(X_train_on, (-1, 10, X_train_on.shape[-1]))[:, :k_shot].reshape((-1, X_train_on.shape[-1])) X_test_k_shot = np.reshape(X_test_on, (-1, 10, X_test_on.shape[-1]))[:, :k_shot].reshape((-1, X_test_on.shape[-1])) y_train_k_shot = np.reshape(y_train_on, (-1, 10))[:, :k_shot].reshape((-1,)) y_test_k_shot = np.reshape(y_test_on, (-1, 10,))[:, :k_shot].reshape((-1,)) # #------------- Incremental training ------------# clp = CLP(supervised=True, learn_novels=True, weights_proto=weights_proto, proto_labels=proto_labels, n_protos=n_protos, n_features=n_features, n_steps_per_sample=n_steps_per_sample, b_fraction=b_fraction, du=du, dv=dv, vth=vth, t_wait=t_wait) s_pattern_inp, s_user_label = clp.generate_input_spikes(X_train_k_shot, y_train_k_shot) clp = clp.setup_procs_and_conns() num_steps = clp.num_steps # Run run_cond = RunSteps(num_steps=num_steps) clp.prototypes.run(condition=run_cond, run_cfg=run_cfg) #---------------- Testing -----------------------# clp.supervised = False clp.learn_novels = False s_pattern_inp, s_user_label = clp.generate_input_spikes(np.vstack((X_test, X_test_on))) clp = clp.setup_procs_and_conns() num_steps = clp.num_steps # Run run_cond = RunSteps(num_steps=num_steps) clp.prototypes.run(condition=run_cond, run_cfg=run_cfg) novelty_spikes, proto_spikes, error_spikes, preds, currs = clp.get_results() y_pred = np.maximum.reduceat(preds.copy(), np.r_[0:len(preds):n_steps_per_sample]) old_cls_acc = accuracy_score(y_test, y_pred[:len(y_test)]) new_cls_acc = accuracy_score(y_test_on, y_pred[len(y_test):]) y_all = np.concatenate((y_test,y_test_on)) total_acc = accuracy_score(y_all, y_pred) print("k = " ,k_shot) print("Accuracy for old classes: ", old_cls_acc.round(3)) print("Accuracy for new classes: ", new_cls_acc.round(3)) print("Total accuracy: ", total_acc.round(3)) print("--------------------------------") old_class_accs.append(old_cls_acc) inc_class_accs.append(new_cls_acc) total_accs.append(total_acc) clp.prototypes.stop() old_class_accs = np.array(old_class_accs) inc_class_accs = np.array(inc_class_accs) total_accs = np.array(total_accs) ``` -------------------------------- ### Initialize Simulation Parameters Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial04-Creating_network_motifs.ipynb Sets the number of simulation steps and the size of the memory buffer. ```python num_steps = 20 mem_buffer_size = (50,) ``` -------------------------------- ### Compile and Run PyLifModel Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial03_process_models.ipynb Instantiates and runs a LIF process model using Loihi1SimCfg for simulation configuration and RunSteps for condition. Prints the final voltage state. ```python from lava.magma.core.run_configs import Loihi1SimCfg from lava.magma.core.run_conditions import RunSteps lif = LIF(shape=(3,), du=0, dv=0, bias_mant=3, vth=10) run_cfg = Loihi1SimCfg() lif.run(condition=RunSteps(num_steps=10), run_cfg=run_cfg) print(lif.v.get()) ``` -------------------------------- ### Create Processes for a Multi-Layer LIF Network Source: https://github.com/lava-nc/lava/blob/main/tutorials/end_to_end/tutorial00_tour_through_lava.ipynb Instantiates LIF and Dense processes to build a multi-layer network. Parameters can be set as scalars for all units or as tuples/arrays for individual unit configuration. ```python import numpy as np # Create processes lif1 = LIF(shape=(3, ), # Number and topological layout of units in the process vth=10., # Membrane threshold dv=0.1, # Inverse membrane time-constant du=0.1, # Inverse synaptic time-constant bias_mant=(1.1, 1.2, 1.3), # Bias added to the membrane voltage in every timestep name="lif1") dense = Dense(weights=np.random.rand(2, 3), # Initial value of the weights, chosen randomly name='dense') lif2 = LIF(shape=(2, ), # Number and topological layout of units in the process vth=10., # Membrane threshold dv=0.1, # Inverse membrane time-constant du=0.1, # Inverse synaptic time-constant bias_mant=0., # Bias added to the membrane voltage in every timestep name='lif2') ``` -------------------------------- ### Instantiate LIF Process Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial02_processes.ipynb Creates an instance of the LIF Process, specifying the number of neurons and initial parameter values. This is the first step before interacting with or running the process. ```python n_neurons = 3 lif = LIF(shape=(3,), du=0, dv=0, bias=3, vth=10) ``` -------------------------------- ### Configure and Run CLP for Novelty Detection Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/clp/tutorial02_clp_on_coil100.ipynb Configures CLP for novelty detection (supervised=False, learn_novels=False), generates input spikes, sets up processes and connections, and runs the prototypes. ```python clp.supervised = False clp.learn_novels = False s_pattern_inp, s_user_label = clp.generate_input_spikes(X_ood) clp = clp.setup_procs_and_conns() num_steps = clp.num_steps # Run run_cond = RunSteps(num_steps=num_steps) clp.prototypes.run(condition=run_cond, run_cfg=run_cfg) novelty_spikes, proto_spikes, error_spikes, preds, _ = clp.get_results() ``` -------------------------------- ### Configure Loihi 2 Backend Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial04-Creating_network_motifs.ipynb Configures the Lava-VA backend to use either Loihi 2 hardware or the CPU simulator based on availability. Sets up the run configuration. ```python import lava.utils.loihi loihi.use_slurm_host(loihi_gen=loihi.ChipGeneration.N3B3) use_loihi2 = loihi.is_installed() #use_loihi2 = False if use_loihi2: run_cfg = lv.Loihi2HwCfg() print("Running on Loihi 2") else: run_cfg = lv.Loihi2SimCfg(select_tag='fixed_pt') print("Loihi2 compiler is not available in this system. " "This tutorial will execute on CPU backend.") ``` -------------------------------- ### Execute a single LIF Process Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial04_execution.ipynb Create and execute a single Leaky Integrate-and-Fire (LIF) Process for a specified number of time steps using simulation configuration. ```python from lava.proc.lif.process import LIF from lava.magma.core.run_conditions import RunSteps from lava.magma.core.run_configs import Loihi1SimCfg # create a Process for a LIF neuron lif = LIF(shape=(1,)) # execute that Process for 42 time steps in simulation lif.run(condition=RunSteps(num_steps=42), run_cfg=Loihi1SimCfg()) ``` -------------------------------- ### Save and load a compiled Lava network Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial11_serialization.ipynb Demonstrates saving a compiled Lava network to a file and then loading it back. Uses a temporary directory for file management. ```python # Context manager is used only to ensure clean up with tempfile.TemporaryDirectory() as tmpdirname: # Store the Lava network save(processes=[lif_in, lif_out, dense], filename=tmpdirname + "test", executable=ex) # Load executable again procs, loaded_ex = load(tmpdirname + "test.pickle") ``` -------------------------------- ### Instantiate Lava Objects for Dot Product Source: https://github.com/lava-nc/lava/blob/main/tutorials/lava_va/Tutorial01-Fixed_point_dot_product.ipynb Initializes input vectors, dense connections, and output vectors for a fixed-point dot product computation. Ensure Loihi2 compatibility if needed. ```python invec1 = lv.InputVec(inp_data, loihi2=use_loihi2) invec2 = lv.InputVec(inp_data2, loihi2=use_loihi2) in_out_syn1 = lv.GradedDense(weights=weights, exp=weight_exp) in_out_syn2 = lv.GradedDense(weights=weights2, exp=weight_exp) outvec = lv.GradedVec(shape=(weights.shape[0],), vth=1) out_monitor = lv.OutputVec(shape=outvec.shape, buffer=num_steps, loihi2=use_loihi2) ``` -------------------------------- ### Instantiate Custom LearningRule Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial09_custom_learning_rules.ipynb Creates an instance of the Loihi2FLearningRule, configuring it with the defined STDP learning rule string and associated parameters like trace impulses, decay constants, and epoch length. ```python # Create custom LearningRule stdp = Loihi2FLearningRule(dw=dw, x1_impulse=x1_impulse, x1_tau=x1_tau, y1_impulse=y1_impulse, y1_tau=y1_tau, t_epoch=t_epoch) ``` -------------------------------- ### Instantiate and Connect Processes Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial05_connect_processes.ipynb Instantiate Lava Processes and connect their ports to enable data flow between them. This establishes the communication channel in the network. ```python sender = P1() recv = P2() # Connecting output port to an input port sender.out.connect(recv.inp) sender = P1() recv = P2() ``` -------------------------------- ### Configure Run for DenseLayer Processes Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial06_hierarchical_processes.ipynb Sets up the necessary imports and configurations for running Lava processes, including run configurations and conditions. ```python from lava.magma.core.run_configs import RunConfig, Loihi1SimCfg from lava.magma.core.run_conditions import RunSteps from lava.proc.io import sink, source dim = (3, 3) ``` -------------------------------- ### Import Run Configurations Source: https://github.com/lava-nc/lava/blob/main/tutorials/in_depth/tutorial08_stdp.ipynb Imports necessary classes for configuring and running the simulation. RunSteps defines the simulation duration, and Loihi2SimCfg specifies the execution environment. ```python from lava.magma.core.run_conditions import RunSteps from lava.magma.core.run_configs import Loihi2SimCfg ```