| 1 |
from typing import Optional |
| 2 |
import pytest |
| 3 |
from numpy.testing import assert_allclose |
| 4 |
from numpy.linalg import norm |
| 5 |
import numpy as np |
| 6 |
import eagerpy as ep |
| 7 |
from eagerpy import Tensor |
| 8 |
from eagerpy.norms import l0, l1, l2, linf, lp |
| 9 |
|
| 10 |
norms = {0: l0, 1: l1, 2: l2, ep.inf: linf} |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture |
| 14 |
def x1d(dummy: Tensor) -> Tensor: |
| 15 |
return ep.arange(dummy, 10).float32() / 7.0 |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def x2d(dummy: Tensor) -> Tensor: |
| 20 |
return ep.arange(dummy, 12).float32().reshape((3, 4)) / 7.0 |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture |
| 24 |
def x4d(dummy: Tensor) -> Tensor: |
| 25 |
return ep.arange(dummy, 2 * 3 * 4 * 5).float32().reshape((2, 3, 4, 5)) / 7.0 |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize("p", [0, 1, 2, ep.inf]) |
| 29 |
def test_1d(x1d: Tensor, p: float) -> None: |
| 30 |
assert_allclose(lp(x1d, p).numpy(), norm(x1d.numpy(), ord=p)) |
| 31 |
assert_allclose(norms[p](x1d).numpy(), norm(x1d.numpy(), ord=p)) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.parametrize("p", [0, 1, 2, 3, 4, ep.inf]) |
| 35 |
@pytest.mark.parametrize("axis", [0, 1, -1]) |
| 36 |
@pytest.mark.parametrize("keepdims", [False, True]) |
| 37 |
def test_2d(x2d: Tensor, p: float, axis: int, keepdims: bool) -> None: |
| 38 |
assert isinstance(axis, int) # see test4d for the more general test |
| 39 |
assert_allclose( |
| 40 |
lp(x2d, p, axis=axis, keepdims=keepdims).numpy(), |
| 41 |
norm(x2d.numpy(), ord=p, axis=axis, keepdims=keepdims), |
| 42 |
rtol=1e-6, |
| 43 |
) |
| 44 |
if p not in norms: |
| 45 |
return |
| 46 |
assert_allclose( |
| 47 |
norms[p](x2d, axis=axis, keepdims=keepdims).numpy(), |
| 48 |
norm(x2d.numpy(), ord=p, axis=axis, keepdims=keepdims), |
| 49 |
rtol=1e-6, |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize("p", [0, 1, 2, 3, 4, ep.inf]) |
| 54 |
@pytest.mark.parametrize( |
| 55 |
"axis", |
| 56 |
[ |
| 57 |
None, |
| 58 |
0, |
| 59 |
1, |
| 60 |
2, |
| 61 |
3, |
| 62 |
-1, |
| 63 |
-2, |
| 64 |
-3, |
| 65 |
-4, |
| 66 |
(0, 1), |
| 67 |
(1, 2), |
| 68 |
(1, 3), |
| 69 |
(1, 2, 3), |
| 70 |
(0, 1, 3), |
| 71 |
(2, 1, 0), |
| 72 |
], |
| 73 |
) |
| 74 |
@pytest.mark.parametrize("keepdims", [False, True]) |
| 75 |
def test_4d( |
| 76 |
x4d: Tensor, p: float, axis: Optional[ep.types.AxisAxes], keepdims: bool |
| 77 |
) -> None: |
| 78 |
actual = lp(x4d, p, axis=axis, keepdims=keepdims).numpy() |
| 79 |
|
| 80 |
# numpy does not support arbitrary axes (limited to vector and matrix norms) |
| 81 |
if axis is None: |
| 82 |
axes = tuple(range(x4d.ndim)) |
| 83 |
elif not isinstance(axis, tuple): |
| 84 |
axes = (axis,) |
| 85 |
else: |
| 86 |
axes = axis |
| 87 |
del axis |
| 88 |
axes = tuple(i % x4d.ndim for i in axes) |
| 89 |
x = x4d.numpy() |
| 90 |
other = tuple(i for i in range(x.ndim) if i not in axes) |
| 91 |
x = np.transpose(x, other + axes) |
| 92 |
x = x.reshape(x.shape[: len(other)] + (-1,)) |
| 93 |
desired = norm(x, ord=p, axis=-1) |
| 94 |
if keepdims: |
| 95 |
shape = tuple(1 if i in axes else x4d.shape[i] for i in range(x4d.ndim)) |
| 96 |
desired = desired.reshape(shape) |
| 97 |
|
| 98 |
assert_allclose(actual, desired, rtol=1e-6) |