-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from beringresearch/ivis-supvised-softmax
Ivis supvised softmax
- Loading branch information
Showing
25 changed files
with
728 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ __pycache__/ | |
*.swp | ||
.ipynb_checkpoints | ||
_build | ||
*.pyc |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\nApplying ivis to the MNIST dataset\n==================================\n\nIvis can be applied easily applied to unstructured datasets, including images.\nHere we visualise the MNSIT digits dataset using two-dimensional ivis\nembeddings.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import fetch_openml\nfrom ivis import Ivis\n\nmnist = fetch_openml('mnist_784', version=1)\n\nivis = Ivis(model='maaten', verbose=0)\nembeddings = ivis.fit_transform(mnist.data)\n\ncolor = mnist.target.astype(int)\n\nplt.figure(figsize=(8, 8), dpi=150)\nplt.scatter(x=embeddings[:, 0],\n y=embeddings[:, 1], c=color, cmap=\"Spectral\", s=0.1)\nplt.xlabel('ivis 1')\nplt.ylabel('ivis 2')\nplt.show()\n\nos.remove('annoy.index')" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Applying ivis to the MNIST dataset | ||
================================== | ||
Ivis can be applied easily applied to unstructured datasets, including images. | ||
Here we visualise the MNSIT digits dataset using two-dimensional ivis | ||
embeddings. | ||
""" | ||
|
||
import os | ||
import matplotlib.pyplot as plt | ||
|
||
from sklearn.datasets import fetch_openml | ||
from ivis import Ivis | ||
|
||
mnist = fetch_openml('mnist_784', version=1) | ||
|
||
ivis = Ivis(model='maaten', verbose=0) | ||
embeddings = ivis.fit_transform(mnist.data) | ||
|
||
color = mnist.target.astype(int) | ||
|
||
plt.figure(figsize=(8, 8), dpi=150) | ||
plt.scatter(x=embeddings[:, 0], | ||
y=embeddings[:, 1], c=color, cmap="Spectral", s=0.1) | ||
plt.xlabel('ivis 1') | ||
plt.ylabel('ivis 2') | ||
plt.show() | ||
|
||
os.remove('annoy.index') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
.. note:: | ||
:class: sphx-glr-download-link-note | ||
|
||
Click :ref:`here <sphx_glr_download_auto_examples_mnist.py>` to download the full example code | ||
.. rst-class:: sphx-glr-example-title | ||
|
||
.. _sphx_glr_auto_examples_mnist.py: | ||
|
||
|
||
Applying ivis to the MNIST dataset | ||
================================== | ||
|
||
Ivis can be applied easily applied to unstructured datasets, including images. | ||
Here we visualise the MNSIT digits dataset using two-dimensional ivis | ||
embeddings. | ||
|
||
.. code-block:: default | ||
import os | ||
import matplotlib.pyplot as plt | ||
from sklearn.datasets import fetch_openml | ||
from ivis import Ivis | ||
mnist = fetch_openml('mnist_784', version=1) | ||
ivis = Ivis(model='maaten', verbose=0) | ||
embeddings = ivis.fit_transform(mnist.data) | ||
color = mnist.target.astype(int) | ||
plt.figure(figsize=(8, 8), dpi=150) | ||
plt.scatter(x=embeddings[:, 0], | ||
y=embeddings[:, 1], c=color, cmap="Spectral", s=0.1) | ||
plt.xlabel('ivis 1') | ||
plt.ylabel('ivis 2') | ||
plt.show() | ||
os.remove('annoy.index') | ||
.. rst-class:: sphx-glr-timing | ||
|
||
**Total running time of the script:** ( 0 minutes 0.000 seconds) | ||
|
||
|
||
.. _sphx_glr_download_auto_examples_mnist.py: | ||
|
||
|
||
.. only :: html | ||
.. container:: sphx-glr-footer | ||
:class: sphx-glr-footer-example | ||
.. container:: sphx-glr-download | ||
:download:`Download Python source code: mnist.py <mnist.py>` | ||
.. container:: sphx-glr-download | ||
:download:`Download Jupyter notebook: mnist.ipynb <mnist.ipynb>` | ||
.. only:: html | ||
|
||
.. rst-class:: sphx-glr-signature | ||
|
||
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
.. _supervised: | ||
|
||
Supervised Dimensionality Reduction | ||
=================================== | ||
|
||
``ivis`` is able to make use of any provided class labels to perform | ||
supervised dimensionality reduction. Supervised embeddings | ||
combine the distance-based characteristics of the unsupervised ``ivis`` | ||
algorithm with clear class boundaries between the class categories. The | ||
resulting embeddings encode relevant class-specific information into | ||
lower dimensional space, making them useful for enhancing the | ||
performance of a classifier. | ||
|
||
To train ``ivis`` in supervised mode, simply provide the labels to the | ||
fit method's ``Y`` parameter. These labels should be a list of 0-indexed | ||
integers with each integer corresponding to a class. | ||
|
||
:: | ||
|
||
from keras.datasets import mnist | ||
import numpy as np | ||
|
||
(X_train, Y_train), (X_test, Y_test) = mnist.load_data() | ||
|
||
# Rescale to 0-1 | ||
X_train = X_train / 255. | ||
X_test = X_test / 255. | ||
|
||
# Flatten images to 1D vectors | ||
X_train = np.reshape(X_train, (len(X_train), 28 * 28)) | ||
X_test = np.reshape(X_test, (len(X_test), 28 * 28)) | ||
|
||
model = Ivis(n_epochs_without_progress=5) | ||
model.fit(X_train, Y_train) | ||
|
||
Experimental data has shown that ``ivis`` converges to a solution faster | ||
in supervised mode. Therefore, our suggestion is to lower the value of | ||
the ``n_epochs_without_progress`` parameter from the default of 50 to | ||
around 5. Here are the resulting embeddings: | ||
|
||
.. image:: _static/mnist-embedding-comparison_titled.png | ||
|
||
It is possible to control the relative importance Ivis places on the | ||
labels when training in supervised mode with the | ||
``classification_weight`` parameter. This variable should be a float | ||
between 0.0 to 1.0, with higher values resulting in classification | ||
affecting the training process more, and smaller values resulting in it | ||
impacting the training less. By default, the parameter is set to 0.5. | ||
Increasing it to 0.8 will result in more cleanly separated classes. | ||
|
||
:: | ||
|
||
weight = 0.8 | ||
model = Ivis(n_epochs_without_progress=5, | ||
classification_weight=weight) | ||
model.fit(X_train, Y_train) | ||
|
||
As an illustration of the impact the ``classification_weight`` has on | ||
the resulting embeddings, see the following plot of supervised ``ivis`` | ||
applied to MNIST with different weight values: | ||
|
||
.. image:: _static/classification-weight-impact-mnist.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from . import data | ||
from . import nn | ||
from .ivis import Ivis | ||
from .ivis import Ivis | ||
|
||
__version__ = '1.2.0' |
Oops, something went wrong.