Example #1
0
 def conditional_mean(
     self, tag: str, x: ep.Tensor, cond: ep.Tensor, step: int
 ) -> None:
     cond_ = cond.numpy()
     if ~cond_.any():
         return
     x_ = x.numpy()
     x_ = x_[cond_]
     self.writer.add_scalar(tag, x_.mean(axis=0).item(), step)
Example #2
0
def test_2d(x2d: Tensor, p: float, axis: int, keepdims: bool) -> None:
    assert isinstance(axis, int)  # see test4d for the more general test
    assert_allclose(
        lp(x2d, p, axis=axis, keepdims=keepdims).numpy(),
        norm(x2d.numpy(), ord=p, axis=axis, keepdims=keepdims),
        rtol=1e-6,
    )
    if p not in norms:
        return
    assert_allclose(
        norms[p](x2d, axis=axis, keepdims=keepdims).numpy(),
        norm(x2d.numpy(), ord=p, axis=axis, keepdims=keepdims),
        rtol=1e-6,
    )
Example #3
0
 def clear(self, dims: ep.Tensor):
     if self.tensor is None:
         self.tensor = dims
     dims = dims.numpy()
     assert dims.shape == (self.N, )
     assert dims.dtype == np.bool
     self.data[:, dims] = np.nan
Example #4
0
 def append(self, x: ep.Tensor):
     if self.tensor is None:
         self.tensor = x
     x = x.numpy()
     assert x.shape == (self.N, )
     self.data[self.next] = x
     self.next = (self.next + 1) % self.maxlen
Example #5
0
 def histogram(
     self, tag: str, x: ep.Tensor, step: int, *, first: bool = True
 ) -> None:
     x = x.numpy()
     self.writer.add_histogram(tag, x, step)
     if first:
         self.writer.add_scalar(tag + "/0", x[0].item(), step)
Example #6
0
def test_4d(x4d: Tensor, p: float, axis: Optional[ep.types.AxisAxes],
            keepdims: bool) -> None:
    actual = lp(x4d, p, axis=axis, keepdims=keepdims).numpy()

    # numpy does not support arbitrary axes (limited to vector and matrix norms)
    if axis is None:
        axes = tuple(range(x4d.ndim))
    elif not isinstance(axis, tuple):
        axes = (axis, )
    else:
        axes = axis
    del axis
    axes = tuple(i % x4d.ndim for i in axes)
    x = x4d.numpy()
    other = tuple(i for i in range(x.ndim) if i not in axes)
    x = np.transpose(x, other + axes)
    x = x.reshape(x.shape[:len(other)] + (-1, ))
    desired = norm(x, ord=p, axis=-1)
    if keepdims:
        shape = tuple(1 if i in axes else x4d.shape[i]
                      for i in range(x4d.ndim))
        desired = desired.reshape(shape)

    assert_allclose(actual, desired, rtol=1e-6)
Example #7
0
def test_numpy_inplace(t: Tensor) -> None:
    copy = t + 0
    a = t.numpy().copy()
    a[:] += 1
    assert (t == copy).all()
Example #8
0
def test_numpy_readonly(t: Tensor) -> None:
    a = t.numpy()
    assert a.flags.writeable is False
    with pytest.raises(ValueError, match="read-only"):
        a[:] += 1
Example #9
0
def test_1d(x1d: Tensor, p: float) -> None:
    assert_allclose(lp(x1d, p).numpy(), norm(x1d.numpy(), ord=p))
    assert_allclose(norms[p](x1d).numpy(), norm(x1d.numpy(), ord=p))