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)
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()
def test_array_reduce3(x): return array_reduce(scalar_add, x, ())
def test_array_reduce2(x): return array_reduce(scalar_add, x, (3, ))
def test_array_reduce(x): return array_reduce(scalar_add, x, (1, 3))
def before(x): return array_reduce(scalar_add, x, (3, 1))
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)
def prod(xs): p = array_reduce(lambda x, y: x * y, xs, ()) return array_to_scalar(p)
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, ()))
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)
def test_dot(x, y): d = dot(x, y) sm = array_reduce(scalar_add, d, ()) return array_to_scalar(sm)
def test_array_operations_std(xs, ys): div = xs / ys sm = array_reduce(scalar_add, div, ()) return array_to_scalar(sm)
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)
def cost(model, x, y): yy = model.apply(x) diff = (yy - y) return (array_reduce(scalar_add, diff**2, ())).item()
def helper(fn): return array_reduce(fn, xs, ()), array_reduce(fn, ys, ())
def test_array_reduce(ary, shp): def f(a, b): return a + b return array_reduce(f, ary, shp)
def f(x): def add(x, y): return x + y return array_reduce(add, x, (1, 3))
def f(xs, ys): return array_reduce(scalar_add, xs * ys, ())
def test_array_operations(xs, ys): div = array_map(scalar_div, xs, ys) sm = array_reduce(scalar_add, div, ()) return array_to_scalar(sm)