├── images ├── gitignore ├── 1.jpg ├── 1.png ├── 2.png ├── 3.png ├── rf.png ├── 100.png ├── 101.png ├── test.png ├── train.png ├── center.png ├── graph1.png ├── graph2.png ├── graph3.png ├── graph4.png ├── neuron1.png ├── neuron2.png ├── neuron3.png ├── neuron4.png ├── spikes.jpg ├── figure_1.png ├── figure_11.png ├── figure_12.png ├── figure_13.png ├── figure_14.png ├── figure_2.png ├── figure_3.png ├── figure_4.png ├── imp_train.png ├── stdp_curve.jpg └── classify_neurons │ ├── 1.JPG │ ├── 2.JPG │ ├── 3.JPG │ ├── 4.JPG │ ├── 5.JPG │ ├── 6.JPG │ ├── 7.JPG │ └── 8.JPG ├── training ├── gitignore ├── parameters.py ├── rl.py ├── neuron.py ├── var_th.py ├── reconstruct.py ├── recep_field.py ├── spike_train.py └── learning.py ├── classification ├── gitignore ├── neuron.pyc ├── recep_field.pyc ├── spike_train.pyc ├── training_images │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png ├── weight_initialization.pyc ├── weight_initialization.py ├── neuron.py ├── recep_field.py ├── spike_train.py └── classify.py ├── temp-snn ├── training │ ├── gitignore │ └── learning.py ├── classification │ ├── gitignore │ ├── weight_initialization.py │ └── classify.py ├── docs │ ├── images │ │ ├── gitignore │ │ ├── 1.jpg │ │ ├── 1.png │ │ ├── 2.png │ │ ├── 3.png │ │ ├── 100.png │ │ ├── 101.png │ │ ├── rf.png │ │ ├── center.png │ │ ├── graph1.png │ │ ├── graph2.png │ │ ├── graph3.png │ │ ├── graph4.png │ │ ├── spikes.jpg │ │ ├── test.png │ │ ├── train.png │ │ ├── figure_1.png │ │ ├── figure_2.png │ │ ├── figure_3.png │ │ ├── figure_4.png │ │ ├── neuron1.png │ │ ├── neuron2.png │ │ ├── neuron3.png │ │ ├── neuron4.png │ │ ├── figure_11.png │ │ ├── figure_12.png │ │ ├── figure_13.png │ │ ├── figure_14.png │ │ ├── imp_train.png │ │ └── stdp_curve.jpg │ └── README.md ├── snn │ ├── __init__.py │ ├── test │ │ ├── 0.png │ │ ├── 1.png │ │ └── 2.png │ ├── training │ │ ├── 0.png │ │ ├── 1.png │ │ └── 2.png │ ├── rl.py │ ├── parameters.py │ ├── var_th.py │ ├── neuron.py │ ├── weight_initialization.py │ ├── reconstruct.py │ ├── recep_field.py │ ├── classify.py │ ├── spike_train.py │ ├── learning.py │ ├── weights.txt │ └── weights_training.txt ├── .gitignore ├── data │ ├── test │ │ ├── 0.png │ │ ├── 1.png │ │ └── 2.png │ └── training │ │ ├── 0.png │ │ ├── 1.png │ │ └── 2.png ├── README.md ├── recreate.py └── weights.txt ├── neuron ├── neuron.png ├── spikes.png ├── neuron.py └── README.md ├── multi_layer ├── weights │ └── .gitignore ├── training_images │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png ├── rl.py ├── neuron.py ├── var_th.py ├── parameters.py ├── recep_field.py ├── reconstruct.py ├── spike_train.py └── learning.py ├── receptive_field ├── README.md └── receptive_field.py ├── synapse ├── README.md └── synapse.py ├── encoding ├── README.md └── spike_train.py ├── README.md └── LICENSE /images/gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /training/gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /classification/gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /temp-snn/training/gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /temp-snn/classification/gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /temp-snn/docs/images/gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /temp-snn/snn/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | -------------------------------------------------------------------------------- /images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/1.jpg -------------------------------------------------------------------------------- /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/3.png -------------------------------------------------------------------------------- /images/rf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/rf.png -------------------------------------------------------------------------------- /images/100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/100.png -------------------------------------------------------------------------------- /images/101.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/101.png -------------------------------------------------------------------------------- /images/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/test.png -------------------------------------------------------------------------------- /images/train.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/train.png -------------------------------------------------------------------------------- /images/center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/center.png -------------------------------------------------------------------------------- /images/graph1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/graph1.png -------------------------------------------------------------------------------- /images/graph2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/graph2.png -------------------------------------------------------------------------------- /images/graph3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/graph3.png -------------------------------------------------------------------------------- /images/graph4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/graph4.png -------------------------------------------------------------------------------- /images/neuron1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/neuron1.png -------------------------------------------------------------------------------- /images/neuron2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/neuron2.png -------------------------------------------------------------------------------- /images/neuron3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/neuron3.png -------------------------------------------------------------------------------- /images/neuron4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/neuron4.png -------------------------------------------------------------------------------- /images/spikes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/spikes.jpg -------------------------------------------------------------------------------- /neuron/neuron.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/neuron/neuron.png -------------------------------------------------------------------------------- /neuron/spikes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/neuron/spikes.png -------------------------------------------------------------------------------- /images/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_1.png -------------------------------------------------------------------------------- /images/figure_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_11.png -------------------------------------------------------------------------------- /images/figure_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_12.png -------------------------------------------------------------------------------- /images/figure_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_13.png -------------------------------------------------------------------------------- /images/figure_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_14.png -------------------------------------------------------------------------------- /images/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_2.png -------------------------------------------------------------------------------- /images/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_3.png -------------------------------------------------------------------------------- /images/figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/figure_4.png -------------------------------------------------------------------------------- /images/imp_train.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/imp_train.png -------------------------------------------------------------------------------- /images/stdp_curve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/stdp_curve.jpg -------------------------------------------------------------------------------- /multi_layer/weights/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /temp-snn/snn/test/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/snn/test/0.png -------------------------------------------------------------------------------- /temp-snn/snn/test/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/snn/test/1.png -------------------------------------------------------------------------------- /temp-snn/snn/test/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/snn/test/2.png -------------------------------------------------------------------------------- /classification/neuron.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/neuron.pyc -------------------------------------------------------------------------------- /temp-snn/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.py[cod] 3 | *$py.class 4 | 5 | .spyderproject 6 | .spyproject 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /temp-snn/data/test/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/data/test/0.png -------------------------------------------------------------------------------- /temp-snn/data/test/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/data/test/1.png -------------------------------------------------------------------------------- /temp-snn/data/test/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/data/test/2.png -------------------------------------------------------------------------------- /temp-snn/docs/images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/1.jpg -------------------------------------------------------------------------------- /temp-snn/docs/images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/1.png -------------------------------------------------------------------------------- /temp-snn/docs/images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/2.png -------------------------------------------------------------------------------- /temp-snn/docs/images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/3.png -------------------------------------------------------------------------------- /temp-snn/data/training/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/data/training/0.png -------------------------------------------------------------------------------- /temp-snn/data/training/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/data/training/1.png -------------------------------------------------------------------------------- /temp-snn/data/training/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/data/training/2.png -------------------------------------------------------------------------------- /temp-snn/docs/images/100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/100.png -------------------------------------------------------------------------------- /temp-snn/docs/images/101.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/101.png -------------------------------------------------------------------------------- /temp-snn/docs/images/rf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/rf.png -------------------------------------------------------------------------------- /temp-snn/snn/training/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/snn/training/0.png -------------------------------------------------------------------------------- /temp-snn/snn/training/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/snn/training/1.png -------------------------------------------------------------------------------- /temp-snn/snn/training/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/snn/training/2.png -------------------------------------------------------------------------------- /classification/recep_field.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/recep_field.pyc -------------------------------------------------------------------------------- /classification/spike_train.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/spike_train.pyc -------------------------------------------------------------------------------- /images/classify_neurons/1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/1.JPG -------------------------------------------------------------------------------- /images/classify_neurons/2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/2.JPG -------------------------------------------------------------------------------- /images/classify_neurons/3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/3.JPG -------------------------------------------------------------------------------- /images/classify_neurons/4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/4.JPG -------------------------------------------------------------------------------- /images/classify_neurons/5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/5.JPG -------------------------------------------------------------------------------- /images/classify_neurons/6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/6.JPG -------------------------------------------------------------------------------- /images/classify_neurons/7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/7.JPG -------------------------------------------------------------------------------- /images/classify_neurons/8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/images/classify_neurons/8.JPG -------------------------------------------------------------------------------- /temp-snn/docs/images/center.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/center.png -------------------------------------------------------------------------------- /temp-snn/docs/images/graph1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/graph1.png -------------------------------------------------------------------------------- /temp-snn/docs/images/graph2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/graph2.png -------------------------------------------------------------------------------- /temp-snn/docs/images/graph3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/graph3.png -------------------------------------------------------------------------------- /temp-snn/docs/images/graph4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/graph4.png -------------------------------------------------------------------------------- /temp-snn/docs/images/spikes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/spikes.jpg -------------------------------------------------------------------------------- /temp-snn/docs/images/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/test.png -------------------------------------------------------------------------------- /temp-snn/docs/images/train.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/train.png -------------------------------------------------------------------------------- /multi_layer/training_images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/multi_layer/training_images/1.png -------------------------------------------------------------------------------- /multi_layer/training_images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/multi_layer/training_images/2.png -------------------------------------------------------------------------------- /multi_layer/training_images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/multi_layer/training_images/3.png -------------------------------------------------------------------------------- /multi_layer/training_images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/multi_layer/training_images/4.png -------------------------------------------------------------------------------- /multi_layer/training_images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/multi_layer/training_images/5.png -------------------------------------------------------------------------------- /multi_layer/training_images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/multi_layer/training_images/6.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_1.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_2.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_3.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_4.png -------------------------------------------------------------------------------- /temp-snn/docs/images/neuron1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/neuron1.png -------------------------------------------------------------------------------- /temp-snn/docs/images/neuron2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/neuron2.png -------------------------------------------------------------------------------- /temp-snn/docs/images/neuron3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/neuron3.png -------------------------------------------------------------------------------- /temp-snn/docs/images/neuron4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/neuron4.png -------------------------------------------------------------------------------- /classification/training_images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/training_images/1.png -------------------------------------------------------------------------------- /classification/training_images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/training_images/2.png -------------------------------------------------------------------------------- /classification/training_images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/training_images/3.png -------------------------------------------------------------------------------- /classification/training_images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/training_images/4.png -------------------------------------------------------------------------------- /classification/training_images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/training_images/5.png -------------------------------------------------------------------------------- /classification/training_images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/training_images/6.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_11.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_12.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_13.png -------------------------------------------------------------------------------- /temp-snn/docs/images/figure_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/figure_14.png -------------------------------------------------------------------------------- /temp-snn/docs/images/imp_train.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/imp_train.png -------------------------------------------------------------------------------- /temp-snn/docs/images/stdp_curve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/temp-snn/docs/images/stdp_curve.jpg -------------------------------------------------------------------------------- /classification/weight_initialization.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shikhargupta/Spiking-Neural-Network/HEAD/classification/weight_initialization.pyc -------------------------------------------------------------------------------- /receptive_field/README.md: -------------------------------------------------------------------------------- 1 | # Receptive Field 2 | 3 | Receptive field is an area in which stimulation leads to response of a particular sensory neuron. In the case of an SNN, where the input is an image, receptive field of a sensory neuron is the part of the image which increases the its membrane potential. Here on-centered receptive field is used. 4 |

