<commit_before># -*- coding: utf-8 -*-

import pytest

import numpy as np
import tensorflow as tf

from inferbeddings.models import TranslatingModel, BilinearDiagonalModel, BilinearModel
from inferbeddings.models import similarities


def test_translating_embeddings_score():
    batch_size = 5
    embedding_size = 10

    rs = np.random.RandomState(0)

    E = rs.rand(batch_size, 2, embedding_size)
    R = rs.rand(batch_size, 1, embedding_size)

    vE = tf.Variable(E, name='E')
    vR = tf.Variable(R, name='R')

    model = TranslatingModel(vE, vR, similarities.negative_l1_distance)
    scores = model()

    init_op = tf.initialize_all_variables()

    with tf.Session() as session:
        session.run(init_op)

        sscores = session.run(scores)
        assert(sscores.shape[0] == batch_size)

        tmp = - np.sum(np.abs(E[:, 0, :] + R[:, 0, :] - E[:, 1, :]), axis=1)
        assert(np.isclose(sscores, tmp).all())


def test_bilinear_diagonal_score():
    batch_size = 5
    embedding_size = 10

    rs = np.random.RandomState(0)

    E = rs.rand(batch_size, 2, embedding_size)
    R = rs.rand(batch_size, 1, embedding_size)

    vE = tf.Variable(E, name='E')
    vR = tf.Variable(R, name='R')

    model = BilinearDiagonalModel(vE, vR, similarities.negative_l1_distance)
    scores = model()

    init_op = tf.initialize_all_variables()

    with tf.Session() as session:
        session.run(init_op)

        sscores = session.run(scores)
        assert(sscores.shape[0] == batch_size)

        tmp = - np.sum(np.abs(E[:, 0, :] * R[:, 0, :] - E[:, 1, :]), axis=1)
        assert(np.isclose(sscores, tmp).all())


def test_bilinear_score():
    batch_size = 5
    entity_embedding_size = 2
    predicate_embedding_size = 4

    rs = np.random.RandomState(0)

    E = rs.rand(batch_size, 2, entity_embedding_size)
    R = rs.rand(batch_size, 4, predicate_embedding_size)

    vE = tf.Variable(E, name='E')
    vR = tf.Variable(R, name='R')

    model = BilinearModel(vE, vR, similarities.dot_product, entity_embedding_size=entity_embedding_size)
    scores = model()

    init_op = tf.initialize_all_variables()

    with tf.Session() as session:
        session.run(init_op)

        sscores = session.run(scores)

        assert (sscores.shape[0] == batch_size)

        for i in range(batch_size):
            es, eo = E[i, 0, :], E[i, 1, :]
            ep1 = np.reshape(R[i, 0, :], (entity_embedding_size, entity_embedding_size))
            ep2 = np.reshape(R[i, 1, :], (entity_embedding_size, entity_embedding_size))
            ep3 = np.reshape(R[i, 2, :], (entity_embedding_size, entity_embedding_size))
            ep4 = np.reshape(R[i, 3, :], (entity_embedding_size, entity_embedding_size))
            ep = np.dot(np.dot(np.dot(ep1, ep2), ep3), ep4)

            np.testing.assert_allclose(sscores[i], np.dot(np.dot(es, ep), eo))

if __name__ == '__main__':
    pytest.main([__file__])
<commit_msg>better variable names<commit_after># -*- coding: utf-8 -*-

import pytest

import numpy as np
import tensorflow as tf

from inferbeddings.models import TranslatingModel, BilinearDiagonalModel, BilinearModel
from inferbeddings.models import similarities


def test_translating_embeddings_score():
    batch_size = 5
    embedding_size = 10

    rs = np.random.RandomState(0)

    E = rs.rand(batch_size, 2, embedding_size)
    R = rs.rand(batch_size, 1, embedding_size)

    vE = tf.Variable(E, name='E')
    vR = tf.Variable(R, name='R')

    model = TranslatingModel(vE, vR, similarities.negative_l1_distance)
    scores = model()

    init_op = tf.initialize_all_variables()

    with tf.Session() as session:
        session.run(init_op)

        scores_value = session.run(scores)
        assert(scores_value.shape[0] == batch_size)

        tmp = - np.sum(np.abs(E[:, 0, :] + R[:, 0, :] - E[:, 1, :]), axis=1)
        assert(np.isclose(scores_value, tmp).all())


def test_bilinear_diagonal_score():
    batch_size = 5
    embedding_size = 10

    rs = np.random.RandomState(0)

    E = rs.rand(batch_size, 2, embedding_size)
    R = rs.rand(batch_size, 1, embedding_size)

    vE = tf.Variable(E, name='E')
    vR = tf.Variable(R, name='R')

    model = BilinearDiagonalModel(vE, vR, similarities.negative_l1_distance)
    scores = model()

    init_op = tf.initialize_all_variables()

    with tf.Session() as session:
        session.run(init_op)

        scores_value = session.run(scores)
        assert(scores_value.shape[0] == batch_size)

        tmp = - np.sum(np.abs(E[:, 0, :] * R[:, 0, :] - E[:, 1, :]), axis=1)
        assert(np.isclose(scores_value, tmp).all())


def test_bilinear_score():
    batch_size = 5
    entity_embedding_size = 2
    predicate_embedding_size = 4

    rs = np.random.RandomState(0)

    E = rs.rand(batch_size, 2, entity_embedding_size)
    R = rs.rand(batch_size, 4, predicate_embedding_size)

    vE = tf.Variable(E, name='E')
    vR = tf.Variable(R, name='R')

    model = BilinearModel(vE, vR, similarities.dot_product, entity_embedding_size=entity_embedding_size)
    scores = model()

    init_op = tf.initialize_all_variables()

    with tf.Session() as session:
        session.run(init_op)

        scores_value = session.run(scores)

        assert (scores_value.shape[0] == batch_size)

        for i in range(batch_size):
            es, eo = E[i, 0, :], E[i, 1, :]
            ep1 = np.reshape(R[i, 0, :], (entity_embedding_size, entity_embedding_size))
            ep2 = np.reshape(R[i, 1, :], (entity_embedding_size, entity_embedding_size))
            ep3 = np.reshape(R[i, 2, :], (entity_embedding_size, entity_embedding_size))
            ep4 = np.reshape(R[i, 3, :], (entity_embedding_size, entity_embedding_size))
            ep = np.dot(np.dot(np.dot(ep1, ep2), ep3), ep4)

            np.testing.assert_allclose(scores_value[i], np.dot(np.dot(es, ep), eo))

if __name__ == '__main__':
    pytest.main([__file__])

{'fraction_non_alphanumeric': 0.1388183511088539, 'fraction_numerical': 0.015574741831725072, 'mean_word_length': 3.140154169586545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '7307753', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2103, 'n_words': 416}