Skip to content

Commit de3e24d

Browse files
authored
Merge pull request #9 from arpanvyas/master
Multi Layer Update
2 parents 06b247b + ed674f5 commit de3e24d

15 files changed

Lines changed: 613 additions & 0 deletions

File tree

multi_layer/learning.py

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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+
import pickle
25+
import sys
26+
27+
#@profile
28+
def learning(learning_or_classify):
29+
30+
#1 = learning, 0 = classify
31+
#learning_or_classify = 0
32+
print learning_or_classify
33+
if(learning_or_classify == 0):
34+
print "Starting classify..."
35+
elif(learning_or_classify == 1):
36+
print "Starting learning..."
37+
else:
38+
print "Error in argument, quitting"
39+
quit()
40+
41+
if(learning_or_classify == 0):
42+
par.epoch = 1
43+
44+
#potentials of output neurons
45+
pot_arrays = []
46+
pot_arrays.append([]) #because 0th layer do not require neuron model
47+
for i in range(1,par.num_layers):
48+
pot_arrays_this = []
49+
for j in range(0,par.num_layer_neurons[i]):
50+
pot_arrays_this.append([])
51+
pot_arrays.append(pot_arrays_this)
52+
print "created potential arrays for each layer..."
53+
54+
Pth_array = []
55+
Pth_array.append([]) #because 0th layer do not require neuron model
56+
for i in range(1,par.num_layers):
57+
Pth_array_this = []
58+
for j in range(0,par.num_layer_neurons[i]):
59+
Pth_array_this.append([])
60+
Pth_array.append(Pth_array_this)
61+
print "created potential threshold arrays for each layer..."
62+
63+
64+
train_all = []
65+
for i in range(0,par.num_layers):
66+
train_this = []
67+
for j in range(0,par.num_layer_neurons[i]):
68+
train_this.append([])
69+
train_all.append(train_this)
70+
print "created spike trains for each layer..."
71+
72+
#synapse matrix initialization
73+
synapse = [] #synapse[i] is the matrix for weights from layer i to layer i+1, assuming index from 0
74+
for i in range(0,par.num_layers-1):
75+
synapse_this = np.zeros((par.num_layer_neurons[i+1],par.num_layer_neurons[i]))
76+
synapse.append(synapse_this)
77+
78+
if(learning_or_classify == 1):
79+
for layer in range(0,par.num_layers-1):
80+
for i in range(par.num_layer_neurons[layer+1]):
81+
for j in range(par.num_layer_neurons[layer]):
82+
synapse[layer][i][j] = random.uniform(0,0.4*par.scale)
83+
else:
84+
for layer in range(0,par.num_layers-1):
85+
for i in range(par.num_layer_neurons[layer+1]):
86+
#for j in range(par.num_layer_neurons[layer]):
87+
filename = "weights/layer_"+str(layer)+"_neuron_"+str(i)+".dat"
88+
with open(filename,"rb") as f:
89+
synapse[layer][i] = pickle.load(f)
90+
91+
92+
print "created synapse matrices for each layer..."
93+
94+
95+
#this contains neurons of all layers except first
96+
layers = [] #layers[i] is the list of neurons from layer i, assuming index from 0
97+
layer_this = []
98+
layers.append(layer_this) #0th layer is empty as input layer do not require neuron model
99+
100+
#time series
101+
time = np.arange(1, par.T+1, 1)
102+
103+
# creating each layer of neurons
104+
for i in range(1,par.num_layers):
105+
layer_this = []
106+
for i in range(par.num_layer_neurons[i]):
107+
a = neuron()
108+
layer_this.append(a)
109+
layers.append(layer_this)
110+
print "created neuron for each layer..."
111+
112+
113+
for k in range(par.epoch):
114+
for i in range(1,7):
115+
print "Epoch: ",str(k),", Image: ", str(i)
116+
if(learning_or_classify == 1):
117+
img = cv2.imread("training_images/" + str(i) + ".png", 0)
118+
else:
119+
img = cv2.imread("training_images/" + str(i) + ".png", 0)
120+
121+
122+
#Convolving image with receptive field
123+
pot = rf(img)
124+
#print pot
125+
126+
#training layers i and i+1, assuming 0 indexing, thus n layers require n-1 pairs of training
127+
for layer in range(0,par.num_layers-1):
128+
print "Layer: ", str(layer)
129+
130+
#Generating spike train when the first layer
131+
#else take the spike train from last layer
132+
if(layer == 0):
133+
train_all[layer] = np.array(encode(pot))
134+
train = np.array(encode(pot))
135+
else:
136+
train_all[layer] = np.asarray(train_this_layer)
137+
train = np.array(np.asarray(train_this_layer))
138+
139+
#print train[1]
140+
141+
#calculating threshold value for the image
142+
var_threshold = threshold(train)
143+
#print "var_threshold is ", str(var_threshold)
144+
145+
# print var_threshold
146+
# synapse_act = np.zeros((par.n,par.m))
147+
# var_threshold = 9
148+
# print var_threshold
149+
# var_D = (var_threshold*3)*0.07
150+
151+
var_D = 0.15*par.scale
152+
153+
for x in layers[layer+1]:
154+
x.initial(var_threshold)
155+
156+
#flag for lateral inhibition
157+
f_spike = 0
158+
159+
img_win = 100
160+
161+
active_pot = []
162+
train_this_layer = []
163+
for index1 in range(par.num_layer_neurons[layer+1]):
164+
active_pot.append(0)
165+
train_this_layer.append([])
166+
167+
#print synapse[layer].shape, train.shape
168+
#Leaky integrate and fire neuron dynamics
169+
for t in time:
170+
#print "Time: ", str(t)
171+
for j, x in enumerate(layers[layer+1]):
172+
active = []
173+
if(x.t_rest<t):
174+
x.P = x.P + np.dot(synapse[layer][j], train[:,t])
175+
if(x.P>par.Prest):
176+
x.P -= var_D
177+
active_pot[j] = x.P
178+
179+
#pot_arrays[layer+1][j].append(x.P)
180+
#Pth_array[layer+1][j].append(x.Pth)
181+
182+
# Lateral Inhibition
183+
# Occurs in the training of second last and last layer
184+
#if(f_spike==0 and layer == par.num_layers - 2 and learning_or_classify == 1):
185+
if(f_spike==0 ):
186+
high_pot = max(active_pot)
187+
if(high_pot>var_threshold):
188+
f_spike = 1
189+
winner = np.argmax(active_pot)
190+
img_win = winner
191+
#print "winner is " + str(winner)
192+
for s in range(par.num_layer_neurons[layer+1]):
193+
if(s!=winner):
194+
layers[layer+1][s].P = par.Pmin
195+
196+
#Check for spikes and update weights
197+
for j,x in enumerate(layers[layer+1]):
198+
pot_arrays[layer+1][j].append(x.P)
199+
Pth_array[layer+1][j].append(x.Pth)
200+
s = x.check()
201+
train_this_layer[j].append(s)
202+
if(learning_or_classify == 1):
203+
if(s==1):
204+
x.t_rest = t + x.t_ref
205+
x.P = par.Prest
206+
for h in range(par.num_layer_neurons[layer]):
207+
208+
for t1 in range(-2,par.t_back-1, -1):
209+
if 0<=t+t1<par.T+1:
210+
if train[h][t+t1] == 1:
211+
# print "weight change by" + str(update(synapse[j][h], rl(t1)))
212+
synapse[layer][j][h] = update(synapse[layer][j][h], rl(t1))
213+
214+
215+
for t1 in range(2,par.t_fore+1, 1):
216+
if 0<=t+t1<par.T+1:
217+
if train[h][t+t1] == 1:
218+
# print "weight change by" + str(update(synapse[j][h], rl(t1)))
219+
synapse[layer][j][h] = update(synapse[layer][j][h], rl(t1))
220+
221+
for j in range(par.num_layer_neurons[layer+1]):
222+
train_this_layer[j].append(0)
223+
224+
225+
#if(img_win!=100 and layer == par.num_layers - 2 ):
226+
if(img_win!=100 ):
227+
for p in range(par.num_layer_neurons[layer]):
228+
if sum(train[p])==0:
229+
synapse[layer][img_win][p] -= 0.06*par.scale
230+
if(synapse[layer][img_win][p]<par.w_min):
231+
synapse[layer][img_win][p] = par.w_min
232+
233+
#print train_this_layer
234+
#print synapse[0][0]
235+
236+
237+
train_all[par.num_layers-1] = np.asarray(train_this_layer)
238+
239+
results_each_layer = 1
240+
if(results_each_layer):
241+
for layer in range(par.num_layers-1,par.num_layers):
242+
for i in range(par.num_layer_neurons[layer]):
243+
print "Layer"+ str(layer) + ", Neuron"+str(i+1)+": "+str(sum(train_all[layer][i]))
244+
245+
246+
#print classification results
247+
# if(learning_or_classify == 0):
248+
249+
250+
251+
252+
plot = 0
253+
if (plot == 1):
254+
for layer in range(par.num_layers-1,par.num_layers):
255+
ttt = np.arange(0,len(pot_arrays[layer][0]),1)
256+
257+
#plotting
258+
for i in range(par.num_layer_neurons[layer]):
259+
axes = plt.gca()
260+
axes.set_ylim([-20,50])
261+
plt.plot(ttt,Pth_array[layer][i], 'r' )
262+
plt.plot(ttt,pot_arrays[layer][i])
263+
plt.show()
264+
265+
#Reconstructing weights to analyse training
266+
reconst = 1
267+
if(learning_or_classify != 1):
268+
reconst = 0
269+
270+
if(reconst == 1):
271+
for layer in range(par.num_layers-1):
272+
siz_x = int(par.num_layer_neurons[layer]**(.5))
273+
siz_y = siz_x
274+
for i in range(par.num_layer_neurons[layer+1]):
275+
reconst_weights(synapse[layer][i],i+1,layer,siz_x,siz_y)
276+
277+
#Dumping trained weights of last layer to file
278+
dump = 1
279+
if(learning_or_classify != 1):
280+
dump = 0
281+
if(dump == 1):
282+
for layer in range(par.num_layers-1):
283+
for i in range(par.num_layer_neurons[layer+1]):
284+
filename = "weights/"+"layer_"+str(layer)+"_neuron_"+str(i)+".dat"
285+
with open(filename,'wb') as f:
286+
#f.write(str(synapse[layer][i]))
287+
pickle.dump(synapse[layer][i],f)
288+
289+
290+
if __name__ == '__main__':
291+
learning_or_classify = int(sys.argv[1])
292+
learning(learning_or_classify)

multi_layer/neuron.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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/parameters.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+

0 commit comments

Comments
 (0)