Skip to content

Commit

Permalink
Merge pull request #131 from chicodelarosa/patch-1
Browse files Browse the repository at this point in the history
Activation distance as a function
  • Loading branch information
JustGlowing authored Feb 25, 2022
2 parents 59c2e13 + e434e9d commit a67b707
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions minisom.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def __init__(self, x, y, input_len, sigma=1.0, learning_rate=0.5,
Topology of the map.
Possible values: 'rectangular', 'hexagonal'
activation_distance : string, optional (default='euclidean')
activation_distance : string, callable optional (default='euclidean')
Distance used to activate the map.
Possible values: 'euclidean', 'cosine', 'manhattan', 'chebyshev'
Expand Down Expand Up @@ -205,12 +205,15 @@ def __init__(self, x, y, input_len, sigma=1.0, learning_rate=0.5,
'manhattan': self._manhattan_distance,
'chebyshev': self._chebyshev_distance}

if activation_distance not in distance_functions:
msg = '%s not supported. Distances available: %s'
raise ValueError(msg % (activation_distance,
', '.join(distance_functions.keys())))
if isinstance(activation_distance, str):
if activation_distance not in distance_functions:
msg = '%s not supported. Distances available: %s'
raise ValueError(msg % (activation_distance,
', '.join(distance_functions.keys())))

self._activation_distance = distance_functions[activation_distance]
self._activation_distance = distance_functions[activation_distance]
elif callable(activation_distance):
self._activation_distance = activation_distance

def get_weights(self):
"""Returns the weights of the neural network."""
Expand Down Expand Up @@ -792,3 +795,15 @@ def test_pickling(self):
with open('som.p', 'rb') as infile:
pickle.load(infile)
os.remove('som.p')

def test_callable_activation_distance(self):
def eucledian(x, w):
return linalg.norm(subtract(x, w), axis=-1)

data = random.rand(100, 2)
som1 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, activation_distance=eucledian, random_seed=1)
som1.train_random(data, 10)
som2 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
som2.train_random(data, 10)
# same state after training
assert_array_almost_equal(som1._weights, som2._weights)

0 comments on commit a67b707

Please sign in to comment.