使用session和只使用session有什么区别

2024-10-05 15:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

使用 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中是局部的?在


Tags: inpysessionlibpackagestftensorflowas
1条回答
网友
1楼 · 发布于 2024-10-05 15:20:50

我想你一定已经读过documentation

Most TensorFlow programs start with a dataflow graph construction phase. In this phase, you invoke TensorFlow API functions that construct new tf.Operation (node) and tf.Tensor (edge) objects and add them to a tf.Graph instance.

我想如果你把第二个例子改成这个,那么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

enter image description here

相关问题 更多 >