Beispiel #1
0
    def define_graph(self,
                     a,
                     b,
                     y,
                     alpha=1.0,
                     beta=0.0,
                     transA=False,
                     transB=False,
                     strict_shapes=False):
        if strict_shapes:
            assert b.shape[0] == a.shape[1]
            assert len(y.shape) == 0 or y.shape[0] == a.shape[0]
            assert bool(transB) == bool(transA) and bool(
                transA
            ) == False, f"Strict shape check failed: {transA} != {transB}"

        if transA:
            i = pm.index(0, a.shape[1] - 1)
            j = pm.index(0, b.shape[0] - 1)
            k = pm.index(0, b.shape[1] - 1)
            y[i, k] = pm.sum([j], a[j, i] * b[j, k])
        elif transB:
            i = pm.index(0, a.shape[0] - 1)
            j = pm.index(0, b.shape[1] - 1)
            k = pm.index(0, b.shape[0] - 1)
            y[i, k] = pm.sum([j], a[i, j] * b[k, j])
        else:
            i = pm.index(0, a.shape[0] - 1)
            j = pm.index(0, b.shape[0] - 1)
            k = pm.index(0, b.shape[1] - 1)
            y[i, k] = pm.sum([j], a[i, j] * b[j, k])
Beispiel #2
0
def reco(m_=3, n_=3, k_=2):
    with pm.Node(name="recommender") as graph:
        mu = pm.placeholder("mu")
        m = pm.placeholder("m")
        n = pm.placeholder("n")
        k = pm.placeholder("k")
        x1 = pm.placeholder("x1", shape=k)
        x2 = pm.placeholder("x2", shape=k)

        r1 = pm.placeholder("r1", shape=m)
        y1 = pm.placeholder("y1", shape=m)

        r2 = pm.placeholder("r2", shape=n)
        y2 = pm.placeholder("y2", shape=n)

        w1 = pm.placeholder("w1", shape=(m, k))
        w2 = pm.placeholder("w2", shape=(n, k))
        i = pm.index(0, m - 1, name="i")
        j = pm.index(0, n - 1, name="j")
        l = pm.index(0, k - 1, name="l")
        h1_sum = pm.sum([l], (w1[i, l] * x2[l]).set_name("w1*x2")).set_name("h1_sum")
        h1 = (h1_sum[i] * r1[i]).set_name("h1")
        h2_sum = pm.sum([l], (x1[l] * w2[j, l]).set_name("x1*w2")).set_name("h2_sum")
        h2 = (h2_sum[j] * r2[j]).set_name("h2")
        #
        d1 = (h1[i] - y1[i]).set_name("d1")
        d2 = (h2[j] - y2[j]).set_name("d2")
        g1 = (d1[i] * x2[l]).set_name("g1")
        g2 = (d2[j] * x1[l]).set_name("g2")
        w1_ = (w1[i, l] - g1[i, l]).set_name("w1_")
        w2_ = (w2[i, l] - g2[i, l]).set_name("w2_")

    shape_val_pass = pm.NormalizeGraph({"m": m_, "n": n_, "k": k_})
    new_graph, res = shape_val_pass(graph)
    return new_graph
Beispiel #3
0
    def define_graph(self, data, mean, var, axis=None):
        indices = tuple([pm.index(0, s-1) for s in data.shape])
        if axis is None:
            axis = tuple(list(range(len(data.shape))))
        r_idx = [indices[i] for i in axis]
        o_idx = tuple([indices[i] for i in range(len(data.shape)) if i not in axis])

        denom = 1
        for i in axis:
            denom *= data.shape[i]

        mean[o_idx] = pm.sum(r_idx, data[indices]) / (denom)
        var[o_idx] = pm.sum(r_idx, pm.square(data[indices] - mean[o_idx])) / (denom)
Beispiel #4
0
 def define_graph(self, a, w, out):
     indices = _get_single_node_indices(a)
     sum_idx = indices[-1]
     o_idx = pm.index(0, w.shape[0]-1) if w.shape[-1] == a.shape[-1] else pm.index(0, w.shape[1]-1)
     w_idx = (o_idx, sum_idx) if w.shape[-1] == a.shape[-1] else (sum_idx, o_idx)
     out_idx = indices[:-1] + (o_idx,)
     out[out_idx] = pm.sum([sum_idx], a[indices]*w[w_idx])
