def test_multidim_sigmoid(m_): with pm.Node(name="logistic") 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(w[i] * x[i], name="out") x_ = np.random.randint(0, 10, m_).astype(np.float) w_ = np.random.randint(0, 10, m_).astype(np.float) shape_dict = {"m": m_} input_dict = {"x": x_, "w": w_} np_res = sigmoid((x_ * w_)) coarse_eval = graph("out", input_dict) np.testing.assert_allclose(np_res, coarse_eval) lowered = set_shape_and_lower(graph, shape_dict) keys = [f"out/out({i},)" for i in range(m_)] x_ = np.random.randint(0, 10, m_).astype(np.float) w_ = np.random.randint(0, 10, m_).astype(np.float) input_dict = {} for i in range(m_): input_dict[f"x/x({i},)"] = x_[i] input_dict[f"w/w({i},)"] = w_[i] np_res = sigmoid((x_ * w_)) lower_res = np.asarray(lowered(keys, input_dict)).reshape(np_res.shape) np.testing.assert_allclose(lower_res, np_res)
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]
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)
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"))
def define_graph(self, x, out): indices = _get_single_node_indices(out, shape=out.shape) out[indices] = pm.sigmoid(x[indices])
def define_graph(self, x, w, y): i = pm.index(0, (w.shape[1] - 1), name="i") j = pm.index(0, (w.shape[0] - 1), name="j") y[j] = pm.sigmoid(pm.sum([i], w[j, i] * x[i], name="h"))