使用
with tf.Session(graph = g) as sess:
没有给我一个错误
但是如果我使用
sess = tf.session(graph = g)
我收到错误消息
g = tf.Graph()
with g.as_default():
v1 = tf.Variable(1,name = "v1")
v2 = tf.Variable(2,name = "v2")
with tf.session(graph = g) as sess:
sess.run(tf.global_variables_initializer())
上面没有给我任何错误
import tensorflow as tf
g = tf.Graph()
with g.as_default():
v1 = tf.Variable(1,name = "v1")
v2 = tf.Variable(2,name = "v2")
sess = tf.Session(graph = g)
sess.run(tf.global_variables_initializer())
但这给我一个错误
Traceback (most recent call last):
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 300, in __init__
fetch, allow_tensor=True, allow_operation=True))
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3574, in _as_graph_element_locked
raise ValueError("Operation %s is not an element of this graph." % obj)
ValueError: Operation name: "init"
op: "NoOp"
is not an element of this graph.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "maintest.py", line 14, in <module>
sess.run(tf.global_variables_initializer())
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1137, in _run
self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 471, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 271, in for_fetch
return _ElementFetchMapper(fetches, contraction_fn)
File "/Users/wonjunson/gym/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 307, in __init__
'Tensor. (%s)' % (fetch, str(e)))
ValueError: Fetch argument <tf.Operation 'init' type=NoOp> cannot be interpreted as a Tensor. (Operation name: "init"
op: "NoOp"
is not an element of this graph.)
但是,我看不出它们之间的巨大差异。
我猜这是因为我正在使用global_variables_initializer
。由于我只需要在g中初始化变量。
但是,变量仍然包含在global_variables
中,不是吗?
还是变量在图g中是局部的?
答案 0 :(得分:0)
我认为您必须已经阅读Related objects reference
大多数TensorFlow程序从数据流图构建阶段开始。在此阶段,您将调用TensorFlow API函数,这些函数构造新的tf.Operation(节点)和tf.Tensor(边缘)对象,并将它们添加到tf.Graph实例中。
我认为,如果您将第二个示例更改为此,则init
操作是图形的一部分。
g = tf.Graph()
with g.as_default():
v1 = tf.Variable(1,name = "v1")
v2 = tf.Variable(2,name = "v2")
init = tf.global_variables_initializer() # Part of the graph now
sess = tf.Session(graph = g)
writer = tf.summary.FileWriter("D:/Development_Avecto/TensorFlow/output", sess.graph)
sess.run(init)
writer.close()
您还可以在tensorboard中将其可视化
tensorboard --logdir D:/Development_Avecto/TensorFlow/output
答案 1 :(得分:0)
您还可以使用 as_default()
限制会话和图形上下文以避免错误:
self.sess = tf.Session(graph=self.graph, config=tf_config)
with self.sess.as_default():
with self.graph.as_default():
self.sess.run(tf.global_variables_initializer())