def test_custom_encoder_decoder(moon_dataset): """test using a custom encoder / decoder""" dims = (2, ) n_components = 2 encoder = tf.keras.Sequential([ tf.keras.layers.InputLayer(input_shape=dims), tf.keras.layers.Flatten(), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=n_components, name="z"), ]) decoder = tf.keras.Sequential([ tf.keras.layers.InputLayer(input_shape=n_components), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=np.product(dims), name="recon", activation=None), tf.keras.layers.Reshape(dims), ]) embedder = ParametricUMAP( encoder=encoder, decoder=decoder, dims=dims, parametric_reconstruction=True, verbose=True, ) embedding = embedder.fit_transform(moon_dataset) # completes successfully assert embedding is not None assert embedding.shape == (moon_dataset.shape[0], 2)
def test_create_model(moon_dataset): """test a simple parametric UMAP network""" embedder = ParametricUMAP() embedding = embedder.fit_transform(moon_dataset) # completes successfully assert embedding is not None assert embedding.shape == (moon_dataset.shape[0], 2)
def test_nonparametric(moon_dataset): """test nonparametric embedding""" embedder = ParametricUMAP(parametric_embedding=False) embedding = embedder.fit_transform(moon_dataset) # completes successfully assert embedding is not None assert embedding.shape == (moon_dataset.shape[0], 2)
def test_global_loss(moon_dataset): """test a simple parametric UMAP network""" embedder = ParametricUMAP(global_correlation_loss_weight=1.0) embedding = embedder.fit_transform(moon_dataset) # completes successfully assert embedding is not None assert embedding.shape == (moon_dataset.shape[0], 2)
def test_create_model(): """ test a simple parametric UMAP network """ X, y = make_moons(100) embedder = ParametricUMAP() embedding = embedder.fit_transform(X) return embedding, y
def test_validation(): """tests adding a validation dataset""" X, y = make_moons(100) X_valid, y = make_moons(100) embedder = ParametricUMAP( parametric_reconstruction=True, reconstruction_validation=X_valid, verbose=True, ) embedding = embedder.fit_transform(X)
def test_save_load(): """ tests saving and loading """ X, y = make_moons(100) embedder = ParametricUMAP() embedding = embedder.fit_transform(X) embedder.save("/tmp/model") embedder = load_ParametricUMAP("/tmp/model")
def test_validation(moon_dataset): """tests adding a validation dataset""" X_train, X_valid = train_test_split(moon_dataset, train_size=0.5) embedder = ParametricUMAP(parametric_reconstruction=True, reconstruction_validation=X_valid, verbose=True) embedding = embedder.fit_transform(X_train) # completes successfully assert embedding is not None assert embedding.shape == (X_train.shape[0], 2)
def umap_model(optim, batch_size, epochs, verbose=False, save_path=None, config=None): if save_path is None: return ParametricUMAP(optimizer=optim, batch_size=batch_size, dims=None, encoder=None, # you could enter another network here loss_report_frequency=1, n_training_epochs=epochs, verbose=verbose) else: return load_ParametricUMAP(save_path)
def test_save_load(): """tests saving and loading""" X, y = make_moons(100) embedder = ParametricUMAP() embedding = embedder.fit_transform(X) # if platform.system() != "Windows": # Portable tempfile model_path = tempfile.mkdtemp(suffix="_umap_model") embedder.save(model_path) embedder = load_ParametricUMAP(model_path)
def test_inverse_transform(): """tests inverse_transform""" def norm(x): return (x - np.min(x)) / (np.max(x) - np.min(x)) X, y = make_moons(100) X = norm(X) embedder = ParametricUMAP(parametric_reconstruction=True) embedding = embedder.fit_transform(X) Z = embedder.transform(X) X_r = embedder.inverse_transform(Z)
def test_inverse_transform(moon_dataset): """tests inverse_transform""" def norm(x): return (x - np.min(x)) / (np.max(x) - np.min(x)) X = norm(moon_dataset) embedder = ParametricUMAP(parametric_reconstruction=True) Z = embedder.fit_transform(X) X_r = embedder.inverse_transform(Z) # completes successfully assert X_r is not None assert X_r.shape == X.shape
def test_save_load(moon_dataset): """tests saving and loading""" embedder = ParametricUMAP() embedding = embedder.fit_transform(moon_dataset) # completes successfully assert embedding is not None assert embedding.shape == (moon_dataset.shape[0], 2) # if platform.system() != "Windows": # Portable tempfile model_path = tempfile.mkdtemp(suffix="_umap_model") embedder.save(model_path) loaded_model = load_ParametricUMAP(model_path) assert loaded_model is not None
def test_custom_encoder_decoder(): """ test using a custom encoder / decoder """ X, y = make_moons(100) dims = (2,) n_components = 2 encoder = tf.keras.Sequential( [ tf.keras.layers.InputLayer(input_shape=dims), tf.keras.layers.Flatten(), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=n_components, name="z"), ] ) decoder = tf.keras.Sequential( [ tf.keras.layers.InputLayer(input_shape=n_components), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense(units=100, activation="relu"), tf.keras.layers.Dense( units=np.product(dims), name="recon", activation=None ), tf.keras.layers.Reshape(dims), ] ) embedder = ParametricUMAP( encoder=encoder, decoder=decoder, dims=dims, parametric_reconstruction=True, verbose=True, ) embedding = embedder.fit_transform(X)
def test_save_load(moon_dataset): """tests saving and loading""" embedder = ParametricUMAP() embedding = embedder.fit_transform(moon_dataset) # completes successfully assert embedding is not None assert embedding.shape == (moon_dataset.shape[0], 2) # Portable tempfile model_path = tempfile.mkdtemp(suffix="_umap_model") embedder.save(model_path) loaded_model = load_ParametricUMAP(model_path) assert loaded_model is not None loaded_embedding = loaded_model.transform(moon_dataset) assert_array_almost_equal( embedding, loaded_embedding, decimal=5, err_msg="Loaded model transform fails to match original embedding", )
def test_nonparametric(): """ test nonparametric embedding """ X, y = make_moons(100) embedder = ParametricUMAP(parametric_embedding=False) embedding = embedder.fit_transform(X)