Beispiel #5
0
def test_multi_shapes():
    m_ = 5
    n_ = 4
    p_ = 3
    inp_ = np.random.randint(1, 5, (m_, p_))
    w_ = np.random.randint(1, 5, (p_, n_))
    mapping = {"m": m_, "n": n_, "p": p_, "in": inp_, "w": w_}

    numpy_res1 = np.empty(shape=(m_, p_, n_))
    indices = []
    for i in range(m_):
        for k in range(p_):
            for j in range(n_):
                numpy_res1[i][k][j] = inp_[i][k] * w_[k][j]
                indices.append(tuple([i, k, j]))
    numpy_res = np.sum(numpy_res1)

    with pm.Node(name="mmul") as graph:
        m = pm.placeholder("m")
        n = pm.placeholder("n")
        p = pm.placeholder("p")
        inp = pm.placeholder("in", shape=(m, p))
        wts = pm.placeholder("w", shape=(p, n))
        i = pm.index(0, m - 1, name="i")
        j = pm.index(0, n - 1, name="j")
        k = pm.index(0, p - 1, name="k")
        inp_ik = pm.var_index(inp, [i, k], name="in[i,k]")
        w_kj = pm.var_index(wts, [k, j], name="w[k,j]")
        slice_mul = (inp_ik * w_kj).set_name("w[i,k]*in[k,j]")
        out = pm.sum([i, k, j], slice_mul, name="out")
    graph_res = graph("out", mapping)
    assert graph_res == numpy_res
def test_linear_deserialize():

    graph_name = "linear_reg1"
    with pm.Node(name=graph_name) as graph:
        m = pm.placeholder("m")
        x_ = pm.placeholder("x", shape=(m))
        y_ = pm.placeholder("y")
        w_ = pm.placeholder("w", shape=(m))
        mu = pm.parameter(name="mu", default=1.0)
        i = pm.index(0, (m - 1).set_name("m-1"), name="i")
        h = pm.sum([i], (x_[i] * w_[i]).set_name("x*w"), name="h")
        d = (h - y_).set_name("h-y")
        g = (d * x_[i]).set_name("d*x")
        mug = (mu * g[i]).set_name("mu*g[i]")
        w_ = ((w_[i]) - mug).set_name("w_out")
    x = np.random.randint(0, 10, 10)
    y = np.random.randint(0, 10, 1)[0]
    w = np.random.randint(0, 10, 10)

    graph_res = graph("w_out", {"x": x, "y": y, "w": w})
    actual_res = w - ((np.sum(x * w) - y) * x) * 1.0

    np.testing.assert_allclose(graph_res, actual_res)
    cwd = Path(f"{__file__}").parent
    base_path = f"{cwd}/pmlang_examples"
    full_path = f"{base_path}/outputs"
    pb_path = f"{full_path}/{graph_name}.srdfg"
    pm.pb_store(graph, full_path)
    node = pm.pb_load(pb_path)
    new_graph_res = node("w_out", {"x": x, "y": y, "w": w})
    np.testing.assert_allclose(graph_res, new_graph_res)
    np.testing.assert_allclose(actual_res, new_graph_res)
Beispiel #7
0
 def define_graph(self, data, out, axes=(0, ), keepdims=True):
     # indices = _get_single_node_indices(data)
     indices = tuple([pm.index(0, s - 1) for s in data.shape])
     sum_idx = tuple([indices[i] for i in axes])
     out_idx = tuple(
         [indices[i] for i in range(len(indices)) if i not in axes])
     out[out_idx] = pm.sum([sum_idx], data[indices])
Beispiel #8
0
def test_flatten_result_length():
    with pm.Node(name="linear_reg") as graph:
        m = pm.placeholder("m", type_modifier="param")
        x = pm.placeholder("x", shape=(m), type_modifier="input")
        y = pm.placeholder("y", type_modifier="input")
        w = pm.placeholder("w", shape=(m), type_modifier="state")
        mu = pm.placeholder("mu", default_val=1.0, type_modifier="param")
        i = pm.index(0, (m - 1).set_name("m-1")).set_name("i")
        h = pm.sum([i], (x[i] * w[i]).set_name("x*w"), name="h")
        d = (h - y).set_name("h-y")
        g = (d * x[i]).set_name("d*x")
        w_ = (w[i] - (mu * g[i]).set_name("mu*g")).set_name(("w_out"))

    shape_val_pass = NormalizeGraph({"m": 3})
    count_pass = CountNodes()
    flatten_pass = Lower({})

    new_graph = shape_val_pass(graph)

    flattened_g = flatten_pass(new_graph)
    x = np.random.randint(0, 10, 10)
    y = np.random.randint(0, 10, 1)[0]
    w = np.random.randint(0, 10, 10)

    orig_graph = count_pass(flattened_g)
Beispiel #9
0
 def define_graph(self, x, w, y, y_pred, mu, m):
     i = pm.index(0, (m - 1).set_name("m-1"), name="i")
     h = pm.temp(name="h", shape=(m))
     h = pm.sigmoid(pm.sum([i], (x[i] * w[i]), name="h"))
     d = (h - y).set_name("h-y")
     g = (d * x[i]).set_name("d*x")
     w[i] = w[i] - mu * g[i]
Beispiel #10
0
 def define_graph(self, x, w, y, mu, m):
     i = pm.index(0, (m - 1).set_name("m-1"), name="i")
     h = pm.sum([i], (x[i] * w[i]), name="h")
     c = (y * h).set_name("c")
     ny = (0 - y).set_name("ny")
     p = ((c > 1) * ny).set_name("p")
     g = (p * x[i]).set_name("g")
     w[i] = w[i] - mu * g[i]
Beispiel #11
0
def create_svm_wifi(features, locations, lr=0.0001, deltav=1, train_size=7703):
    with pm.Node(name="svm_wifi") as graph:
        learning_rate = pm.parameter("learning_rate", default=lr)
        delta = pm.parameter("delta", default=deltav)
        n_features = pm.parameter("n_features", default=features)
        n_locations = pm.parameter("n_locations", default=locations)
        x_train = pm.input("x_train", shape=(n_features, ))
        y_train = pm.input("y_train", shape=(n_locations, ))
        y_train_inv = pm.input("y_train_inv", shape=(n_locations, ))
        weights = pm.state("weights", shape=(n_features, n_locations))

        i = pm.index(0, n_features - 1, name="i")
        j = pm.index(0, n_locations - 1, name="j")

        scores = pm.sum([i], (weights[i, j] * x_train[i]), name="scores")
        correct_class_score = pm.sum([j], (scores[j] * y_train[j]),
                                     name="correct_class_score")

        h = ((scores[j] - correct_class_score + delta).set_name("h") > 0)

        # margin = (pm.cast(np.float32, h[j]) * y_train_inv[j]).set_name("margin")
        margin = (h[j] * y_train_inv[j]).set_name("margin")
        valid_margin_count = pm.sum([j], margin[j], name="valid_margin_count")
        partial = (y_train[j] * valid_margin_count).set_name("partial")
        updated_margin = (margin[j] - partial[j]).set_name("updated_margin")
        # # #
        dW = (x_train[i] * updated_margin[j]).set_name("dW")
        weights[i, j] = (weights[i, j] -
                         learning_rate * dW[i, j]).set_name("weights_update")

    shape_dict = {"n_features": features, "n_locations": locations}
    input_info, keys, out_info = svm_wifi_datagen(features,
                                                  locations,
                                                  lr,
                                                  deltav,
                                                  lowered=True)

    cwd = Path(f"{__file__}").parent
    full_path = f"{cwd}/outputs"
    tabla_path = f"{full_path}/{graph.name}_{locations}_{features}_tabla.json"

    tabla_ir, tabla_graph = pm.generate_tabla(graph,
                                              shape_dict,
                                              tabla_path,
                                              context_dict=input_info,
                                              add_kwargs=True)
Beispiel #12
0
 def define_graph(self, x, out):
     # indices = tuple([pm.index(0, s - 1) if s > 1 else 0 for s in shape])
     indices = _get_single_node_indices(out, shape=out.shape)
     m = pm.index(0, x.shape[2] - 1)
     n = pm.index(0, x.shape[3] - 1)
     h = x.shape[2]
     w = x.shape[3]
     out[indices] = (1 / (h * w)) * pm.sum([m, n], x[indices[0], indices[1],
                                                     m, n])
Beispiel #13
0
 def define_graph(self, logs, targets, out, reduction="mean"):
     # gathered = pm.gather_elements(logs, pm.reshape(targets, shape=(logs.shape[0], 1)), axis=1)
     gathered = pm.gather_elements(logs,
                                   pm.reshape(targets, (logs.shape[0], 1)),
                                   axis=1)
     # reshaped = pm.reshape(-1*gathered, shape=(logs.shape[0],))
     reshaped = pm.reshape(-1 * gathered, (logs.shape[0], ))
     idx = (pm.index(0, logs.shape[0] - 1), )
     if reduction == "none":
         out.set_shape(reshaped.shape)
         out[idx] = reshaped[idx]
     elif reduction == "mean":
         out.set_shape((1, ))
         denom = 1
         for s in reshaped.shape:
             denom = denom * s
         out[0] = pm.sum([idx[0]], reshaped[idx]) / denom
     elif reduction == "sum":
         out.set_shape((1, ))
         out[0] = pm.sum([idx[0]], reshaped[idx])
Beispiel #14
0
    def define_graph(self,
                     x,
                     scale,
                     b,
                     mean,
                     var,
                     grad,
                     x_grad,
                     scale_grad,
                     b_grad,
                     optimizer,
                     optimizer_kwargs,
                     eps=1e-5):
        indices = _get_single_node_indices(x, shape=x.shape)
        reduce_idx = (indices[0], indices[2], indices[3])
        N = np.prod((x.shape[0], x.shape[2], x.shape[3]))
        sum_grad = pm.sum([reduce_idx], grad[indices])
        mean_grad_y = sum_grad / N
        mean_x = pm.sum([reduce_idx], x[indices]) / N
        sqr_err = (x[indices] - mean_x[indices[1]])**2
        var_x = pm.sum([reduce_idx], sqr_err[indices]) / N
        grad_y_offset = (grad[indices] - mean_grad_y[indices[1]])
        x_offset = x[indices] - mean_x[indices[1]]
        var_eps = var_x[indices[1]] + eps
        offset_sum = pm.sum([reduce_idx], grad[indices] * x_offset[indices])
        new_mean = offset_sum[indices[1]] / N
        rsqrt_var = (pm.rsqrt(
            var_eps[indices[1]])).set_name(f"{x.name}_rsqrt_var")
        unsq_indices = _get_single_node_indices(rsqrt_var,
                                                shape=(1, x.shape[1], 1, 1))
        coeff = (scale[unsq_indices[1]] * rsqrt_var[unsq_indices])
        grad_sub = ((x_offset[indices] * new_mean[indices[1]]) /
                    (var_eps[indices[1]]))
        x_grad[indices] = coeff[indices[1]] * (grad_y_offset[indices] -
                                               grad_sub[indices])
        scale_grad[indices[1]] = rsqrt_var[indices[1]] * offset_sum[indices[1]]
        b_grad[indices[1]] = sum_grad[indices[1]]

        with self.graph:
            OPTIMIZERS[optimizer](scale, scale_grad, **optimizer_kwargs)
            OPTIMIZERS[optimizer](b, b_grad, **optimizer_kwargs)
Beispiel #15
0
def linear_reg():
    with pm.Node(name="linear_reg") as graph:
        m = pm.placeholder("m")
        x = pm.placeholder("x", shape=(m), type_modifier="input")
        y = pm.placeholder("y", type_modifier="input")
        w = pm.placeholder("w", shape=(m), type_modifier="state")
        mu = pm.parameter(name="mu", default=1.0)
        i = pm.index(0, (graph["m"]-1).set_name("m-1"), name="i")
        h = pm.sum([i], (x[i] * w[i]).set_name("x*w"), name="h")
        d = (h-y).set_name("h-y")
        g = (d*x).set_name("d*x")
        w_ = (w - (mu*g).set_name("mu*g")).set_name("w-mu*g")
Beispiel #16
0
    def define_graph(self, z, y, loss, reduction="mean"):
        a = pm.temp(name=f"temp_{y.name}", shape=z.shape)

        i = pm.index(0, z.shape[1] - 1, name="i")
        indices = [
            pm.index(0, s - 1, name=f"{z.name}[{i}]")
            for i, s in enumerate(z.shape)
        ]
        indices[1] = i
        indices = tuple(indices)
        maxes = pm.max([i], z[indices], name="maxes")
        exp_val = pm.exp((z[indices] - maxes[indices[0]]))
        lse_stable = pm.log(pm.sum([i], exp_val[indices], name="testing_lse"),
                            name="lse_stable")
        a[indices] = z[indices] - maxes[indices[0]] - lse_stable[indices[0]]
        gathered = pm.gather_elements(a,
                                      pm.reshape(y, (a.shape[0], 1),
                                                 name="reshaped1"),
                                      axis=1,
                                      shape=(y.shape[0], ),
                                      name="gathered_elem")
        reshaped = pm.reshape(-1 * gathered, (y.shape[0], ),
                              name="other_reshape")
        idx = (pm.index(0, a.shape[0] - 1), )
        if reduction == "none":
            loss.set_shape(reshaped.shape)
            loss[idx] = reshaped[idx]
        elif reduction == "mean":
            loss.set_shape((1, ))
            denom = 1
            for s in reshaped.shape:
                denom = denom * s
            loss[0] = pm.sum([idx[0]], reshaped[idx],
                             name="test_sum_name") / denom
        elif reduction == "sum":
            loss.set_shape((1, ))
            loss[0] = pm.sum([idx[0]], reshaped[idx])
Beispiel #17
0
def linear_reg_graph():
    graph_name = "linear_reg"
    with pm.Node(name="linear_reg") as graph:
        m = pm.placeholder("m")
        mu = pm.parameter(name="mu", default=1.0)
        x_ = pm.placeholder("x", shape=(m), type_modifier="input")
        y_ = pm.placeholder("y", type_modifier="input")
        w_ = pm.placeholder("w", shape=(m), type_modifier="state")
        i = pm.index(0, m - 1, name="i")
        h = pm.sum([i], (x_[i] * w_[i]).set_name("x*w"), name="h")
        d = (h - y_).set_name("h-y")
        g = (d * x_[i]).set_name("d*x")
        w_out = (w_[i]) - mu * g[i]
        w_out.set_name("res")
    return graph
Beispiel #18
0
 def define_graph(self, data, out, axis=0):
     out.set_shape(data.shape)
     i = pm.index(0, data.shape[axis] - 1, name="i")
     indices = [
         pm.index(0, s - 1, name=f"{data.name}[{i}]")
         for i, s in enumerate(data.shape)
     ]
     indices[axis] = i
     indices = tuple(indices)
     maxes = pm.max([i], data[indices], name="maxes")
     lse_stable = pm.log(pm.sum([i],
                                pm.exp(
                                    (data[indices] - maxes[indices[0]]))),
                         name="lse_stable")
     out[indices] = data[indices] - maxes[indices[0]] - lse_stable[
         indices[0]]
Beispiel #19
0
 def define_graph(self, data, out, axis=0):
     out.set_shape(data.shape)
     i = pm.index(0, data.shape[axis] - 1, name="i")
     j = pm.index(0, data.shape[axis] - 1, name="j")
     indices = [
         pm.index(0, s - 1, name=f"{data.name}[{i}]")
         for i, s in enumerate(data.shape)
     ]
     indices_denom = indices
     indices_denom[axis] = j
     indices[axis] = i
     indices = tuple(indices)
     indices_denom = tuple(indices_denom)
     mval = pm.max([i], data[indices], name="max_test")
     e_x = pm.exp((data[indices] - mval), name="e_x")
     out[indices] = e_x[indices] / pm.sum(
         [indices_denom[axis]], e_x[indices_denom], name="denom")
Beispiel #20
0
def test_sigmoid(m_):

    with pm.Node(name="logistic1") as graph:
        m = pm.parameter(name="m")
        n = pm.parameter(name="n")
        x = pm.input("x", shape=(m))
        w = pm.state("w", shape=(m))
        i = pm.index(0, m - 1, name="i")
        o = pm.sigmoid(pm.sum([i], w[i] * x[i]), name="out")
    x_ = np.random.randint(0, 10, m_)
    w_ = np.random.randint(0, 10, m_)
    input_dict = {"x": x_, "w": w_}
    np_res = int(sigmoid(np.sum(x_ * w_)))
    shape_dict = {"m": m_}

    coarse_eval = graph("out", x=x_, w=w_)
    np.testing.assert_allclose(np_res, coarse_eval)
    lowered = set_shape_and_lower(graph, shape_dict)
Beispiel #21
0
def test_linear_embedded():

    with pm.Node(name="linear_reg") as graph:
        m = pm.placeholder("m")
        x = pm.placeholder("x", shape=(m), type_modifier="input")
        y = pm.placeholder("y", type_modifier="input")
        w = pm.placeholder("w", shape=(m), type_modifier="state")
        mu = pm.parameter(name="mu", default=1.0)
        i = pm.index(0, (graph["m"] - 1).set_name("m-1"), name="i")
        h = pm.sum([i], (x[i] * w[i]).set_name("x*w"), name="h")
        d = (h - y).set_name("h-y")
        g = (d * x).set_name("d*x")
        w_ = (w - (mu * g).set_name("mu*g")).set_name("w-mu*g")
    x = np.random.randint(0, 10, 5)
    y = np.random.randint(0, 10, 1)[0]
    w = np.random.randint(0, 10, 5)

    graph_res = graph("w-mu*g", {"x": x, "y": y, "w": w})
    actual_res = w - ((np.sum(x * w) - y) * x) * 1.0
    np.testing.assert_allclose(graph_res, actual_res)
Beispiel #22
0
    def define_graph(self, x, y, alpha, beta, bias, nsize):
        n = pm.index(0, x.shape[0] - 1)
        c = pm.index(0, x.shape[1] - 1)
        h = pm.index(0, x.shape[2] - 1)
        w = pm.index(0, x.shape[3] - 1)
        c_ = pm.index(0, x.shape[1] - 1)
        ext = pm.temp(name="extended", shape=tuple([*x.shape, x.shape[-3]]))

        bounds = pm.output(name="bounds", shape=(x.shape[1], x.shape[1]))
        radius = nsize // 2
        hbool = ((((x.shape[1] > (c + radius + 1)) * (c + radius)) +
                  (x.shape[1] <= (c + radius + 1)) * (x.shape[1] - 1)) >= c_)
        lbool = ((((c - radius) > 0) * (c - radius)) +
                 (((c - radius) <= 0) * 0) <= c_)
        bounds[c, c_] = hbool * lbool
        ext[n, c, h, w, c_] = x[n, c_, h, w] * bounds[c, c_]
        # y[n, c, h, w] = x[n,c,h,w] / ((bias + (alpha/nsize) * pm.sum([c_], ext[n, c, h, w, c_]**2))**beta)
        y[n, c, h, w] = x[n, c, h, w] / (
            (bias +
             (alpha / nsize) * pm.sum([c_], ext[n, c, h, w, c_]**2))**beta)
