问题描述
有人可以向我解释 name_scope
在 TensorFlow 中是如何工作的吗?
Can someone explain to me how name_scope
works in TensorFlow?
假设我有以下代码:
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default() as g:
with g.name_scope( "g1" ) as scope:
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
tf.reset_default_graph()
g2 = tf.Graph()
with g2.as_default() as g:
with g.name_scope( "g2" ) as scope:
matrix1 = tf.constant([[4., 4.]])
matrix2 = tf.constant([[5.],[5.]])
product = tf.matmul(matrix1, matrix2)
tf.reset_default_graph()
with tf.Session( graph = g1 ) as sess:
result = sess.run( product )
print( result )
当我运行此代码时,我收到以下错误消息:
When I run this code I get the following error message:
Tensor Tensor("g2/MatMul:0", shape=(1, 1), dtype=float32) is not an element of this graph.
我同意g2/MatMul"不是图g1
的元素,但是为什么当会话图设置为g1
时它选择g2/MatMul"?为什么不选择g1/MatMul"?
I agree "g2/MatMul" is not an element of graph g1
, but why is it selecting "g2/MatMul" when the session graph is set to g1
? Why doesn't it select "g1/MatMul"?
以下代码似乎有效:
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default() as g:
with g.name_scope( "g1" ) as g1_scope:
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul( matrix1, matrix2, name = "product")
tf.reset_default_graph()
g2 = tf.Graph()
with g2.as_default() as g:
with g.name_scope( "g2" ) as g2_scope:
matrix1 = tf.constant([[4., 4.]])
matrix2 = tf.constant([[5.],[5.]])
product = tf.matmul( matrix1, matrix2, name = "product" )
tf.reset_default_graph()
use_g1 = False
if ( use_g1 ):
g = g1
scope = g1_scope
else:
g = g2
scope = g2_scope
with tf.Session( graph = g ) as sess:
tf.initialize_all_variables()
result = sess.run( sess.graph.get_tensor_by_name( scope + "product:0" ) )
print( result )
通过拨动开关 use_g1
,图形 g1
或 g2
将在会话中运行.这是名称范围界定的工作方式吗?
By flipping the switch use_g1
, graph g1
or g2
will run in the session. Is this the way name scoping was meant to work?
推荐答案
你的 product
是一个全局变量,你已经将它设置为指向g2/MatMul".
Your product
is a global variable, and you've set it to point to "g2/MatMul".
特别是
试试
print product
你会看到
Tensor("g2/MatMul:0", shape=(1, 1), dtype=float32)
因此系统采用 "g2/MatMul:0"
因为这是张量的名称,并尝试在图 g1
中找到它,因为这是您设置的图会议.顺便说一下,你可以看到图中的所有节点 print [n.name for n in g1.as_graph_def().node]
So the system takes "g2/MatMul:0"
since that's the Tensor's name, and tries to find it in the graph g1
since that's the graph you set for the session. Incidentally you can see all nodes in the graph print [n.name for n in g1.as_graph_def().node]
通常,使用多个图表很少有用.你不能合并它们,也不能在它们之间传递张量.我建议只做
Generally, using more than one graph is rarely useful. You can't merge them and can't pass tensors between them. I'd recommend just doing
tf.reset_default_graph()
a = tf.Constant(2)
sess = tf.InteractiveSession()
....
这样,您将拥有一个默认图表和一个默认会话,并且在大多数情况下您可以省略指定图表或会话.如果您需要明确引用它们,可以从 tf.get_default_graph()
或 tf.get_default_session()
This way you'll have one default graph and one default session and you can omit specifying graph or session in most cases. If you ever need to refer to them explicitly, you can get them from tf.get_default_graph()
or tf.get_default_session()
这篇关于在 TensorFlow 中处理多个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!