-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_functions.py
128 lines (90 loc) · 3.61 KB
/
init_functions.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
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
def p_matrix(X):
"""
Calculates the outer product of a function with broadcasting
"""
average = 0
for index, pattern in enumerate(X):
outter = np.outer(pattern, pattern)
average += outter
p_matrix = average * 1.0 / X.shape[0]
return p_matrix
def w_connectivity(p_vector, p_matrix, N_to_use):
"""
Constructs the matrix of connectivity w
"""
w = np.zeros_like(p_matrix)
for i in range(w.shape[0]):
for j in range(w.shape[1]):
if (p_vector[i] == 0) or (p_vector[j] == 0):
w[i, j] = 0
elif (p_matrix[i, j] == 0):
w[i, j] = 1.0 / N_to_use
else:
w[i, j] = p_matrix[i, j] / (p_vector[i] * p_vector[j])
return w
def prob(N_hypercolumns, units_per_hypercolumn, low_noise, X):
"""
Calculate probabalities for each of the units.
Returns an array of dimensions: (units_per_hypercolumn, N_hypercolumns)
Therefore the row indicates the hypercolumn and the column the unit
whitin a particular hypercolumn.
X first dimensions (rows) represents the different training examples
"""
p = np.zeros((N_hypercolumns, units_per_hypercolumn))
for i in range(units_per_hypercolumn):
p[:, i] = np.sum(X == i, axis=0)
p = p * 1.0 / X.shape[0]
p[p < low_noise] = low_noise
return p
def connectivity(N_hypercolumns, units_per_hypercolumn, X,
low_noise=10e-10, non_zero=True):
"""
Calculates the connectivity matrix for a network with
N_hypercolumns number of hypercolumns and neurons_per_hyper
neurons per hypercolumn. It returns a tensor where the first
two dimensions correspond to the hyercolumn and the last two
dimensions correspond to the neurons in the hypercolum
"""
w = np.zeros((N_hypercolumns, N_hypercolumns,
units_per_hypercolumn, units_per_hypercolumn))
if (low_noise > 1.0 / X.shape[0]):
low_noise = 1.0 / X.shape[0]
p = prob(N_hypercolumns, units_per_hypercolumn, low_noise, X)
for h_pre in range(N_hypercolumns):
for h_post in range(h_pre + 1, N_hypercolumns):
aux = hypercolumn_connection(h_pre, h_post, units_per_hypercolumn,
p, X)
# Symmetric
w[h_pre, h_post, ...] = aux
w[h_post, h_pre, ...] = aux
w = w * 1.0 / X.shape[0]
if non_zero:
w[w < low_noise] = low_noise
return w, p
def hypercolumn_connection(h_pre, h_post, units_per_hypercolumn, p, X):
"""
Creates the connection between two hypercolumns, in this case
fromm the h_pre hypercolumn to the h_post hypercolumn. Neurons
hyper is the number of features or units per hypercolum
"""
h_connectivity = np.zeros((units_per_hypercolumn, units_per_hypercolumn))
for n_i in range(units_per_hypercolumn):
for n_j in range(units_per_hypercolumn):
joint = unitary_connection(h_pre, h_post, n_i, n_j, X)
product = p[h_pre, n_i] * p[h_post, n_j]
if product == 0:
h_connectivity[n_i, n_j] = 0
elif joint == 0:
h_connectivity[n_i, n_j] = 1.0 / X.shape[0]
else:
h_connectivity[n_i, n_j] = joint / product
return h_connectivity
def unitary_connection(h_pre, h_post, n_pre, n_post, X):
"""
Gives the connectivity value between the n_pre unit
in the h_pre hypercolumn and the n_post unit in the h_post column
"""
hits_pre = X[:, h_pre] == n_pre
hits_post = X[:, h_post] == n_post
return np.sum(hits_pre * hits_post)