-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocally_connected.py
131 lines (122 loc) · 5.53 KB
/
Locally_connected.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
129
# Author : Munch Quentin
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import numpy as np
import matplotlib.pyplot as plt
import math
# locally connected 1D layer
def Local_weight_generator_RF(input_size, output_size, RF):
input_range = 1.0 / input_size ** (1/2)
padding = ((RF - 1) // 2)
w = np.zeros(shape=(input_size + 2*padding, output_size))
M = np.zeros(shape=(input_size + 2*padding, output_size))
step = float(w.shape[0] - RF) / (output_size - 1)
for i in range(output_size):
j = int(math.ceil(i * step))
j_next = j + RF
w[j:j_next, i] = np.random.normal(loc=0, scale=input_range, size=(j_next-j))
M[j:j_next, i] = 1
weight_mat = w[padding:-padding, :]
Mask_mat = M[padding:-padding, :]
return weight_mat, Mask_mat
# locally connected 2D layer with 3x3 kernel (input/hidden with odd size only)
def L2D_weight(input_size, output_size):
# init kernel center position in the input
# kernel size = 3x3, so for each position we need to get the element :
# [[kcl,kcc)],[kcl,kcc+1)],[kcl-1,kcc+1],[kcl-1,kcc],[kcl-1,kcc-1],[kcl,kcc-1],[kcl+1,kcc-1],[kcl+1,kcc],[kcl+1,kcc+1]]
# in a case of a inhibitory matrix we have the same position except the center
kcl = 0 # line
kcc = 0 # column
# calculate input range
input_range = 1.0 / (input_size**2) ** (1/2)
# create a binary mask and weight matrix
mask = np.zeros((input_size**2, output_size**2))
weight = np.zeros((input_size**2, output_size**2))
# init hidden number value
hidden_index = 0
# sliding kernel over the dummy input + map the 2d pose to a 1D vector
# stride = 2 (kernel center to kernel center)
for u in range(0,input_size,2):
for v in range(0,input_size,2):
kcl = u
kcc = v
kernel = [[kcl,kcc],[kcl,kcc+1],[kcl-1,kcc+1],[kcl-1,kcc],[kcl-1,kcc-1],[kcl,kcc-1],[kcl+1,kcc-1],[kcl+1,kcc],[kcl+1,kcc+1]]
# add each kernel to the weight and mask matrix
for elem in range(len(kernel)):
if kernel[elem][0] > input_size-1 or kernel[elem][0] < 0 or kernel[elem][1] > input_size-1 or kernel[elem][1] < 0:
pass
else:
# calculate the position in the mask matrix
line = kernel[elem][0]
column = kernel[elem][1]
input_index = line*input_size + column
mask[input_index, hidden_index] = 1
hidden_index += 1
# fill the weight matrix with init weight
for i in range(input_size**2):
for j in range(output_size**2):
if mask[i,j] == 1:
weight[i,j] = np.random.normal(loc=0, scale=input_range)
else:
pass
return weight, mask
# locally connected recurrent unit
def LR_weight(Hidden_size):
W_rec = np.zeros((Hidden_size**2, Hidden_size**2))
Mask_rec = np.zeros((Hidden_size**2, Hidden_size**2))
for i in range(Hidden_size**2):
W_rec[i,i] = np.random.uniform(0,1)
Mask_rec[i,i] = 1
return W_rec, Mask_rec
# lateral inhibition weight matrix
def LI2D_weight(hidden_size):
# init kernel center position in the input
# kernel size = 3x3, so for each position we need to get the element :
# [[kcl,kcc+1)],[kcl-1,kcc+1],[kcl-1,kcc],[kcl-1,kcc-1],[kcl,kcc-1],[kcl+1,kcc-1],[kcl+1,kcc],[kcl+1,kcc+1]]
kcl = 0 # line
kcc = 0 # column
# calculate input range
input_range = 1.0 / (hidden_size**2) ** (1/2)
# create a binary mask and weight matrix
mask = np.zeros((hidden_size**2, hidden_size**2))
weight = np.zeros((hidden_size**2, hidden_size**2))
# init hidden number value
hidden_index = 0
# sliding kernel over the dummy input + map the 2d pose to a 1D vector
# stride = 1 (lateral connection in the hidden state)
for u in range(0,hidden_size):
for v in range(0,hidden_size):
kcl = u
kcc = v
# local inhibition
kernel = [[kcl,kcc+1],[kcl-1,kcc+1],[kcl-1,kcc],[kcl-1,kcc-1],[kcl,kcc-1],[kcl+1,kcc-1],[kcl+1,kcc],[kcl+1,kcc+1]]
# add each kernel to the weight and mask matrix
for elem in range(len(kernel)):
if kernel[elem][0] > hidden_size-1 or kernel[elem][0] < 0 or kernel[elem][1] > hidden_size-1 or kernel[elem][1] < 0:
pass
else:
# calculate the position in the mask matrix
line = kernel[elem][0]
column = kernel[elem][1]
input_index = line*hidden_size + column
mask[input_index, hidden_index] = 1
mask[hidden_index, input_index] = 1
hidden_index += 1
# fill the weight matrix with init weight
for i in range(hidden_size**2):
for j in range(hidden_size**2):
if mask[i,j] == 1:
weight[i,j] = np.random.normal(loc=0, scale=input_range)
else:
pass
return weight, mask