Exemplo n.º 1
0
def test_set_subtensor():
    x = TensorWrapper([1, 2, 3])
    x[:] = [1, 1, 1]
    np.testing.assert_almost_equal(x.numpy(), [1, 1, 1], decimal=6)
    x[[0, 2]] = [3, 2]
    np.testing.assert_almost_equal(x.numpy(), [3, 1, 2], decimal=6)
    x[1:3] = [4, 5]
    np.testing.assert_almost_equal(x.numpy(), [3, 4, 5], decimal=6)
Exemplo n.º 2
0
def test_release():
    def check(f):
        n = 0
        d = None
        for i in range(3):
            f()
            m = len(gc.get_objects())
            d = m - n
            n = m
        assert d == 0

    x = TensorWrapper([0.0])
    dy = TensorWrapper(np.ones_like(x.numpy()))

    @check
    def _():
        g = Grad().wrt(x)
        y = x * x
        g(y, dy)

    @check
    def _():
        with Grad().wrt(x) as g:
            pass

    @check
    def _():
        with Grad().wrt(x) as g:
            y = x * x
Exemplo n.º 3
0
def test_matmul():
    A = TensorWrapper(np.random.rand(5, 7).astype("float32"))
    B = TensorWrapper(np.random.rand(7, 10).astype("float32"))
    C = A @ B
    np.testing.assert_almost_equal(C.numpy(), A.numpy() @ B.numpy(), decimal=6)