Relational Neural Gas

Implements relational neural gas (RNG) as described in the paper

Hammer, B. & Hasenfuss, A. (2007). Relational Neural Gas. Proceedings of the 30th Annual German Conference on AI (KI). URL: https://www.researchgate.net/publication/221562215_Relational_Neural_Gas

class proto_dist_ml.rng.RNG(K, T=50, lambda_0=None)

A relational neural gas clustering model, which assigns data to clusters depending on their proximity to learned prototypes, which are given as convex combinations of data points. Due to its purely distance- based definition, relational neural gas can cope with any kind of distance matrix input, as long as the input distance matrix is symmetric and zero on the diagonal.

K

The number of prototypes to be learned.

Type:int
T

The number of epochs in training.

Type:int (optional, default=50)
lambda_0

The initial neighborhood range, i.e. the rank on which the prototype influence on a datapoint degrades to 1/e.

Type:float (optional, default = K / 2)
_Alpha

A K x m matrix sparse matrix storing the convex coefficients that describe the prototypes. This is not set by the user but during fit().

Type:array_like
_z

A K x 1 vector storing the normalization constants -0.5*_Alpha[k, :] * D² * _Alpha[k, :].T for all k. This is not set by the user but during fit().

Type:array_like
_loss

The relational neural gas loss during the last training run. This is not set by the user but during fit().

Type:array_like
fit(D, y=None, is_squared=False)

Fits a relational neural gas model to the given distance matrix.

In more detail, the training consists of three steps, which are alternated.

  1. We compute the squared data-to-prototype distances via the relational distance formula:
\[d(w_k, x_i)^2 = A_{k, :} \cdot D^2_{:, i} + z_k\]
  1. We compute the ranks of each prototype for each data point, i.e.
\[r_i(k) = |\{ l | d(w_l, x_i)^2 < d(w_k, x_i)^2 \}|\]
  1. We update the coefficients _Alpha via the formula
\[A_{k, i} = \frac{\exp(-r_i(k) / \lambda)}{\sum_l \exp(-r_i(l) / \lambda)}\]

Note that lambda is decreased in each iteration until the assignment is strict as for k-means.

Parameters:
  • D (array_like) – A m x m matrix of pairwise distances, which need to be symmetric and zero on the diagonal.
  • y (array_like) – This parameter is ignored and is only here for API consistency.
  • is_squared (bool (optional, default = False)) – If set to true, this method assumes D is already a matrix of squared distances. Otherwise, the distances get squared.
Returns:

self

Return type:

class proto_dist_ml.rng.RNG

predict(D, is_squared=False)

Predicts the cluster assignments for the data represented by the given test-to-training distance matrix. Each datapoint will be assigned to the closest prototype.

Parameters:
  • D (array_like) – A n x m matrix of distances from the test to the training datapoints.
  • is_squared (bool (optional, default = False)) – If set to true, this method assumes D is already a matrix of squared distances. Otherwise, the distances get squared.
Returns:

y – A n-dimensional vector containing the cluster indices for each datapoint.

Return type:

array_like