@@ -0,0 +1,2542 @@
+import numpy as np%0Aimport cudarray as ca%0Afrom .. import expr as ex%0A%0A%0Aif ca.float_ == np.float32:%0A    eps = 1e-04%0Aelse:%0A    eps = 1e-06%0A%0A%0Adef allclose(a, b, rtol=None, atol=None):%0A    if ca.float_ == np.float32:%0A        rtol = 1e-03 if rtol is None else rtol%0A        atol = 1e-04 if atol is None else atol%0A    else:%0A        rtol = 1e-05 if rtol is None else rtol%0A        atol = 1e-08 if atol is None else atol%0A    return np.allclose(a, b, rtol, atol)%0A%0A%0Adef gradclose(a, b, rtol=None, atol=None):%0A    if ca.float_ == np.float32:%0A        rtol = 1e-03 if rtol is None else rtol%0A        atol = 1e-04 if atol is None else atol%0A    else:%0A        rtol = 1e-05 if rtol is None else rtol%0A        atol = 1e-08 if atol is None else atol%0A    diff = abs(a - b) - atol - rtol * (abs(a) + abs(b))%0A    is_close = np.all(diff %3C 0)%0A    if not is_close:%0A        denom = abs(a) + abs(b)%0A        mask = denom == 0%0A        rel_error = abs(a - b) / (denom + mask)%0A        rel_error%5Bmask%5D = 0%0A        rel_error = np.max(rel_error)%0A        abs_error = np.max(abs(a - b))%0A        print('rel_error=%25.4e, abs_error=%25.4e, rtol=%25.2e, atol=%25.2e'%0A              %25 (rel_error, abs_error, rtol, atol))%0A    return is_close%0A%0A%0Adef approx_fprime(x, f, eps=None, *args):%0A    '''%0A    Central difference approximation of the gradient of a scalar function.%0A    '''%0A    if eps is None:%0A        eps = np.sqrt(np.finfo(ca.float_).eps)%0A    grad = np.zeros_like(x)%0A    step = np.zeros_like(x)%0A    for idx in np.ndindex(x.shape):%0A        step%5Bidx%5D = eps * max(abs(x%5Bidx%5D), 1.0)%0A        grad%5Bidx%5D = (f(*((x+step,) + args)) -%0A                     f(*((x-step,) + args))) / (2*step%5Bidx%5D)%0A        step%5Bidx%5D = 0.0%0A    return grad%0A%0A%0Aclass BPropableSource(ex.base.Op, ex.base.NoBPropMixin, ex.base.NoFPropMixin):%0A    bpropable = True%0A%0A    def __init__(self, array):%0A        if isinstance(array, np.ndarray):%0A            array = ca.array(array)%0A        self.shape = array.shape%0A        self.array = array%0A        self.grad_array = ca.zeros(self.shape)%0A%0A%0Adef graph_funs(src, sink, seed=1):%0A    graph = ex.graph.ExprGraph(sink)%0A    graph.setup()%0A%0A    def fun(x):%0A        ca.random.seed(seed)%0A        src.array = ca.array(x)%0A        graph.fprop()%0A        y = np.array(sink.array).astype(np.float_)%0A        return np.sum(y)%0A%0A    def fun_grad(x):%0A        ca.random.seed(seed)%0A        src.array = ca.array(x)%0A        graph.fprop()%0A        sink.grad_array = ca.ones(sink.shape, dtype=ca.float_)%0A        graph.bprop()%0A        x_grad = np.array(src.grad_array)%0A        return x_grad%0A%0A    return fun, fun_grad%0A

add testing helper functionality