예제 #1
0
    def testCosineDistancesExecution(self):
        raw_dense_x = np.random.rand(25, 10)
        raw_dense_y = np.random.rand(17, 10)

        raw_sparse_x = sps.random(25, 10, density=0.5, format='csr', random_state=0)
        raw_sparse_y = sps.random(17, 10, density=0.4, format='csr', random_state=1)

        for raw_x, raw_y in [
            (raw_dense_x, raw_dense_y),
            (raw_sparse_x, raw_sparse_y)
        ]:
            for chunk_size in (25, 6):
                x = mt.tensor(raw_x, chunk_size=chunk_size)
                y = mt.tensor(raw_y, chunk_size=chunk_size)

                d = cosine_distances(x, y)

                result = self.executor.execute_tensor(d, concat=True)[0]
                expected = sk_cosine_distances(raw_x, raw_y)

                np.testing.assert_almost_equal(np.asarray(result), expected)

                d = cosine_distances(x)

                result = self.executor.execute_tensor(d, concat=True)[0]
                expected = sk_cosine_distances(raw_x)

                np.testing.assert_almost_equal(np.asarray(result), expected)
예제 #2
0
def test_cosine_distances_execution(setup, raw_x, raw_y, chunk_size):
    x = mt.tensor(raw_x, chunk_size=chunk_size)
    y = mt.tensor(raw_y, chunk_size=chunk_size)

    d = cosine_distances(x, y)

    result = d.execute().fetch()
    expected = sk_cosine_distances(raw_x, raw_y)

    np.testing.assert_almost_equal(np.asarray(result), expected)

    d = cosine_distances(x)

    result = d.execute().fetch()
    expected = sk_cosine_distances(raw_x)

    np.testing.assert_almost_equal(np.asarray(result), expected)