def test_loop(self): workspace.FeedBlob("cond", np.array(True)) workspace.FeedBlob("ONE", np.array(1)) workspace.FeedBlob("TWO", np.array(2)) workspace.FeedBlob("TEN", np.array(10)) workspace.FeedBlob("counter", np.array(0)) workspace.FeedBlob("output_blob", np.array(0)) loop_model = ModelHelper(name="loop_test_model") loop_model.net.Add(["output_blob", "TWO"], "output_blob") cond_model = ModelHelper(name="cond_test_model") cond_model.net.Add(["counter", "ONE"], "counter") comp_res = cond_model.net.LT(["counter", "TEN"]) cond_model.net.Copy(comp_res, "cond") model = ModelHelper(name="test_model") brew.loop( model=model, cond_blob="cond", external_blobs=["cond", "ONE", "TWO", "TEN", "counter", "output_blob"], loop_model=loop_model, cond_model=cond_model) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) output_value = workspace.FetchBlob("output_blob") self.assertEqual(output_value, 18)
def test_loop(self): workspace.FeedBlob("cond", np.array(True)) workspace.FeedBlob("ONE", np.array(1)) workspace.FeedBlob("TWO", np.array(2)) workspace.FeedBlob("TEN", np.array(10)) workspace.FeedBlob("counter", np.array(0)) workspace.FeedBlob("output_blob", np.array(0)) loop_model = ModelHelper(name="loop_test_model") loop_model.net.Add(["output_blob", "TWO"], "output_blob") cond_model = ModelHelper(name="cond_test_model") cond_model.net.Add(["counter", "ONE"], "counter") comp_res = cond_model.net.LT(["counter", "TEN"]) cond_model.net.Copy(comp_res, "cond") model = ModelHelper(name="test_model") brew.loop(model=model, cond_blob="cond", external_blobs=[ "cond", "ONE", "TWO", "TEN", "counter", "output_blob" ], loop_model=loop_model, cond_model=cond_model) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) output_value = workspace.FetchBlob("output_blob") self.assertEqual(output_value, 18)
model.param_init_net.ConstantFill([], ["i"], shape=[1], value=0) model.param_init_net.ConstantFill([], ["one"], shape=[1], value=1) model.param_init_net.ConstantFill([], ["seven"], shape=[1], value=7) model.param_init_net.ConstantFill([], ["y"], shape=[1], value=0) loop_model = ModelHelper(name="loop_test_model") loop_model.net.Add(["i", "y"], ["y"]) cond_model = ModelHelper(name="cond_test_model") cond_model.net.Add(["i", "one"], "i") cond_model.net.LE(["i", "seven"], "cond") brew.loop( model=model, cond_blob="cond", # explicitly specifying condition blob external_blobs=["cond", "i", "one", "seven", "y"], loop_model=loop_model, cond_model=cond_model # condition model is optional ) # Corresponding blob values: # In[12]: RunNetOnce(model.param_init_net) RunNetOnce(model.net) print("i = ", FetchBlob("i")) print("y = ", FetchBlob("y")) # ### Backpropagation
# Initialize a loop_model that represents the code to run inside of loop loop_model = ModelHelper(name="loop_test_model") loop_model.net.Add(["i", "y"], ["y"]) # Initialize cond_model that represents the conditional test that the loop # abides by, as well as the incrementation step cond_model = ModelHelper(name="cond_test_model") cond_model.net.Add(["i", "one"], "i") cond_model.net.LE(["i", "seven"], "cond") # Use brew's loop operator to facilitate the creation of the loop's operator graph brew.loop( model=model, # main model that contains data cond_blob="cond", # explicitly specifying condition blob external_blobs=["cond", "i", "one", "seven", "y"], # data blobs used in execution of the loop loop_model=loop_model, # pass loop_model cond_model=cond_model # pass condition model (optional) ) # Once again, let's visualize the net using the `net_drawer`. # In[13]: graph = net_drawer.GetPydotGraph(model.net, rankdir="LR") display.Image(graph.create_png(), width=800) # Finally, we'll run the `param_init_net` and `net` and print our final blob values.
model.param_init_net.ConstantFill([], ["i"], shape=[1], value=0) model.param_init_net.ConstantFill([], ["one"], shape=[1], value=1) model.param_init_net.ConstantFill([], ["seven"], shape=[1], value=7) model.param_init_net.ConstantFill([], ["y"], shape=[1], value=0) loop_model = ModelHelper(name="loop_test_model") loop_model.net.Add(["i", "y"], ["y"]) cond_model = ModelHelper(name="cond_test_model") cond_model.net.Add(["i", "one"], "i") cond_model.net.LE(["i", "seven"], "cond") brew.loop( model=model, cond_blob="cond", # explicitly specifying condition blob external_blobs=["cond", "i", "one", "seven", "y"], loop_model=loop_model, cond_model=cond_model # condition model is optional ) # Corresponding blob values: # In[12]: RunNetOnce(model.param_init_net) RunNetOnce(model.net) print("i = ", FetchBlob("i")) print("y = ", FetchBlob("y"))