Example #1
0
    def testNormExecution(self):
        d = np.arange(9) - 4
        d2 = d.reshape(3, 3)

        ma = [tensor(d, chunk_size=2), tensor(d, chunk_size=9),
              tensor(d2, chunk_size=(2, 3)), tensor(d2, chunk_size=3)]

        for i, a in enumerate(ma):
            data = d if i < 2 else d2
            for ord in (None, 'nuc', np.inf, -np.inf, 0, 1, -1, 2, -2):
                for axis in (0, 1, (0, 1)):
                    for keepdims in (True, False):
                        try:
                            expected = np.linalg.norm(data, ord=ord, axis=axis, keepdims=keepdims)
                            t = norm(a, ord=ord, axis=axis, keepdims=keepdims)
                            concat = t.ndim > 0
                            res = self.executor.execute_tensor(t, concat=concat)[0]

                            np.testing.assert_allclose(res, expected, atol=.0001)
                        except ValueError:
                            continue

        m = norm(tensor(d))
        expected = self.executor.execute_tensor(m)[0]
        res = np.linalg.norm(d)
        self.assertEqual(expected, res)

        d = uniform(-0.5, 0.5, size=(500, 2), chunk_size=50)
        inside = (norm(d, axis=1) < 0.5).sum().astype(float)
        t = inside / 500 * 4
        res = self.executor.execute_tensor(t)[0]
        self.assertAlmostEqual(res, 3.14, delta=1)
Example #2
0
def test_norm_execution(setup):
    d = np.arange(9) - 4
    d2 = d.reshape(3, 3)

    ma = [tensor(d, chunk_size=2), tensor(d2, chunk_size=(2, 3))]

    for i, a in enumerate(ma):
        data = d if i < 1 else d2
        for ord in (None, 'nuc', np.inf, -np.inf, 0, 1, -1, 2, -2):
            for axis in (0, 1, (0, 1), -1):
                for keepdims in (True, False):
                    try:
                        expected = np.linalg.norm(data,
                                                  ord=ord,
                                                  axis=axis,
                                                  keepdims=keepdims)
                        t = norm(a, ord=ord, axis=axis, keepdims=keepdims)
                        res = t.execute().fetch()

                        expected_shape = expected.shape
                        t_shape = t.shape
                        assert expected_shape == t_shape

                        np.testing.assert_allclose(res, expected, atol=.0001)
                    except ValueError:
                        continue

    m = norm(tensor(d))
    expected = m.execute().fetch()
    res = np.linalg.norm(d)
    assert expected == res

    d = uniform(-0.5, 0.5, size=(5000, 2), chunk_size=1000)
    inside = (norm(d, axis=1) < 0.5).sum().astype(float)
    t = inside / 5000 * 4
    res = t.execute().fetch()
    np.testing.assert_almost_equal(3.14, res, decimal=1)

    raw = np.random.RandomState(0).rand(10, 10)
    d = norm(tensor(raw, chunk_size=5))
    expected = d.execute().fetch()
    result = np.linalg.norm(raw)
    np.testing.assert_allclose(expected, result)