### Install BrainCog Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md Clone the BrainCog repository and install the library using the provided setup script. This is the initial step before running any examples. ```shell git clone https://github.com/xxx/Brain-Cog.git cd braincog python setup install --user ``` -------------------------------- ### Install Tianshou Framework Source: https://github.com/braincog-x/brain-cog/blob/main/examples/decision_making/RL/README.md Before running the PL-SDQN model, ensure you have installed the 'tianshou' framework. Refer to the official Tianshou GitHub repository for detailed installation instructions. ```shell pip install tianshou ``` -------------------------------- ### Run Human Brain Simulation Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Multi-scale_Brain_Structure_Simulation/HumanBrain.md Navigate to the example directory and execute the main Python script to start the simulation. Ensure all requirements are met before running. ```shell cd ~/examples/Multi-scale Brain Structure Simulation/HumanBrain/ python brainSimHum.py ``` -------------------------------- ### Install BrainCog Online Source: https://github.com/braincog-x/brain-cog/blob/main/README.md Install the braincog package using pip. This is the quickest way to get started. ```shell pip install braincog ``` -------------------------------- ### Install BrainCog Locally from Source Source: https://github.com/braincog-x/brain-cog/blob/main/README.md After cloning the repository, navigate to the Brain-Cog directory and install it locally in editable mode. This is recommended for developers. ```shell cd Brain-Cog pip install -e . ``` -------------------------------- ### Run Example Training Script Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md Navigate to the example directory and execute the main training script with specified parameters. This command is used for training SNNs on DVS-CIFAR10 dataset. ```shell cd examples/img_cls/bp python main.py --model dvs_convnet --node-type LIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGate --device 0 ``` -------------------------------- ### Run Human Brain Simulation Example Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Multiscale_Brain_Structure_Simulation/HumanBrain/README.md Execute the human brain simulation example script. Ensure you are in the correct directory before running. ```shell cd ~/examples/Multi-scale Brain Structure Simulation/HumanBrain/ python human_brain.py ``` -------------------------------- ### Train PL-SDQN Model Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Decision_Making/RL.md Execute this command to start the training process for the PL-SDQN model. Ensure all requirements are met before running. ```bash python ./sdqn/main.py ``` -------------------------------- ### Install BrainCog from GitHub Source: https://github.com/braincog-x/brain-cog/blob/main/README.md Install the latest version of braincog directly from its GitHub repository. This is useful for developers or when needing the most recent features. ```shell pip install git+https://github.com/braincog-X/Brain-Cog.git ``` -------------------------------- ### Install einops Dependency Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Hardware_acceleration/README.md Install the 'einops' library, which is required for running SNN models on the FPGA. This command should be executed within the cloned project directory. ```shell cd firefly_v1_common pip install einops-0.6.0-py3-none-any.whl ``` -------------------------------- ### Run Macaque Brain Simulation Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Multi-scale_Brain_Structure_Simulation/MacaqueBrain.md Navigate to the example directory and execute the simulation script. Ensure all requirements are met before running. ```shell cd ~/examples/Multi-scale Brain Structure Simulation/MacaqueBrain/ python brainSimMaq.py ``` -------------------------------- ### Example: Image Classification Source: https://github.com/braincog-x/brain-cog/blob/main/README.md Run an example for image classification using a CIFAR-10 dataset and a convolutional neural network. Specify the device and number of steps. ```shell cd ./examples/Perception_and_Learning/img_cls/bp python main.py --model cifar_convnet --dataset cifar10 --node-type LIFNode --step 8 --device 0 ``` -------------------------------- ### Training the QSNN Model Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/QSNN.md Execute this command to start the training process for the Quantum Superposition Inspired Spiking Neural Network. Ensure the DATA_DIR in main.py is correctly set. ```shell python ./main.py ``` -------------------------------- ### Example: Event Classification Source: https://github.com/braincog-x/brain-cog/blob/main/README.md Run an example for event classification using a DVS-based dataset and a convolutional neural network. Configure batch size, activation function, and device. ```shell cd ./examples/Perception_and_Learning/img_cls/bp python main.py --model dvs_convnet --node-type LIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Spiking Deep Q Network (SDQN) Examples (Python) Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md Code repository for spiking deep Q networks addressing feature information vanishing problems using potential-based normalization. ```python https://github.com/BrainCog-X/Brain-Cog/tree/main/examples/decision_making/RL/sdqn ``` -------------------------------- ### Run DSD-SNN Training Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Structural_Development/DSD-SNN/README.md Execute the main script to start the training process for DSD-SNN. Ensure CUDA is available and configured. ```bash CUDA_VISIBLE_DEVICES=0 python main_simplified.py ``` -------------------------------- ### Motor Control Experimental Examples (Python) Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md Code repository containing experimental examples for motor control using brain-inspired AI. ```python https://github.com/BrainCog-X/Brain-Cog/tree/main/examples/MotorControl/experimental ``` -------------------------------- ### Run Evolution Script Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Structure_Evolution/MSE-NAS/readme.md Execute the main evolution script to start the neural architecture search process. ```bash evolution.py ``` -------------------------------- ### BackEISNN Image Classification Example Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md This snippet demonstrates the implementation of the BackEISNN model for image classification, as described in the associated paper. It is part of the perception and learning examples. ```Python import torch import torch.nn as nn from torch.utils.data import DataLoader from torchvision import datasets, transforms # Assuming BackEISNN model definition is available # from models.backei_snn import BackEISNN # Placeholder for the actual BackEISNN model definition class BackEISNN(nn.Module): def __init__(self): super(BackEISNN, self).__init__() # Define layers here self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1) self.relu1 = nn.ReLU() self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1) self.relu2 = nn.ReLU() self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = nn.Linear(32 * 7 * 7, 10) # Assuming input image size is 28x28 def forward(self, x): x = self.pool1(self.relu1(self.conv1(x))) x = self.pool2(self.relu2(self.conv2(x))) x = x.view(-1, 32 * 7 * 7) x = self.fc1(x) return x def train_backei(model, dataloader, criterion, optimizer, epochs=10): model.train() for epoch in range(epochs): running_loss = 0.0 for i, (inputs, labels) in enumerate(dataloader): optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() if i % 100 == 99: # Print every 100 mini-batches print(f'Epoch [{epoch+1}/{epochs}], Step [{i+1}/{len(dataloader)}], Loss: {running_loss / 100:.4f}') running_loss = 0.0 print('Finished Training') def main(): # Hyperparameters batch_size = 64 learning_rate = 0.001 epochs = 10 # MNIST Dataset transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]) train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform) train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) # Model, Loss, Optimizer model = BackEISNN() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Train the model train_backei(model, train_loader, criterion, optimizer, epochs) if __name__ == '__main__': main() ``` -------------------------------- ### Initialize SMAC Parallel Environment Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Social_Cognition/ToCM/smac/examples/pettingzoo/README.rst Creates a SMAC environment supporting parallel execution where agents act simultaneously. This uses the parallel API template of PettingZoo. Requires the `smac` library to be installed. ```python from smac.env.pettingzoo import StarCraft2PZEnv env = StarCraft2PZEnv.parallel_env() ``` -------------------------------- ### Initialize SMAC AEC Environment Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Social_Cognition/ToCM/smac/examples/pettingzoo/README.rst Creates a SMAC environment using the Agent Environment Cycle (AEC) game model from PettingZoo. Requires the `smac` library to be installed. ```python from smac.env.pettingzoo import StarCraft2PZEnv env = StarCraft2PZEnv.env() ``` -------------------------------- ### Multisensory Integration Framework Example Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md This snippet outlines a framework for multisensory concept learning using Spiking Neural Networks (SNNs). It focuses on integrating information from different sensory modalities. ```Python import torch import torch.nn as nn import snntorch as snn # Define parameters for SNN neurons beta = 0.9 threshold = 1.0 num_time_steps = 100 class MultisensorySNN(nn.Module): def __init__(self, input_dims, hidden_dims, output_dims): super(MultisensorySNN, self).__init__() self.input_dims = input_dims self.hidden_dims = hidden_dims self.output_dims = output_dims # Layer for processing sensory modality 1 (e.g., visual) self.fc1_mod1 = nn.Linear(input_dims[0], hidden_dims) self.lif1_mod1 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold) # Layer for processing sensory modality 2 (e.g., auditory) self.fc1_mod2 = nn.Linear(input_dims[1], hidden_dims) self.lif1_mod2 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold) # Integration layer # Combines features from both modalities self.fc_integration = nn.Linear(hidden_dims * 2, hidden_dims) self.lif_integration = snn.LeakyIntegrateFire(beta=beta, threshold=threshold) # Output layer self.fc_output = nn.Linear(hidden_dims, output_dims) self.lif_output = snn.LeakyIntegrateFire(beta=beta, threshold=threshold) def forward(self, x_mod1, x_mod2): # Process modality 1 cur1_mod1 = self.fc1_mod1(x_mod1) spk1_mod1, _ = self.lif1_mod1(cur1_mod1) # Process modality 2 cur1_mod2 = self.fc1_mod2(x_mod2) spk1_mod2, _ = self.lif1_mod2(cur1_mod2) # Integrate information combined_features = torch.cat((spk1_mod1, spk1_mod2), dim=1) cur_integration = self.fc_integration(combined_features) spk_integration, _ = self.lif_integration(cur_integration) # Output layer cur_output = self.fc_output(spk_integration) spk_output, _ = self.lif_output(cur_output) return spk_output def main(): # Example input dimensions for two modalities input_dims = [128, 64] # e.g., visual features, auditory features hidden_dims = 256 output_dims = 10 # e.g., number of concepts model = MultisensorySNN(input_dims, hidden_dims, output_dims) print("Multisensory SNN model initialized.") # Example dummy input data for two modalities # In a real scenario, these would be spike trains or rate-based inputs dummy_input_mod1 = torch.randn(1, input_dims[0]) dummy_input_mod2 = torch.randn(1, input_dims[1]) # Simulate a forward pass over time steps (simplified) output_spikes = [] for t in range(num_time_steps): # In a real simulation, inputs might change over time spikes = model(dummy_input_mod1, dummy_input_mod2) output_spikes.append(spikes) # Aggregate or analyze output_spikes for concept prediction print("Simulated forward pass complete.") if __name__ == '__main__': main() ``` -------------------------------- ### Train CIFAR100 with IF+QGateGrad on resnet18 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Execute this command to train CIFAR100 using the IFNode model and QGateGrad activation on the resnet18 architecture. This setup is useful for comparing QGateGrad performance in a ResNet context. ```bash main.py --num-classes 100 --model resnet18 --node-type IFNode --dataset cifar100 --step 4 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Brain-Inspired Decision-Making SNN (BDM-SNN) Example (Python) Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md An example of a brain-inspired decision-making spiking neural network applied to unmanned aerial vehicles. ```python https://github.com/BrainCog-X/Brain-Cog/blob/main/examples/decision_making/BDM-SNN/BDM-SNN-hh.py ``` -------------------------------- ### Run IMNet Multisensory Integration Demo Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/MultisensoryIntegration.md Execute the IMNet model for multisensory integration. Navigate to the code directory and run the Python script. ```bash cd examples/MultisensoryIntegration/code python MultisensoryIntegrationDEMO_IM.py ``` -------------------------------- ### Run AMNet Multisensory Integration Demo Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/MultisensoryIntegration.md Execute the AMNet model for multisensory integration. Navigate to the code directory and run the Python script. ```bash cd examples/MultisensoryIntegration/code python MultisensoryIntegrationDEMO_AM.py ``` -------------------------------- ### DVS-G: LIF+QGateGrad with dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Runs the main script for DVS-Gesture dataset with LIFNode, QGateGradGrad activation, and dvs_convnet model. Batch size is set to 128. ```python main.py --num-classes 11 --model dvs_convnet --node-type LIFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Unsupervised STDP Learning Example Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md This snippet provides an example of an unsupervised Spiking Neural Network (SNN) trained using Spike-Timing-Dependent Plasticity (STDP). This approach is inspired by biologically plausible learning rules. ```Python import torch import torch.nn as nn import snntorch as snn # Define parameters num_inputs = 784 num_hidden = 256 num_outputs = 10 beta = 0.95 # Membrane potential decay threshold = 1.0 # Neuron firing threshold # Define the STDP learning rule parameters eta_plus = 0.01 # STDP potentiation learning rate eta_minus = 0.005 # STDP depression learning rate class UnsupervisedSTDPNet(nn.Module): def __init__(self): super(UnsupervisedSTDPNet, self).__init__() # Input layer: Connects inputs to hidden layer neurons self.fc1 = nn.Linear(num_inputs, num_hidden) # Hidden layer neurons with leaky integrate-and-fire dynamics self.lif1 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold, learn_beta=True, learn_threshold=True) # Output layer: Connects hidden layer neurons to output neurons self.fc2 = nn.Linear(num_hidden, num_outputs) # Output layer neurons (can be LIF or simple threshold) self.lif2 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold, learn_beta=True, learn_threshold=True) def forward(self, x, spk_rec=None, syn_rec=None): if spk_rec is None: spk_rec = [] if syn_rec is None: syn_rec = [] # Input to hidden layer cur1 = self.fc1(x) spk1, mem1 = self.lif1(cur1) spk_rec.append(spk1) syn_rec.append(cur1) # Hidden to output layer cur2 = self.fc2(spk1) spk2, mem2 = self.lif2(cur2) spk_rec.append(spk2) syn_rec.append(cur2) return spk_rec, syn_rec def update_weights_stdp(self, spk_rec, syn_rec): # This is a simplified STDP update. Real implementations are more complex. # For each synapse, update weights based on pre- and post-synaptic spike times. # This requires tracking spike times, which is not fully implemented in this basic forward pass. # Example: If pre-synaptic spike occurs before post-synaptic spike, increase weight. # If post-synaptic spike occurs before pre-synaptic spike, decrease weight. pass # Placeholder for STDP weight update logic def main(): net = UnsupervisedSTDPNet() print("Unsupervised STDP Network initialized.") # Training loop would involve feeding data, recording spikes, and updating weights using STDP. # Example: # for data_batch in dataloader: # spk_rec, syn_rec = net(data_batch) # net.update_weights_stdp(spk_rec, syn_rec) if __name__ == '__main__': main() ``` -------------------------------- ### Create Braincog Virtual Environment Source: https://github.com/braincog-x/brain-cog/blob/main/examples/TIM/README.md Commands to create and activate a conda environment for Braincog and install the library. ```bash conda create -n braincog python=3.8 conda activate braincog pip install braincog ``` -------------------------------- ### Run resnet18 on NCALTECH101 with LIF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command runs the resnet18 model on the NCALTECH101 dataset using LIF+QGate. It is configured for 100 classes, a step of 10, batch size of 128, and uses QGateGrad activation on device 0. ```bash python main.py --num-classes 100 --model resnet18 --node-type LIFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Run Main Script Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Structure_Evolution/Adaptive_lsm/BrainCog-Version/README.md Execute the main Python script to run the simulation. Ensure all requirements are met before running. ```bash main.py ``` -------------------------------- ### Collision Avoidance Example (Python) Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md Implements a reward-modulated spiking neural network for collision avoidance in drone swarms, inspired by nature. ```python https://github.com/BrainCog-X/Brain-Cog/blob/main/examples/decision_making/swarm/Collision-Avoidance.py ``` -------------------------------- ### Run dvs_convnet on NCALTECH101 with LIF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command runs the dvs_convnet model on the NCALTECH101 dataset with LIF+QGate node types. It is configured for 100 classes, a step of 10, batch size of 128, and uses QGateGrad activation on device 0. ```bash python main.py --num-classes 100 --model dvs_convnet --node-type LIFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Run Main Script Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Structural_Development/SD-SNN/README.md Execute the main script for the SNN project. Ensure CUDA is available and set the visible devices. ```bash CUDA_VISIBLE_DEVICES=0 python main.py ``` -------------------------------- ### Mode-Conditioned Learning Script Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Knowledge_Representation_and_Reasoning/musicMemory/README.md Run this script to learn and generate melodies with different modes and keys. The results are saved as a MIDI file. ```python Run the script *task/mode-conditioned learning.py* to learn and generate melodies with different mode and keys, the result will be recorded in a midi file. ``` -------------------------------- ### Multi-compartment FQ-SNN for Deep Distributional RL Examples (Python) Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md Code repository for multi-compartment neuron and population encoding improved spiking neural networks for deep distributional reinforcement learning. ```python https://github.com/BrainCog-X/Brain-Cog/tree/main/examples/decision_making/RL/mcs-fqf ``` -------------------------------- ### DVS-G: PLIF+QGateGrad with dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Runs the main script for DVS-Gesture dataset with PLIFNode, QGateGradGrad activation, and dvs_convnet model. Batch size is set to 128. ```python main.py --num-classes 11 --model dvs_convnet --node-type PLIFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Measure and Visualize Multisensory Integration Results Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/MultisensoryIntegration.md Run the script to measure and visualize the results of the multisensory integration. Ensure you have navigated to the code directory first. ```bash cd examples/MultisensoryIntegration/code python measure_and_visualization.py ``` -------------------------------- ### Run DVS-CIFAR10 with LIFNode and QGateGradGrad Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to run experiments with the LIFNode neuron type and QGateGradGrad activation function on the DVS-CIFAR10 dataset using the dvs_convnet model. ```bash main.py --model dvs_convnet --node-type LIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Spiking Neural Network Conversion Example Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md This snippet shows how to convert a trained Spiking Neural Network (SNN) for tasks like object detection and segmentation. It is useful for optimizing SNNs for deployment. ```Python import torch import torch.nn as nn from torchvision import datasets, transforms from torch.utils.data import DataLoader # Assuming a standard PyTorch CNN model for demonstration class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) self.relu = nn.ReLU() self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.fc = nn.Linear(32 * 16 * 16, 10) # Assuming input size 32x32 def forward(self, x): x = self.pool(self.relu(self.conv1(x))) x = x.view(-1, 32 * 16 * 16) x = self.fc(x) return x # Placeholder for SNN conversion function def convert_to_snn(model, conversion_method='spike_calibration'): print(f"Converting model to SNN using method: {conversion_method}") # Actual conversion logic would go here # This might involve replacing layers, quantizing weights, etc. snn_model = model # Placeholder return snn_model def main(): # Load a pre-trained model (or define a new one) # For demonstration, we use a simple CNN structure model = SimpleCNN() # Assume model is trained on CIFAR-10 or similar dataset # Convert the model to an SNN snn_model = convert_to_snn(model, conversion_method='spike_calibration') # Further steps would involve evaluating the SNN performance print("SNN conversion complete. Model is ready for evaluation.") if __name__ == '__main__': main() ``` -------------------------------- ### CIFAR10 Benchmark: ResNet18 with LIFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command configures and runs a benchmark for a ResNet18 model on the CIFAR10 dataset using LIFNode and QGateGrad activation function. ```shell python main.py --model resnet18 --node-type LIFNode --dataset cifar10 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Train CIFAR100 with LIF+Atan on dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md This command trains the CIFAR100 dataset using the LIFNode neuron model and AtanGrad activation function with the dvs_convnet model. Adjust parameters as needed for your specific setup. ```bash main.py --num-classes 100 --model cifar_convnet --node-type LIFNode --dataset cifar100 --step 4 --batch-size 128 --act-fun AtanGrad --device 0 ``` -------------------------------- ### CIFAR10 Benchmark: ResNet18 with PLIFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command configures and runs a benchmark for a ResNet18 model on the CIFAR10 dataset using PLIFNode and QGateGrad activation function. ```shell python main.py --model resnet18 --node-type PLIFNode --dataset cifar10 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### CIFAR10 Benchmark: ConvNet with LIFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command configures and runs a benchmark for a convolutional neural network on the CIFAR10 dataset using LIFNode and QGateGrad activation function. ```shell python main.py --model cifar_convnet --node-type LIFNode --dataset cifar10 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Train SNN on DVS-CIFAR10 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Example command to train a Spiking Neural Network (SNN) on the DVS-CIFAR10 dataset using a specific model, node type, and activation function. Ensure you are in the correct directory before running. ```shell cd examples/Perception_and_Learning/img_cls/bp python main.py --model dvs_convnet --node-type LIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### DVS-G: IF+QGateGrad with dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Runs the main script for DVS-Gesture dataset with IFNode, QGateGradGrad activation, and dvs_convnet model. Batch size is set to 128. ```python main.py --num-classes 11 --model dvs_convnet --node-type IFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Backpropagation for Deep SNNs Example Source: https://github.com/braincog-x/brain-cog/blob/main/documents/Pub_brain_inspired_AI.md This snippet demonstrates the use of backpropagation with biologically plausible spatiotemporal adjustment for training deep Spiking Neural Networks (SNNs). It's relevant for gradient-based learning in SNNs. ```Python import torch import torch.nn as nn import snntorch as snn # Define parameters num_inputs = 784 num_hidden1 = 1024 num_hidden2 = 512 num_outputs = 10 beta = 0.95 threshold = 1.0 class SpikingCNNBackprop(nn.Module): def __init__(self): super(SpikingCNNBackprop, self).__init__() # Input layer (example: flatten input) self.fc1 = nn.Linear(num_inputs, num_hidden1) self.lif1 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold, surrogate_grad=snn.surrogate.fast_sigmoid()) # Hidden layer 1 self.fc2 = nn.Linear(num_hidden1, num_hidden2) self.lif2 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold, surrogate_grad=snn.surrogate.fast_sigmoid()) # Output layer self.fc3 = nn.Linear(num_hidden2, num_outputs) self.lif3 = snn.LeakyIntegrateFire(beta=beta, threshold=threshold, surrogate_grad=snn.surrogate.fast_sigmoid()) def forward(self, x): # Input layer processing cur1 = self.fc1(x) spk1, mem1 = self.lif1(cur1) # Hidden layer 1 processing cur2 = self.fc2(spk1) spk2, mem2 = self.lif2(cur2) # Output layer processing cur3 = self.fc3(spk2) spk3, mem3 = self.lif3(cur3) # Return output spikes (or other relevant outputs) return spk3 def main(): # Initialize the SNN model model = SpikingCNNBackprop() print("Spiking CNN model for backpropagation initialized.") # Define loss function and optimizer criterion = nn.CrossEntropyLoss() # Example loss for classification optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # Training loop would involve: # 1. Feeding input data (potentially over time steps) # 2. Computing output spikes # 3. Calculating loss based on output spikes and target labels # 4. Backpropagating the error using surrogate gradients # 5. Updating weights using the optimizer # Example of a single forward pass with dummy data dummy_input = torch.randn(1, num_inputs) output_spikes = model(dummy_input) print(f"Example output spikes shape: {output_spikes.shape}") if __name__ == '__main__': main() ``` -------------------------------- ### Train CIFAR100 with LIF+Atan on resnet18 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to train CIFAR100 with the LIFNode neuron model and AtanGrad activation function on the resnet18 architecture. This setup allows for evaluating LIF neurons within a ResNet framework. ```bash main.py --num-classes 100 --model resnet18 --node-type LIFNode --dataset cifar100 --step 4 --batch-size 128 --act-fun AtanGrad --device 0 ``` -------------------------------- ### Run DVS-CIFAR10 with PLIFNode and QGateGradGrad Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to run experiments with the PLIFNode neuron type and QGateGradGrad activation function on the DVS-CIFAR10 dataset using the dvs_convnet model. ```bash main.py --model dvs_convnet --node-type PLIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run resnet18 on NCALTECH101 with IF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command initiates training for the resnet18 model on the NCALTECH101 dataset with IF+QGate node types. It specifies 100 classes, a batch size of 128, and uses QGateGrad activation. ```bash python main.py --num-classes 100 --model resnet18 --node-type IFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Run ANN Training and SNN Conversion Scripts Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/Conversion.md Execute the script to train the ANN model first, followed by the script to perform SNN inference using the converted model. ```shell python CIFAR10_VGG16.py ``` ```shell python converted_CIFAR10.py ``` -------------------------------- ### DVS-G: LIF+QGateGrad with resnet18 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Runs the main script for DVS-Gesture dataset with LIFNode, QGateGradGrad activation, and resnet18 model. Batch size is set to 128. ```python main.py --num-classes 11 --model resnet18 --node-type LIFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### CIFAR10 Benchmark: ResNet18 with IFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command configures and runs a benchmark for a ResNet18 model on the CIFAR10 dataset using IFNode and QGateGrad activation function. ```shell python main.py --model resnet18 --node-type IFNode --dataset cifar10 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### CIFAR10 Benchmark: ConvNet with IFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command configures and runs a benchmark for a convolutional neural network on the CIFAR10 dataset using IFNode and QGateGrad activation function. ```shell python main.py --model cifar_convnet --node-type IFNode --dataset cifar10 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Run DVS-CIFAR10 with IFNode and QGateGradGrad Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to run experiments with the IFNode neuron type and QGateGradGrad activation function on the DVS-CIFAR10 dataset using the dvs_convnet model. ```bash main.py --model dvs_convnet --node-type IFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run DVS-CIFAR10 with LIFNode and QGateGradGrad (ResNet18) Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to run experiments with the LIFNode neuron type and QGateGradGrad activation function on the DVS-CIFAR10 dataset using the resnet18 model. ```bash main.py --model resnet18 --node-type LIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### CIFAR10 Benchmark: ConvNet with PLIFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command configures and runs a benchmark for a convolutional neural network on the CIFAR10 dataset using PLIFNode and QGateGrad activation function. ```shell python main.py --model cifar_convnet --node-type PLIFNode --dataset cifar10 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Train DVS-Convnet with LIF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md Use this command to train the dvs_convnet model with LIFNode and QGateGrad. This configuration is suitable for experiments with leaky neurons and the QGate activation. ```bash python main.py --num-classes 11 --model dvs_convnet --node-type LIFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Run dvs_convnet on NCALTECH101 with PLIF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command executes the dvs_convnet model for image classification on NCALTECH101 using PLIF+QGate. It includes parameters for class count, dataset, simulation step, batch size, activation function, and target device. ```bash python main.py --num-classes 100 --model dvs_convnet --node-type PLIFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### SHD: LIF+QGateGrad with shd_snn Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Run image classification on SHD dataset using the LIFNode and QGateGrad activation function with the shd_snn model. Includes parameters for learning rate, tau, threshold, and uses AMP. ```bash main.py --model SHD_SNN --node-type LIFNode --dataset shd --step 15 --batch-size 256 --act-fun QGateGrad --device 1 --tau 10. --threshold 0.3 --lr 5e-3 --min-lr 1e-4 --loss-fn onehot-mse --num-classes 20 --amp --weight-decay 0.01 ``` -------------------------------- ### Run DVS-CIFAR10 with PLIFNode and QGateGradGrad (ResNet18) Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to run experiments with the PLIFNode neuron type and QGateGradGrad activation function on the DVS-CIFAR10 dataset using the resnet18 model. ```bash main.py --model resnet18 --node-type PLIFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run resnet18 on NCALTECH101 with PLIF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command executes the resnet18 model for image classification on NCALTECH101 with PLIF+QGate. Parameters include class count, dataset, simulation step, batch size, activation function, and target device. ```bash python main.py --num-classes 100 --model resnet18 --node-type PLIFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Music Generation Script Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Knowledge_Representation_and_Reasoning/musicMemory/README.md Run this script to learn and generate melodies with different styles. The results are saved as a MIDI file. ```python Run the script *task/musicGeneration.py* to learn and generate melodies with different styles, the result will be recorded in a midi file. ``` -------------------------------- ### NCALTECH101: LIF+QGateGrad with dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Run image classification on NCALTECH101 using the LIFNode and QGateGrad activation function with the dvs_convnet model. Batch size is set to 128. ```bash main.py --num-classes 100 --model dvs_convnet --node-type LIFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run DVS-CIFAR10 with IFNode and QGateGradGrad (ResNet18) Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to run experiments with the IFNode neuron type and QGateGradGrad activation function on the DVS-CIFAR10 dataset using the resnet18 model. ```bash main.py --model resnet18 --node-type IFNode --dataset dvsc10 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run DPAP Project Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Structural_Development/DPAP/README.md Execute the main Python script for the DPAP project using CUDA. ```bash CUDA_VISIBLE_DEVICES=0 python prun_ main.py ``` -------------------------------- ### Visualize Loss Landscape with MPI Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/transfer_for_dvs/README.md Visualize the loss landscape using MPI for distributed processing. This command requires cloning a specific repository and specifies evaluation checkpoints and plot parameters. ```shell HDF5_USE_FILE_LOCKING="FALSE" mpirun -n 4 -mca btl ^openib python main_visual_losslandscape.py --model VGG_SNN --node-type LIFNode --source-dataset cifar10 --target-dataset dvsc10 --step 10 --batch-size 1000 --eval --eval_checkpoint /home/TransferLearning_For_DVS/Resultes_new_compare/Baseline/VGG_SNN-dvsc10-10-seed_42-bs_120-DA_True-ls_0.0-traindataratio_0.1-TET_first_False-TET_second_False/last.pth.tar --mpi --x=-1.0:1.0:51 --y=-1.0:1.0:51 --dir_type weights --xnorm filter --xignore biasbn --ynorm filter --yignore biasbn --plot --DVS-DA --smoothing 0.0 --traindata-ratio 0.1 ``` -------------------------------- ### Visualize Grad-CAM++ for Transfer Learning Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/transfer_for_dvs/README.md Visualize Grad-CAM++ heatmaps for a transfer learning model. This command requires cloning a specific repository and specifies evaluation checkpoints for model analysis. ```shell python GradCAM_visualization.py --model Transfer_VGG_SNN --node-type LIFNode --source-dataset cifar10 --target-dataset dvsc10 --step 10 --batch-size 1 --act-fun QGateGrad --device 6 --seed 42 --smoothing 0.0 --DVS-DA --eval --eval_checkpoint /home/TransferLearning_For_DVS/Results_lastest/train_TCKA_test/Transfer_VGG_SNN-dvsc10-10-bs_120-seed_42-DA_True-ls_0.0-lr_0.005-SNR_0-domainLoss_True-semanticLoss_True-domain_loss_coefficient1.0-semantic_loss_coefficient0.5-traindataratio_1.0-TETfirst_True-TETsecond_True/model_best.pth.tar ``` -------------------------------- ### Visualize Loss Landscape for Transfer Learning Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/transfer_for_dvs/README.md Visualize the loss landscape for a transfer learning model. This command specifies evaluation checkpoints, MPI usage, and plotting parameters for weights and biases. ```shell python main_visual_losslandscape.py --model Transfer_VGG_SNN --node-type LIFNode --source-dataset CALTECH101 --target-dataset NCALTECH101 --step 10 --batch-size 500 --eval --eval_checkpoint /home/TransferLearning_For_DVS/Results_new_compare/train_TCKA_test/Transfer_VGG_SNN-NCALTECH101-10-bs_120-seed_47-DA_False-ls_0.0-lr_0.005-SNR_0-domainLoss_True-semanticLoss_True-domain_loss_coefficient1.0-semantic_loss_coefficient0.001-traindataratio_0.1-TETfirst_True-TETsecond_True/last.pth.tar --mpi --x=-1.0:1.0:51 --y=-1.0:1.0:51 --dir_type weights --xnorm filter --xignore biasbn --ynorm filter --yignore biasbn --plot --smoothing 0.0 --traindata-ratio 0.1 --num-classes 101 --device 5& ``` -------------------------------- ### Run BCM.py Script Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Structure_Evolution/Adaptive_lsm/raw/README.md Execute the BCM.py script to run the simulation. Ensure all requirements are met before execution. ```bash python BCM.py ``` -------------------------------- ### Run Baseline VGG_SNN on N-Caltech 101 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/transfer_for_dvs/README.md Execute the baseline VGG_SNN model training on the N-Caltech 101 dataset. This command includes parameters for dataset, number of classes, and other training configurations. ```shell python main.py --model VGG_SNN --node-type LIFNode --dataset NCALTECH101 --step 10 --batch-size 120 --act-fun QGateGrad --device 7 --seed 42 --num-classes 101 --traindata-ratio 1.0 --smoothing 0.0 --TET-loss-first --TET-loss-second ``` -------------------------------- ### DVS-G: PLIF+QGateGrad with resnet18 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Runs the main script for DVS-Gesture dataset with PLIFNode, QGateGradGrad activation, and resnet18 model. Batch size is set to 128. ```python main.py --num-classes 11 --model resnet18 --node-type PLIFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Train ResNet18 with PLIF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command trains the resnet18 model with PLIFNode and QGateGrad. It's useful for exploring plasticity effects with QGate activation in the ResNet architecture. ```bash python main.py --num-classes 11 --model resnet18 --node-type PLIFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Run CIFAR100 Classification with LIFNode and QGateGrad Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md Use this command to run CIFAR100 classification with an LIFNode and QGateGrad activation function on the dvs_convnet model. Adjust batch size and device as needed. ```bash python main.py --num-classes 100 --model cifar_convnet --node-type LIFNode --dataset cifar100 --step 4 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### NCALTECH101: LIF+QGateGrad with resnet18 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Run image classification on NCALTECH101 using the LIFNode and QGateGrad activation function with the resnet18 model. EventMix data augmentation is used. Batch size is set to 128. ```bash main.py --num-classes 100 --model resnet18 --node-type LIFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run decisionmaking.py Source: https://github.com/braincog-x/brain-cog/blob/main/examples/decision_making/BDM-SNN/README.md Execute the core decision-making spiking neural network code with LIF neurons. ```shell python decisionmaking.py ``` -------------------------------- ### Train CIFAR100 with IF+QGateGrad on dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md This command trains CIFAR100 using the IFNode model and QGateGrad activation on the dvs_convnet architecture. It's useful for exploring the impact of QGateGrad on performance. ```bash main.py --num-classes 100 --model cifar_convnet --node-type IFNode --dataset cifar100 --step 4 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run dvs_convnet on NCALTECH101 with IF+QGate Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Perception_and_Learning/img_cls/bp.md This command executes the dvs_convnet model for image classification on the NCALTECH101 dataset using IF+QGate node types. It specifies the number of classes, dataset, simulation step, batch size, activation function, and device. ```bash python main.py --num-classes 100 --model dvs_convnet --node-type IFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGrad --device 0 ``` -------------------------------- ### Train CIFAR100 with LIF+QGateGrad on dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Use this command to train CIFAR100 with the LIFNode neuron model and QGateGrad activation function on the dvs_convnet architecture. This configuration allows for evaluating QGateGrad with LIF neurons. ```bash main.py --num-classes 100 --model cifar_convnet --node-type LIFNode --dataset cifar100 --step 4 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### NCALTECH101: IF+QGateGrad with dvs_convnet Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Run image classification on NCALTECH101 using the IFNode and QGateGrad activation function with the dvs_convnet model. Batch size is set to 128. ```bash main.py --num-classes 100 --model dvs_convnet --node-type IFNode --dataset NCALTECH101 --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### DVS-G: IF+QGateGrad with resnet18 Source: https://github.com/braincog-x/brain-cog/blob/main/examples/Perception_and_Learning/img_cls/bp/README.md Runs the main script for DVS-Gesture dataset with IFNode, QGateGradGrad activation, and resnet18 model. Batch size is set to 128. ```python main.py --num-classes 11 --model resnet18 --node-type IFNode --dataset dvsg --step 10 --batch-size 128 --act-fun QGateGradGrad --device 0 ``` -------------------------------- ### Run Sequence Production SNN Model Source: https://github.com/braincog-x/brain-cog/blob/main/docs/source/examples/Knowledge_Representation_and_Reasoning/SPSNN.md Execute the main script to build and run the Sequence Production SNN model. This command initiates the model's learning and reconstruction processes for symbol sequences. ```shell python main.py file ```