Beispiel #23
0
def test_lower_group_op():
    with pm.Node(name="linear_reg1") as graph:
        m = pm.parameter(name="m")
        x = pm.input("x", shape=(m))
        y = pm.input("y")
        w = pm.state("w", shape=(m))
        i = pm.index(0, m - 1, name="i")
        h = pm.sum([i], w[i] * x[i], name="h")
    m_ = 3
    n_ = 3
    x_ = np.random.randint(0, 10, m_)
    w_ = np.random.randint(0, 10, (m_))
    np_result = np.sum(x_ * w_)
    np.testing.assert_allclose(graph("h", {"w": w_, "x": x_}), np_result)
    np.testing.assert_allclose(graph("h", w=w_, x=x_), np_result)
    shape_pass = NormalizeGraph({"m": m_, "n": n_})
    graph_shapes = shape_pass(graph)
    shape_res = graph_shapes("h", x=x_, w=w_)
    np.testing.assert_allclose(shape_res, np_result)

    lower_pass = Lower({})
    lowered_graph = lower_pass(graph_shapes)
    input_info = {f"w/w({i},)": w_[i] for i in range(len(w_))}
    input_info.update({f"x/x({i},)": x_[i] for i in range(len(x_))})
    #
    fine_grained_eval = lowered_graph("h/h(4,)", input_info)
    assert fine_grained_eval == np_result

    pb_path = f"{OUTPATH}/linear_reg1.srdfg"

    pm.pb_store(lowered_graph, OUTPATH)
    loaded_node = pm.pb_load(pb_path)  #
    input_info = {f"w/w({i},)": w_[i] for i in range(len(w_))}
    input_info.update({f"x/x({i},)": x_[i] for i in range(len(x_))})

    loaded_res = loaded_node("h/h(4,)", input_info)

    assert loaded_node.func_hash() == lowered_graph.func_hash()
    assert loaded_res == np_result
