Package: n2d
Welcome to the stable master branch of N2D!! The active development branch is ondev, if you are contributing, please make branches from that!ChangesFinally easily extensible (no more writing classes!)Model saving and loading![x] TF2 readyNot Too Deep ClusteringThis is a library implementation ofn2d. To learn more about N2D, and clustering manifolds of autoencoded embeddings, please refer to theamazing paperpublished August 2019.What is it?Not too deep clustering is a state of the art "deep" clustering technique, in which first, the data is embedded using an autoencoder. Then, instead of clustering that using some deep clustering network, we use a manifold learner to find the underlying (local) manifold in the embedding. Then, we cluster that manifold. In the paper, this was shown to produce high quality clusters without the standard extreme feature engineering required for clustering.In this repository, a framework for A) reproducing the study and B) extending the study is given, for further research and use in a variety of applicationsDocumentationFull documentation is available onread the docsInstallationN2D isavailable on pypipipinstalln2dUsageFirst, lets load in some data. In this example, we will use the Human Activity Recognition(HAR) dataset. In this dataset, sets of time series with data from mobile devices is used to classify what the person is doing (walking, sitting, etc.)importn2dasndimportn2d.datasetsasdatax,y,y_names=data.load_har()Next, lets set up our deep learning environment, as well as load in necessary libraries:importnumpyasnpimportmatplotlibimportmatplotlib.pyplotaspltimportseabornassnsplt.style.use(['seaborn-white','seaborn-paper'])sns.set_context("paper",font_scale=1.3)matplotlib.use('agg')Finally, we are ready to get clustering!The first step of any not too deep clustering algorithm is to use an autoencoder to learn an embedding, so that is what we will do!n_clusters=6ae=n2d.AutoEncoder(x.shape[-1],n_clusters)The next step in this framework is to initialize a manifold clustering algorithm, in general UmapGMM, which builds an autoencoder network and gets everything ready for clustering:manifold_clusterer=n2d.UmapGMM(n_clusters)Finally, we pass both of these into the N2D class:harcluster=n2d.n2d(ae,manifold_clusterer)Next, we fit the data. In this step, the autoencoder is trained on the data, setting up weights.harcluster.fit(x,weight_id="har.h5")The next time we want to use this autoencoder, we will instead use the weights argument:harcluster.fit(x,weights="har.h5")Now we can make a prediction, as well as visualize and assess. In this step, the manifold learner learns the manifold for the data, which is then clustered. By default, it makes the prediction on the data stored internally, however you can specify a newxin order to make predictions on new data.preds=harcluster.predict(x)# predictions are stored in harcluster.predsharcluster.visualize(y,y_names,n_clusters=n_clusters)print(harcluster.assess(y))# (0.81212, 0.71669, 0.64013)Before viewing the results, lets talk about the metrics. The first metric is cluster accuracy, which we see here is 81.2%, which is absolutely state of the art for the HAR dataset. The next metric is NMI, which is another metric which describes cluster quality based on labels, independent of the number of clusters. We have an NMI of 0.717, which is again absolutely state of the art for this dataset. The last metric, ARI, shows another comparison between the actual groupings and our grouping. A value of 1 means the groupings are nearly the same, while a value of 0 means they completely disagree. We have a value of 0.64013, which indicates that are predictions are more or less in agreement with the truth, however they are not perfect.N2D predictionActual clustersExtendingThis library comes with 2 special generator classes, which make it much easier to extend the library. For more in depth discussion, please seethe documentationReplacing the manifold clustering mechanismLets assume we want to use hdbscan to cluster our manifold, instead of gaussian mixing. In this case, we use themanifold_cluster_generatorclass. Below is a short example using hdbscanimportn2dimportnumpyasnpimportn2d.datasetsasdataimporthdbscanimportumap# load up mnist examplex,y=data.load_mnist()# autoencoder can be just passed normally, see the other examples for extending# itae=n2d.AutoEncoder(input_dim=x.shape[-1],latent_dim=40)# arguments for clusterer go in a dicthdbscan_args={"min_samples":10,"min_cluster_size":500,'prediction_data':True}# arguments for manifold learner go in a dictumap_args={"metric":"euclidean","n_components":2,"n_neighbors":30,"min_dist":0}# pass the classes and dicts into the generator# manifold class, manifold args, cluster class, cluster argsdb=n2d.manifold_cluster_generator(umap.UMAP,umap_args,hdbscan.HDBSCAN,hdbscan_args)# pass the manifold-cluster tool and the autoencoder into the n2d classdb_clust=n2d.n2d(ae,db)# fitdb_clust.fit(x,epochs=10)# the clusterer is a normal hdbscan objectprint(db_clust.clusterer.probabilities_)print(db_clust.clusterer.labels_)# access the manifold learner atprint(db_clust.manifolder)# if the parent classes have a method you can likely use it (make an issue if not)db_clust.fit_predict(x,epochs=10)# however this will error because hdbscan doesnt have that methoddb_clust.predict(x)# predict on new data with the approximate predictionx_test,y_test=data.load_mnist_test()# access the parts of the autoencoder within n2d or outside of ittest_embedding=db_clust.encoder.predict(x_test)test_embedding-test_n2d_embedding# all zerostest_labels,strengths=hdbscan.approximate_predict(db_clust.clusterer,db_clust.manifolder.transform(test_embedding))print(test_labels)print(strengths)Replacing the embedding mechanismWe can also pretty easily replace the autoencoder with a new one using theautoencoder_generatorclass:importn2dfromn2dimportdatasetsasdatafromtensorflow.keras.layersimportDense,Inputfromtensorflow.keras.modelsimportModelimportseabornassnsimportumapimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlibplt.style.use(['seaborn-white','seaborn-paper'])sns.set_context("paper",font_scale=1.3)# load up datax,y,y_names=data.load_fashion()# define number of clustersn_clusters=10# set up manifold learnerumapgmm=n2d.UmapGMM(n_clusters)# set up parameters for denoising autoencoderdefadd_noise(x,noise_factor):x_clean=xx_noisy=x_clean+noise_factor*np.random.normal(loc=0.0,scale=1.0,size=x_clean.shape)x_noisy=np.clip(x_noisy,0.,1.)returnx_noisy# define stages of networkshidden_dims=[500,500,2000]input_dim=x.shape[-1]inputs=Input(input_dim)encoded=inputsfordinhidden_dims:encoded=Dense(d,activation="relu")(encoded)encoded=Dense(n_clusters)(encoded)decoded=encodedfordinhidden_dims[::-1]:decoded=Dense(d,activation="relu")(decoded)outputs=Dense(input_dim)(decoded)# inputs: iterable of inputs, center, outputs of ae, lambda for noise (x_lambda is not a necessary argument)denoising_ae=n2d.autoencoder_generator((inputs,encoded,outputs),x_lambda=lambdax:add_noise(x,0.5))# define modelmodel=n2d.n2d(denousing_ae,umapgmm)# fit the modelmodel.fit(x,epochs=10)# make some predictionsmodel.predict(x)model.visualize(y,y_names,n_clusters=n_clusters)plt.show()print(model.assess(y))RoadmapPackage libraryPackage dataEarly stoppingImplement data augmentation techniques for images, sequences, and time seriesMake autoencoder interchangeable just like the restSimpler way to extract embeddingImplement other types of autoencoders as well as convolutional layersManage file saving paths betterImplement other promising methodsMake assessment/visualization more extensibleDocumentation?Find an elegant way to deal with pre training weightsPackage on NixBlog post?Clean up code examples!ContributingN2D is a work in progress and is open to suggestions to make it faster, more extensible, and generally more usable. Please make an issue if you have any ideas of how to be more accessible and more usable!CitationIf you use N2D in your research, please credit the original authors of the paper. Bibtex included below:@article{2019arXiv190805968M,
  title = {N2D:(Not Too) Deep Clustering via Clustering the Local Manifold of an Autoencoded Embedding},
  author = {{McConville}, Ryan and {Santos-Rodriguez}, Raul and {Piechocki}, Robert J and {Craddock}, Ian},
  journal = {arXiv preprint arXiv:1908.05968},
  year = "2019",
}