<jupyter_start><jupyter_text>Custom embedder for parametric UMAP. This notebook shows you how to run a UMAP projection with a custom embedder.   load data<jupyter_code>from tensorflow.keras.datasets import mnist
(train_images, Y_train), (test_images, Y_test) = mnist.load_data()
train_images = train_images.reshape((train_images.shape[0], -1))/255.
test_images = test_images.reshape((test_images.shape[0], -1))/255.<jupyter_output><empty_output><jupyter_text>define the encoder network<jupyter_code>dims = (28,28, 1)
n_components = 2
encoder = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=dims),
    tf.keras.layers.Conv2D(
        filters=64, kernel_size=3, strides=(2, 2), activation="relu", padding="same"
    ),
    tf.keras.layers.Conv2D(
        filters=128, kernel_size=3, strides=(2, 2), activation="relu", padding="same"
    ),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(units=512, activation="relu"),
    tf.keras.layers.Dense(units=512, activation="relu"),
    tf.keras.layers.Dense(units=n_components),
])
encoder.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 14, 14, 64)        640       
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 7, 7, 128)         73856     
_________________________________________________________________
flatten (Flatten)            (None, 6272)              0         
_________________________________________________________________
dense (Dense)                (None, 512)               3211776   
_________________________________________________________________
dense_1 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 1026      
========================================================[...]<jupyter_text>create parametric umap model<jupyter_code>from umap.parametric_umap import ParametricUMAP
embedder = ParametricUMAP(encoder=encoder, dims=dims, n_training_epochs=5, verbose=True)
embedding = embedder.fit_transform(train_images)<jupyter_output>ParametricUMAP(dims=(28, 28, 1),
               encoder=<tensorflow.python.keras.engine.sequential.Sequential object at 0x7f9b902396a0>,
               n_training_epochs=5,
               optimizer=<tensorflow.python.keras.optimizer_v2.adam.Adam object at 0x7f9968e0ef60>)
Construct fuzzy simplicial set
Sun Aug 16 18:21:05 2020 Finding Nearest Neighbors
Sun Aug 16 18:21:05 2020 Building RP forest with 17 trees
Sun Aug 16 18:21:07 2020 parallel NN descent for 16 iterations
	 0  /  16
	 1  /  16
	 2  /  16
	 3  /  16
	 4  /  16
Sun Aug 16 18:21:17 2020 Finished Nearest Neighbor Search
Sun Aug 16 18:21:20 2020 Construct embedding
Epoch 1/50
  1/724 [..............................] - ETA: 0s - loss: 5.5447<jupyter_text>plot results<jupyter_code>embedding = embedder.embedding_
import matplotlib.pyplot as plt
fig, ax = plt.subplots( figsize=(8, 8))
sc = ax.scatter(
    embedding[:, 0],
    embedding[:, 1],
    c=Y_train.astype(int),
    cmap="tab10",
    s=0.1,
    alpha=0.5,
    rasterized=True,
)
ax.axis('equal')
ax.set_title("UMAP in Tensorflow embedding", fontsize=20)
plt.colorbar(sc, ax=ax);<jupyter_output><empty_output><jupyter_text>plotting loss<jupyter_code>embedder._history.keys()
fig, ax = plt.subplots()
ax.plot(embedder._history['loss'])
ax.set_ylabel('Cross Entropy')
ax.set_xlabel('Epoch')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14382448537378115, 'fraction_numerical': 0.05985915492957746, 'mean_word_length': 3.977088948787062, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '416859', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1134, 'n_words': 236, 'path': 'notebooks/Parametric_UMAP/02.0-parametric-umap-mnist-embedding-convnet.ipynb', 'repo_name': 'EricThomson/umap', 'size': '3692'}