Exemplo n.º 1
0
def test_transpose2(x, y, axis1, axis2):
    perm = (scalar_cast(axis1, u64), scalar_cast(axis2, u64))
    xt = transpose(x, perm)
    yt = transpose(y, perm)
    d = dot(xt, yt)
    sm = array_reduce(scalar_add, d, ())
    return array_to_scalar(sm)
Exemplo n.º 2
0
def test_prim_array_reduce():
    def add(a, b):
        return a + b

    tests = [
        (add, (2, 3, 7), (1, 3, 1), 14),
        (add, (2, 3, 7), (1, 3, 8), ValueError),
        (add, (2, 3, 7), (1, 2, 3, 7), ValueError),
        (add, (2, 3, 7), (3, 1), 14),
        (add, (2, 3, 7), (1, 1, 1), 42),
        (add, (2, 3, 7), (), 42),
    ]

    for f, inshp, outshp, value in tests:
        v = np.ones(inshp)
        try:
            res = array_reduce(f, v, outshp)
        except Exception as e:
            if isinstance(value, type) and isinstance(e, value):
                continue
            else:
                print(f'Expected {value}, but got {e}')
                raise

        assert res.shape == outshp
        assert (res == value).all()
Exemplo n.º 3
0
def test_transpose(x, y):
    xt = transpose(x, (1, 0))
    yt = transpose(y, (1, 0))
    d = dot(xt, yt)
    sm = array_reduce(scalar_add, d, ())
    return array_to_scalar(sm)
Exemplo n.º 4
0
def test_dot(x, y):
    d = dot(x, y)
    sm = array_reduce(scalar_add, d, ())
    return array_to_scalar(sm)
Exemplo n.º 5
0
def test_array_operations_std(xs, ys):
    div = xs / ys
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Exemplo n.º 6
0
def test_array_operations_reshape(xs, ys):
    xs = reshape(xs, (6, ))
    ys = reshape(ys, (6, ))
    div = array_map(scalar_div, xs, ys)
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Exemplo n.º 7
0
def test_array_operations_distribute(x, y):
    xs = distribute(scalar_to_array(x, AA), (4, 3))
    ys = distribute(scalar_to_array(y, AA), (4, 3))
    div = array_map(scalar_div, xs, ys)
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Exemplo n.º 8
0
def test_array_operations(xs, ys):
    div = array_map(scalar_div, xs, ys)
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Exemplo n.º 9
0
    def f(x):
        def add(x, y):
            return x + y

        return array_reduce(add, x, (1, 3))
Exemplo n.º 10
0
def test_array_reduce2(x):
    return array_reduce(scalar_add, x, (3, ))
Exemplo n.º 11
0
def test_array_reduce(x):
    return array_reduce(scalar_add, x, (1, 3))
Exemplo n.º 12
0
 def helper(fn):
     return array_reduce(fn, xs, ()), array_reduce(fn, ys, ())
Exemplo n.º 13
0
 def before(x):
     return array_reduce(scalar_add, x, (3, 1))
Exemplo n.º 14
0
def cost(model, x, y):
    yy = model.apply(x)
    diff = (yy - y)
    return (array_reduce(scalar_add, diff**2, ())).item()