- Python Reinforcement Learning
- Sudharsan Ravichandiran Sean Saito Rajalingappaa Shanmugamani Yang Wenzhuo
- 316字
- 2021-06-24 15:17:29
Adding scope
Scoping is used to reduce complexity and helps us to better understand the model by grouping the related nodes together. For instance, in the previous example, we can break down our graph into two different groups called computation and result. If you look at the previous example, you can see that nodes a to e perform the computation and node g calculates the result. So we can group them separately using the scope for easy understanding. Scoping can be created using the tf.name_scope() function.
Let's use the tf.name_scope() function using Computation:
with tf.name_scope("Computation"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
Let's use the tf.name_scope() function using Result:
with tf.name_scope("Result"):
g = tf.add(c,f)
Look at the Computation scope; we can further break down into separate parts for even more understanding. We can create a scope as Part 1, which has nodes a to c, and a scope as Part 2, which has nodes d to e, as part 1 and 2 are independent of each other:
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
Scoping can be better understood by visualizing them in the TensorBoard. The complete code is as follows:
import tensorflow as tf
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
with tf.name_scope("Result"):
g = tf.add(c,f)
with tf.Session() as sess:
writer = tf.summary.FileWriter("output", sess.graph)
print(sess.run(g))
writer.close()
If you look at the following diagram, you can easily understand how scope helps us to reduce complexity in understanding by grouping the similar nodes together. Scoping is widely used while working on a complex project to better understand the functionality and dependencies of nodes:

- 數據庫基礎教程(SQL Server平臺)
- 數據挖掘原理與實踐
- 數據庫原理及應用教程(第4版)(微課版)
- 云計算環境下的信息資源集成與服務
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- 3D計算機視覺:原理、算法及應用
- 數據庫技術實用教程
- Oracle高性能SQL引擎剖析:SQL優化與調優機制詳解
- R Machine Learning Essentials
- 數據庫原理及應用:SQL Server 2016
- 一本書讀懂大數據
- Delphi High Performance
- 數據分析方法及應用:基于SPSS和EXCEL環境
- 數據庫原理及應用實驗:基于GaussDB的實現方法
- Hadoop大數據技術開發實戰