Esempio n. 1
0
def build_loss(embeddings):
    """Return a pair (loss, p) given a theano shared variable representing the
    `embeddings`.

    `loss` is a theano variable for the loss. `p` is a symbolic variable
    representing the target neighbour probabilities on which the loss depends.
    """
    # Probability that two points are neighbours in the embedding space.
    emb_dists = distance_matrix(embeddings)
    emb_top = zero_diagonal(1 / (1 + emb_dists))
    emb_bottom = emb_top.sum(axis=0)
    q = emb_top / emb_bottom

    # Incorrect normalization which does not matter since we normalize p i
    # the same way.
    q /= q.sum()
    q = T.maximum(q, 1E-12)

    p_ji_var = T.matrix('neighbour_probabilities')
    p_ji_var.tag.test_value = np.random.random(
            (10, 10)).astype(theano.config.floatX)
    p_ji_var_floored = T.maximum(p_ji_var, 1E-12)

    # t-distributed stochastic neighbourhood embedding loss.
    loss = (p_ji_var * T.log(p_ji_var_floored / q)).sum()

    return loss, p_ji_var
Esempio n. 2
0
def build_loss(embeddings):
    """Return a pair (loss, p) given a theano shared variable representing the
    `embeddings`.

    `loss` is a theano variable for the loss. `p` is a symbolic variable
    representing the target neighbour probabilities on which the loss depends.
    """
    # Probability that two points are neighbours in the embedding space.
    emb_dists = distance_matrix(embeddings)
    emb_top = zero_diagonal(1 / (1 + emb_dists))
    emb_bottom = emb_top.sum(axis=0)
    q = emb_top / emb_bottom

    # Incorrect normalization which does not matter since we normalize p i
    # the same way.
    q /= q.sum()
    q = T.maximum(q, 1E-12)

    p_ji_var = T.matrix('neighbour_probabilities')
    p_ji_var_floored = T.maximum(p_ji_var, 1E-12)

    # t-distributed stochastic neighbourhood embedding loss.
    loss = (p_ji_var * T.log(p_ji_var_floored / q)).sum()

    return loss, p_ji_var
Esempio n. 3
0
def test_distance_matrix():
    X = T.matrix()
    D = distance_matrix(X)
    f = theano.function([X], D, mode='FAST_COMPILE')
    x = np.array([[1], [2], [3]]).astype(theano.config.floatX)
    res = f(x)
    print res
    correct = np.allclose(res, np.array([[0, 1, 4], [1, 0, 1], [4, 1, 0]]))
    assert correct, 'distance matrix not working right'
Esempio n. 4
0
def test_distance_matrix():
    X = T.matrix()
    D = distance_matrix(X) ** 2
    f = theano.function([X], D, mode='FAST_COMPILE')
    x = np.array([[1], [2], [3]]).astype(theano.config.floatX)
    res = f(x)
    print res
    correct = np.allclose(res, (np.array([[0, 1, 4], [1, 0, 1], [4, 1, 0]])))
    assert correct, 'distance matrix not working right'
Esempio n. 5
0
def test_distance_matrix():
    X = T.matrix()
    D = distance_matrix(X)
    f = theano.function([X], D, mode='FAST_COMPILE')
    x = np.array([[1], [2], [3]])
    res = f(x)
    print res
    correct = roughly(res, np.array([[0, 1, 4], [1, 0, 1], [4, 1, 0]]))
    assert correct, 'distance matrix not working right'