Callbacks

Callbacks are called periodically during training of the ivis model. These allow you to get an insight into the progress being made during training. With this information, you may decide to terminate a training session early due to a lack of improvements to the visualizations, for example. They also provide helpful logging features, allowing you to periodically save a checkpoint of an ivis model which can be used to resuming training later.

To use a callback during training, simply pass a list of callback objects to the Ivis object when creating it using the callbacks keyword argument. The ivis.nn.callbacks module contains a set of callbacks provided for use with ivis models. However, any keras.callbacks.Callbacks object can be passed and will be used during training: for example, keras.callbacks.TensorBoard.

ModelCheckpoint

class ivis.nn.callbacks.ModelCheckpoint(log_dir='./model_checkpoints', filename='model-checkpoint_{}.ivis', epoch_interval=1)

Bases: sphinx.ext.autodoc.importer._MockObject

Periodically saves the model during training. By default, it saves the model every epoch; increasing the epoch_interval will make checkpointing less frequent.

If the given filename contains the {} string, the epoch number will be subtituted in, resulting in multiple checkpoint folders with different names. If a filename such as ‘ivis-checkpoint’ is provided, only the latest checkpoint will be kept.

Parameters:
  • log_dir (str) – Folder to save resulting embeddings.
  • filename (str) – Filename to save each file as. {} in string will be substituted with the epoch number.

Example usage:

from ivis.nn.callbacks import ModelCheckpoint
from ivis import Ivis

# Save only the latest checkpoint to current directory every 10 epochs
checkpoint_callback = ModelCheckpoint(log_dir='.',
                                    filename='latest-checkpoint.ivis',
                                    epoch_interval=10)

model = Ivis(callbacks=[checkpoint_callback])

EmbeddingsLogging

class ivis.nn.callbacks.EmbeddingsLogging(data, log_dir='./embeddings_logs', filename='{}_embeddings.npy', epoch_interval=1)

Bases: sphinx.ext.autodoc.importer._MockObject

Periodically saves embeddings of the data provided to data using the latest state of the Ivis model. By default, saves embeddings every epoch; increasing the epoch_interval will save the embeddings less frequently.

Parameters:
  • data (list[float]) – Data to embed with the latest Ivis object
  • log_dir (str) – Folder to save resulting embeddings.
  • filename (str) – Filename to save each file as. {} in string will be substituted with the epoch number.

Example usage:

from ivis.nn.callbacks import EmbeddingsLogging
from ivis import Ivis
from tensorflow.keras.datasets import mnsit

(X_train, Y_train), (X_test, Y_test)  = mnist.load_data()

# Save embeddings of test set every epoch
embeddings_callback = EmbeddingsLogging(X_test,
                                        log_dir='test-embeddings',
                                        filename='{}_test_embeddings.npy',
                                        epoch_interval=1)

model = Ivis(callbacks=[embeddings_callback])

# Train on training set
model.fit(X_train)

EmbeddingsImage

class ivis.nn.callbacks.EmbeddingsImage(data, labels=None, log_dir='./logs', filename='{}_embeddings.png', epoch_interval=1)

Bases: sphinx.ext.autodoc.importer._MockObject

Periodically generates and plots 2D embeddings of the data provided to data using the latest state of the Ivis model. By default, saves plots of the embeddings every epoch; increasing the epoch_interval will save the plots less frequently.

Parameters:
  • data (list[float]) – Data to embed and plot with the latest Ivis model
  • labels (list[int]) – Labels with which to colour plotted embeddings. If None all points will have the same color.
  • log_dir (str) – Folder to save resulting embeddings.
  • filename (str) – Filename to save each file as. {} in string will be substituted with the epoch number.

Example usage:

from ivis.nn.callbacks import EmbeddingsImage
from ivis import Ivis
from tensorflow.keras.datasets import mnsit

(X_train, Y_train), (X_test, Y_test)  = mnist.load_data()

# Plot embeddings of test set every epoch colored by labels
embeddings_callback = EmbeddingsImage(X_test, Y_test,
                                        log_dir='test-embeddings',
                                        filename='{}_test_embeddings.npy',
                                        epoch_interval=1)

model = Ivis(callbacks=[embeddings_callback])

# Train on training set
model.fit(X_train)

TensorBoardEmbeddingsImage

class ivis.nn.callbacks.TensorBoardEmbeddingsImage(data, labels=None, log_dir='./logs', epoch_interval=1)

Bases: sphinx.ext.autodoc.importer._MockObject

Periodically generates and plots 2D embeddings of the data provided to data using the latest state of the Ivis model. The plots are designed to be viewed in Tensorboard, which will provide an image that shows the history of embeddings plots through training. By default, saves plots of the embeddings every epoch; increasing the epoch_interval will save the plots less frequently.

Parameters:
  • data (list[float]) – Data to embed and plot with the latest Ivis
  • labels (list[int]) – Labels with which to colour plotted embeddings. If None all points will have the same color.
  • log_dir (str) – Folder to save resulting embeddings.
  • filename (str) – Filename to save each file as. {} in string will be substituted with the epoch number.

Example usage:

from ivis.nn.callbacks import TensorBoardEmbeddingsImage
from ivis import Ivis
from tensorflow.keras.datasets import mnsit

(X_train, Y_train), (X_test, Y_test)  = mnist.load_data()

# Plot embeddings of test set every epoch colored by labels
embeddings_callback = TensorBoardEmbeddingsImage(X_test, Y_test,
                                        log_dir='test-embeddings',
                                        filename='{}_test_embeddings.npy',
                                        epoch_interval=1)

model = Ivis(callbacks=[embeddings_callback])

# Train on training set
model.fit(X_train)