Ejemplo 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)
Ejemplo 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()
Ejemplo n.º 3
0
def test_array_reduce3(x):
    return array_reduce(scalar_add, x, ())
Ejemplo n.º 4
0
def test_array_reduce2(x):
    return array_reduce(scalar_add, x, (3, ))
Ejemplo n.º 5
0
def test_array_reduce(x):
    return array_reduce(scalar_add, x, (1, 3))
Ejemplo n.º 6
0
 def before(x):
     return array_reduce(scalar_add, x, (3, 1))
Ejemplo n.º 7
0
def test_array_operations_distribute(x, y):
    xs = distribute(scalar_to_array(x), (4, 3))
    ys = distribute(scalar_to_array(y), (4, 3))
    div = array_map(scalar_div, xs, ys)
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Ejemplo n.º 8
0
 def prod(xs):
     p = array_reduce(lambda x, y: x * y, xs, ())
     return array_to_scalar(p)
Ejemplo n.º 9
0
def cost(model, x, target):
    y = model.apply(x)
    diff = target - y
    diffsqr = diff * diff
    return array_to_scalar(array_reduce(scalar_add, diff * diff, ()))
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
def test_dot(x, y):
    d = dot(x, y)
    sm = array_reduce(scalar_add, d, ())
    return array_to_scalar(sm)
Ejemplo n.º 12
0
def test_array_operations_std(xs, ys):
    div = xs / ys
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
def cost(model, x, y):
    yy = model.apply(x)
    diff = (yy - y)
    return (array_reduce(scalar_add, diff**2, ())).item()
Ejemplo n.º 15
0
 def helper(fn):
     return array_reduce(fn, xs, ()), array_reduce(fn, ys, ())
Ejemplo n.º 16
0
def test_array_reduce(ary, shp):
    def f(a, b):
        return a + b

    return array_reduce(f, ary, shp)
Ejemplo n.º 17
0
    def f(x):
        def add(x, y):
            return x + y

        return array_reduce(add, x, (1, 3))
Ejemplo n.º 18
0
 def f(xs, ys):
     return array_reduce(scalar_add, xs * ys, ())
Ejemplo n.º 19
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)