Toy example of setting up a delayed-execution graph, or "dataflow" in TensorFlow terminology.
Check out the stackoverflow question that inspired this demo.
Use the @dataflow
decorator to wrap a function with dataflow capabilities:
@dataflow
def f(x, y):
return x**2 % y
@dataflow
def g(a, b, c):
return a * b + c
@dataflow
def h(i, j):
return i / j
Calling these function will now store the function and arguments for execution at a later point.
In other words, no mathematical operations will happen until you call
graph.execute()
in step 3.
Compose many @dataflow
functions into a graph:
graph = f(g(1, 2, h(3, 4)), 5)
Execute the function:
answer = graph.execute()
You can also define variables that get bound to values at execution time:
z = Variable()
seed = Variable()
graph = f(g(z, 2, h(4, z)), seed)
You can execute the same graph with different values bound to the variables:
answer1 = graph.execute({z: 1., seed: 5})
answer2 = graph.execute({z: 3., seed: 13})