Creating a name scope

Scoping is used to reduce complexity and helps us to better understand a model by grouping related nodes together. Having a name scope helps us to group similar operations in a graph. It comes in handy when we are building a complex architecture. Scoping can be created using tf.name_scope(). In the previous example, we performed two operations, Product and sum. We can simply group them into two different name scopes as Product and sum.

In the previous section, we saw how prod1 and prod2 perform multiplication and compute the result. We'll define a name scope called Product, and group the prod1 and prod2 operations, as shown in the following code:

with tf.name_scope("Product"):
with tf.name_scope("prod1"):
prod1 = tf.multiply(x,y,name='prod1')

with tf.name_scope("prod2"):
prod2 = tf.multiply(a,b,name='prod2')

Now, define the name scope for sum:

with tf.name_scope("sum"):
sum = tf.add(prod1,prod2,name='sum')

Store the file in the graphs directory:

with tf.Session() as sess:
writer = tf.summary.FileWriter('./graphs', sess.graph)
print(sess.run(sum))

Visualize the graph in TensorBoard:

tensorboard --logdir=graphs --port=8000

As you may notice, now, we have only two nodes, sum and Product:

Once we double-click on the nodes, we can see how the computation is happening. As you can see, the prod1 and prod2 nodes are grouped under the Product scope, and their results are sent to the sum node, where they will be added. You can see how the prod1 and prod2 nodes compute their value:

The preceding example is just a simple example. When we are working on a complex project with a lot of operations, name scoping helps us to group similar operations together and enables us to understand the computational graph better.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset