def main(): # Two sources and single distination v_in = Vertex("x") # one source v1 = Edge(name="e0")(v_in) v2 = Edge(name="e1")(v1) v3 = Edge(name="e2")(v2, v1) # fork and concate v4 = Edge(name="e3")(v3) v5 = Edge(name="e4")(v4) y = Vertex("y") # second source y.value = np.random.rand(4, 3) v_out = SquareLoss(name="square-loss")(v5, y) # single distination print "----- Vertices and Edges in Graph -----" print len(cg.vertices) print len(cg.edges) print "----- Forward pass (Inference) -----" inputs = np.random.rand(4, 3) v_in.forward(inputs) labels = np.random.rand(4, 3) y.forward(labels) print v1.value print v5.value print "----- Compute Loss -----" v_out.backward(1) print v1.grad
def main(): # Two sources and single distination v_in = Vertex("x") # one source v1_w = Vertex("w1", value=np.random.rand(10, 5)) v1 = Affine(name="e1")(v1_w, v_in) v2_w = Vertex("w2", value=np.random.rand(5, 1)) v2 = Affine(name="e2")(v2_w, v1) y = Vertex("y") # second source v_out = SquareLoss(name="square-loss")(v2, y) # single distination print "----- Vertices and Edges in Graph -----" print len(cg.vertices) print len(cg.edges) print "----- Forward pass (Inference) -----" inputs = np.random.rand(100, 10) v_in.forward(inputs) v1_w.forward() v2_w.forward() labels = np.random.rand(100, 1) y.forward(labels) print v2.value print "----- Compute Loss -----" v_out.backward(1) v2_w.grad
def main(): # Fork and concate v_in = Vertex("x") # single source v1 = Edge(name="e0")(v_in) v2 = Edge(name="e1")(v1) v3 = Edge(name="e2")(v2, v1) # fork and concate v4 = Edge(name="e3")(v3) v5 = Edge(name="e4")(v4) print "----- Vertices and Edges in Graph -----" print len(cg.vertices) print len(cg.edges) print "----- Forward pass (Inference) -----" inputs = np.random.rand(4, 3) v_in.forward(inputs) print v1.value print v5.value print "----- Backward pass (from the middle) -----" grad = np.random.rand(4, 3) v3.backward(grad) print v1.grad
def main(): # Two sources and one distination v1_in = Vertex("x1") # one source v1_1 = Edge(name="e1_0")(v1_in) v1_2 = Edge(name="e1_1")(v1_1) v1_3 = Edge(name="e1_2")(v1_2) v1_4 = Edge(name="e1_3")(v1_3) v1_5 = Edge(name="e1_4")(v1_4) y1 = Vertex("y1") # second source y1.value = np.random.rand(4, 3) v1_out = SquareLoss(name="square-loss")(v1_5, y1) # one distination # Two sources and one distination v2_in = Vertex("x2") # one source v2_1 = Edge(name="e2_0")(v2_in) v2_2 = Edge(name="e2_1")(v2_1) v2_3 = Edge(name="e2_2")(v2_2) v2_4 = Edge(name="e2_3")(v2_3) v2_5 = Edge(name="e2_4")(v2_4) y2 = Vertex("y2") # second source y2.value = np.random.rand(4, 3) v2_out = SquareLoss(name="square-loss")(v2_5, y2) # one distination # Objective function v_obj = v1_out + v2_out print "----- Vertices and Edges in Graph -----" print len(cg.vertices) print len(cg.edges) print "----- Forward pass (Inference) -----" inputs1 = np.random.rand(4, 3) inputs2 = np.random.rand(4, 3) v1_in.forward(inputs1), v2_in.forward(inputs2) labels1 = np.random.rand(4, 3) labels2 = np.random.rand(4, 3) y1.forward(labels1), y2.forward(labels2) print "----- Compute Loss -----" v_obj.backward() print v1_1.grad
def main(): # Additon print "# Additon" v = np.asarray(np.arange(1, 10)).reshape(3, 3).astype(np.float32) v0 = Vertex(name="", value=v) v1 = Vertex(name="", value=v) v2 = v0 + v1 v0.forward(), v1.forward() print v2.value v2.backward() print v0.grad print v1.grad print "" # Subtraction print "# Subtraction" v = np.asarray(np.arange(1, 10)).reshape(3, 3).astype(np.float32) v0 = Vertex(name="", value=v) v1 = Vertex(name="", value=v) v2 = v0 - v1 v0.forward(), v1.forward() print v2.value v2.backward() print v0.grad print v1.grad print "" # Multiplication print "# Multiplication" v = np.asarray(np.arange(1, 10)).reshape(3, 3).astype(np.float32) v0 = Vertex(name="", value=v) v1 = Vertex(name="", value=v) v2 = v0 * v1 v0.forward(), v1.forward() print v2.value v2.backward() print v0.grad print v1.grad print "" # Division print "# Division" v = np.asarray(np.arange(1, 10)).reshape(3, 3).astype(np.float32) v0 = Vertex(name="", value=v) v1 = Vertex(name="", value=v) v2 = v0 / v1 v0.forward(), v1.forward() print v2.value v2.backward() print v0.grad print v1.grad print ""