使用
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())
上面说的没有错
^{pr2}$但这给了我一个错误
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中是局部的?在
我想你一定已经读过documentation
我想如果你把第二个例子改成这个,那么
init
操作就是图的一部分。在你也可以在tensorboard中看到这一点
tensorboard logdir D:/Development_Avecto/TensorFlow/output
相关问题 更多 >
编程相关推荐