forked from spiglerg/Kohonen_SOM_Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsom.py
184 lines (118 loc) · 6.11 KB
/
som.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import tensorflow as tf
import numpy as np
class SOM:
"""
Efficient implementation of Kohonen Self-Organizing maps using Tensorflow.
map_size_n: size of the (square) map
num_expected_iterations: number of iterations to be used during training. This parameter is used to derive good parameters for the learning rate and the neighborhood radius.
"""
def __init__(self, input_shape, map_size_n, num_expected_iterations, session):
input_shape = tuple([i for i in input_shape if i is not None])
self.input_shape = input_shape
self.sigma_act = tf.constant( 2.0*(reduce(lambda x, y:x*y, self.input_shape, 1)*0.05)**2, dtype=tf.float32 )
self.n = map_size_n
self.session = session
self.alpha = tf.constant( 0.5 )
self.timeconst_alpha = tf.constant( 2.0*num_expected_iterations/6.0) #2/6
self.sigma = tf.constant( self.n/2.0 ) #self.n/2.0
self.timeconst_sigma = tf.constant( 2.0*num_expected_iterations/5.0 ) #2/5
self.num_iterations = 0
self.num_expected_iterations = num_expected_iterations
# Pre-initialize neighborhood function's data for efficiency
self.row_indices = np.zeros((self.n, self.n))
self.col_indices = np.zeros((self.n, self.n))
for r in range(self.n):
for c in range(self.n):
self.row_indices[r, c] = r
self.col_indices[r, c] = c
self.row_indices = np.reshape(self.row_indices, [-1])
self.col_indices = np.reshape(self.col_indices, [-1])
## Compute d^2/2 for each pair of units, so that the neighborhood function can be computed as exp(-dist/sigma^2)
self.dist = np.zeros((self.n*self.n, self.n*self.n))
for i in range(self.n*self.n):
for j in range(self.n*self.n):
self.dist[i, j] = ( (self.row_indices[i]-self.row_indices[j])**2 + (self.col_indices[i]-self.col_indices[j])**2 )
self.initialize_graph()
def initialize_graph(self):
self.weights = tf.Variable( tf.random_uniform((self.n*self.n, )+self.input_shape, 0.0, 1.0) ) ##TODO: match with input type, and check that my DQN implementation actually uses values in 0-255 vs 0-1?
# 1) The first part computes the winning unit, potentially in batch mode. It only requires 'input_placeholder_' and 'current_iteration' to be filled in. This is used in get_batch_winner and get_batch_winner_activity, to be used in clustering novel vectors after training is complete.
# The batch placeholder is not used in training, where only a single vector is supported at the moment.
self.input_placeholder = tf.placeholder(tf.float32, (None,)+self.input_shape)
self.current_iteration = tf.placeholder(tf.float32)
## Compute the current iteration's neighborhood sigma and learning rate alpha:
self.sigma_tmp = self.sigma * tf.exp( - self.current_iteration/self.timeconst_sigma )
self.sigma2 = 2.0*tf.mul(self.sigma_tmp, self.sigma_tmp)
self.alpha_tmp = self.alpha * tf.exp( - self.current_iteration/self.timeconst_alpha )
self.input_placeholder_ = tf.expand_dims(self.input_placeholder, 1)
self.input_placeholder_ = tf.tile(self.input_placeholder_, (1,self.n*self.n,1) )
self.diff = self.input_placeholder_ - self.weights
self.diff_sq = tf.square(self.diff)
self.diff_sum = tf.reduce_sum( self.diff_sq, reduction_indices=range(2, 2+len(self.input_shape)) )
# Get the index of the best matching unit
self.bmu_index = tf.argmin(self.diff_sum, 1)
self.bmu_dist = tf.reduce_min(self.diff_sum, 1)
self.bmu_activity = tf.exp( -self.bmu_dist/self.sigma_act )
self.diff = tf.squeeze(self.diff)
## 2) The second part computes and applies the weight update. It requires 'diff_2' and 'dist_sliced' to be filled in. dist_sliced = self.dist[bmu_index, :]
self.diff_2 = tf.placeholder(tf.float32, (self.n*self.n,)+self.input_shape)
self.dist_sliced = tf.placeholder(tf.float32, (self.n*self.n,))
self.distances = tf.exp(-self.dist_sliced / self.sigma2 )
self.lr_times_neigh = tf.mul( self.alpha_tmp, self.distances )
for i in range(len(self.input_shape)):
self.lr_times_neigh = tf.expand_dims(self.lr_times_neigh, -1)
self.lr_times_neigh = tf.tile(self.lr_times_neigh, (1,)+self.input_shape )
self.delta_w = self.lr_times_neigh * self.diff_2
self.update_weights = tf.assign_add(self.weights, self.delta_w)
def train(self, input_x): #TODO: try training a batch all together, to optimize gpu usage?
# Compute the winning unit
bmu_index, diff = self.session.run([self.bmu_index, self.diff], {self.input_placeholder:[input_x], self.current_iteration:self.num_iterations})
# Update the network's weights
self.session.run(self.update_weights, {self.diff_2:diff, self.dist_sliced:self.dist[bmu_index[0],:], self.current_iteration:self.num_iterations})
self.num_iterations = min(self.num_iterations+1, self.num_expected_iterations)
def get_batch_winner(self, batch_input):
"""
Returns the index of the units in the network that best match each batch_input vector.
"""
indices = self.session.run([self.bmu_index], {self.input_placeholder:batch_input, self.current_iteration:self.num_iterations})
return indices
def get_batch_winner_activity(self, batch_input):
"""
Returns the activation value of the units in the network that best match each batch_input vector.
"""
activity = self.session.run([self.bmu_activity], {self.input_placeholder:batch_input, self.current_iteration:self.num_iterations})
return activity
def get_weights(self):
"""
Returns the full list of weights as [N*N, input_shape]
"""
return self.weights.eval()
## EXAMPLE : Color clustering
with tf.device("gpu:0"):
sess = tf.InteractiveSession()
num_training = 400
s = SOM( (3,), 30, num_training, sess )
sess.run(tf.initialize_all_variables())
#For plotting the images
from matplotlib import pyplot as plt
#Training inputs for RGBcolors
colors = np.array(
[[0., 0., 0.],
[0., 0., 1.],
[0., 0., 0.5],
[0.125, 0.529, 1.0],
[0.33, 0.4, 0.67],
[0.6, 0.5, 1.0],
[0., 1., 0.],
[1., 0., 0.],
[0., 1., 1.],
[1., 0., 1.],
[1., 1., 0.],
[1., 1., 1.],
[.33, .33, .33],
[.5, .5, .5],
[.66, .66, .66]])
for i in range(num_training):
rnd_ind = np.random.randint(0, len(colors))
s.train(colors[rnd_ind,:])
plt.imshow( np.reshape(s.get_weights(), [30, 30, 3]) )
plt.show()