How to use tf.reset_default_graph ()

Whenever I try to use tf.reset_default_graph(), I get this error: IndexError: list index out of rangeor. '' What part of my code should I use this? When should I use this?

Edit:

I updated the code, but the error is still happening.

def evaluate():
    with tf.name_scope("loss"):
        global x # x is a tf.placeholder()
        xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=neural_network(x))
        loss = tf.reduce_mean(xentropy, name="loss")

    with tf.name_scope("train"):
        optimizer = tf.train.AdamOptimizer()
        training_op = optimizer.minimize(loss)

    with tf.name_scope("exec"):
        with tf.Session() as sess:
            for i in range(1, 2):
                sess.run(tf.global_variables_initializer())
                sess.run(training_op, feed_dict={x: np.array(train_data).reshape([-1, 1]), y: label})
                print "Training " + str(i)
                saver = tf.train.Saver()
                saver.save(sess, "saved_models/testing")
                print "Model Saved."


def predict():
    with tf.name_scope("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")
            output_ = tf.get_default_graph().get_tensor_by_name('output_layer:0')
            print sess.run(output_, feed_dict={x: np.array([12003]).reshape([-1, 1])})


def main():
    print "Starting Program..."
    evaluate()
    writer = tf.summary.FileWriter("mygraph/logs", tf.get_default_graph())
    predict()

If I remove tf.reset_default_graph () from the updated code, I get this error: ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already used

Based on my current understanding, tf.reset_default_graph () deletes all graphs, so I avoided the error that I mentioned above ( ValueError: cannot add op with name hidden_layer1/kernel/Adam as that name is already used)

+10
source share
3 answers

This is probably how you use this:

import tensorflow as tf
a = tf.constant(1)
with tf.Session() as sess:
    tf.reset_default_graph()

, . tf.reset_default_graph():

, tf.Session tf.InteractiveSession, . tf.Operation tf.Tensor


tf.reset_default_graph() ( ) , Jupyter. , , .

, :

import tensorflow as tf
# create some graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(...)

, , . , . , :

tf.reset_default_graph()
# create a new graph
with tf.Session() as sess:
    print sess.run(...)

, :

with tf.name_scope("predict"):
    tf.reset_default_graph()

. , tf.name_scope - . " - ", TF , , - .

+14

- FOR LOTS OF TIMES, , ! : -)

import tensorflow as tf
from my_models import Classifier

for i in range(10):
    tf.reset_default_graph()
    # build the graph
    global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
    classifier = Classifier(global_step)
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        print("do sth here.")
+1

Simply put, use to clear the previous placeholder that you created with sess.run ().

0
source

All Articles