What does the graph argument in tf.Session () do?

I am having trouble understanding the graph argument in tf.Session(). I tried to take a look at the TensorFlow website: link , but it could not understand much.

I am trying to find different values ​​between tf.Session()and tf.Session(graph=some_graph_inserted_here).

Question Context

Code A (not working):

def predict():
    with tf.name_scope("predict"):
        with tf.Session() as sess:
            saver = tf.train.import_meta_graph("saved_models/testing.meta")
            saver.restore(sess, "saved_models/testing")
            loaded_graph = tf.get_default_graph()
            output_ = loaded_graph.get_tensor_by_name('loss/network/output_layer/BiasAdd:0')
            _x = loaded_graph.get_tensor_by_name('x:0')
            print sess.run(output_, feed_dict={_x: np.array([12003]).reshape([-1, 1])})

This code gives the following error: ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already usedwhen trying to load a chartsaver = tf.train.import_meta_graph("saved_models/testing.meta")

Code B (working):

def predict():
    with tf.name_scope("predict"):
        loaded_graph = tf.Graph()
        with tf.Session(graph=loaded_graph) as sess:
            saver = tf.train.import_meta_graph("saved_models/testing.meta")
            saver.restore(sess, "saved_models/testing")
            output_ = loaded_graph.get_tensor_by_name('loss/network/output_layer/BiasAdd:0')
            _x = loaded_graph.get_tensor_by_name('x:0')
            print sess.run(output_, feed_dict={_x: np.array([12003]).reshape([-1, 1])})

Codes do not work if I replaced loaded_graph = tf.Graph()with loaded_graph = tf.get_default_graph(). What for?

Full code if it helps: ( https://gist.github.com/duemaster/f8cf05c0923ebabae476b83e895619ab )

+4
source share
4 answers

TensorFlow Graph - , tf.Tensor tf.Operation.

(, tf.Variable tf.constant) (, tf.matmul), ( Graph , , ). , , tf.get_default_graph.

graphes :

g = tf.Graph()
with g.as_default():
    [your code]

, , tf.Session, TensorFlow, .

A

  • ,
  • ( , )
  • ,

B

  • ,
  • ( , )
  • .

:

tf.Graph API

:

Code A (I reset , name_scope).

def predict():
    tf.reset_default_graph()
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph("saved_models/testing.meta")
        saver.restore(sess, "saved_models/testing")
        loaded_graph = tf.get_default_graph()
        output_ = loaded_graph.get_tensor_by_name('loss/network/output_layer/BiasAdd:0')
        _x = loaded_graph.get_tensor_by_name('x:0')
        print(sess.run(output_, feed_dict={_x: np.array([12003]).reshape([-1, 1])}))
+2

Tensorflow . Tensorflow ( ) ( , tf.get_default_graph()). Session .

( ), . , tf.train.import_meta_graph(). , , , , , .

, tf.Graph() Session ( ), , .

+2

tf.train.import_meta_graph("saved_models/testing.meta") meta current graph. current graph - default_graph, , . , !.

+1

.

, , ( tf.get_default_graph).

A , , node, .

Your B code works because you put a new empyt graph (created with tf.Graph()) in the session : when you import the graph definition, there is no collision between the existing nodes in the current session (0 because the graph is empty) and the ones you import

+1
source

Source: https://habr.com/ru/post/1680705/


All Articles