Exemple #1
0
def test_custom_loss_ivis_callable(model_filepath):
    iris = datasets.load_iris()
    X = iris.data

    class EuclideanDistance:
        def __init__(self, margin=1):
            self.margin = margin
            self.__name__ = self.__class__.__name__

        def _euclidean_distance(self, x, y):
            return K.sqrt(
                K.maximum(K.sum(K.square(x - y), axis=-1, keepdims=True),
                          K.epsilon()))

        def __call__(self, y_true, y_pred):
            anchor, positive, negative = tf.unstack(y_pred)
            return K.mean(
                K.maximum(
                    self._euclidean_distance(anchor, positive) -
                    self._euclidean_distance(anchor, negative) + self.margin,
                    0))

    model = Ivis(distance=EuclideanDistance(margin=2),
                 k=15,
                 batch_size=16,
                 epochs=5)
    y_pred = model.fit_transform(X)

    # Test model saving and loading
    model.save_model(model_filepath, overwrite=True)
    model_2 = Ivis(distance=EuclideanDistance(margin=2))
    model_2.load_model(model_filepath)
    model_2.fit(X)
Exemple #2
0
def test_ivis_model_saving(model_filepath):
    model = Ivis(k=15, batch_size=16, n_epochs_without_progress=5)
    iris = datasets.load_iris()
    X = iris.data

    model.fit(X)
    model.save_model(model_filepath)

    model_2 = Ivis()
    model_2.load_model(model_filepath)

    # Check that model predictions are same
    assert np.all(model.transform(X) == model_2.transform(X))
    assert model.__getstate__() == model_2.__getstate__(
    )  # Serializable dict eles same

    # Check all weights are the same
    for model_layer, model_2_layer in zip(model.encoder.layers,
                                          model_2.encoder.layers):
        model_layer_weights = model_layer.get_weights()
        model_2_layer_weights = model_2_layer.get_weights()
        for i in range(len(model_layer_weights)):
            assert np.all(model_layer_weights[i] == model_2_layer_weights[i])

    # Check optimizer weights are the same
    for model_optimizer_weights, model_2_optimizer_weights in zip(
            model.model_.optimizer.get_weights(),
            model_2.model_.optimizer.get_weights()):
        assert np.all(model_optimizer_weights == model_2_optimizer_weights)

    # Check that trying to save over an existing folder raises an Exception
    with pytest.raises(FileExistsError) as exception_info:
        model.save_model(model_filepath)
    assert isinstance(exception_info.value, FileExistsError)
Exemple #3
0
def test_custom_model_saving(model_filepath):
    iris = datasets.load_iris()
    X = iris.data
    Y = iris.target

    # Create a custom model
    inputs = tf.keras.layers.Input(shape=(X.shape[-1], ))
    x = tf.keras.layers.Dense(128, activation='relu')(inputs)
    custom_model = tf.keras.Model(inputs, x)

    model = Ivis(k=15,
                 batch_size=16,
                 epochs=5,
                 supervision_metric='sparse_categorical_crossentropy',
                 model=custom_model)

    model.fit(X, Y)
    model.save_model(model_filepath, overwrite=True)

    model_2 = Ivis()
    model_2.load_model(model_filepath)

    # Check that model embeddings are same
    assert np.all(model.transform(X) == model_2.transform(X))
    # Check that model supervised predictions are same
    assert np.all(model.score_samples(X) == model_2.score_samples(X))
    # Serializable dict eles same
    assert model.__getstate__() == model_2.__getstate__()

    # Check all weights are the same
    for model_layer, model_2_layer in zip(model.encoder.layers,
                                          model_2.encoder.layers):
        model_layer_weights = model_layer.get_weights()
        model_2_layer_weights = model_2_layer.get_weights()
        for i in range(len(model_layer_weights)):
            assert np.all(model_layer_weights[i] == model_2_layer_weights[i])

    # Check optimizer weights are the same
    for w1, w2 in zip(model.model_.optimizer.get_weights(),
                      model_2.model_.optimizer.get_weights()):
        assert np.all(w1 == w2)

    # Train new model
    y_pred_2 = model_2.fit_transform(X, Y)
Exemple #4
0
def test_custom_loss_ivis(model_filepath):
    iris = datasets.load_iris()
    X = iris.data

    def euclidean_loss(y_true, y_pred):
        margin = 1
        anchor, positive, negative = tf.unstack(y_pred)
        return K.mean(
            K.maximum(
                euclidean_distance(anchor, positive) -
                euclidean_distance(anchor, negative) + margin, 0))

    model = Ivis(distance=euclidean_loss, k=15, batch_size=16, epochs=3)
    y_pred = model.fit_transform(X)

    # Test model saving and loading
    model.save_model(model_filepath, overwrite=True)
    model_2 = Ivis(distance=euclidean_loss)
    model_2.load_model(model_filepath)

    model_3 = Ivis()
    with pytest.raises(ValueError):
        model_3.load_model(model_filepath)
def test_save_overwriting(model_filepath):
    model = Ivis(k=15, batch_size=16, epochs=2)
    iris = datasets.load_iris()
    X = iris.data

    model.fit(X)
    model.save_model(model_filepath)

    # Check that trying to save over an existing folder raises an Exception
    with pytest.raises(FileExistsError) as exception_info:
        model.save_model(model_filepath)
        assert isinstance(exception_info.value, FileExistsError)

    # Check that can overwrite existing model if requested
    model.save_model(model_filepath, overwrite=True)
Exemple #6
0
def test_supervised_model_saving(model_filepath):
    model = Ivis(k=15,
                 batch_size=16,
                 epochs=5,
                 supervision_metric='sparse_categorical_crossentropy')
    iris = datasets.load_iris()
    X = iris.data
    Y = iris.target

    model.fit(X, Y)
    model.save_model(model_filepath, overwrite=True)

    model_2 = Ivis()
    model_2.load_model(model_filepath)

    # Check that model embeddings are same
    assert np.all(model.transform(X) == model_2.transform(X))
    # Check that model supervised predictions are same
    assert np.all(model.score_samples(X) == model_2.score_samples(X))
    # Serializable dict eles same
    assert model.__getstate__() == model_2.__getstate__()

    # Check all weights are the same
    for model_layer, model_2_layer in zip(model.encoder.layers,
                                          model_2.encoder.layers):
        model_layer_weights = model_layer.get_weights()
        model_2_layer_weights = model_2_layer.get_weights()
        for i in range(len(model_layer_weights)):
            assert np.all(model_layer_weights[i] == model_2_layer_weights[i])

    # Check optimizer weights are the same
    for w1, w2 in zip(model.model_.optimizer.get_weights(),
                      model_2.model_.optimizer.get_weights()):
        assert np.all(w1 == w2)

    # Check that trying to save over an existing folder raises an Exception
    with pytest.raises(FileExistsError) as exception_info:
        model.save_model(model_filepath)
        assert isinstance(exception_info.value, FileExistsError)

    # Check that can overwrite existing model if requested
    model.save_model(model_filepath, overwrite=True)

    # Train new model
    y_pred_2 = model_2.fit_transform(X, Y)
def test_untrained_model_persistence(model_filepath):
    model = Ivis(k=15, batch_size=16, epochs=2)
    model.save_model(model_filepath)