Package: proto-dist-ml
Prototype-based Machine Learning on Distance DataCopyright (C) 2019 - Benjamin PaassenMachine Learning Research GroupCenter of Excellence Cognitive Interaction Technology (CITEC)Bielefeld UniversityThis program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with this program; if not, seehttp://www.gnu.org/licenses/.IntroductionThisscikit-learncompatible, Python3 library provides several algorithms
to learn prototype models on distance data. At this time, this library features
the following algorithms:Relational Neural Gas (Hammer and Hasenfuss, 2007) for clustering,Relational Generalized Learning Vector Quantization (Hammer, Hofmann, Schleif, and Zhu, 2014) for classification, andMedian Generalized Learning Vector Quantization (Nebel, Hammer, Frohberg, and Villmann, 2015) for classification.Refer to the Quickstart Guide for a note on how to use these models and
refer to the Background section for more details on the algorithms.Note that this library follows theIf you intend to use this library in academic work, please cite the respective
reference paper.InstallationThis package is available onpypiasproto_dist_ml. You can install
it viapip install proto_dist_mlQuickStart GuideFor an example we recommend to take a look at the demo in the notebookdemo.ipynb. In general, all models in this library follow thescikit-learnconvention, i.e. you need to perform the following steps:Instantiate your model, e.g. viamodel = proto_dist_ml.rng.RNG(K)whereKis the number of prototypes.Fit your model to training data, e.g. viamodel.fit(D), whereDis the
matrix of pairwise distances between your training data points.Perform a prediction for test data, e.g. viamodel.predict(D), whereDis the matrix of distances from the test to the training data points.BackgroundThe basic idea of prototype models is that we can cluster and
classify data by assigning them to the cluster/class of the closestprototype,
where a prototype is a data point that represents the cluster/class well.In case of distance data, we can not express a prototype in vectorial form but
instead need to use an indirect form, namely a convex combination of existing
data points. In other words, our $k$th prototype $w_k$ is defined as\vec w_k = \sum_{i=1}^m \alpha_{k, i} \cdot \vec x_i
\qquad \text{where } \sum_{i=1}^m \alpha_{k, i} = 1
\text{ and } \alpha_{k, i} \geq 0 \quad \forall iwhere $\vec x_1, \ldots, \vec x_m$ are the training data points and where
$\alpha_{k, 1}, \ldots, \alpha_{k, m}$ are the convex coefficiants
representing prototype $w_k$. Because the prototype is fully specified by
the data and the convex coefficients, we do not need any explicit form for
$w_k$ anymore.To cluster/classify new data, we now need to determine the distance between a
data point $x$ and a prototpe $w_k$. As it turns out, this distance can
also be expressed solely in terms of the convex coefficients and the
data-to-data distances. In particular, we obtain:d(x, w_k)^2 = \sum_{i=1}^m \alpha_{k, i} \cdot d(x, x_i)^2
- \frac{1}{2} \sum_{i=1}^m \sum_{j=1}^m \alpha_{k, i} \cdot \alpha_{k, j} \cdot d(x_i, x_j)^2In matrix-vector notation we obtain:d(x, w_k)^2 = {\vec \alpha_k}^T \cdot \vec d^2
- \frac{1}{2} {\vec \alpha_k}^T \cdot D^2 \cdot \vec \alpha_kwhere $\vec d^2$ the vector of distances between $x$ and all training
data points $x_i$ and where $D^2$ is the distance matrix between the
training data points.The main challenge for distance-based prototype learning is now to optimize
the coefficients $\alpha_{k, i}$ according to some meaningful loss function.
The loss function and its optimization differs between the algorithms. In more
detail, we take the following approaches.Relational Neural GasRelational neural gas (RNG;Hammer and Hasenfuss, 2007) is a
clustering approach that tries to optimize the loss function\sum_{i=1}^m \sum_{k=1}^K h_{i, k} \cdot d(x_i, w_k)^2where $h_{i, k}$ quantifies how responsible prototype $w_k$ is for
data point $x_i$. This term is calculated as follows:h_{i, k} = \exp(-r_{i, k} / \lambda) \qquad \text{where } r_{i, k} = |\{ l | d(x_i, w_l) < d(x_i, w_k) \}|In other words, $w_k$ is the $r_{i, k}$-closest prototype to data point
$x_i$ and the lower ranked a prototype is (i.e. the closer it is), the higher
is its responsibility for the data point. $\lambda$ is a scaling factor that
expresses how many prototypes are still considered. Per default, we start with
$\lambda = K / 2$ and then anneal $\lambda$ until $\lambda = 0.01$, i.e.
only the closest prototype is considered. At that point, the loss above is
equivalent to the $K$-means loss.Given the current values for $h_{i, k}$, optimizing the convex coefficients
$\alpha_{k, i}$ is possible in closed form. In particular, we obtain:
$\alpha_{k, i} = h_{i, k} / \sum_j h_{j, k}$. The RNG
training procedure thus consists of three steps which are iterated in each
training epoch:Compute the responsibilities $h_{i, k}$.Compute the new convex coefficients $\alpha_{k, i}$.Decrease $\lambda$.Relational Generalized Learning Vector QuantizationRelational generalized learning vector quantization (RGLVQ;Hammer, Hofmann, Schleif, and Zhu, 2014)
is a classification approach which aims at optimizing the generalized learning
vector quantization loss:\sum_{i=1}^m \Phi\Big(\frac{d_i^+ - d_i^-}{d_i^+ + d_i^-}\Big)where $d_i^+$ is the distance of data point $x_i$ to the closest prototype
with the same label, $d_i^-$ is the distance of data point $x_i$ to the
closest prototype with a different label, and $\Phi$ is a squashing
function (such as tanh or the logistic function).
Note that data point $x_i$ is correctly classified if and only
if $d_i^+ - d_i^- < 0$. As such, the GLVQ loss can be regarded as a soft
approximation of the classification error.Note that this loss has the drawback that distances need to be strictly
positive in order to guarantee a nonzero denominator. This excludes
non-Euclidean distances (i.e. distances which do not correspond to an inner
product) because these may imply negative data-to-prototype distances.We optimize this loss via L-BFGS, restricting the coefficients to be convex
in each step. The gradient follows directly from the
formula above and the distance formula above. For more details, refer to
(Hammer et al., 2014).Median Generalized Learning Vector QuantizationMedian generalized learning vector quantization
(MGLVQ;Nebel, Hammer, Frohberg, and Villmann, 2015) is a variant
of GLVQ that restricts prototypes to be strictly data points, i.e. for each
prototype $w_k$ there exists exactly one $i$, such that $\alpha_{k, i} = 1$
and every other coefficient is zero. This has two key advantages. First, it
permits non-Euclidean and even asymmetric distances because we do not rely on
an interpolation between data points. Second, it is more efficient during
classification because we can compute the distances to the prototypes directly
and do not need to use the relational distance formula above.However, MGLVQ is also more challenging to train because we can not perform a
smooth gradient method but instead must apply a discrete optimization scheme.
In this toolbox, we optimize the GLVQ loss (see above) via greedy hill climbing,
i.e. we try to find any prototype-datapoint combination that would reduce the
loss and apply the first such combination we find until no such move exists
anymore.ContentsThis library contains the following files.demo.ipynb: A demo script illustrating how to use this library.LICENSE.md: A copy of the GPLv3 license.mglvq_test.py: A set of unit tests formglvq.py.proto_dist_ml/mglvq.py: An implementation of median generalized
learning vector quantization.proto_dist_ml/rglvq.py: An implementation of relational generalized
learning vector quantization.proto_dist_ml/rng.py: An implementation of relational neural gas.README.md: This file.rglvq_test.py: A set of unit tests forrglvq.py.rng_test.py: A set of unit tests forrng.py.LicensingThis library is licensed under theGNU General Public License Version 3.DependenciesThis library depends onNumPyfor matrix operations, onscikit-learnfor the base interfaces and onSciPyfor optimization.LiteratureHammer, B. & Hasenfuss, A. (2007). Relational Neural Gas. Proceedings of the
30th Annual German Conference on AI (KI 2007), 190-204. doi:10.1007/978-3-540-74565-5_16.LinkHammer, B., Hofmann, D., Schleif, F., & Zhu, X. (2014). Learning vector
quantization for (dis-)similarities. Neurocomputing, 131, 43-51.
doi:10.1016/j.neucom.2013.05.054.LinkNebel, D., Hammer, B., Frohberg, K., & Villmann, T. (2015). Median variants
of learning vector quantization for learning of dissimilarity data.
Neurocomputing, 169, 295-305. doi:10.1016/j.neucom.2014.12.096