<jupyter_start><jupyter_text>Using callbacks to monitor Ivis performance A callback is an object that can perform a useful action at a specific stage of model training. In addition to supporting [traditional tensorflow callbacks](https://www.tensorflow.org/api_docs/python/tf/keras/callbacks) that ship with tensorflow, [`Ivis`](https://github.com/beringresearch/ivis) callbacks can be used to:* Create a model checkpoint* Save intermediate embeddings* Visualise intermediate embeddings* Send outputs to TensorboardYou can checkout a more detailed API on our [documentation page](https://bering-ivis.readthedocs.io/en/latest/api.htmlmodule-ivis.nn.callbacks).<jupyter_code>!pip install git+https://github.com/beringresearch/ivis
# Ivis uses several stochastic processes.
# To make sure that results are consistent from one run to another,
# we fix the random seed

import os
os.environ["PYTHONHASHSEED"]="1234"

import random
import numpy as np

import tensorflow as tf

np.random.seed(1234)
random.seed(1234)
tf.random.set_seed(1234)<jupyter_output><empty_output><jupyter_text>1. Data Import and Processing<jupyter_code>from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import MinMaxScaler
data = load_breast_cancer()
X = data['data']
y = data['target']
scaler = MinMaxScaler()
X = scaler.fit_transform(X)<jupyter_output><empty_output><jupyter_text>2. Using callbacks with Ivis<jupyter_code>import tensorflow as tf

from ivis.nn.callbacks import EmbeddingsImage, EmbeddingsLogging, ModelCheckpoint
from ivis import Ivis
# Keras callback logging training loss at the end of every epoch
logger = tf.keras.callbacks.CSVLogger('ivis_loss.log', append=False)

# Generate an embedding plot at the end of every epoch. Requires seaborn
image_viewer = EmbeddingsImage(data=X)

# Save ivis model at the end of every epoch
checkpoint = ModelCheckpoint()
ivis = Ivis(model='maaten', k=15, n_epochs_without_progress=5,
           callbacks=[logger, image_viewer, checkpoint])
ivis.fit(X)<jupyter_output>100%|██████████| 569/569 [00:00<00:00, 54949.32it/s]<jupyter_text>This training run will generate two directories - `./logs` and `./model_checkpoints` for epoch-wise embeddings and intermediate models respectively. Names of these directories are fully configurable.  3. Visualise final embeddings<jupyter_code>import matplotlib.pyplot as plt

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')
embeddings = ivis.transform(X)
plt.scatter(embeddings[:, 0], embeddings[:, 1], c=y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09507042253521127, 'fraction_numerical': 0.018779342723004695, 'mean_word_length': 6.4985337243401755, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '448972', 'license': 'Apache-2.0', 'n_tokens_mistral': 849, 'n_tokens_neox': 791, 'n_words': 235, 'path': 'notebooks/using_callbacks_with_ivis.ipynb', 'repo_name': 'escherba/ivis', 'size': '2556'}