Пример #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)
Пример #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()
Пример #3
0
def test_array_reduce3(x):
    return array_reduce(scalar_add, x, ())
Пример #4
0
def test_array_reduce2(x):
    return array_reduce(scalar_add, x, (3, ))
Пример #5
0
def test_array_reduce(x):
    return array_reduce(scalar_add, x, (1, 3))
Пример #6
0
 def before(x):
     return array_reduce(scalar_add, x, (3, 1))
Пример #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)
Пример #8
0
 def prod(xs):
     p = array_reduce(lambda x, y: x * y, xs, ())
     return array_to_scalar(p)
Пример #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, ()))
Пример #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)
Пример #11
0
def test_dot(x, y):
    d = dot(x, y)
    sm = array_reduce(scalar_add, d, ())
    return array_to_scalar(sm)
Пример #12
0
def test_array_operations_std(xs, ys):
    div = xs / ys
    sm = array_reduce(scalar_add, div, ())
    return array_to_scalar(sm)
Пример #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)
Пример #14
0
def cost(model, x, y):
    yy = model.apply(x)
    diff = (yy - y)
    return (array_reduce(scalar_add, diff**2, ())).item()
Пример #15
0
 def helper(fn):
     return array_reduce(fn, xs, ()), array_reduce(fn, ys, ())
Пример #16
0
def test_array_reduce(ary, shp):
    def f(a, b):
        return a + b

    return array_reduce(f, ary, shp)
Пример #17
0
    def f(x):
        def add(x, y):
            return x + y

        return array_reduce(add, x, (1, 3))
Пример #18
0
 def f(xs, ys):
     return array_reduce(scalar_add, xs * ys, ())
Пример #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)