Beispiel #24
0
    def define_graph(self, inp, out, kh, kw, stride=1, pad=0):
        oh = ((inp.shape[2] + 2 * pad - kh) // stride + 1)
        ow = ((inp.shape[3] + 2 * pad - kw) // stride + 1)
        out.set_shape((inp.shape[0], inp.shape[1], oh, ow))

        b = pm.index(0, inp.shape[0] - 1, name="b")
        c = pm.index(0, inp.shape[1] - 1, name="c")
        y = pm.index(0, oh - 1, name="y")
        x = pm.index(0, ow - 1, name="x")
        m = pm.index(0, kh - 1, name="m")
        n = pm.index(0, kw - 1, name="n_")
        ihp = (inp.shape[2] + pad * 2)
        iwp = inp.shape[3] + pad * 2
        ihp_ = pm.index(0, ihp - 1, name="ihp")
        iwp_ = pm.index(0, iwp - 1, name="iwp")
        iy = pm.index(0, inp.shape[2] - 1, name="iy")
        ix = pm.index(0, inp.shape[3] - 1, name="ix")
        padded = pm.temp(shape=(inp.shape[0], inp.shape[1], ihp, iwp))
        padded[b, c, ihp_, iwp_] = 0
        padded[b, c, iy + pad, ix + pad] = inp[b, c, iy, ix]
        out[b, c, y,
            x] = ((1 / (kh * kw)) *
                  pm.sum([m, n], padded[b, c, stride * y + m, stride * x + n]))
Beispiel #25
0
    def define_graph(self, data, out, kh, kw, stride=(1, 1), pad=(0, 0)):
        sx, sy = stride
        oh = ((data.shape[-2] + 2 * pad[0] - kh) // stride[0] + 1)
        ow = ((data.shape[-1] + 2 * pad[1] - kw) // stride[1] + 1)

        y = pm.index(0, oh - 1, name="y")
        x = pm.index(0, ow - 1, name="x")
        m = pm.index(0, kh - 1, name="m")
        n = pm.index(0, kw - 1, name="n_")
        ihp = (data.shape[-2] + pad[0] * 2)
        iwp = data.shape[-1] + pad[1] * 2
        ihp_ = pm.index(0, ihp - 1, name="ihp")
        iwp_ = pm.index(0, iwp - 1, name="iwp")
        iy = pm.index(0, data.shape[-2] - 1, name="iy")
        ix = pm.index(0, data.shape[-1] - 1, name="ix")

        if len(data.shape) > 3:
            b = pm.index(0, data.shape[0] - 1, name="b")
            c = pm.index(0, data.shape[1] - 1, name="c")

            o_indices = [b, c]
            p_shape = (data.shape[0], data.shape[1], ihp, iwp)
            out.set_shape((data.shape[0], data.shape[1], oh, ow))

        else:
            c = pm.index(0, data.shape[0] - 1, name="c")
            o_indices = [c]
            p_shape = (data.shape[0], ihp, iwp)
            out.set_shape((data.shape[0], oh, ow))
        o_indices = tuple(o_indices)
        padded = pm.temp(shape=p_shape)
        padded[o_indices + (ihp_, iwp_)] = 0
        padded[o_indices + (iy + pad[0], ix + pad[1])] = data[o_indices +
                                                              (iy, ix)]
        out[o_indices + (y, x)] = pm.sum(
            [m, n], padded[o_indices + (sx * y + m, sy * x + n)]) * (1 /
                                                                     (kh * kw))
Beispiel #26
0
 def define_graph(self, x, w, y_pred, mu, m):
     i = pm.index(0, (m - 1).set_name("m-1"), name="i")
     h = pm.sigmoid(pm.sum([i], (x[i] * w[i]), name="h"))
Beispiel #27
0
    def define_graph(self, data, w, out, stride=1, pad=0, dilation=1):

        if not isinstance(stride, (tuple, list)):
            stride_h = stride_w = stride
        else:
            stride_h, stride_w = stride

        if not isinstance(stride, (tuple, list)):
            dilation_h = dilation_w = dilation
        else:
            dilation_h, dilation_w = dilation

        if not isinstance(stride, (tuple, list)):
            pad = (pad, pad)

        batch, in_channel, in_height, in_width = data.shape
        num_filter, channel, kernel_h, kernel_w = w.shape
        # compute the output shape
        dilated_kernel_h = (kernel_h - 1) * dilation_h + 1
        dilated_kernel_w = (kernel_w - 1) * dilation_w + 1
        pad_top, pad_left, pad_down, pad_right = get_pad_tuple(
            pad, (dilated_kernel_h, dilated_kernel_w))
        out_channel = num_filter
        oh = (in_height - dilated_kernel_h + pad_top +
              pad_down) // stride_h + 1
        ow = (in_width - dilated_kernel_w + pad_left +
              pad_right) // stride_w + 1
        pad_before = [0, 0, pad_top, pad_left]
        pad_after = [0, 0, pad_down, pad_right]
        c = pm.index(0, w.shape[0] - 1, name="c")
        y = pm.index(0, oh - 1, name="y_")
        x = pm.index(0, ow - 1, name="x_")
        dy = pm.index(0, w.shape[2] - 1, name="dy")
        dx = pm.index(0, w.shape[3] - 1, name="dx")
        iy = pm.index(0, data.shape[-2] - 1, name="iy")
        ix = pm.index(0, data.shape[-1] - 1, name="ix")
        k = pm.index(0, data.shape[-3] - 1, name="k")
        ihp = data.shape[-2] + pad_top + pad_down
        iwp = data.shape[-1] + pad_left + pad_right
        ihp_ = pm.index(0, ihp - 1, name="ihp")
        iwp_ = pm.index(0, iwp - 1, name="iwp")
        if len(data.shape) > 3:
            b = pm.index(0, data.shape[0] - 1, name="b")
            o_indices = (b, c)
            p_indices = (
                b,
                k,
            )
            p_shape = (data.shape[0], data.shape[1], ihp, iwp)
            out.set_shape((data.shape[0], w.shape[0], oh, ow))
        else:
            o_indices = (c, )
            p_indices = (k, )
            p_shape = (data.shape[0], ihp, iwp)
            out.set_shape((w.shape[0], oh, ow))

        padded = pm.temp(shape=p_shape)
        padded[p_indices + (ihp_, iwp_)] = 0

        padded[p_indices + (iy + pad_top, ix + pad_left)] = data[p_indices +
                                                                 (iy, ix)]

        # out[o_indices + (y, x)] = pm.sum([dy, dx, k], (padded[p_indices + (dy + stride*y, dx + stride*x)] * w[c, k, dy, dx])) + bias[c]

        out[o_indices + (y, x)] = pm.sum(
            [dy, dx, k],
            (padded[p_indices +
                    (dy * dilation_h + stride * y,
                     dx * dilation_w + stride * x)] * w[c, k, dy, dx]))
Beispiel #28
0
def test_flatten_reco():
    with pm.Node(name="recommender") as graph:
        m = pm.parameter("m")
        n = pm.parameter("n")
        k = pm.parameter("k")
        x1 = pm.input("x1", shape=(k, ))
        x2 = pm.input("x2", shape=(k, ))

        r1 = pm.input("r1", shape=(m, ))
        y1 = pm.input("y1", shape=(m, ))

        r2 = pm.input("r2", shape=(n, ))
        y2 = pm.input("y2", shape=(n, ))

        w1 = pm.state("w1", shape=(m, k))
        w2 = pm.state("w2", shape=(n, k))
        i = pm.index(0, m - 1, name="i")
        j = pm.index(0, n - 1, name="j")
        l = pm.index(0, k - 1, name="l")
        h1_sum = pm.sum([l], (w1[i, l] *
                              x2[l]).set_name("w1*x2")).set_name("h1_sum")
        h1 = (h1_sum[i] * r1[i]).set_name("h1")
        h2_sum = pm.sum([l], (w2[j, l] *
                              x1[l]).set_name("w2*x1")).set_name("h2_sum")
        h2 = (h2_sum[j] * r2[j]).set_name("h2")

        d1 = (h1[i] - y1[i]).set_name("d1")
        d2 = (h2[j] - y2[j]).set_name("d2")
        g1 = (d1[i] * x2[l]).set_name("g1")
        g2 = (d2[j] * x1[l]).set_name("g2")
        w1[i, l] = (w1[i, l] - g1[i, l])
        w2[j, l] = (w2[j, l] - g2[j, l])
    m_ = 3
    n_ = 3
    k_ = 2
    input_info = {}
    input_info["m"] = m_
    input_info["n"] = n_
    input_info["k"] = k_
    input_info["w1"] = np.random.randint(1, 6, m_ * k_).reshape(m_, k_)
    input_info["w2"] = np.random.randint(1, 6, n_ * k_).reshape(n_, k_)
    input_info["x1"] = np.random.randint(1, 6, k_)
    input_info["x2"] = np.random.randint(1, 6, k_)

    input_info["r1"] = np.random.randint(0, 2, m_)
    input_info["y1"] = np.random.randint(0, 6, m_)
    input_info["r2"] = np.random.randint(0, 2, n_)
    input_info["y2"] = np.random.randint(0, 6, n_)
    out_info = numpy_reco(input_info)
    shape_val_pass = NormalizeGraph({"m": m_, "n": n_, "k": k_})
    flatten_pass = Lower({})

    new_graph = shape_val_pass(graph)
    test_res = new_graph(["w1", "w2"], input_info)
    np.testing.assert_allclose(test_res[0], out_info["w1"])
    np.testing.assert_allclose(test_res[1], out_info["w2"])
    flattened_g = flatten_pass(new_graph)
    input_info = {}
    input_info["m"] = m_
    input_info["n"] = n_
    input_info["k"] = k_
    input_info["w1"] = np.random.randint(1, 6, m_ * k_).reshape(m_, k_)
    input_info["w2"] = np.random.randint(1, 6, n_ * k_).reshape(n_, k_)
    input_info["x1"] = np.random.randint(1, 6, k_)
    input_info["x2"] = np.random.randint(1, 6, k_)

    input_info["r1"] = np.random.randint(0, 2, m_)
    input_info["y1"] = np.random.randint(0, 6, m_)
    input_info["r2"] = np.random.randint(0, 2, n_)
    input_info["y2"] = np.random.randint(0, 6, n_)
    new_out_info = numpy_reco(input_info)

    pairs_w1 = list(
        product(*tuple([np.arange(i) for i in input_info["w1"].shape])))
    pairs_w2 = list(
        product(*tuple([np.arange(i) for i in input_info["w2"].shape])))
    w1_init = input_info["w1"]
    for p in pairs_w1:
        input_info[f"w1/w1({p[0]}, {p[1]})"] = input_info["w1"][p]
    input_info.pop("w1")
    w2_init = input_info["w2"]

    for p in pairs_w2:
        input_info[f"w2/w2({p[0]}, {p[1]})"] = input_info["w2"][p]
    input_info.pop("w2")

    for p in range(k_):
        input_info[f"x1/x1({p},)"] = input_info["x1"][p]
        input_info[f"x2/x2({p},)"] = input_info["x2"][p]
    input_info.pop("x1")
    input_info.pop("x2")

    for p in range(m_):
        input_info[f"r1/r1({p},)"] = input_info["r1"][p]
        input_info[f"y1/y1({p},)"] = input_info["y1"][p]
    input_info.pop("r1")
    input_info.pop("y1")

    for p in range(n_):
        input_info[f"r2/r2({p},)"] = input_info["r2"][p]
        input_info[f"y2/y2({p},)"] = input_info["y2"][p]
    input_info.pop("r2")
    input_info.pop("y2")

    w1_keys = [f"w1/w1({p[0]}, {p[1]})" for p in pairs_w1]
    w2_keys = [f"w2/w2({p[0]}, {p[1]})" for p in pairs_w2]

    all_vals = flattened_g(w1_keys + w2_keys, input_info)
    out1 = np.asarray(list(all_vals[0:6])).reshape(new_out_info["w2"].shape)
    out2 = np.asarray(list(all_vals[6:])).reshape(new_out_info["w2"].shape)
    np.testing.assert_allclose(
        new_out_info["w1"],
        np.asarray(list(all_vals[0:6])).reshape(new_out_info["w2"].shape))
    np.testing.assert_allclose(
        new_out_info["w2"],
        np.asarray(list(all_vals[6:])).reshape(new_out_info["w2"].shape))
Beispiel #29
0
 def define_graph(self, x, w, y, mu, m):
     i = pm.index(0, (m - 1).set_name("m-1"), name="i")
     h = pm.sum([i], (x[i] * w[i]), name="h")
     d = (h - y).set_name("h-y")
     g = (d * x[i]).set_name("d*x")
     w[i] = w[i] - mu * g[i]
Beispiel #30
0
def rvmatmul(a, b, shape=None, name=None, **kwargs):
    i = pm.index(0, a.shape[0] - 1)
    j = pm.index(0, b.shape[0] - 1)
    return pm.sum([j], a[i, j] * b[j], name=name)