forked from zxzhijia/Brian2STDPMNIST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiking_MNIST_random_conn_generator.py
126 lines (91 loc) · 3.66 KB
/
spiking_MNIST_random_conn_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'''
Created on 15.12.2014
@author: Peter U. Diehl
'''
import scipy.ndimage as sp
import numpy as np
import pylab
def randomDelay(minDelay, maxDelay):
return np.random.rand()*(maxDelay-minDelay) + minDelay
def computePopVector(popArray):
size = len(popArray)
complex_unit_roots = np.array([np.exp(1j*(2*np.pi/size)*cur_pos) for cur_pos in xrange(size)])
cur_pos = (np.angle(np.sum(popArray * complex_unit_roots)) % (2*np.pi)) / (2*np.pi)
return cur_pos
def sparsenMatrix(baseMatrix, pConn):
weightMatrix = np.zeros(baseMatrix.shape)
numWeights = 0
numTargetWeights = baseMatrix.shape[0] * baseMatrix.shape[1] * pConn
weightList = [0]*int(numTargetWeights)
while numWeights < numTargetWeights:
idx = (np.int32(np.random.rand()*baseMatrix.shape[0]), np.int32(np.random.rand()*baseMatrix.shape[1]))
if not (weightMatrix[idx]):
weightMatrix[idx] = baseMatrix[idx]
weightList[numWeights] = (idx[0], idx[1], baseMatrix[idx])
numWeights += 1
return weightMatrix, weightList
def create_weights():
nInput = 784
nE = 400
nI = nE
dataPath = './random/'
weight = {}
weight['ee_input'] = 0.3
weight['ei_input'] = 0.2
weight['ee'] = 0.1
weight['ei'] = 10.4
weight['ie'] = 17.0
weight['ii'] = 0.4
pConn = {}
pConn['ee_input'] = 1.0
pConn['ei_input'] = 0.1
pConn['ee'] = 1.0
pConn['ei'] = 0.0025
pConn['ie'] = 0.9
pConn['ii'] = 0.1
print('create random connection matrices')
connNameList = ['XeAe']
for name in connNameList:
weightMatrix = np.random.random((nInput, nE)) + 0.01
weightMatrix *= weight['ee_input']
if pConn['ee_input'] < 1.0:
weightMatrix, weightList = sparsenMatrix(weightMatrix, pConn['ee_input'])
else:
weightList = [(i, j, weightMatrix[i,j]) for j in range(nE) for i in range(nInput)]
np.save(dataPath+name, weightList)
print('create connection matrices from E->I which are purely random')
connNameList = ['XeAi']
for name in connNameList:
weightMatrix = np.random.random((nInput, nI))
weightMatrix *= weight['ei_input']
weightMatrix, weightList = sparsenMatrix(weightMatrix, pConn['ei_input'])
print('save connection matrix', name)
np.save(dataPath+name, weightList)
print('create connection matrices from E->I which are purely random')
connNameList = ['AeAi']
for name in connNameList:
if nE == nI:
weightList = [(i, i, weight['ei']) for i in range(nE)]
else:
weightMatrix = np.random.random((nE, nI))
weightMatrix *= weight['ei']
weightMatrix, weightList = sparsenMatrix(weightMatrix, pConn['ei'])
print('save connection matrix', name)
np.save(dataPath+name, weightList)
print('create connection matrices from I->E which are purely random')
connNameList = ['AiAe']
for name in connNameList:
if nE == nI:
weightMatrix = np.ones((nI, nE))
weightMatrix *= weight['ie']
for i in range(nI):
weightMatrix[i,i] = 0
weightList = [(i, j, weightMatrix[i,j]) for i in range(nI) for j in range(nE)]
else:
weightMatrix = np.random.random((nI, nE))
weightMatrix *= weight['ie']
weightMatrix, weightList = sparsenMatrix(weightMatrix, pConn['ie'])
print('save connection matrix', name)
np.save(dataPath+name, weightList)
if __name__ == "__main__":
create_weights()