Skip to content

Commit

Permalink
feat: add mexican hat neighborhood function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthiboust committed Nov 30, 2023
1 parent c1102d6 commit 60b1c7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/somap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .distance import EuclidianDist, wdist_l2
from .grid import distance_map
from .learning_rate import AbstractLr, ConstantLr, DsomLr, KsomLr
from .neighborhood import AbstractNbh, DsomNbh, GaussianNbh, KsomNbh
from .neighborhood import AbstractNbh, DsomNbh, GaussianNbh, KsomNbh, MexicanHatNbh
from .plot import plot, save_plot
from .plot_backends import set_plot_backend
from .serialisation import load, save
Expand Down Expand Up @@ -49,6 +49,7 @@
"DsomNbh",
"GaussianNbh",
"KsomNbh",
"MexicanHatNbh",
"plot",
"save_plot",
"set_plot_backend",
Expand Down
22 changes: 22 additions & 0 deletions src/somap/neighborhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,25 @@ def __call__(
return jnp.exp(
-(distance_map**2) / (self.plasticity**2 * quantization_error**2)
)


class MexicanHatNbh(AbstractNbh):
"""Mexican Hat neighborhood function."""

sigma: float | Float[Array, "..."] = 0.1

def __call__(self, distance_map: Float[Array, "x y"], _, __) -> Float[Array, "x y"]:
"""Computes the Mexican Hat neighboring value of each grid element.
Args:
self:
self.sigma: Scale factor for the spread of the neighborhood.
distance_map: Distance of each element from the winner element.
_: Not used
__: Not used
Returns:
The Mexican Hat neighborhood distance.
"""
r2_norm = distance_map**2 / self.sigma**2
return (1 - 0.5 * r2_norm) * jnp.exp(-r2_norm / 2)
3 changes: 2 additions & 1 deletion src/somap/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def __call__(
w_bu: Float[Array, "..."],
) -> Float[Array, "..."]:
"""Updates the prototype weights."""
return w_bu + (lr * nbh)[:, :, jnp.newaxis] * (input_bu - w_bu)
out = w_bu + (lr * nbh)[:, :, jnp.newaxis] * (input_bu - w_bu)
return jnp.clip(out, 0, 1.0)


@experimental_warning
Expand Down

0 comments on commit 60b1c7e

Please sign in to comment.