5 | 6 |

7 | To realise an on centerd receptive field, a sliding window is used whose cells are weighted according to the [Manhattan Distance] (https://xlinux.nist.gov/dads/HTML/manhattanDistance.html) from the centre of the window. The fields for different neurons are overlapping. 8 |

9 | 10 |

11 | -------------------------------------------------------------------------------- /classification/weight_initialization.py: -------------------------------------------------------------------------------- 1 | ################################ README ######################################## 2 | # This file is used to initialize the network with trained weights.'image_names' 3 | # consists of names of the images that are needed to be read. 4 | ################################################################################ 5 | 6 | import numpy as np 7 | import imageio 8 | 9 | def learned_weights(): 10 | image_names = ["1", "2", "3", "4", "5", "6"] 11 | ans = [] 12 | for image in image_names: 13 | temp = [] 14 | img = imageio.imread("training_images/" + image + ".png") 15 | for i in img: 16 | for j in i: 17 | if(j==0): 18 | temp.append(-0.7) 19 | else: 20 | temp.append(1) 21 | ans.append(temp) 22 | return ans 23 | 24 | if __name__ == '__main__': 25 | a = learned_weights() 26 | print a 27 | -------------------------------------------------------------------------------- /synapse/README.md: -------------------------------------------------------------------------------- 1 | ## Synapse 2 | 3 | In neurobiology synapse is a junction between two nerve cells, consisting of a minute gap across which impulses pass by diffusion of a neurotransmitter. 4 | In an SNN, synapse is the weighted path for generated spikes from one neuron to the other connected neurons. 5 |

6 | 7 |

8 | 9 | This is the implementation of a simple network of 2 layers with 5 neurons in the first layer and 3 in the second as shown in the figure. Each neuron in the first layer is connected to all the neurons in the second layer via synapse. Synapses are realised by a 2D matrix of size (5x3) initialised with random weights. 10 | 11 | This provides a framework for the SNN with learned weights so that it can be used for classification (or prediction). It can be expanded to any number of layers with any number of neurons in it. 12 | -------------------------------------------------------------------------------- /training/parameters.py: -------------------------------------------------------------------------------- 1 | ################################################ README ######################################################### 2 | 3 | # This file contains all the parameters of the network. 4 | 5 | ################################################################################################################# 6 | 7 | class param: 8 | scale = 1 9 | T = 200 10 | t_back = -20 11 | t_fore = 20 12 | 13 | pixel_x = 28 14 | Prest = 0 15 | m = pixel_x*pixel_x #Number of neurons in first layer 16 | n = 3 #Number of neurons in second layer 17 | Pmin = -500*scale 18 | # Pth = 5 19 | # D = 0.7 20 | w_max = 1.5*scale 21 | w_min = -1.2*scale 22 | sigma = 0.1 #0.02 23 | A_plus = 0.8 # time difference is positive i.e negative reinforcement 24 | A_minus = 0.3 # 0.01 # time difference is negative i.e positive reinforcement 25 | tau_plus = 8 26 | tau_minus = 5 27 | 28 | epoch = 12 29 | 30 | 31 | fr_bits = 12 32 | int_bits = 12 -------------------------------------------------------------------------------- /multi_layer/rl.py: -------------------------------------------------------------------------------- 1 | ########################################################## README ########################################################### 2 | 3 | # This file implements STDP curve and weight update rule 4 | 5 | ############################################################################################################################## 6 | 7 | 8 | 9 | import numpy as np 10 | from matplotlib import pyplot as plt 11 | from parameters import param as par 12 | 13 | #STDP reinforcement learning curve 14 | def rl(t): 15 | 16 | if t>0: 17 | return -par.A_plus*np.exp(-float(t)/par.tau_plus) 18 | if t<=0: 19 | return par.A_minus*np.exp(float(t)/par.tau_minus) 20 | 21 | 22 | #STDP weight update rule 23 | def update(w, del_w): 24 | if del_w<0: 25 | return w + par.sigma*del_w*(w-abs(par.w_min))*par.scale 26 | elif del_w>0: 27 | return w + par.sigma*del_w*(par.w_max-w)*par.scale 28 | 29 | if __name__ == '__main__': 30 | 31 | print rl(-20)*par.sigma 32 | 33 | -------------------------------------------------------------------------------- /temp-snn/snn/rl.py: -------------------------------------------------------------------------------- 1 | ########################################################## README ########################################################### 2 | 3 | # This file implements STDP curve and weight update rule 4 | 5 | ############################################################################################################################## 6 | 7 | 8 | 9 | import numpy as np 10 | from matplotlib import pyplot as plt 11 | from parameters import param as par 12 | 13 | #STDP reinforcement learning curve 14 | def rl(t): 15 | 16 | if t>0: 17 | return -par.A_plus*np.exp(-float(t)/par.tau_plus) 18 | if t<=0: 19 | return par.A_minus*np.exp(float(t)/par.tau_minus) 20 | 21 | 22 | #STDP weight update rule 23 | def update(w, del_w): 24 | if del_w<0: 25 | return w + par.sigma*del_w*(w-abs(par.w_min))*par.scale 26 | elif del_w>0: 27 | return w + par.sigma*del_w*(par.w_max-w)*par.scale 28 | 29 | if __name__ == '__main__': 30 | 31 | print(rl(-20)*par.sigma) 32 | 33 | -------------------------------------------------------------------------------- /training/rl.py: -------------------------------------------------------------------------------- 1 | ########################################################## README ########################################################### 2 | 3 | # This file implements STDP curve and weight update rule 4 | 5 | ############################################################################################################################## 6 | 7 | 8 | 9 | import numpy as np 10 | from matplotlib import pyplot as plt 11 | from parameters import param as par 12 | 13 | #STDP reinforcement learning curve 14 | def rl(t): 15 | 16 | if t>0: 17 | return -par.A_plus*np.exp(-float(t)/par.tau_plus) 18 | if t<=0: 19 | return par.A_minus*np.exp(float(t)/par.tau_minus) 20 | 21 | 22 | #STDP weight update rule 23 | def update(w, del_w): 24 | if del_w<0: 25 | return w + par.sigma*del_w*(w-abs(par.w_min))*par.scale 26 | elif del_w>0: 27 | return w + par.sigma*del_w*(par.w_max-w)*par.scale 28 | 29 | if __name__ == '__main__': 30 | 31 | print rl(-20)*par.sigma 32 | 33 | -------------------------------------------------------------------------------- /training/neuron.py: -------------------------------------------------------------------------------- 1 | ############################################################ README ############################################################## 2 | 3 | # This is neuron class which defines the dynamics of a neuron. All the parameters are initialised and methods are included to check 4 | # for spikes and apply lateral inhibition. 5 | 6 | ################################################################################################################################### 7 | 8 | import numpy as np 9 | import random 10 | from matplotlib import pyplot as plt 11 | from parameters import param as par 12 | 13 | class neuron: 14 | def __init__(self): 15 | self.t_ref = 30 16 | self.t_rest = -1 17 | self.P = par.Prest 18 | def check(self): 19 | if self.P>= self.Pth: 20 | self.P = par.Prest 21 | return 1 22 | elif self.P < par.Pmin: 23 | self.P = par.Prest 24 | return 0 25 | else: 26 | return 0 27 | def inhibit(self): 28 | self.P = par.Pmin 29 | def initial(self, th): 30 | self.Pth = th 31 | self.t_rest = -1 32 | self.P = par.Prest -------------------------------------------------------------------------------- /multi_layer/neuron.py: -------------------------------------------------------------------------------- 1 | ############################################################ README ############################################################## 2 | 3 | # This is neuron class which defines the dynamics of a neuron. All the parameters are initialised and methods are included to check 4 | # for spikes and apply lateral inhibition. 5 | 6 | ################################################################################################################################### 7 | 8 | import numpy as np 9 | import random 10 | from matplotlib import pyplot as plt 11 | from parameters import param as par 12 | 13 | class neuron: 14 | def __init__(self): 15 | self.t_ref = 30 16 | self.t_rest = -1 17 | self.P = par.Prest 18 | def check(self): 19 | if self.P> self.Pth: 20 | self.P = par.Prest 21 | return 1 22 | elif self.P < par.Pmin: 23 | self.P = par.Prest 24 | return 0 25 | else: 26 | return 0 27 | def inhibit(self): 28 | self.P = par.Pmin 29 | def initial(self, th): 30 | self.Pth = th 31 | self.t_rest = -1 32 | self.P = par.Prest 33 | -------------------------------------------------------------------------------- /temp-snn/snn/parameters.py: -------------------------------------------------------------------------------- 1 | ################################################ README ######################################################### 2 | 3 | # This file contains all the parameters of the network. 4 | 5 | ################################################################################################################# 6 | 7 | class param: 8 | scale = 1 9 | T = 150 10 | t_back = -20 11 | t_fore = 20 12 | 13 | #pixel_x = 28 14 | pixel_x = 16 15 | m = pixel_x*pixel_x #Number of neurons in first layer 16 | n = 3 #Number of neurons in second layer 17 | Pref = 0. 18 | Prest = 0. 19 | Pmin = -5.0*scale 20 | Pth = 50.0*scale 21 | D = 0.75*scale 22 | 23 | w_max = 2.0*scale 24 | w_min = -1.2*scale 25 | sigma = 0.02 #0.02 26 | A_plus = 0.8 # time difference is positive i.e negative reinforcement 27 | A_minus = 0.3 # 0.01 # time difference is negative i.e positive reinforcement 28 | tau_plus = 10 29 | tau_minus = 10 30 | 31 | epoch = 20 32 | 33 | 34 | fr_bits = 12 35 | int_bits = 12 36 | -------------------------------------------------------------------------------- /classification/neuron.py: -------------------------------------------------------------------------------- 1 | ########################### README ############################################# 2 | # This is a neuron class which holds all the parameters and functions associated 3 | # with a neuron of the network. 4 | ################################################################################ 5 | 6 | import numpy as np 7 | 8 | global Pref, Pmin, Pth, D, Prest 9 | Pref = 0 10 | Prest = 0 11 | Pmin = -1 12 | Pth = 140 #Should be Pth = 6 for deterministic spike train 13 | D = 0.5 14 | 15 | class neuron: 16 | def __init__(self): 17 | self.Pth = Pth 18 | self.t_ref = 4 19 | self.t_rest = -1 20 | self.P = Prest 21 | self.D = D 22 | self.Pmin = Pmin 23 | self.Prest = Prest 24 | #Check if membrane potential has crossed the thresold value 25 | def check(self): 26 | if self.P>= Pth: 27 | self.P = Pref 28 | return 1 29 | elif self.P < Pmin: 30 | self.P = Prest 31 | return 0 32 | else: 33 | return 0 34 | #Lateral Inhibition 35 | def inhibit(self): 36 | self.P = Pmin 37 | def initial(self): 38 | self.t_rest = -1 39 | self.P = Prest 40 | -------------------------------------------------------------------------------- /neuron/neuron.py: -------------------------------------------------------------------------------- 1 | from numpy import * 2 | import random 3 | from matplotlib import pyplot as plt 4 | 5 | #defining time scale 6 | T = 50; 7 | dt = 0.125; 8 | time = arange(0, T+dt, dt) 9 | 10 | #generating random spike train to be fed to neuron 11 | S = [] 12 | for k in range(len(time)): 13 | a = random.randrange(0,2) 14 | S.append(a) 15 | 16 | #initialising membrane potential vector 17 | Pn = zeros(len(time)) 18 | 19 | #definig other parameters 20 | Pref = 0 #resting potential 21 | Pmin = -1 #minimum potential 22 | Pth = 25 #threshold 23 | D = 0.25 #leakage factor 24 | Pspike = 4 #spike potential 25 | 26 | count = 0 #refractory counter 27 | t_ref = 5 #refractory period 28 | t_rest = 0 29 | 30 | #updating membrane potential according to simplified equations 31 | for i, t in enumerate(time): 32 | if i==0: 33 | Pn[i] = S[i] - D 34 | else: 35 | if t<=t_rest: 36 | Pn[i] = Pref 37 | elif t>t_rest: 38 | if Pn[i-1]>Pmin: 39 | Pn[i] = Pn[i-1] + S[i] - D 40 | else: 41 | Pn[i] = 0 42 | if Pn[i]>=Pth: 43 | Pn[i] += Pspike 44 | t_rest = t + t_ref 45 | 46 | 47 | -------------------------------------------------------------------------------- /receptive_field/receptive_field.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | 4 | #Initializing a random input matrix. During implementation this would be a 16x16 image 5 | inp = np.random.randint(0, 255, size=(16,16)) 6 | 7 | w = np.zeros([5,5]) #5x5 window initialized with zeros 8 | pot = np.zeros([16,16]) #Initializing membrane potential matrix for 256 input neurons 9 | ran = [-2,-1,0,1,2] #Weight vectors for the window 10 | ox = 2 #Origin x coordinate in the window matrix 11 | oy = 2 #Origin y coordinate in the window matrix 12 | w[ox][oy] = 1 #Manhattan distance zero 13 | 14 | #Assigning weights to matrix elements according to mahanttan distance from the origin 15 | for i in range(5): 16 | for j in range(5): 17 | d = abs(ox-i) + abs(oy-j) 18 | w[i][j] = (-0.375)*d + 1 19 | 20 | #Calculating membrane potential for each of the 256 input neurons 21 | for i in range(16): 22 | for j in range(16): 23 | summ = 0 24 | for m in ran: 25 | for n in ran: 26 | if (i+m)>=0 and (i+m)<=15 and (j+n)>=0 and (j+n)<=15: 27 | summ = summ + w[ox+m][oy+n]*inp[i+m][j+n] 28 | pot[i][j] = summ 29 | -------------------------------------------------------------------------------- /neuron/README.md: -------------------------------------------------------------------------------- 1 | ## Neuron 2 | Neuron is the basic building block of an SNN and several interconnected neurons form the input, hidden and output layers. This neuron imitates the general [integrate and fire model] (http://neuronaldynamics.epfl.ch/online/Ch1.S3.html). 3 | * In the absence of stimulus, the membrane possesses a resting potential. Every input spike from connected neurons increases or decrease its membrane potential. 4 | * When the potential crosses a threshold value, neuron enters into refractory period in which no new input is allowed and the potential remains constant. 5 | * To avoid strong negative polarization of membrane, its potential is limited by Pmin. 6 | * As long as Pn>Pmin, there is a constant leakage of potential. 7 | 8 | ## Graphs 9 | 10 |

11 | 12 |

13 | 14 | Above graph shows the membrane potential throughout the 50 time units (TU) as a result of input spike train. Below is the corresponding randomly generated input spike train. 15 | 16 |

17 | 18 |

19 | -------------------------------------------------------------------------------- /temp-snn/README.md: -------------------------------------------------------------------------------- 1 | # Spiking-Neural-Network 2 | 3 | This project implements a very basic SNN library that supports a very simple image classification example. 4 | 5 | While simple, the code here implements a variety of useful and important SNN functions and produces a functional classifier. 6 | 7 | To read more look at [the docs](docs/README.md). 8 | 9 | ## Running 10 | * cd into the root where the project is cloned 11 | * run classification/classify.py 12 | 13 | it will use pregenerated weights and output the results of classifying the test images 14 | 15 | You can also run training/learning.py to generate a new weights file and output `neuron[1-3].png` 16 | which will show the reconstructed weights. One neuron should look random, the other two will produce 17 | a pattern similar to the O, and another a pattern similar to the X. 18 | 19 | To use the new weights, the weights for the X must be on the first line of weights.txt and 20 | the weights for the O on the second line. When the weights are generated the first line of weights_training.txt 21 | will corrispond to the weights shown in neuron1.png, the second line to neuron2.png and so 22 | on, reorder them if needed. -------------------------------------------------------------------------------- /temp-snn/snn/var_th.py: -------------------------------------------------------------------------------- 1 | ############################################## README ################################################# 2 | 3 | # This calculates threshold for an image depending upon its spiking activity. 4 | 5 | ######################################################################################################## 6 | 7 | 8 | import numpy as np 9 | from neuron import neuron 10 | import random 11 | from matplotlib import pyplot as plt 12 | from recep_field import rf 13 | from spike_train import encode 14 | from rl import rl 15 | from rl import update 16 | from reconstruct import reconst_weights 17 | from parameters import param as par 18 | import os 19 | 20 | 21 | def threshold(train): 22 | 23 | tu = np.shape(train[0])[0] 24 | thresh = 0 25 | for i in range(tu): 26 | simul_active = sum(train[:,i]) 27 | if simul_active>thresh: 28 | thresh = simul_active 29 | 30 | return (thresh/3)*par.scale 31 | 32 | 33 | if __name__ == '__main__': 34 | 35 | # img = cv2.imread("mnist1/" + str(1) + ".png", 0) 36 | img = np.array(Image.open("mnist1/" + str(1) + ".png", 0)) 37 | print(img) 38 | # pot = rf(img) 39 | # train = np.array(encode(pot)) 40 | # print threshold(train) -------------------------------------------------------------------------------- /training/var_th.py: -------------------------------------------------------------------------------- 1 | ############################################## README ################################################# 2 | 3 | # This calculates threshold for an image depending upon its spiking activity. 4 | 5 | ######################################################################################################## 6 | 7 | 8 | import numpy as np 9 | from neuron import neuron 10 | import random 11 | from matplotlib import pyplot as plt 12 | from recep_field import rf 13 | import cv2 14 | from spike_train import encode 15 | from rl import rl 16 | from rl import update 17 | from reconstruct import reconst_weights 18 | from parameters import param as par 19 | import os 20 | 21 | 22 | def threshold(train): 23 | 24 | tu = np.shape(train[0])[0] 25 | thresh = 0 26 | for i in range(tu): 27 | simul_active = sum(train[:,i]) 28 | if simul_active>thresh: 29 | thresh = simul_active 30 | 31 | return (thresh/3)*par.scale 32 | 33 | 34 | if __name__ == '__main__': 35 | 36 | # img = cv2.imread("mnist1/" + str(1) + ".png", 0) 37 | img = np.array(Image.open("mnist1/" + str(1) + ".png", 0)) 38 | print img 39 | # pot = rf(img) 40 | # train = np.array(encode(pot)) 41 | # print threshold(train) -------------------------------------------------------------------------------- /temp-snn/snn/neuron.py: -------------------------------------------------------------------------------- 1 | ############################################################ README ############################################################## 2 | 3 | # This is neuron class which defines the dynamics of a neuron. All the parameters are initialised and methods are included to check 4 | # for spikes and apply lateral inhibition. 5 | 6 | ################################################################################################################################### 7 | 8 | import numpy as np 9 | import random 10 | from matplotlib import pyplot as plt 11 | from parameters import param as par 12 | 13 | class neuron: 14 | def __init__(self): 15 | self.t_ref = 30 16 | self.t_rest = -1 17 | self.P = par.Prest 18 | self.Prest = par.Prest 19 | def check(self): 20 | if self.P>= self.Pth: 21 | self.P = self.Prest 22 | return 1 23 | elif self.P < par.Pmin: 24 | self.P = par.Prest 25 | return 0 26 | else: 27 | return 0 28 | def inhibit(self): 29 | self.P = par.Pmin 30 | def initial(self, th): 31 | self.Pth = th 32 | self.t_rest = -1 33 | self.P = par.Prest -------------------------------------------------------------------------------- /temp-snn/snn/weight_initialization.py: -------------------------------------------------------------------------------- 1 | # Read and return the weights produced by training.py for the X matching synapses 2 | def learned_weights_x(): 3 | ans = [] 4 | with open('weights.txt', 'r') as weight_file: 5 | lines = weight_file.readlines() 6 | for i in lines[0].split('\t'): 7 | ans.append(float(i)) 8 | return ans 9 | 10 | # Read and return the weights produced by training.py for the O matching synapses 11 | def learned_weights_o(): 12 | ans = [] 13 | 14 | with open('weights.txt', 'r') as weight_file: 15 | lines = weight_file.readlines() 16 | for i in lines[1].split('\t'): 17 | ans.append(float(i)) 18 | return ans 19 | 20 | def learned_weights_synapse(id): 21 | ans = [] 22 | with open('weights.txt', 'r') as weight_file: 23 | lines = weight_file.readlines() 24 | if (len(lines) <= id): 25 | return ans 26 | for i in lines[id].split('\t'): 27 | ans.append(float(i)) 28 | return ans 29 | 30 | # Just show that we read the weights and processed them into a sequence to feed to the classification 31 | if __name__ == '__main__': 32 | a = learned_weights_x() 33 | print(a) 34 | -------------------------------------------------------------------------------- /temp-snn/classification/weight_initialization.py: -------------------------------------------------------------------------------- 1 | # Read and return the weights produced by training.py for the X matching synapses 2 | def learned_weights_x(): 3 | ans = [] 4 | with open('weights.txt', 'r') as weight_file: 5 | lines = weight_file.readlines() 6 | for i in lines[0].split('\t'): 7 | ans.append(float(i)) 8 | return ans 9 | 10 | # Read and return the weights produced by training.py for the O matching synapses 11 | def learned_weights_o(): 12 | ans = [] 13 | 14 | with open('weights.txt', 'r') as weight_file: 15 | lines = weight_file.readlines() 16 | for i in lines[1].split('\t'): 17 | ans.append(float(i)) 18 | return ans 19 | 20 | def learned_weights_synapse(id): 21 | ans = [] 22 | with open('weights.txt', 'r') as weight_file: 23 | lines = weight_file.readlines() 24 | if (len(lines) <= id): 25 | return ans 26 | for i in lines[id].split('\t'): 27 | ans.append(float(i)) 28 | return ans 29 | 30 | # Just show that we read the weights and processed them into a sequence to feed to the classification 31 | if __name__ == '__main__': 32 | a = learned_weights_x() 33 | print(a) 34 | -------------------------------------------------------------------------------- /multi_layer/var_th.py: -------------------------------------------------------------------------------- 1 | ############################################## README ################################################# 2 | 3 | # This calculates threshold for an image depending upon its spiking activity. 4 | 5 | ######################################################################################################## 6 | 7 | 8 | import numpy as np 9 | from neuron import neuron 10 | import random 11 | from matplotlib import pyplot as plt 12 | from recep_field import rf 13 | import cv2 14 | from spike_train import encode 15 | from rl import rl 16 | from rl import update 17 | from reconstruct import reconst_weights 18 | from parameters import param as par 19 | import os 20 | 21 | 22 | def threshold(train): 23 | 24 | tu = np.shape(train[0])[0] 25 | thresh = float(0) 26 | for i in range(tu): 27 | simul_active = float(sum(train[:,i])) 28 | if simul_active>thresh: 29 | thresh = simul_active 30 | 31 | #return (thresh/3)*par.scale 32 | return (thresh/10)*par.scale 33 | 34 | 35 | if __name__ == '__main__': 36 | 37 | # img = cv2.imread("mnist1/" + str(1) + ".png", 0) 38 | img = np.array(Image.open("mnist1/" + str(1) + ".png", 0)) 39 | print img 40 | # pot = rf(img) 41 | # train = np.array(encode(pot)) 42 | # print threshold(train) 43 | -------------------------------------------------------------------------------- /temp-snn/recreate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import math 6 | import snn 7 | import imageio 8 | from snn.parameters import param as par 9 | from snn.recep_field import rf 10 | from snn.spike_train import encode, encode2 11 | 12 | img = imageio.imread("/Users/johnsoni/Downloads/mnist_png/training/5/0.png") 13 | #img = imageio.imread("data/training/0.png") 14 | 15 | pot = rf(img) 16 | 17 | # for i in pot: 18 | # m.append(max(i)) 19 | # n.append(min(i)) 20 | 21 | # print max(m), min(n) 22 | #train = encode2(img) 23 | train = encode(pot) 24 | f = open('train6.txt', 'w') 25 | print(np.shape(train)) 26 | 27 | for j in range(len(train)): 28 | for i in range(len(train[j])): 29 | f.write(str(int(train[j][i]))) 30 | f.write('\n') 31 | 32 | f.close() 33 | 34 | with open('train6.txt','r') as f: 35 | pixels = [] 36 | for line in f: 37 | sum = 0 38 | for i in line: 39 | if i == '1': 40 | sum += 1 41 | pixels.append(sum) 42 | pixels = np.array(pixels, dtype='float64') 43 | pixels = pixels/np.max(pixels) * 255.0 44 | dim = int(math.sqrt(len(pixels))) 45 | img = np.array(pixels, dtype='uint8').reshape((dim, dim)) 46 | plt.imshow(img) 47 | plt.show() 48 | 49 | -------------------------------------------------------------------------------- /encoding/README.md: -------------------------------------------------------------------------------- 1 | # Generating Spike Trains 2 | 3 | Input neuron layer has to be fed with the stimulus caused by its receptive field. Stimulus calculated from sliding window is an analog value and has to be converted into a spike train so that neuron can understand it. This encoder serves as an interface between numerical data (from the physical world, digital simulations, etc) and SNNs. It makes the conversion of information to artificial neuron spikes. The type of encoding adopted here is **rate coding**. It suggests that the information is carried by the firing rate of the neuron. Hence, spike train generated has frequency proportional to the corresponding membrane potential. 4 | 5 | The average firing rate of retinal ganglion neurons lies between 1-200 Hz therefore the potential is scaled accordingly. Here are some examples of varying firing rates 6 |

7 | 8 |

9 |

10 | 11 |

12 |

13 | 14 |

15 | 16 | The image used as the input is the one from the [semeion dataset] (https://archive.ics.uci.edu/ml/machine-learning-databases/semeion/) of handwritten integers 17 |

18 | 19 |

20 | -------------------------------------------------------------------------------- /encoding/spike_train.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import math 4 | from matplotlib import pyplot as plt 5 | 6 | # Sliding window implementation of receptive field 7 | w = np.zeros([5,5]) 8 | pot = np.zeros([16,16]) 9 | ran = [-2,-1,0,1,2] 10 | ox = 2 11 | oy = 2 12 | w[ox][oy] = 1 13 | 14 | for i in range(5): 15 | for j in range(5): 16 | d = abs(ox-i) + abs(oy-j) 17 | w[i][j] = (-0.375)*d + 1 18 | 19 | #reading dataset image (16x16) 20 | img = cv2.imread('1.png', 0) 21 | 22 | #calculating potential map of the image (256 input neuron potential) 23 | for i in range(16): 24 | for j in range(16): 25 | summ = 0 26 | for m in ran: 27 | for n in ran: 28 | if (i+m)>=0 and (i+m)<=15 and (j+n)>=0 and (j+n)<=15: 29 | summ = summ + w[ox+m][oy+n]*img[i+m][j+n] 30 | pot[i][j] = summ 31 | 32 | #defining time frame of 1s with steps of 5ms 33 | T = 1; 34 | dt = 0.005 35 | time = np.arange(0, T+dt, dt) 36 | 37 | #initializing spike train 38 | train = [] 39 | 40 | for l in range(16): 41 | for m in range(16): 42 | 43 | temp = np.zeros([201,]) 44 | #calculating firing rate proportional to the membrane potential 45 | freq = math.ceil(0.102*pot[l][m] + 52.02) 46 | freq1 = math.ceil(200/freq) 47 | 48 | #generating spikes according to the firing rate 49 | k = 0 50 | while k<200: 51 | temp[k] = 1 52 | k = k + freq1 53 | train.append(temp) 54 | -------------------------------------------------------------------------------- /multi_layer/parameters.py: -------------------------------------------------------------------------------- 1 | ################################################ README ######################################################### 2 | 3 | # This file contains all the parameters of the network. 4 | 5 | ################################################################################################################# 6 | 7 | class param: 8 | scale = 1 9 | T = 200 10 | t_back = -20 11 | t_fore = 20 12 | 13 | pixel_x = 28 14 | Prest = float(0) 15 | m = pixel_x*pixel_x #Number of neurons in first layer 16 | n = 8 #Number of neurons in second layer 17 | Pmin = -500*scale 18 | # Pth = 5 19 | # D = 0.7 20 | w_max = 1.5*scale 21 | w_min = -1.2*scale 22 | sigma = 0.1 #0.02 23 | A_plus = 0.8 # time difference is positive i.e negative reinforcement 24 | A_minus = 0.3 # 0.01 # time difference is negative i.e positive reinforcement 25 | tau_plus = 8 26 | tau_minus = 5 27 | 28 | epoch = 12 29 | 30 | 31 | fr_bits = 12 32 | int_bits = 12 33 | 34 | num_layers = 2 #input layer + hidden layers + output layer 35 | num_layer_neurons = [m, n] 36 | num_layers = 3 #input layer + hidden layers + output layer 37 | num_layer_neurons = [m, 16,n] 38 | num_layers = 4 #input layer + hidden layers + output layer 39 | num_layer_neurons = [m, 64,16,n] 40 | #num_layers = 5 #input layer + hidden layers + output layer 41 | #num_layer_neurons = [m, 256,64,16,n] 42 | 43 | -------------------------------------------------------------------------------- /classification/recep_field.py: -------------------------------------------------------------------------------- 1 | ############################ README ############################################ 2 | # This file is used to apply receptive field to the image to imitate how 3 | # retinal ganglion cells perceive in real world scenario. Here 'w' is the filter 4 | # that need to be convoluted with the image. Sophisticated python libraries for 5 | # convolution can be used for optimization. 6 | ################################################################################ 7 | 8 | import numpy as np 9 | 10 | def rf(inp): 11 | w = [[-0.5,-0.125, 0.25, -0.125, -0.5 ], 12 | [-0.125 , 0.25 , 0.625 , 0.25 , -0.125], 13 | [ 0.25 ,0.625 , 1. , 0.625 , 0.25 ], 14 | [-0.125 , 0.25 , 0.625 , 0.25, -0.125], 15 | [-0.5 , -0.125 , 0.25 , -0.125 ,-0.5 ]] 16 | pot = np.zeros([28,28]) 17 | ran = [-2,-1,0,1,2] 18 | ox = 2 19 | oy = 2 20 | 21 | #Convolution 22 | for i in range(28): 23 | for j in range(28): 24 | summ = 0 25 | for m in ran: 26 | for n in ran: 27 | if (i+m)>=0 and (i+m)<=15 and (j+n)>=0 and (j+n)<=15: 28 | summ = summ + w[ox+m][oy+n]*inp[i+m][j+n]/255 29 | pot[i][j] = summ 30 | return pot 31 | 32 | # if __name__ == '__main__': 33 | 34 | # maxx = -1000 35 | # minn = 1000 36 | 37 | # for j in range(1,1500): 38 | # img = cv2.imread("images/" + str(j) + ".png", 0) 39 | # pot = rf(img) 40 | # for c in pot: 41 | # if max(c)>maxx: 42 | # maxx= max(c) 43 | # if min(c)=0 and (i+m)<=par.pixel_x-1 and (j+n)>=0 and (j+n)<=par.pixel_x-1: 38 | summ = summ + w[ox+m][oy+n]*inp[i+m][j+n]/255 39 | pot[i][j] = summ 40 | return pot 41 | 42 | if __name__ == '__main__': 43 | 44 | img = cv2.imread("mnist1/" + str(1) + ".png", 0) 45 | pot = rf(img) 46 | max_a = [] 47 | min_a = [] 48 | for i in pot: 49 | max_a.append(max(i)) 50 | min_a.append(min(i)) 51 | print "max", max(max_a) 52 | print "min", min(min_a) -------------------------------------------------------------------------------- /multi_layer/recep_field.py: -------------------------------------------------------------------------------- 1 | ####################################################### README ######################################################### 2 | 3 | # This file consists of function that convolves an image with a receptive field so that input to the network is 4 | # close to the form perceived by our eyes. 5 | 6 | ######################################################################################################################### 7 | 8 | 9 | import numpy as np 10 | import cv2 11 | from parameters import param as par 12 | 13 | def rf(inp): 14 | sca1 = 0.625 15 | sca2 = 0.125 16 | sca3 = -0.125 17 | sca4 = -.5 18 | 19 | #Receptive field kernel 20 | w = [[ sca4 ,sca3 , sca2 ,sca3 ,sca4], 21 | [ sca3 ,sca2 , sca1 ,sca2 ,sca3], 22 | [ sca2 ,sca1 , 1 ,sca1 ,sca2], 23 | [ sca3 ,sca2 , sca1 ,sca2 ,sca3], 24 | [ sca4 ,sca3 , sca2 ,sca3 ,sca4]] 25 | 26 | pot = np.zeros([par.pixel_x,par.pixel_x]) 27 | ran = [-2,-1,0,1,2] 28 | ox = 2 29 | oy = 2 30 | 31 | #Convolution 32 | for i in range(par.pixel_x): 33 | for j in range(par.pixel_x): 34 | summ = 0 35 | for m in ran: 36 | for n in ran: 37 | if (i+m)>=0 and (i+m)<=par.pixel_x-1 and (j+n)>=0 and (j+n)<=par.pixel_x-1: 38 | summ = summ + w[ox+m][oy+n]*inp[i+m][j+n]/255 39 | pot[i][j] = summ 40 | return pot 41 | 42 | if __name__ == '__main__': 43 | 44 | img = cv2.imread("mnist1/" + str(1) + ".png", 0) 45 | pot = rf(img) 46 | max_a = [] 47 | min_a = [] 48 | for i in pot: 49 | max_a.append(max(i)) 50 | min_a.append(min(i)) 51 | print "max", max(max_a) 52 | print "min", min(min_a) -------------------------------------------------------------------------------- /temp-snn/snn/reconstruct.py: -------------------------------------------------------------------------------- 1 | ###################################################### README ##################################################### 2 | 3 | # This file is used to leverage the generative property of a Spiking Neural Network. reconst_weights function is used 4 | # for that purpose. Looking at the reconstructed images helps to analyse training process. 5 | 6 | #################################################################################################################### 7 | 8 | 9 | import numpy as np 10 | from numpy import interp 11 | import imageio 12 | from recep_field import rf 13 | from parameters import param as par 14 | 15 | 16 | def reconst_weights(weights, num): 17 | weights = np.array(weights) 18 | weights = np.reshape(weights, (par.pixel_x,par.pixel_x)) 19 | img = np.zeros((par.pixel_x,par.pixel_x)) 20 | for i in range(par.pixel_x): 21 | for j in range(par.pixel_x): 22 | img[i][j] = int(interp(weights[i][j], [par.w_min,par.w_max], [0,255])) 23 | 24 | imageio.imwrite('neuron' + str(num) + '.png', img) 25 | return img 26 | 27 | def reconst_rf(weights, num): 28 | weights = np.array(weights) 29 | weights = np.reshape(weights, (par.pixel_x,par.pixel_x)) 30 | img = np.zeros((par.pixel_x,par.pixel_x)) 31 | for i in range(par.pixel_x): 32 | for j in range(par.pixel_x): 33 | img[i][j] = int(interp(weights[i][j], [-2,3.625], [0,255])) 34 | 35 | imageio.imwrite('neuron' + str(num) + '.png', img) 36 | return img 37 | 38 | 39 | if __name__ == '__main__': 40 | 41 | img = imageio.imread("images2/" + "69" + ".png") 42 | pot = rf(img) 43 | reconst_rf(pot, 12) -------------------------------------------------------------------------------- /multi_layer/reconstruct.py: -------------------------------------------------------------------------------- 1 | # uncompyle6 version 3.2.4 2 | # Python bytecode 2.7 (62211) 3 | # Decompiled from: Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 4 | # [GCC 7.3.0] 5 | # Embedded file name: /home/vonfaust/data/snn/codebase/python_imple/training/reconstruct.py 6 | # Compiled at: 2018-12-30 05:37:08 7 | import numpy as np 8 | from numpy import interp 9 | import cv2 10 | from recep_field import rf 11 | from parameters import param as par 12 | 13 | def reconst_weights(weights, num, layer, reshape_x, reshape_y): 14 | weights = np.array(weights) 15 | weights = np.reshape(weights, (reshape_x, reshape_y)) 16 | img = np.zeros((reshape_x, reshape_y)) 17 | for i in range(reshape_x): 18 | for j in range(reshape_y): 19 | img[i][j] = int(interp(weights[i][j], [par.w_min, par.w_max], [0, 255])) 20 | 21 | img = np.resize(img, (28, 28)) 22 | cv2.imwrite('weights/layer_' + str(layer) + '_neuron_' + str(num) + '.png', img) 23 | return img 24 | 25 | 26 | def reconst_rf(weights, num): 27 | weights = np.array(weights) 28 | weights = np.reshape(weights, (par.pixel_x, par.pixel_x)) 29 | img = np.zeros((par.pixel_x, par.pixel_x)) 30 | for i in range(par.pixel_x): 31 | for j in range(par.pixel_x): 32 | img[i][j] = int(interp(weights[i][j], [-2, 3.625], [0, 255])) 33 | 34 | cv2.imwrite('neuron' + str(num) + '.png', img) 35 | return img 36 | 37 | 38 | if __name__ == '__main__': 39 | img = cv2.imread('images2/69.png', 0) 40 | pot = rf(img) 41 | reconst_rf(pot, 12) 42 | # okay decompiling reconstruct.pyc 43 | -------------------------------------------------------------------------------- /temp-snn/snn/recep_field.py: -------------------------------------------------------------------------------- 1 | ####################################################### README ######################################################### 2 | 3 | # This file consists of function that convolves an image with a receptive field so that input to the network is 4 | # close to the form perceived by our eyes. 5 | 6 | ######################################################################################################################### 7 | 8 | 9 | import numpy as np 10 | import imageio 11 | from parameters import param as par 12 | 13 | def rf(inp): 14 | sca1 = 0.625 15 | sca2 = 0.125 16 | sca3 = -0.125 17 | sca4 = -.5 18 | 19 | #Receptive field kernel 20 | w = [[ sca4 ,sca3 , sca2 ,sca3 ,sca4], 21 | [ sca3 ,sca2 , sca1 ,sca2 ,sca3], 22 | [ sca2 ,sca1 , 1 ,sca1 ,sca2], 23 | [ sca3 ,sca2 , sca1 ,sca2 ,sca3], 24 | [ sca4 ,sca3 , sca2 ,sca3 ,sca4]] 25 | 26 | pot = np.zeros([inp.shape[0],inp.shape[1]]) 27 | ran = [-2,-1,0,1,2] 28 | ox = 2 29 | oy = 2 30 | 31 | #Convolution 32 | for i in range(inp.shape[0]): 33 | for j in range(inp.shape[1]): 34 | summ = 0 35 | for m in ran: 36 | for n in ran: 37 | if (i+m)>=0 and (i+m)<=inp.shape[0]-1 and (j+n)>=0 and (j+n)<=inp.shape[0]-1: 38 | summ = summ + w[ox+m][oy+n] * inp[i+m][j+n]/255 39 | pot[i][j] = summ 40 | return pot 41 | 42 | if __name__ == '__main__': 43 | img = imageio.imread("images/" + str(100) + ".png") 44 | pot = rf(img) 45 | max_a = [] 46 | min_a = [] 47 | for i in pot: 48 | max_a.append(max(i)) 49 | min_a.append(min(i)) 50 | for i in range(16): 51 | temp = '' 52 | for j in pot[i]: 53 | temp += '%02d ' % int(j) 54 | print(temp) 55 | print("max", max(max_a)) 56 | print("min", min(min_a)) -------------------------------------------------------------------------------- /training/spike_train.py: -------------------------------------------------------------------------------- 1 | ######################################################## README ############################################################# 2 | 3 | # This file generates rate based spike train from the potential map. 4 | 5 | ############################################################################################################################ 6 | 7 | 8 | import numpy as np 9 | from numpy import interp 10 | from neuron import neuron 11 | import random 12 | from matplotlib import pyplot as plt 13 | from recep_field import rf 14 | import cv2 15 | from rl import rl 16 | from rl import update 17 | import math 18 | from parameters import param as par 19 | 20 | def encode(pot): 21 | 22 | #initializing spike train 23 | train = [] 24 | 25 | for l in range(par.pixel_x): 26 | for m in range(par.pixel_x): 27 | 28 | temp = np.zeros([(par.T+1),]) 29 | 30 | #calculating firing rate proportional to the membrane potential 31 | freq = interp(pot[l][m], [-1.069,2.781], [1,20]) 32 | 33 | # print freq 34 | if freq<=0: 35 | print error 36 | 37 | freq1 = math.ceil(600/freq) 38 | 39 | #generating spikes according to the firing rate 40 | k = freq1 41 | if(pot[l][m]>0): 42 | while k<(par.T+1): 43 | temp[k] = 1 44 | k = k + freq1 45 | train.append(temp) 46 | # print sum(temp) 47 | return train 48 | 49 | if __name__ == '__main__': 50 | # m = [] 51 | # n = [] 52 | img = cv2.imread("mnist1/6/" + str(15) + ".png", 0) 53 | 54 | pot = rf(img) 55 | 56 | # for i in pot: 57 | # m.append(max(i)) 58 | # n.append(min(i)) 59 | 60 | # print max(m), min(n) 61 | train = encode(pot) 62 | f = open('look_ups/train6.txt', 'w') 63 | print np.shape(train) 64 | 65 | for i in range(201): 66 | for j in range(784): 67 | f.write(str(int(train[j][i]))) 68 | f.write('\n') 69 | 70 | f.close() -------------------------------------------------------------------------------- /multi_layer/spike_train.py: -------------------------------------------------------------------------------- 1 | ######################################################## README ############################################################# 2 | 3 | # This file generates rate based spike train from the potential map. 4 | 5 | ############################################################################################################################ 6 | 7 | 8 | import numpy as np 9 | from numpy import interp 10 | from neuron import neuron 11 | import random 12 | from matplotlib import pyplot as plt 13 | from recep_field import rf 14 | import cv2 15 | from rl import rl 16 | from rl import update 17 | import math 18 | from parameters import param as par 19 | 20 | def encode(pot): 21 | 22 | #initializing spike train 23 | train = [] 24 | 25 | for l in range(par.pixel_x): 26 | for m in range(par.pixel_x): 27 | 28 | temp = np.zeros([(par.T+1),]) 29 | 30 | #calculating firing rate proportional to the membrane potential 31 | freq = interp(pot[l][m], [-1.069,2.781], [1,20]) 32 | 33 | # print freq 34 | if freq<=0: 35 | print error 36 | 37 | freq1 = math.ceil(600/freq) 38 | 39 | #generating spikes according to the firing rate 40 | k = freq1 41 | if(pot[l][m]>0): 42 | 43 | while k<(par.T+1): 44 | k = int(k) 45 | temp[k] = 1 46 | k = k + freq1 47 | 48 | train.append(temp) 49 | #print temp 50 | #print sum(temp) 51 | return train 52 | 53 | if __name__ == '__main__': 54 | # m = [] 55 | # n = [] 56 | img = cv2.imread("mnist1/6/" + str(15) + ".png", 0) 57 | 58 | pot = rf(img) 59 | 60 | # for i in pot: 61 | # m.append(max(i)) 62 | # n.append(min(i)) 63 | 64 | # print max(m), min(n) 65 | train = encode(pot) 66 | f = open('look_ups/train6.txt', 'w') 67 | print np.shape(train) 68 | 69 | for i in range(201): 70 | for j in range(784): 71 | f.write(str(int(train[j][i]))) 72 | f.write('\n') 73 | 74 | f.close() 75 | -------------------------------------------------------------------------------- /classification/spike_train.py: -------------------------------------------------------------------------------- 1 | ########################### README ############################################ 2 | # This file is used to generate spike train from potential map. There are two 3 | # methods to do so. One is deterministic where we calculate the spike frequency 4 | # directly proportional to the potential of that pixel and construct a train 5 | # with equally spaced spikes. Other one is stochastic where we calculate the 6 | # probability of the pixel to fire a spike and construct a spike train 7 | # accordingly 8 | ############################################################################### 9 | 10 | import numpy as np 11 | from numpy import interp 12 | from neuron import neuron 13 | import random 14 | from recep_field import rf 15 | import imageio 16 | import math 17 | from sklearn.preprocessing import normalize 18 | 19 | # Builds a probabilistic spike train 20 | def encode_stochastic(img): 21 | T = 200 22 | train = [] 23 | pot1 = normalize(img, norm='l2') 24 | for l in range(28): 25 | for m in range(28): 26 | temp = np.random.uniform(size=(T+1)) 27 | temp = (temp < pot1[l][m]) 28 | train.append(temp) 29 | return train 30 | 31 | def encode_deterministic(pot): 32 | #defining time frame of 1s with steps of 5ms 33 | T = 200; 34 | #initializing spike train 35 | train = [] 36 | 37 | for l in range(28): 38 | for m in range(28): 39 | temp = np.zeros([(T+1),]) 40 | #calculating firing rate proportional to the membrane potential 41 | freq = interp(pot[l][m], [-2,5], [1,20]) 42 | # print freq 43 | if freq>0: 44 | freq1 = math.ceil(T/freq) 45 | #generating spikes according to the firing rate 46 | k = freq1 47 | while k<(T+1): 48 | temp[int(k)] = 1 49 | k = k + freq1 50 | train.append(temp) 51 | # print sum(temp) 52 | return train 53 | 54 | 55 | if __name__ == '__main__': 56 | m = [] 57 | n = [] 58 | img = imageio.imread("training_images/1.png") 59 | # pot = rf(img) 60 | # train = encode_deterministic(pot) 61 | # print train 62 | # print img 63 | encode_stochastic(img) 64 | -------------------------------------------------------------------------------- /synapse/synapse.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from matplotlib import pyplot as plt 4 | 5 | #constant global parameters which are same for the whole network 6 | global Pref, Pmin, Pth, D, Pspike, time, T, dt 7 | T = 500 8 | dt = 0.125 9 | Pref = 0 10 | Pmin = -1 11 | Pth = 5 12 | D = 1 13 | Pspike = 4 14 | t_ref = 5 15 | time = np.arange(0, T+dt, dt) 16 | 17 | #neuron class which can be instantiated in the main function as many times as required. It follows the integrate and fire model. 18 | #The out function takes in the matrices of spikes and weights and returns the output train of spikes. 19 | class neuron: 20 | def __init__(self): 21 | self.t_rest = 0 22 | self.Pn = np.zeros(len(time)) 23 | self.spike = np.zeros(len(time)) 24 | def out(self,S, w): 25 | for i, t in enumerate(time): 26 | if i==0: 27 | a1 = S[:,i] 28 | self.Pn[i] = np.dot(w,a1) - D 29 | self.spike[i] = 0 30 | else: 31 | if t<=self.t_rest: 32 | self.Pn[i] = Pref 33 | self.spike[i] = 0 34 | elif t>self.t_rest: 35 | if self.Pn[i-1]>Pmin: 36 | a1 = S[:,i] 37 | self.Pn[i] = self.Pn[i-1] + np.dot(w,a1) - 0.25 38 | self.spike[i] = 0 39 | else: 40 | self.Pn[i] = 0 41 | self.spike[i] = 0 42 | if self.Pn[i]>=Pth: 43 | self.Pn[i] += Pspike 44 | self.t_rest = t + t_ref 45 | self.spike[i] = 1 46 | 47 | return self.spike 48 | 49 | 50 | if __name__=='__main__': 51 | m = 5 #Number of neurons in first layer 52 | n = 3 #Number of neurons in second layer 53 | #creating two layers of m and n neurons 54 | layer1 = [] 55 | layer2 = [] 56 | 57 | for i in range(m): 58 | a = neuron() 59 | layer1.append(a) 60 | for i in range(n): 61 | a = neuron() 62 | layer2.append(a) 63 | 64 | #initialising synapse array with random integers 65 | synapse = np.random.randint(0, 5, size=(n,m)) 66 | S_in = [] 67 | 68 | #initialising the input spike trains 69 | for l in range(m): 70 | temp = [] 71 | for k in range(len(time)): 72 | a = random.randrange(0,2) 73 | temp.append(a) 74 | S_in.append(temp) 75 | 76 | #output of the first layer 77 | out_l1 = [] 78 | w_in = np.eye(m) 79 | S_in = np.array(S_in) 80 | for l in range(m): 81 | temp = [] 82 | temp = layer1[l].out(S_in,w_in[l]) 83 | out_l1.append(temp) 84 | out_l1 = np.array(out_l1) 85 | 86 | #output of the second layer which was fed with the output of the first layer 87 | out_l2 = [] 88 | for l in range(n): 89 | temp = [] 90 | temp = layer2[l].out(out_l1,synapse[l]) 91 | out_l2.append(temp) 92 | -------------------------------------------------------------------------------- /temp-snn/snn/classify.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from neuron import neuron 3 | import random 4 | from recep_field import rf 5 | from spike_train import encode 6 | from parameters import param as par 7 | from weight_initialization import learned_weights_x, learned_weights_o, learned_weights_synapse 8 | import imageio 9 | 10 | #Parameters 11 | global time, T, dt, t_back, t_fore, w_min 12 | time = np.arange(1, par.T+1, 1) 13 | 14 | layer2 = [] 15 | 16 | # creating the hidden layer of neurons 17 | for i in range(par.n): 18 | a = neuron() 19 | layer2.append(a) 20 | 21 | #synapse matrix 22 | synapse = np.zeros((par.n,par.m)) 23 | 24 | #learned weights 25 | synapse[0] = learned_weights_x() 26 | synapse[1] = learned_weights_o() 27 | 28 | #random initialization for rest of the synapses 29 | for i in range(par.n): 30 | synapse[i] = learned_weights_synapse(i) 31 | #for j in range(par.m): 32 | # synapse[i][j] = random.uniform(0, 0.4*par.scale) 33 | 34 | for k in range(1): 35 | 36 | for i in range(3): 37 | spike_count = [0,0,0,0] 38 | 39 | #read the image to be classified 40 | img = imageio.imread("test/{}.png".format(i)) 41 | 42 | #initialize the potentials of output neurons 43 | for x in layer2: 44 | x.initial(par.Pth) 45 | 46 | #calculate teh membrane potentials of input neurons 47 | pot = rf(img) 48 | 49 | #generate spike trains 50 | train = np.array(encode(pot)) 51 | 52 | #flag for lateral inhibition 53 | f_spike = 0 54 | 55 | active_pot = [0,0,0,0] 56 | 57 | for t in time: 58 | for j, x in enumerate(layer2): 59 | active = [] 60 | 61 | #update potential if not in refractory period 62 | if(x.t_restpar.Prest): 65 | x.P -= par.D 66 | active_pot[j] = x.P 67 | 68 | # Lateral Inhibition 69 | if(f_spike==0): 70 | high_pot = max(active_pot) 71 | if(high_pot>par.Pth): 72 | f_spike = 1 73 | winner = np.argmax(active_pot) 74 | print(i, winner) 75 | for s in range(par.n): 76 | if(s!=winner): 77 | layer2[s].P = par.Prest 78 | 79 | #Check for spikes 80 | for j,x in enumerate(layer2): 81 | s = x.check() 82 | if(s==1): 83 | print(j, s) 84 | spike_count[j] += 1 85 | x.t_rest = t + x.t_ref 86 | print(spike_count) 87 | -------------------------------------------------------------------------------- /temp-snn/classification/classify.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from snn.neuron import neuron 3 | import random 4 | from snn.recep_field import rf 5 | from snn.spike_train import encode 6 | from snn.parameters import param as par 7 | from classification.weight_initialization import learned_weights_x, learned_weights_o, learned_weights_synapse 8 | import imageio 9 | 10 | #Parameters 11 | global time, T, dt, t_back, t_fore, w_min 12 | time = np.arange(1, par.T+1, 1) 13 | 14 | layer2 = [] 15 | 16 | # creating the hidden layer of neurons 17 | for i in range(par.n): 18 | a = neuron() 19 | layer2.append(a) 20 | 21 | #synapse matrix 22 | synapse = np.zeros((par.n,par.m)) 23 | 24 | #learned weights 25 | synapse[0] = learned_weights_x() 26 | synapse[1] = learned_weights_o() 27 | 28 | #random initialization for rest of the synapses 29 | for i in range(par.n): 30 | synapse[i] = learned_weights_synapse(i) 31 | #for j in range(par.m): 32 | # synapse[i][j] = random.uniform(0, 0.4*par.scale) 33 | 34 | for k in range(1): 35 | 36 | for i in range(3): 37 | spike_count = [0,0,0,0] 38 | 39 | #read the image to be classified 40 | img = imageio.imread("data/test/{}.png".format(i)) 41 | 42 | #initialize the potentials of output neurons 43 | for x in layer2: 44 | x.initial(par.Pth) 45 | 46 | #calculate teh membrane potentials of input neurons 47 | pot = rf(img) 48 | 49 | #generate spike trains 50 | train = np.array(encode(pot)) 51 | 52 | #flag for lateral inhibition 53 | f_spike = 0 54 | 55 | active_pot = [0,0,0,0] 56 | 57 | for t in time: 58 | for j, x in enumerate(layer2): 59 | active = [] 60 | 61 | #update potential if not in refractory period 62 | if(x.t_restpar.Prest): 65 | x.P -= par.D 66 | active_pot[j] = x.P 67 | 68 | # Lateral Inhibition 69 | if(f_spike==0): 70 | high_pot = max(active_pot) 71 | if(high_pot>par.Pth): 72 | f_spike = 1 73 | winner = np.argmax(active_pot) 74 | print(i, winner) 75 | for s in range(par.n): 76 | if(s!=winner): 77 | layer2[s].P = par.Prest 78 | 79 | #Check for spikes 80 | for j,x in enumerate(layer2): 81 | s = x.check() 82 | if(s==1): 83 | print(j, s) 84 | spike_count[j] += 1 85 | x.t_rest = t + x.t_ref 86 | print(spike_count) 87 | -------------------------------------------------------------------------------- /classification/classify.py: -------------------------------------------------------------------------------- 1 | ##################### README ################################################### 2 | # This file executes the classification algorithm over input testing images. 3 | # Winner neurons inhibit other neurons by a phenomenon called Lateral inhibition 4 | # Spike for each output neuron at each time stamp is monitored. 5 | ################################################################################ 6 | import numpy as np 7 | from neuron import neuron 8 | import random 9 | from recep_field import rf 10 | import imageio 11 | from spike_train import * 12 | from weight_initialization import learned_weights 13 | 14 | #Parameters 15 | global time, T, dt, t_back, t_fore, w_min 16 | T = 200 17 | time = np.arange(1, T+1, 1) 18 | t_back = -20 19 | t_fore = 20 20 | Pth = 150 #Should be Pth = 6 for deterministic spike train 21 | m = 784 #Number of neurons in first layer 22 | n = 8 #Number of neurons in second layer 23 | epoch = 1 24 | num_of_images = 6 25 | w_max = 0.5 26 | w_min = -0.5 27 | 28 | layer2 = [] 29 | # creating the hidden layer of neurons 30 | for i in range(n): 31 | a = neuron() 32 | layer2.append(a) 33 | 34 | #synapse matrix 35 | synapse = np.zeros((n,m)) 36 | #learned weights 37 | weight_matrix = learned_weights() 38 | for i in range (num_of_images): 39 | synapse[i] = weight_matrix[i] 40 | 41 | #random initialization for rest of the synapses 42 | for i in range(num_of_images,n): 43 | for j in range(m): 44 | synapse[i][j] = random.uniform(w_min,w_max) 45 | 46 | for k in range(epoch): 47 | for i in range(1,7): 48 | spike_count = np.zeros((n,1)) 49 | 50 | #read the image to be classified 51 | img = imageio.imread("training_images/" + str(i) + ".png") 52 | 53 | #initialize the potentials of output neurons 54 | for x in layer2: 55 | x.initial() 56 | 57 | #calculate teh membrane potentials of input neurons 58 | pot = rf(img) 59 | 60 | #generate spike trains. Select between deterministic and stochastic 61 | # train = np.array(encode_deterministic(pot)) 62 | train = np.array(encode_stochastic(img)) 63 | 64 | #flag for lateral inhibition 65 | f_spike = 0 66 | active_pot = np.zeros((n,1)) 67 | for t in time: 68 | for j, x in enumerate(layer2): 69 | active = [] 70 | 71 | #update potential if not in refractory period 72 | if(x.t_restx.Prest): 75 | x.P -= x.D 76 | active_pot[j] = x.P 77 | 78 | # Lateral Inhibition 79 | if(f_spike==0): 80 | high_pot = max(active_pot) 81 | if(high_pot>Pth): 82 | f_spike = 1 83 | winner = np.argmax(active_pot) 84 | for s in range(n): 85 | if(s!=winner): 86 | layer2[s].P = layer2[s].Pmin 87 | 88 | #Check for spikes 89 | for j,x in enumerate(layer2): 90 | s = x.check() 91 | if(s==1): 92 | spike_count[j] += 1 93 | x.t_rest = t + x.t_ref 94 | print spike_count 95 | -------------------------------------------------------------------------------- /temp-snn/snn/spike_train.py: -------------------------------------------------------------------------------- 1 | ######################################################## README ############################################################# 2 | 3 | # This file generates rate based spike train from the potential map. 4 | 5 | ############################################################################################################################ 6 | 7 | 8 | import numpy as np 9 | from numpy import interp 10 | from matplotlib import pyplot as plt 11 | import imageio 12 | import math 13 | from parameters import param as par 14 | from recep_field import rf 15 | 16 | def encode2(pixels): 17 | 18 | #initializing spike train 19 | train = [] 20 | 21 | for l in range(pixels.shape[0]): 22 | for m in range(pixels.shape[1]): 23 | 24 | temp = np.zeros([(par.T+1),]) 25 | 26 | #calculating firing rate proportional to the membrane potential 27 | freq = interp(pixels[l][m], [0, 255], [1,20]) 28 | #print(pot[l][m], freq) 29 | # print freq 30 | 31 | assert freq > 0 32 | 33 | freq1 = math.ceil(600/freq) 34 | 35 | #generating spikes according to the firing rate 36 | k = freq1 37 | if(pixels[l][m]>0): 38 | while k<(par.T+1): 39 | temp[k] = 1 40 | k = k + freq1 41 | train.append(temp) 42 | # print sum(temp) 43 | return train 44 | 45 | def encode(pot): 46 | 47 | #initializing spike train 48 | train = [] 49 | 50 | for l in range(pot.shape[0]): 51 | for m in range(pot.shape[1]): 52 | 53 | temp = np.zeros([(par.T+1),]) 54 | 55 | #calculating firing rate proportional to the membrane potential 56 | freq = interp(pot[l][m], [-1.069,2.781], [1,20]) 57 | #print(pot[l][m], freq) 58 | # print freq 59 | 60 | assert freq > 0 61 | 62 | freq1 = math.ceil(600/freq) 63 | 64 | #generating spikes according to the firing rate 65 | k = freq1 66 | if(pot[l][m]>0): 67 | while k<(par.T+1): 68 | temp[int(k)] = 1 69 | k = k + freq1 70 | train.append(temp) 71 | # print sum(temp) 72 | return train 73 | 74 | if __name__ == '__main__': 75 | # m = [] 76 | # n = [] 77 | img = imageio.imread("/Users/johnsoni/Downloads/mnist_png/training/5/0.png") 78 | #img = imageio.imread("data/training/0.png") 79 | 80 | pot = rf(img) 81 | 82 | # for i in pot: 83 | # m.append(max(i)) 84 | # n.append(min(i)) 85 | 86 | # print max(m), min(n) 87 | #train = encode2(img) 88 | train = encode(pot) 89 | f = open('train6.txt', 'w') 90 | print(np.shape(train)) 91 | 92 | for j in range(len(train)): 93 | for i in range(len(train[j])): 94 | f.write(str(int(train[j][i]))) 95 | f.write('\n') 96 | 97 | f.close() 98 | -------------------------------------------------------------------------------- /training/learning.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | ####################################################### README #################################################################### 4 | 5 | # This is the main file which calls all the functions and trains the network by updating weights 6 | 7 | 8 | ##################################################################################################################################### 9 | 10 | 11 | import numpy as np 12 | from neuron import neuron 13 | import random 14 | from matplotlib import pyplot as plt 15 | from recep_field import rf 16 | import cv2 17 | from spike_train import encode 18 | from rl import rl 19 | from rl import update 20 | from reconstruct import reconst_weights 21 | from parameters import param as par 22 | from var_th import threshold 23 | import os 24 | 25 | #potentials of output neurons 26 | pot_arrays = [] 27 | for i in range(par.n): 28 | pot_arrays.append([]) 29 | 30 | #time series 31 | time = np.arange(1, par.T+1, 1) 32 | 33 | layer2 = [] 34 | 35 | # creating the hidden layer of neurons 36 | for i in range(par.n): 37 | a = neuron() 38 | layer2.append(a) 39 | 40 | #synapse matrix initialization 41 | synapse = np.zeros((par.n,par.m)) 42 | 43 | for i in range(par.n): 44 | for j in range(par.m): 45 | synapse[i][j] = random.uniform(0,0.4*par.scale) 46 | 47 | 48 | for k in range(par.epoch): 49 | for i in range(322,323): 50 | print i," ",k 51 | img = cv2.imread("mnist1/" + str(i) + ".png", 0) 52 | 53 | #Convolving image with receptive field 54 | pot = rf(img) 55 | 56 | #Generating spike train 57 | train = np.array(encode(pot)) 58 | 59 | #calculating threshold value for the image 60 | var_threshold = threshold(train) 61 | 62 | # print var_threshold 63 | # synapse_act = np.zeros((par.n,par.m)) 64 | # var_threshold = 9 65 | # print var_threshold 66 | # var_D = (var_threshold*3)*0.07 67 | 68 | var_D = 0.15*par.scale 69 | 70 | for x in layer2: 71 | x.initial(var_threshold) 72 | 73 | #flag for lateral inhibition 74 | f_spike = 0 75 | 76 | img_win = 100 77 | 78 | active_pot = [] 79 | for index1 in range(par.n): 80 | active_pot.append(0) 81 | 82 | #Leaky integrate and fire neuron dynamics 83 | for t in time: 84 | for j, x in enumerate(layer2): 85 | active = [] 86 | if(x.t_restpar.Prest): 89 | x.P -= var_D 90 | active_pot[j] = x.P 91 | 92 | pot_arrays[j].append(x.P) 93 | 94 | # Lateral Inhibition 95 | if(f_spike==0): 96 | high_pot = max(active_pot) 97 | if(high_pot>var_threshold): 98 | f_spike = 1 99 | winner = np.argmax(active_pot) 100 | img_win = winner 101 | print "winner is " + str(winner) 102 | for s in range(par.n): 103 | if(s!=winner): 104 | layer2[s].P = par.Pmin 105 | 106 | #Check for spikes and update weights 107 | for j,x in enumerate(layer2): 108 | s = x.check() 109 | if(s==1): 110 | x.t_rest = t + x.t_ref 111 | x.P = par.Prest 112 | for h in range(par.m): 113 | for t1 in range(-2,par.t_back-1, -1): 114 | if 0<=t+t1