Module dimdrop.layers.clustering_layer
Clustering layer implementation by Chengwei Zhang: https://www.dlology.com/blog/how-to-do-unsupervised-clustering-with-keras/
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Source code
"""
Clustering layer implementation by Chengwei Zhang:
https://www.dlology.com/blog/how-to-do-unsupervised-clustering-with-keras/
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import numpy as np
import keras.backend as K
from keras.engine.topology import Layer, InputSpec
class ClusteringLayer(Layer):
"""
Clustering layer converts input sample (feature) to soft label, i.e. a
vector that represents the probability of the sample belonging to each
cluster. The probability is calculated with student's t-distribution.
Example
-------
```
model.add(ClusteringLayer(n_clusters=10))
```
Parameters
----------
n_clusters: int
number of clusters.
weights: list of Numpy array, shape `(n_clusters, n_features)`, optional
Represents the initial cluster centers.
alpha: float, optional
degrees of freedom parameter in Student's t-distribution.
Default to 1.0.
Input shape
-----------
2D tensor with shape: `(n_samples, n_features)`.
Output shape
------------
2D tensor with shape: `(n_samples, n_clusters)`.
"""
def __init__(self, n_clusters, weights=None, alpha=1.0, **kwargs):
if 'input_shape' not in kwargs and 'input_dim' in kwargs:
kwargs['input_shape'] = (kwargs.pop('input_dim'),)
super(ClusteringLayer, self).__init__(**kwargs)
self.n_clusters = n_clusters
self.alpha = alpha
self.initial_weights = weights
self.input_spec = InputSpec(ndim=2)
def build(self, input_shape):
assert len(input_shape) == 2
input_dim = input_shape[1]
self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim))
self.clusters = self.add_weight(name='clusters', shape=(
self.n_clusters, input_dim), initializer='glorot_uniform', )
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
self.built = True
def call(self, inputs, **kwargs):
""" student t-distribution, as same as used in t-SNE algorithm.
Measure the similarity between embedded point z_i and centroid µ_j.
q_ij = 1/(1+dist(x_i, µ_j)^2), then normalize it.
q_ij can be interpreted as the probability of assigning
sample i to cluster j. (i.e., a soft assignment)
Parameters
-----------
inputs: tensor of shape `(n_samples, n_features)`
the variable containing data
Returns
-------
q: tensor of shape `(n_samples, n_clusters)`
student's t-distribution, or soft labels for each sample.
"""
q = 1.0 / (1.0 + (K.sum(K.square(
K.expand_dims(inputs, axis=1) - self.clusters
), axis=2) / self.alpha))
q **= (self.alpha + 1.0) / 2.0
# Make sure each sample's 10 values add up to 1.
q = K.transpose(K.transpose(q) / K.sum(q, axis=1))
return q
def compute_output_shape(self, input_shape):
assert input_shape and len(input_shape) == 2
return input_shape[0], self.n_clusters
def get_config(self):
config = {'n_clusters': self.n_clusters}
base_config = super(ClusteringLayer, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
Classes
class ClusteringLayer (n_clusters, weights=None, alpha=1.0, **kwargs)
-
Clustering layer converts input sample (feature) to soft label, i.e. a vector that represents the probability of the sample belonging to each cluster. The probability is calculated with student's t-distribution.
Example
model.add(ClusteringLayer(n_clusters=10))
Parameters
n_clusters
:int
- number of clusters.
weights
:list
ofNumpy
array
,shape
(
n_clusters,
n_features)
, optional- Represents the initial cluster centers.
alpha
:float
, optional- degrees of freedom parameter in Student's t-distribution. Default to 1.0.
Input shape
2D tensor with shape: `(n_samples, n_features)`.
Output shape
2D tensor with shape: `(n_samples, n_clusters)`.
Source code
class ClusteringLayer(Layer): """ Clustering layer converts input sample (feature) to soft label, i.e. a vector that represents the probability of the sample belonging to each cluster. The probability is calculated with student's t-distribution. Example ------- ``` model.add(ClusteringLayer(n_clusters=10)) ``` Parameters ---------- n_clusters: int number of clusters. weights: list of Numpy array, shape `(n_clusters, n_features)`, optional Represents the initial cluster centers. alpha: float, optional degrees of freedom parameter in Student's t-distribution. Default to 1.0. Input shape ----------- 2D tensor with shape: `(n_samples, n_features)`. Output shape ------------ 2D tensor with shape: `(n_samples, n_clusters)`. """ def __init__(self, n_clusters, weights=None, alpha=1.0, **kwargs): if 'input_shape' not in kwargs and 'input_dim' in kwargs: kwargs['input_shape'] = (kwargs.pop('input_dim'),) super(ClusteringLayer, self).__init__(**kwargs) self.n_clusters = n_clusters self.alpha = alpha self.initial_weights = weights self.input_spec = InputSpec(ndim=2) def build(self, input_shape): assert len(input_shape) == 2 input_dim = input_shape[1] self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim)) self.clusters = self.add_weight(name='clusters', shape=( self.n_clusters, input_dim), initializer='glorot_uniform', ) if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights self.built = True def call(self, inputs, **kwargs): """ student t-distribution, as same as used in t-SNE algorithm. Measure the similarity between embedded point z_i and centroid µ_j. q_ij = 1/(1+dist(x_i, µ_j)^2), then normalize it. q_ij can be interpreted as the probability of assigning sample i to cluster j. (i.e., a soft assignment) Parameters ----------- inputs: tensor of shape `(n_samples, n_features)` the variable containing data Returns ------- q: tensor of shape `(n_samples, n_clusters)` student's t-distribution, or soft labels for each sample. """ q = 1.0 / (1.0 + (K.sum(K.square( K.expand_dims(inputs, axis=1) - self.clusters ), axis=2) / self.alpha)) q **= (self.alpha + 1.0) / 2.0 # Make sure each sample's 10 values add up to 1. q = K.transpose(K.transpose(q) / K.sum(q, axis=1)) return q def compute_output_shape(self, input_shape): assert input_shape and len(input_shape) == 2 return input_shape[0], self.n_clusters def get_config(self): config = {'n_clusters': self.n_clusters} base_config = super(ClusteringLayer, self).get_config() return dict(list(base_config.items()) + list(config.items()))
Ancestors
- keras.engine.base_layer.Layer
Methods
def build(self, input_shape)
-
Creates the layer weights.
Must be implemented on all layers that have weights.
Arguments
input_shape: Keras tensor (future input to layer) or list/tuple of Keras tensors to reference for weight shape computations.
Source code
def build(self, input_shape): assert len(input_shape) == 2 input_dim = input_shape[1] self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim)) self.clusters = self.add_weight(name='clusters', shape=( self.n_clusters, input_dim), initializer='glorot_uniform', ) if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights self.built = True
def call(self, inputs, **kwargs)
-
student t-distribution, as same as used in t-SNE algorithm. Measure the similarity between embedded point z_i and centroid µ_j. q_ij = 1/(1+dist(x_i, µ_j)^2), then normalize it. q_ij can be interpreted as the probability of assigning sample i to cluster j. (i.e., a soft assignment)
Parameters
inputs
:tensor
ofshape
(
n_samples,
n_features)
- the variable containing data
Returns
q
:tensor
ofshape
(
n_samples,
n_clusters)
- student's t-distribution, or soft labels for each sample.
Source code
def call(self, inputs, **kwargs): """ student t-distribution, as same as used in t-SNE algorithm. Measure the similarity between embedded point z_i and centroid µ_j. q_ij = 1/(1+dist(x_i, µ_j)^2), then normalize it. q_ij can be interpreted as the probability of assigning sample i to cluster j. (i.e., a soft assignment) Parameters ----------- inputs: tensor of shape `(n_samples, n_features)` the variable containing data Returns ------- q: tensor of shape `(n_samples, n_clusters)` student's t-distribution, or soft labels for each sample. """ q = 1.0 / (1.0 + (K.sum(K.square( K.expand_dims(inputs, axis=1) - self.clusters ), axis=2) / self.alpha)) q **= (self.alpha + 1.0) / 2.0 # Make sure each sample's 10 values add up to 1. q = K.transpose(K.transpose(q) / K.sum(q, axis=1)) return q
def compute_output_shape(self, input_shape)
-
Computes the output shape of the layer.
Assumes that the layer will be built to match that input shape provided.
Arguments
input_shape: Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
Returns
An input shape tuple.
Source code
def compute_output_shape(self, input_shape): assert input_shape and len(input_shape) == 2 return input_shape[0], self.n_clusters
def get_config(self)
-
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by
Network
(one layer of abstraction above).Returns
Python dictionary.
Source code
def get_config(self): config = {'n_clusters': self.n_clusters} base_config = super(ClusteringLayer, self).get_config() return dict(list(base_config.items()) + list(config.items()))