Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mexican hat neighborhood function #2

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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