Skip to content

Instantly share code, notes, and snippets.

@benjamingorman
Created October 24, 2017 20:49
Show Gist options
  • Save benjamingorman/a04387fbe3119801824aea2b6b53cab5 to your computer and use it in GitHub Desktop.
Save benjamingorman/a04387fbe3119801824aea2b6b53cab5 to your computer and use it in GitHub Desktop.
import math
import numpy as np
import sys
lattice_width = 100
lattice_height = 100
initial_learning_rate = 0.01
input_dimensions = 3
weights = np.random.rand(lattice_height, lattice_width, input_dimensions)
initial_neighbourhood_radius = 15.0
neighbourhood_shrink_factor = 1000.0
def discriminant_function(w1, w2):
s = 0
for i in range(len(w1)):
s += math.pow(w2[i] - w1[i], 2)
return s
def find_winning_neuron(pattern, weights):
best_d = sys.float_info.max
best_neuron = (0, 0)
for y in range(lattice_height):
for x in range(lattice_width):
neuron_weights = weights[y][x]
d = discriminant_function(neuron_weights, pattern)
print(d)
if d < best_d:
best_d = d
best_neuron = (x, y)
print("best d", best_d)
print("best neuron", best_neuron)
return best_neuron
def neighbourhood_size(time):
global initial_neighbourhood_radius, neighbourhood_shrink_factor
return initial_neighbourhood_radius * math.exp(-time / neighbourhood_shrink_factor)
def neighbourhood_function(neuron1, neuron2, time):
(x1, y1) = neuron1
(x2, y2) = neuron2
dist = math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2)
T = math.exp(-dist / 2 * neighbourhood_size(time))
return T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment