コード例 #1
0
    def _eval_unpacked(self,
                       params,
                       ngrads=None,
                       names=None,
                       final_indices=None):
        pv = []
        pg = []
        b = []
        for param in params:
            if isinstance(param, parameter_wrapper):
                pvv, pgg = param.value, param.grad_values
            else:
                pvv, pgg = param, np.array([[]])
                b.append(pgg is not None)

        if np.any(b):
            res = evaluate_grad_operator(self.op_grad,
                                         params,
                                         ngrads=ngrads,
                                         names=names,
                                         final_indices=final_indices)
        else:
            res_value = self.op_base(*pv)
            res_grad = None
            res = parameter_wrapper(None,
                                    res_value,
                                    grads=names,
                                    grad_values=res_grad)
        return res
コード例 #2
0
 def __call__(self, *args):
     new_args = []
     have_grad = False
     arg_names = self.fbase.arg_names
     for name, arg in zip(arg_names, args):
         if isinstance(arg, parameter_wrapper):
             have_grad = True
         else:
             arg = parameter_wrapper(name, arg)
         new_args.append(arg)
     if have_grad:
         return self.eval_normal_grad(new_args)
     else:
         return self.eval_normal(args)
コード例 #3
0
def evaluate_grad_operator(op_grad,
                           parameter_wrappers,
                           ngrads=None,
                           names=None,
                           final_indices=None):
    if ngrads is None or names is None or final_indices is None:
        ngrads, names, final_indices = sift_parameters(parameter_wrappers)

    args = [(x.value,
             np.array([[]]) if x.grad_values is None else x.grad_values)
            for x in parameter_wrappers]
    args += final_indices
    args += [ngrads]

    res, res_grad = op_grad(*args)
    return parameter_wrapper(None, res, grads=names, grad_values=res_grad)
コード例 #4
0
    def eval(self, param0, ngrads=None, names=None, final_indices=None):
        if isinstance(param0, parameter_wrapper):
            p0v, p0g = param0.value, param0.grad_values
        else:
            p0v, p0g = param0, None

        if p0g is None:
            res_value = self.op_base(p0v)
            res_grad = None
            names = None
        else:
            res_value, res_grad = self.op_grad((p0v, p0g))
            names = param0.grads

        return parameter_wrapper(None,
                                 res_value,
                                 grads=names,
                                 grad_values=res_grad)
コード例 #5
0
    def eval(self,
             param0,
             param1,
             ngrads=None,
             names=None,
             final_indices=None):
        if isinstance(param0, parameter_wrapper):
            p0v, p0g = param0.value, param0.grad_values
        else:
            p0v, p0g = param0, None
        if isinstance(param1, parameter_wrapper):
            p1v, p1g = param1.value, param1.grad_values
        else:
            p1v, p1g = param1, None

        b0 = p0g is not None
        b1 = p1g is not None
        b = b0 << 1 | b1
        op = self.ops[b]
        if b == 0:
            res_value = op(p0v, p1v)
            res_grad = None
            names = None
        elif b == 1:
            res_value, res_grad = op(p0v, (p1v, p1g))
            names = param1.grads
        elif b == 2:
            res_value, res_grad = op((p0v, p0g), p1v)
            names = param0.grads
        elif b == 3:
            res = evaluate_grad_operator(op, [param0, param1],
                                         ngrads=ngrads,
                                         names=names,
                                         final_indices=final_indices)
            return res

        return parameter_wrapper(None,
                                 res_value,
                                 grads=names,
                                 grad_values=res_grad)
コード例 #6
0
# Register the methods in this namespace
for method in methods:
    set_op(globals(), method, method, 1, False)


if __name__ == "__main__":

    def f(a, b, c, d):
        y = a + b
        z = c + d
        r = (y ** 2) * z
        return r

    from parameter_wrapper import parameter_wrapper, sift_parameters

    a = parameter_wrapper("a", 1, grads=["g"], grad_values=[1])
    b = parameter_wrapper("b", 1, grads=["g"], grad_values=[1])
    c = parameter_wrapper("c", 1, grads=["h"], grad_values=[1])
    d = parameter_wrapper("d", 1, grads=["h"], grad_values=[1])

    na = Parameter("a", value=a)
    nb = Parameter("b", value=b)
    nc = Parameter("c", value=c)
    nd = Parameter("d", value=d)

    res = f(na, nb, nc, nd)
    print(res)
    print(res.value)
    print(res.value.value)
    print(res.value.grads)
    print(res.value.grad_values)
コード例 #7
0
    unary_operator('log2', ad.log2, ad.log2_grad),
    'sqrt':
    unary_operator('sqrt', ad.sqrt, ad.sqrt_grad),
    'sum':
    unary_operator('sum', ad.sum, ad.sum_grad),
}

if __name__ == "__main__":
    p = operators['plus']

    def f(a, b, c, d):
        y = a + b
        z = c + d
        r = (y**2) * z
        return r

    def f(a, b, c, d):
        return operators['mul'].eval(
            operators['pow'].eval(operators['plus'].eval(a, b), 2),
            operators['plus'].eval(c, d))

    a = parameter_wrapper('a', 1, grads=['g'], grad_values=[1])
    b = parameter_wrapper('b', 1, grads=['g'], grad_values=[1])
    c = parameter_wrapper('c', 1, grads=['h'], grad_values=[1])
    d = parameter_wrapper('d', 1, grads=['h'], grad_values=[1])

    res = f(a, b, c, d)
    print(res.value)
    print(res.grads)
    print(res.grad_values)
コード例 #8
0
    the_store = store(default_cache_size=1, default_probe_func=True)

    the_store.add_prop("f", ["a", "b", "c", "d"], f)
    the_store.add_prop("a", ["g"], a)
    the_store.add_prop("b", ["g"], b)
    the_store.add_prop("c", ["h"], c)
    the_store.add_prop("d", ["h"], d)
    the_store.initialize()

    params = {"g": 1.0, "h": 2.0}

    print("f:", the_store["f", params])
    print("f:", the_store["f", params])

    params = {
        "g": parameter_wrapper("g", 1.0, grads=["g"], grad_values=[1]),
        "h": parameter_wrapper("h", 2.0, grads=["h"], grad_values=[1]),
    }

    res = the_store["f", params]
    print(res)
    print(res.value)
    print(res.grads)
    print(res.grad_values)
    res = the_store["f", params]
    print(res)
    print(res.value)
    print(res.grads)
    print(res.grad_values)