"""SOMUtils functions."""
from typing import Sequence, Tuple
import numpy as np
from sklearn.utils.multiclass import check_classification_targets
from sklearn.utils.validation import check_array, check_X_y
[docs]
def decreasing_rate(
a_1: float, a_2: float, *, iteration_max: int, iteration: int, mode: str
) -> float:
"""Return a decreasing rate from collection.
Parameters
----------
a_1 : float
Starting value of decreasing rate
a_2 : float
End value of decreasing rate
iteration_max : int
Maximum number of iterations
iteration : int
Current number of iterations
mode : str
Mode (= formula) of the decreasing rate
Returns
-------
rate : float
Decreasing rate
Examples
---------
>>> import susi
>>> susi.decreasing_rate(0.8, 0.1, 100, 5, "exp")
"""
rate = None
if mode == "min":
rate = a_1 * np.power(a_2 / a_1, iteration / iteration_max)
elif mode == "exp":
rate = a_1 * np.exp(-5 * np.divide(iteration, iteration_max))
elif mode == "expsquare":
rate = a_1 * np.exp(
-5 * np.power(np.divide(iteration, iteration_max), 2)
)
elif mode == "linear":
rate = (a_1 - a_2) * (1 - np.divide(iteration, iteration_max)) + a_2
elif mode == "inverse":
rate = a_1 / iteration
elif mode == "root":
rate = np.power(a_1, iteration / iteration_max)
else:
raise ValueError("Invalid decreasing rate mode: " + str(mode))
# # prevent zero:
# rate = max(rate, a_2)
return rate
[docs]
def modify_weight_matrix_online(
som_array: np.ndarray,
*,
dist_weight_matrix: np.ndarray,
true_vector: np.ndarray,
learning_rate: float,
) -> np.ndarray:
"""Modify weight matrix of the SOM for the online algorithm.
Parameters
----------
som_array : np.ndarray
Weight vectors of the SOM
shape = (self.n_rows, self.n_columns, X.shape[1])
dist_weight_matrix : np.ndarray of float
Current distance weight of the SOM for the specific node
true_vector : np.ndarray
True vector
learning_rate : float
Current learning rate of the SOM
Returns
-------
np.array
Weight vector of the SOM after the modification
"""
return som_array + np.multiply(
learning_rate,
np.multiply(dist_weight_matrix, -np.subtract(som_array, true_vector)),
)