Example #1
0
def test_balanced_pair_dataset_n_pairs():
    x = np.zeros((2, 784))

    try:
        y = np.zeros(2)
        new_balanced_pair_dataset = pair.BalancedPairDataset(x, y)
    except:
        y = np.asarray([0, 1])
        new_balanced_pair_dataset = pair.BalancedPairDataset(x, y)

    assert new_balanced_pair_dataset.n_pairs == 2
Example #2
0
def test_balanced_pair_dataset_batches():
    x = np.zeros((2, 784))
    y = np.asarray([0, 1])

    new_balanced_pair_dataset = pair.BalancedPairDataset(x, y)

    assert new_balanced_pair_dataset.batches is not None
Example #3
0
def test_cross_entropy_fit():
    (x, y), (_, _) = tf.keras.datasets.mnist.load_data()
    train = pair.BalancedPairDataset(x, y, n_pairs=10, input_shape=(x.shape[0], 784))

    new_base = mlp.MLP()
    new_siamese = cross_entropy.CrossEntropySiamese(new_base)
    new_siamese.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001))

    new_siamese.fit(train.batches, epochs=1)
Example #4
0
def test_balanced_pair_dataset_create_pairs():
    x = np.zeros((2, 784))
    y = np.asarray([0, 1])

    new_balanced_pair_dataset = pair.BalancedPairDataset(x, y)

    pairs = new_balanced_pair_dataset.create_pairs(x, y)

    assert len(pairs) == 3
Example #5
0
def test_balanced_pair_dataset_build():
    x = np.zeros((2, 784))
    y = np.asarray([0, 1])

    new_balanced_pair_dataset = pair.BalancedPairDataset(x, y, shuffle=True)

    pairs = new_balanced_pair_dataset.create_pairs(x, y)

    new_balanced_pair_dataset._build(pairs)

    assert new_balanced_pair_dataset.batches is not None

    new_balanced_pair_dataset = pair.BalancedPairDataset(x, y, shuffle=False)

    pairs = new_balanced_pair_dataset.create_pairs(x, y)

    new_balanced_pair_dataset._build(pairs)

    assert new_balanced_pair_dataset.batches is not None
Example #6
0
def test_balanced_pair_dataset_batches_setter():
    x = np.zeros((2, 784))
    y = np.asarray([0, 1])

    new_balanced_pair_dataset = pair.BalancedPairDataset(x, y)

    try:
        new_balanced_pair_dataset.batches = 1
    except:
        pass

    assert new_balanced_pair_dataset.batches != 1
Example #7
0
def test_contrastive_predict():
    (x, y), (_, _) = tf.keras.datasets.mnist.load_data()
    train = pair.BalancedPairDataset(x,
                                     y,
                                     n_pairs=10,
                                     input_shape=(x.shape[0], 784))

    new_base = mlp.MLP()
    new_siamese = contrastive.ContrastiveSiamese(new_base)
    new_siamese.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001))

    new_siamese.fit(train.batches, epochs=1)

    x1 = tf.ones((1, 784))
    x2 = tf.ones((1, 784))

    new_siamese.distance = "L1"
    new_siamese.predict(x1, x2)

    new_siamese.distance = "L2"
    new_siamese.predict(x1, x2)

    new_siamese.distance = "angular"
    new_siamese.predict(x1, x2)