09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10

# Calculating Neighbourhood function

distance_square = tf.reduce_sum(tf.pow(tf.subtract(

self._topography, tf.stack([self.WTU_loc for i in range(m *

n)])), 2), 1)

neighbourhood_func = tf.exp(tf.negative(tf.math.divide(tf.cast(

distance_square, "float32"), tf.pow(_sigma_new, 2))))

# multiply learning rate with neighbourhood func

eta_into_Gamma = tf.multiply(_eta_new, neighbourhood_func)

# Shape it so that it can be multiplied to calculate dW

weight_multiplier = tf.stack([tf.tile(tf.slice(

eta_into_Gamma, np.array([i]), np.array([1])), [self.dim])

for i in range(m * n)])

delta_W = tf.multiply(weight_multiplier,

tf.subtract(tf.stack([x for i in range(m * n)]),self._W))

new_W = self._W + delta_W

self._W = new_W

The fit() function is a helper function that calls the train() function and stores the

centroid grid for easy retrieval:

def fit(self, X):

"""

Function to carry out training

"""

for i in range(self._num_iterations):

for x in X:

self.training(x,i)

# Store a centroid grid for easy retrieval

centroid_grid = [[] for i in range(self._m)]

self._Wts = list(self._W)

self._locations = list(self._topography)

for i, loc in enumerate(self._locations):

centroid_grid[loc[0]].append(self._Wts[i])

self._centroid_grid = centroid_grid

self._learned = True

[ 389 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!