How to predict values with a trained Tensorflow model





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















I've trained my NN in Tensorflow and saved the model like this:



def neural_net(x):
layer_1 = tf.layers.dense(inputs=x, units=195, activation=tf.nn.sigmoid)
out_layer = tf.layers.dense(inputs=layer_1, units=6)
return out_layer

train_x = pd.read_csv("data_x.csv", sep=" ")
train_y = pd.read_csv("data_y.csv", sep=" ")
train_x = train_x / 6 - 0.5

train_size = 0.9
train_cnt = int(floor(train_x.shape[0] * train_size))
x_train = train_x.iloc[0:train_cnt].values
y_train = train_y.iloc[0:train_cnt].values
x_test = train_x.iloc[train_cnt:].values
y_test = train_y.iloc[train_cnt:].values

x = tf.placeholder("float", [None, 386])
y = tf.placeholder("float", [None, 6])

nn_output = neural_net(x)

cost = tf.reduce_mean(tf.losses.mean_squared_error(labels=y, predictions=nn_output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

training_epochs = 5000
display_step = 1000
batch_size = 30

keep_prob = tf.placeholder("float")

saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
total_batch = int(len(x_train) / batch_size)
x_batches = np.array_split(x_train, total_batch)
y_batches = np.array_split(y_train, total_batch)
for i in range(total_batch):
batch_x, batch_y = x_batches[i], y_batches[i]
_, c = sess.run([optimizer, cost],
feed_dict={
x: batch_x,
y: batch_y,
keep_prob: 0.8
})
saver.save(sess, 'trained_model', global_step=1000)


Now I want to use the trained model in a different file. Of course there are many many examples of restoring and saving the model, I went through lots of them. Still I couldn't make any of them work, there is always some kind of error. So this is my restore file, could you please help me to make it restore the saved model?



saver = tf.train.import_meta_graph('trained_model-1000.meta')
y_pred =
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('./'))
sess.run([y_pred], feed_dict={x: input_values})


E.g. this attempt gave me the error "The session graph is empty. Add operations to the graph before calling run()." So what operation should I add to the graph and how? I don't know what that operation should be in my model... I don't understand this whole concept of saving/restoring in Tensorflow. Or should I do the restoring completely differently? Thanks in advance!










share|improve this question

























  • Have you tried putting saver = tf.train.import_meta_graph('trained_model-1000.meta') within the with tf.Session() as sess: ? Maybe with tf.reset_default_graph() before that just to be sure...

    – gdelab
    Oct 12 '17 at 11:50













  • Hi, yes, it's solved this particular error, thanks. But the model is still not properly restored. I updated the code so there's more context.

    – T.Poe
    Oct 12 '17 at 17:00











  • Or maybe it is restored, but I just don't know how to use it to make new predictions.

    – T.Poe
    Oct 12 '17 at 17:10











  • @T.Poe Are there any updates on how to handle this?

    – Low Yield Bond
    Apr 23 '18 at 19:08











  • @LowYieldBond You have to define neural_net(x) the same way as in the training file, then restore its data the way like in my question or in the CAta.RAy's answer below. Then you predict like in Alli Abbasi's answer.

    – T.Poe
    Apr 25 '18 at 20:33




















5















I've trained my NN in Tensorflow and saved the model like this:



def neural_net(x):
layer_1 = tf.layers.dense(inputs=x, units=195, activation=tf.nn.sigmoid)
out_layer = tf.layers.dense(inputs=layer_1, units=6)
return out_layer

train_x = pd.read_csv("data_x.csv", sep=" ")
train_y = pd.read_csv("data_y.csv", sep=" ")
train_x = train_x / 6 - 0.5

train_size = 0.9
train_cnt = int(floor(train_x.shape[0] * train_size))
x_train = train_x.iloc[0:train_cnt].values
y_train = train_y.iloc[0:train_cnt].values
x_test = train_x.iloc[train_cnt:].values
y_test = train_y.iloc[train_cnt:].values

x = tf.placeholder("float", [None, 386])
y = tf.placeholder("float", [None, 6])

nn_output = neural_net(x)

cost = tf.reduce_mean(tf.losses.mean_squared_error(labels=y, predictions=nn_output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

training_epochs = 5000
display_step = 1000
batch_size = 30

keep_prob = tf.placeholder("float")

saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
total_batch = int(len(x_train) / batch_size)
x_batches = np.array_split(x_train, total_batch)
y_batches = np.array_split(y_train, total_batch)
for i in range(total_batch):
batch_x, batch_y = x_batches[i], y_batches[i]
_, c = sess.run([optimizer, cost],
feed_dict={
x: batch_x,
y: batch_y,
keep_prob: 0.8
})
saver.save(sess, 'trained_model', global_step=1000)


Now I want to use the trained model in a different file. Of course there are many many examples of restoring and saving the model, I went through lots of them. Still I couldn't make any of them work, there is always some kind of error. So this is my restore file, could you please help me to make it restore the saved model?



saver = tf.train.import_meta_graph('trained_model-1000.meta')
y_pred =
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('./'))
sess.run([y_pred], feed_dict={x: input_values})


E.g. this attempt gave me the error "The session graph is empty. Add operations to the graph before calling run()." So what operation should I add to the graph and how? I don't know what that operation should be in my model... I don't understand this whole concept of saving/restoring in Tensorflow. Or should I do the restoring completely differently? Thanks in advance!










share|improve this question

























  • Have you tried putting saver = tf.train.import_meta_graph('trained_model-1000.meta') within the with tf.Session() as sess: ? Maybe with tf.reset_default_graph() before that just to be sure...

    – gdelab
    Oct 12 '17 at 11:50













  • Hi, yes, it's solved this particular error, thanks. But the model is still not properly restored. I updated the code so there's more context.

    – T.Poe
    Oct 12 '17 at 17:00











  • Or maybe it is restored, but I just don't know how to use it to make new predictions.

    – T.Poe
    Oct 12 '17 at 17:10











  • @T.Poe Are there any updates on how to handle this?

    – Low Yield Bond
    Apr 23 '18 at 19:08











  • @LowYieldBond You have to define neural_net(x) the same way as in the training file, then restore its data the way like in my question or in the CAta.RAy's answer below. Then you predict like in Alli Abbasi's answer.

    – T.Poe
    Apr 25 '18 at 20:33
















5












5








5


2






I've trained my NN in Tensorflow and saved the model like this:



def neural_net(x):
layer_1 = tf.layers.dense(inputs=x, units=195, activation=tf.nn.sigmoid)
out_layer = tf.layers.dense(inputs=layer_1, units=6)
return out_layer

train_x = pd.read_csv("data_x.csv", sep=" ")
train_y = pd.read_csv("data_y.csv", sep=" ")
train_x = train_x / 6 - 0.5

train_size = 0.9
train_cnt = int(floor(train_x.shape[0] * train_size))
x_train = train_x.iloc[0:train_cnt].values
y_train = train_y.iloc[0:train_cnt].values
x_test = train_x.iloc[train_cnt:].values
y_test = train_y.iloc[train_cnt:].values

x = tf.placeholder("float", [None, 386])
y = tf.placeholder("float", [None, 6])

nn_output = neural_net(x)

cost = tf.reduce_mean(tf.losses.mean_squared_error(labels=y, predictions=nn_output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

training_epochs = 5000
display_step = 1000
batch_size = 30

keep_prob = tf.placeholder("float")

saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
total_batch = int(len(x_train) / batch_size)
x_batches = np.array_split(x_train, total_batch)
y_batches = np.array_split(y_train, total_batch)
for i in range(total_batch):
batch_x, batch_y = x_batches[i], y_batches[i]
_, c = sess.run([optimizer, cost],
feed_dict={
x: batch_x,
y: batch_y,
keep_prob: 0.8
})
saver.save(sess, 'trained_model', global_step=1000)


Now I want to use the trained model in a different file. Of course there are many many examples of restoring and saving the model, I went through lots of them. Still I couldn't make any of them work, there is always some kind of error. So this is my restore file, could you please help me to make it restore the saved model?



saver = tf.train.import_meta_graph('trained_model-1000.meta')
y_pred =
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('./'))
sess.run([y_pred], feed_dict={x: input_values})


E.g. this attempt gave me the error "The session graph is empty. Add operations to the graph before calling run()." So what operation should I add to the graph and how? I don't know what that operation should be in my model... I don't understand this whole concept of saving/restoring in Tensorflow. Or should I do the restoring completely differently? Thanks in advance!










share|improve this question
















I've trained my NN in Tensorflow and saved the model like this:



def neural_net(x):
layer_1 = tf.layers.dense(inputs=x, units=195, activation=tf.nn.sigmoid)
out_layer = tf.layers.dense(inputs=layer_1, units=6)
return out_layer

train_x = pd.read_csv("data_x.csv", sep=" ")
train_y = pd.read_csv("data_y.csv", sep=" ")
train_x = train_x / 6 - 0.5

train_size = 0.9
train_cnt = int(floor(train_x.shape[0] * train_size))
x_train = train_x.iloc[0:train_cnt].values
y_train = train_y.iloc[0:train_cnt].values
x_test = train_x.iloc[train_cnt:].values
y_test = train_y.iloc[train_cnt:].values

x = tf.placeholder("float", [None, 386])
y = tf.placeholder("float", [None, 6])

nn_output = neural_net(x)

cost = tf.reduce_mean(tf.losses.mean_squared_error(labels=y, predictions=nn_output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

training_epochs = 5000
display_step = 1000
batch_size = 30

keep_prob = tf.placeholder("float")

saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
total_batch = int(len(x_train) / batch_size)
x_batches = np.array_split(x_train, total_batch)
y_batches = np.array_split(y_train, total_batch)
for i in range(total_batch):
batch_x, batch_y = x_batches[i], y_batches[i]
_, c = sess.run([optimizer, cost],
feed_dict={
x: batch_x,
y: batch_y,
keep_prob: 0.8
})
saver.save(sess, 'trained_model', global_step=1000)


Now I want to use the trained model in a different file. Of course there are many many examples of restoring and saving the model, I went through lots of them. Still I couldn't make any of them work, there is always some kind of error. So this is my restore file, could you please help me to make it restore the saved model?



saver = tf.train.import_meta_graph('trained_model-1000.meta')
y_pred =
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('./'))
sess.run([y_pred], feed_dict={x: input_values})


E.g. this attempt gave me the error "The session graph is empty. Add operations to the graph before calling run()." So what operation should I add to the graph and how? I don't know what that operation should be in my model... I don't understand this whole concept of saving/restoring in Tensorflow. Or should I do the restoring completely differently? Thanks in advance!







python tensorflow save restore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 12 '17 at 16:57







T.Poe

















asked Oct 12 '17 at 10:07









T.PoeT.Poe

4982625




4982625













  • Have you tried putting saver = tf.train.import_meta_graph('trained_model-1000.meta') within the with tf.Session() as sess: ? Maybe with tf.reset_default_graph() before that just to be sure...

    – gdelab
    Oct 12 '17 at 11:50













  • Hi, yes, it's solved this particular error, thanks. But the model is still not properly restored. I updated the code so there's more context.

    – T.Poe
    Oct 12 '17 at 17:00











  • Or maybe it is restored, but I just don't know how to use it to make new predictions.

    – T.Poe
    Oct 12 '17 at 17:10











  • @T.Poe Are there any updates on how to handle this?

    – Low Yield Bond
    Apr 23 '18 at 19:08











  • @LowYieldBond You have to define neural_net(x) the same way as in the training file, then restore its data the way like in my question or in the CAta.RAy's answer below. Then you predict like in Alli Abbasi's answer.

    – T.Poe
    Apr 25 '18 at 20:33





















  • Have you tried putting saver = tf.train.import_meta_graph('trained_model-1000.meta') within the with tf.Session() as sess: ? Maybe with tf.reset_default_graph() before that just to be sure...

    – gdelab
    Oct 12 '17 at 11:50













  • Hi, yes, it's solved this particular error, thanks. But the model is still not properly restored. I updated the code so there's more context.

    – T.Poe
    Oct 12 '17 at 17:00











  • Or maybe it is restored, but I just don't know how to use it to make new predictions.

    – T.Poe
    Oct 12 '17 at 17:10











  • @T.Poe Are there any updates on how to handle this?

    – Low Yield Bond
    Apr 23 '18 at 19:08











  • @LowYieldBond You have to define neural_net(x) the same way as in the training file, then restore its data the way like in my question or in the CAta.RAy's answer below. Then you predict like in Alli Abbasi's answer.

    – T.Poe
    Apr 25 '18 at 20:33



















Have you tried putting saver = tf.train.import_meta_graph('trained_model-1000.meta') within the with tf.Session() as sess: ? Maybe with tf.reset_default_graph() before that just to be sure...

– gdelab
Oct 12 '17 at 11:50







Have you tried putting saver = tf.train.import_meta_graph('trained_model-1000.meta') within the with tf.Session() as sess: ? Maybe with tf.reset_default_graph() before that just to be sure...

– gdelab
Oct 12 '17 at 11:50















Hi, yes, it's solved this particular error, thanks. But the model is still not properly restored. I updated the code so there's more context.

– T.Poe
Oct 12 '17 at 17:00





Hi, yes, it's solved this particular error, thanks. But the model is still not properly restored. I updated the code so there's more context.

– T.Poe
Oct 12 '17 at 17:00













Or maybe it is restored, but I just don't know how to use it to make new predictions.

– T.Poe
Oct 12 '17 at 17:10





Or maybe it is restored, but I just don't know how to use it to make new predictions.

– T.Poe
Oct 12 '17 at 17:10













@T.Poe Are there any updates on how to handle this?

– Low Yield Bond
Apr 23 '18 at 19:08





@T.Poe Are there any updates on how to handle this?

– Low Yield Bond
Apr 23 '18 at 19:08













@LowYieldBond You have to define neural_net(x) the same way as in the training file, then restore its data the way like in my question or in the CAta.RAy's answer below. Then you predict like in Alli Abbasi's answer.

– T.Poe
Apr 25 '18 at 20:33







@LowYieldBond You have to define neural_net(x) the same way as in the training file, then restore its data the way like in my question or in the CAta.RAy's answer below. Then you predict like in Alli Abbasi's answer.

– T.Poe
Apr 25 '18 at 20:33














3 Answers
3






active

oldest

votes


















2














Forgive me if I am wrong but tf.train.Saver() only saves the variable values not the graph itself. This means that if you want to load the model in a different file you need to rebuild the graph or somehow load the graph as well. Tensorflow documentation states:




The tf.train.Saver object not only saves variables to checkpoint files, it also restores variables. Note that when you restore variables from a file you do not have to initialize them beforehand.




Consider the following example:



One file that saves the model:



# Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1)

# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, and save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
# Do some work with the model.
inc_v1.op.run()
dec_v2.op.run()
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in file: %s" % save_path)


The other file that loads the previously saved model:



tf.reset_default_graph()

# Create some variables.
v1 = tf.get_variable("v1", shape=[3])
v2 = tf.get_variable("v2", shape=[5])

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "/tmp/model.ckpt")
print("Model restored.")
# Check the values of the variables
print("v1 : %s" % v1.eval())
print("v2 : %s" % v2.eval())





share|improve this answer


























  • Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

    – T.Poe
    Oct 12 '17 at 16:59











  • What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

    – Lasse Jacobs
    Oct 13 '17 at 6:49













  • As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

    – T.Poe
    Oct 13 '17 at 8:23











  • @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

    – pfc
    Aug 15 '18 at 5:51



















0














 output = sess.run(nn_output, feed_dict={ x: batch_x, keep_prob: 0.8 })


Where nn_output is the name is the output variable of the last layer of you network. You can save you variable using:



saver = tf.train.Saver([nn_output])
saver.save(sess, 'my_test_model',global_step=1000) # save every 1000 steps


and therefore in your code:



out_layer = tf.layers.dense(inputs=layer_1, units=6)


should be :



out_layer = tf.layers.dense(inputs=layer_1, units=6, name='nn_output')


To restore:



with tf.Session() as sess:    
saver = tf.train.import_meta_graph('my_test_model')
saver.restore(sess,tf.train.latest_checkpoint('./'))


Now you should have access to that node of the graph. If the name is not specified, it is difficult to recover that particular layer.






share|improve this answer

































    0














    You can know use tf.saved_model.builder.SavedModelBuilder function.



    The main lines for the saving:



    builder = tf.saved_model.builder.SavedModelBuilder(graph_location)

    builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])

    builder.save()


    A code to save the model :



    ...
    def main(_):
    # Import data
    mnist = input_data.read_data_sets(FLAGS.data_dir)

    # Create the model
    x = tf.placeholder(tf.float32, [None, 784])

    # Define loss and optimizer
    y_ = tf.placeholder(tf.int64, [None])

    # Build the graph for the deep net
    y_conv, keep_prob = deepnn(x) # an unknow model model

    with tf.name_scope('loss'):
    cross_entropy = tf.losses.sparse_softmax_cross_entropy(
    labels=y_, logits=y_conv)
    cross_entropy = tf.reduce_mean(cross_entropy)

    with tf.name_scope('adam_optimizer'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

    with tf.name_scope('accuracy'):
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
    correct_prediction = tf.cast(correct_prediction, tf.float32)
    accuracy = tf.reduce_mean(correct_prediction)

    graph_location ="tmp/"
    print('Saving graph to: %s' % graph_location)
    **builder = tf.saved_model.builder.SavedModelBuilder(graph_location)**

    train_writer = tf.summary.FileWriter(graph_location)
    train_writer.add_graph(tf.get_default_graph())

    saver = tf.train.Saver(max_to_keep=1)

    with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    **builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])**
    for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
    train_accuracy = accuracy.eval(feed_dict={
    x: batch[0], y_: batch[1], keep_prob: 1.0})
    print('step %d, training accuracy %g' % (i, train_accuracy))
    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    print('test accuracy %g' % accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

    **builder.save()**
    saver.save(sess, "tmp/my_checkpoint.ckpt", global_step =0)

    if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', type=str,
    default='/tmp/tensorflow/mnist/input_data',
    help='Directory for storing input data')
    FLAGS, unparsed = parser.parse_known_args()
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
    `


    A code to restore the model :



    import tensorflow as tf

    # récupération des poids

    export_dir = "tmp"
    sess = tf.Session()
    tf.saved_model.loader.load(sess,["cnn_mnist"], export_dir)

    #trainable_var = tf.trainable_variables()
    trainable_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    for var in trainable_var:
    print(var.name)`





    share|improve this answer


























    • after that you can use the checkpoints to restore the weights properly ...

      – Ismaïla
      Jun 12 '18 at 14:57












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46706888%2fhow-to-predict-values-with-a-trained-tensorflow-model%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Forgive me if I am wrong but tf.train.Saver() only saves the variable values not the graph itself. This means that if you want to load the model in a different file you need to rebuild the graph or somehow load the graph as well. Tensorflow documentation states:




    The tf.train.Saver object not only saves variables to checkpoint files, it also restores variables. Note that when you restore variables from a file you do not have to initialize them beforehand.




    Consider the following example:



    One file that saves the model:



    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
    v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

    inc_v1 = v1.assign(v1+1)
    dec_v2 = v2.assign(v2-1)

    # Add an op to initialize the variables.
    init_op = tf.global_variables_initializer()

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, initialize the variables, do some work, and save the
    # variables to disk.
    with tf.Session() as sess:
    sess.run(init_op)
    # Do some work with the model.
    inc_v1.op.run()
    dec_v2.op.run()
    # Save the variables to disk.
    save_path = saver.save(sess, "/tmp/model.ckpt")
    print("Model saved in file: %s" % save_path)


    The other file that loads the previously saved model:



    tf.reset_default_graph()

    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3])
    v2 = tf.get_variable("v2", shape=[5])

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, use the saver to restore variables from disk, and
    # do some work with the model.
    with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "/tmp/model.ckpt")
    print("Model restored.")
    # Check the values of the variables
    print("v1 : %s" % v1.eval())
    print("v2 : %s" % v2.eval())





    share|improve this answer


























    • Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

      – T.Poe
      Oct 12 '17 at 16:59











    • What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

      – Lasse Jacobs
      Oct 13 '17 at 6:49













    • As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

      – T.Poe
      Oct 13 '17 at 8:23











    • @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

      – pfc
      Aug 15 '18 at 5:51
















    2














    Forgive me if I am wrong but tf.train.Saver() only saves the variable values not the graph itself. This means that if you want to load the model in a different file you need to rebuild the graph or somehow load the graph as well. Tensorflow documentation states:




    The tf.train.Saver object not only saves variables to checkpoint files, it also restores variables. Note that when you restore variables from a file you do not have to initialize them beforehand.




    Consider the following example:



    One file that saves the model:



    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
    v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

    inc_v1 = v1.assign(v1+1)
    dec_v2 = v2.assign(v2-1)

    # Add an op to initialize the variables.
    init_op = tf.global_variables_initializer()

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, initialize the variables, do some work, and save the
    # variables to disk.
    with tf.Session() as sess:
    sess.run(init_op)
    # Do some work with the model.
    inc_v1.op.run()
    dec_v2.op.run()
    # Save the variables to disk.
    save_path = saver.save(sess, "/tmp/model.ckpt")
    print("Model saved in file: %s" % save_path)


    The other file that loads the previously saved model:



    tf.reset_default_graph()

    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3])
    v2 = tf.get_variable("v2", shape=[5])

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, use the saver to restore variables from disk, and
    # do some work with the model.
    with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "/tmp/model.ckpt")
    print("Model restored.")
    # Check the values of the variables
    print("v1 : %s" % v1.eval())
    print("v2 : %s" % v2.eval())





    share|improve this answer


























    • Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

      – T.Poe
      Oct 12 '17 at 16:59











    • What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

      – Lasse Jacobs
      Oct 13 '17 at 6:49













    • As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

      – T.Poe
      Oct 13 '17 at 8:23











    • @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

      – pfc
      Aug 15 '18 at 5:51














    2












    2








    2







    Forgive me if I am wrong but tf.train.Saver() only saves the variable values not the graph itself. This means that if you want to load the model in a different file you need to rebuild the graph or somehow load the graph as well. Tensorflow documentation states:




    The tf.train.Saver object not only saves variables to checkpoint files, it also restores variables. Note that when you restore variables from a file you do not have to initialize them beforehand.




    Consider the following example:



    One file that saves the model:



    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
    v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

    inc_v1 = v1.assign(v1+1)
    dec_v2 = v2.assign(v2-1)

    # Add an op to initialize the variables.
    init_op = tf.global_variables_initializer()

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, initialize the variables, do some work, and save the
    # variables to disk.
    with tf.Session() as sess:
    sess.run(init_op)
    # Do some work with the model.
    inc_v1.op.run()
    dec_v2.op.run()
    # Save the variables to disk.
    save_path = saver.save(sess, "/tmp/model.ckpt")
    print("Model saved in file: %s" % save_path)


    The other file that loads the previously saved model:



    tf.reset_default_graph()

    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3])
    v2 = tf.get_variable("v2", shape=[5])

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, use the saver to restore variables from disk, and
    # do some work with the model.
    with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "/tmp/model.ckpt")
    print("Model restored.")
    # Check the values of the variables
    print("v1 : %s" % v1.eval())
    print("v2 : %s" % v2.eval())





    share|improve this answer















    Forgive me if I am wrong but tf.train.Saver() only saves the variable values not the graph itself. This means that if you want to load the model in a different file you need to rebuild the graph or somehow load the graph as well. Tensorflow documentation states:




    The tf.train.Saver object not only saves variables to checkpoint files, it also restores variables. Note that when you restore variables from a file you do not have to initialize them beforehand.




    Consider the following example:



    One file that saves the model:



    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
    v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

    inc_v1 = v1.assign(v1+1)
    dec_v2 = v2.assign(v2-1)

    # Add an op to initialize the variables.
    init_op = tf.global_variables_initializer()

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, initialize the variables, do some work, and save the
    # variables to disk.
    with tf.Session() as sess:
    sess.run(init_op)
    # Do some work with the model.
    inc_v1.op.run()
    dec_v2.op.run()
    # Save the variables to disk.
    save_path = saver.save(sess, "/tmp/model.ckpt")
    print("Model saved in file: %s" % save_path)


    The other file that loads the previously saved model:



    tf.reset_default_graph()

    # Create some variables.
    v1 = tf.get_variable("v1", shape=[3])
    v2 = tf.get_variable("v2", shape=[5])

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    # Later, launch the model, use the saver to restore variables from disk, and
    # do some work with the model.
    with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "/tmp/model.ckpt")
    print("Model restored.")
    # Check the values of the variables
    print("v1 : %s" % v1.eval())
    print("v2 : %s" % v2.eval())






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 12 '17 at 12:32

























    answered Oct 12 '17 at 12:25









    Lasse JacobsLasse Jacobs

    417610




    417610













    • Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

      – T.Poe
      Oct 12 '17 at 16:59











    • What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

      – Lasse Jacobs
      Oct 13 '17 at 6:49













    • As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

      – T.Poe
      Oct 13 '17 at 8:23











    • @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

      – pfc
      Aug 15 '18 at 5:51



















    • Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

      – T.Poe
      Oct 12 '17 at 16:59











    • What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

      – Lasse Jacobs
      Oct 13 '17 at 6:49













    • As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

      – T.Poe
      Oct 13 '17 at 8:23











    • @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

      – pfc
      Aug 15 '18 at 5:51

















    Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

    – T.Poe
    Oct 12 '17 at 16:59





    Thanks for response! This seems like lots of other examples I saw elsewhere. There are these variables v1, v2 which are saved, then read. I understand this. What I don't get is what is this saved 'variable' in my code? I updated the code so there is more context.

    – T.Poe
    Oct 12 '17 at 16:59













    What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

    – Lasse Jacobs
    Oct 13 '17 at 6:49







    What saved variable? When save a model, it only saves the values assigned to each tensorflow value in the current session. If you want to restore that model in another session you will have to rebuild the graph in that session. If the answer was useful please mark it an answer a lot of time goes into answering these questions ;)

    – Lasse Jacobs
    Oct 13 '17 at 6:49















    As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

    – T.Poe
    Oct 13 '17 at 8:23





    As I said, your code is an example exactly same as dozens of others (actually copy paste from the original tensorflow tutorial which of course I've seen already). But I still do not know "How to predict values with a trained model" which is the original question, so I can't mark it as an answer, still thank you.

    – T.Poe
    Oct 13 '17 at 8:23













    @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

    – pfc
    Aug 15 '18 at 5:51





    @LasseJacobs You mean if I want to rebuild the model in another session, I have to rewrite all the codes building the graph? Like "x=tf.placeholder(something)" and "layer = tf.nn.conv2d(input=x)"? Is there any way that I can also load the graph structure from files?

    – pfc
    Aug 15 '18 at 5:51













    0














     output = sess.run(nn_output, feed_dict={ x: batch_x, keep_prob: 0.8 })


    Where nn_output is the name is the output variable of the last layer of you network. You can save you variable using:



    saver = tf.train.Saver([nn_output])
    saver.save(sess, 'my_test_model',global_step=1000) # save every 1000 steps


    and therefore in your code:



    out_layer = tf.layers.dense(inputs=layer_1, units=6)


    should be :



    out_layer = tf.layers.dense(inputs=layer_1, units=6, name='nn_output')


    To restore:



    with tf.Session() as sess:    
    saver = tf.train.import_meta_graph('my_test_model')
    saver.restore(sess,tf.train.latest_checkpoint('./'))


    Now you should have access to that node of the graph. If the name is not specified, it is difficult to recover that particular layer.






    share|improve this answer






























      0














       output = sess.run(nn_output, feed_dict={ x: batch_x, keep_prob: 0.8 })


      Where nn_output is the name is the output variable of the last layer of you network. You can save you variable using:



      saver = tf.train.Saver([nn_output])
      saver.save(sess, 'my_test_model',global_step=1000) # save every 1000 steps


      and therefore in your code:



      out_layer = tf.layers.dense(inputs=layer_1, units=6)


      should be :



      out_layer = tf.layers.dense(inputs=layer_1, units=6, name='nn_output')


      To restore:



      with tf.Session() as sess:    
      saver = tf.train.import_meta_graph('my_test_model')
      saver.restore(sess,tf.train.latest_checkpoint('./'))


      Now you should have access to that node of the graph. If the name is not specified, it is difficult to recover that particular layer.






      share|improve this answer




























        0












        0








        0







         output = sess.run(nn_output, feed_dict={ x: batch_x, keep_prob: 0.8 })


        Where nn_output is the name is the output variable of the last layer of you network. You can save you variable using:



        saver = tf.train.Saver([nn_output])
        saver.save(sess, 'my_test_model',global_step=1000) # save every 1000 steps


        and therefore in your code:



        out_layer = tf.layers.dense(inputs=layer_1, units=6)


        should be :



        out_layer = tf.layers.dense(inputs=layer_1, units=6, name='nn_output')


        To restore:



        with tf.Session() as sess:    
        saver = tf.train.import_meta_graph('my_test_model')
        saver.restore(sess,tf.train.latest_checkpoint('./'))


        Now you should have access to that node of the graph. If the name is not specified, it is difficult to recover that particular layer.






        share|improve this answer















         output = sess.run(nn_output, feed_dict={ x: batch_x, keep_prob: 0.8 })


        Where nn_output is the name is the output variable of the last layer of you network. You can save you variable using:



        saver = tf.train.Saver([nn_output])
        saver.save(sess, 'my_test_model',global_step=1000) # save every 1000 steps


        and therefore in your code:



        out_layer = tf.layers.dense(inputs=layer_1, units=6)


        should be :



        out_layer = tf.layers.dense(inputs=layer_1, units=6, name='nn_output')


        To restore:



        with tf.Session() as sess:    
        saver = tf.train.import_meta_graph('my_test_model')
        saver.restore(sess,tf.train.latest_checkpoint('./'))


        Now you should have access to that node of the graph. If the name is not specified, it is difficult to recover that particular layer.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 4 at 13:21









        LaSul

        625421




        625421










        answered Feb 1 '18 at 23:42









        CAta.RAyCAta.RAy

        339216




        339216























            0














            You can know use tf.saved_model.builder.SavedModelBuilder function.



            The main lines for the saving:



            builder = tf.saved_model.builder.SavedModelBuilder(graph_location)

            builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])

            builder.save()


            A code to save the model :



            ...
            def main(_):
            # Import data
            mnist = input_data.read_data_sets(FLAGS.data_dir)

            # Create the model
            x = tf.placeholder(tf.float32, [None, 784])

            # Define loss and optimizer
            y_ = tf.placeholder(tf.int64, [None])

            # Build the graph for the deep net
            y_conv, keep_prob = deepnn(x) # an unknow model model

            with tf.name_scope('loss'):
            cross_entropy = tf.losses.sparse_softmax_cross_entropy(
            labels=y_, logits=y_conv)
            cross_entropy = tf.reduce_mean(cross_entropy)

            with tf.name_scope('adam_optimizer'):
            train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

            with tf.name_scope('accuracy'):
            correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
            correct_prediction = tf.cast(correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(correct_prediction)

            graph_location ="tmp/"
            print('Saving graph to: %s' % graph_location)
            **builder = tf.saved_model.builder.SavedModelBuilder(graph_location)**

            train_writer = tf.summary.FileWriter(graph_location)
            train_writer.add_graph(tf.get_default_graph())

            saver = tf.train.Saver(max_to_keep=1)

            with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            **builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])**
            for i in range(20000):
            batch = mnist.train.next_batch(50)
            if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0})
            print('step %d, training accuracy %g' % (i, train_accuracy))
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

            print('test accuracy %g' % accuracy.eval(feed_dict={
            x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

            **builder.save()**
            saver.save(sess, "tmp/my_checkpoint.ckpt", global_step =0)

            if __name__ == '__main__':
            parser = argparse.ArgumentParser()
            parser.add_argument('--data_dir', type=str,
            default='/tmp/tensorflow/mnist/input_data',
            help='Directory for storing input data')
            FLAGS, unparsed = parser.parse_known_args()
            tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
            `


            A code to restore the model :



            import tensorflow as tf

            # récupération des poids

            export_dir = "tmp"
            sess = tf.Session()
            tf.saved_model.loader.load(sess,["cnn_mnist"], export_dir)

            #trainable_var = tf.trainable_variables()
            trainable_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
            for var in trainable_var:
            print(var.name)`





            share|improve this answer


























            • after that you can use the checkpoints to restore the weights properly ...

              – Ismaïla
              Jun 12 '18 at 14:57
















            0














            You can know use tf.saved_model.builder.SavedModelBuilder function.



            The main lines for the saving:



            builder = tf.saved_model.builder.SavedModelBuilder(graph_location)

            builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])

            builder.save()


            A code to save the model :



            ...
            def main(_):
            # Import data
            mnist = input_data.read_data_sets(FLAGS.data_dir)

            # Create the model
            x = tf.placeholder(tf.float32, [None, 784])

            # Define loss and optimizer
            y_ = tf.placeholder(tf.int64, [None])

            # Build the graph for the deep net
            y_conv, keep_prob = deepnn(x) # an unknow model model

            with tf.name_scope('loss'):
            cross_entropy = tf.losses.sparse_softmax_cross_entropy(
            labels=y_, logits=y_conv)
            cross_entropy = tf.reduce_mean(cross_entropy)

            with tf.name_scope('adam_optimizer'):
            train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

            with tf.name_scope('accuracy'):
            correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
            correct_prediction = tf.cast(correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(correct_prediction)

            graph_location ="tmp/"
            print('Saving graph to: %s' % graph_location)
            **builder = tf.saved_model.builder.SavedModelBuilder(graph_location)**

            train_writer = tf.summary.FileWriter(graph_location)
            train_writer.add_graph(tf.get_default_graph())

            saver = tf.train.Saver(max_to_keep=1)

            with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            **builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])**
            for i in range(20000):
            batch = mnist.train.next_batch(50)
            if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0})
            print('step %d, training accuracy %g' % (i, train_accuracy))
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

            print('test accuracy %g' % accuracy.eval(feed_dict={
            x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

            **builder.save()**
            saver.save(sess, "tmp/my_checkpoint.ckpt", global_step =0)

            if __name__ == '__main__':
            parser = argparse.ArgumentParser()
            parser.add_argument('--data_dir', type=str,
            default='/tmp/tensorflow/mnist/input_data',
            help='Directory for storing input data')
            FLAGS, unparsed = parser.parse_known_args()
            tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
            `


            A code to restore the model :



            import tensorflow as tf

            # récupération des poids

            export_dir = "tmp"
            sess = tf.Session()
            tf.saved_model.loader.load(sess,["cnn_mnist"], export_dir)

            #trainable_var = tf.trainable_variables()
            trainable_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
            for var in trainable_var:
            print(var.name)`





            share|improve this answer


























            • after that you can use the checkpoints to restore the weights properly ...

              – Ismaïla
              Jun 12 '18 at 14:57














            0












            0








            0







            You can know use tf.saved_model.builder.SavedModelBuilder function.



            The main lines for the saving:



            builder = tf.saved_model.builder.SavedModelBuilder(graph_location)

            builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])

            builder.save()


            A code to save the model :



            ...
            def main(_):
            # Import data
            mnist = input_data.read_data_sets(FLAGS.data_dir)

            # Create the model
            x = tf.placeholder(tf.float32, [None, 784])

            # Define loss and optimizer
            y_ = tf.placeholder(tf.int64, [None])

            # Build the graph for the deep net
            y_conv, keep_prob = deepnn(x) # an unknow model model

            with tf.name_scope('loss'):
            cross_entropy = tf.losses.sparse_softmax_cross_entropy(
            labels=y_, logits=y_conv)
            cross_entropy = tf.reduce_mean(cross_entropy)

            with tf.name_scope('adam_optimizer'):
            train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

            with tf.name_scope('accuracy'):
            correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
            correct_prediction = tf.cast(correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(correct_prediction)

            graph_location ="tmp/"
            print('Saving graph to: %s' % graph_location)
            **builder = tf.saved_model.builder.SavedModelBuilder(graph_location)**

            train_writer = tf.summary.FileWriter(graph_location)
            train_writer.add_graph(tf.get_default_graph())

            saver = tf.train.Saver(max_to_keep=1)

            with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            **builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])**
            for i in range(20000):
            batch = mnist.train.next_batch(50)
            if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0})
            print('step %d, training accuracy %g' % (i, train_accuracy))
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

            print('test accuracy %g' % accuracy.eval(feed_dict={
            x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

            **builder.save()**
            saver.save(sess, "tmp/my_checkpoint.ckpt", global_step =0)

            if __name__ == '__main__':
            parser = argparse.ArgumentParser()
            parser.add_argument('--data_dir', type=str,
            default='/tmp/tensorflow/mnist/input_data',
            help='Directory for storing input data')
            FLAGS, unparsed = parser.parse_known_args()
            tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
            `


            A code to restore the model :



            import tensorflow as tf

            # récupération des poids

            export_dir = "tmp"
            sess = tf.Session()
            tf.saved_model.loader.load(sess,["cnn_mnist"], export_dir)

            #trainable_var = tf.trainable_variables()
            trainable_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
            for var in trainable_var:
            print(var.name)`





            share|improve this answer















            You can know use tf.saved_model.builder.SavedModelBuilder function.



            The main lines for the saving:



            builder = tf.saved_model.builder.SavedModelBuilder(graph_location)

            builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])

            builder.save()


            A code to save the model :



            ...
            def main(_):
            # Import data
            mnist = input_data.read_data_sets(FLAGS.data_dir)

            # Create the model
            x = tf.placeholder(tf.float32, [None, 784])

            # Define loss and optimizer
            y_ = tf.placeholder(tf.int64, [None])

            # Build the graph for the deep net
            y_conv, keep_prob = deepnn(x) # an unknow model model

            with tf.name_scope('loss'):
            cross_entropy = tf.losses.sparse_softmax_cross_entropy(
            labels=y_, logits=y_conv)
            cross_entropy = tf.reduce_mean(cross_entropy)

            with tf.name_scope('adam_optimizer'):
            train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

            with tf.name_scope('accuracy'):
            correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
            correct_prediction = tf.cast(correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(correct_prediction)

            graph_location ="tmp/"
            print('Saving graph to: %s' % graph_location)
            **builder = tf.saved_model.builder.SavedModelBuilder(graph_location)**

            train_writer = tf.summary.FileWriter(graph_location)
            train_writer.add_graph(tf.get_default_graph())

            saver = tf.train.Saver(max_to_keep=1)

            with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            **builder.add_meta_graph_and_variables(sess, ["cnn_mnist"])**
            for i in range(20000):
            batch = mnist.train.next_batch(50)
            if i % 100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
            x: batch[0], y_: batch[1], keep_prob: 1.0})
            print('step %d, training accuracy %g' % (i, train_accuracy))
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

            print('test accuracy %g' % accuracy.eval(feed_dict={
            x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

            **builder.save()**
            saver.save(sess, "tmp/my_checkpoint.ckpt", global_step =0)

            if __name__ == '__main__':
            parser = argparse.ArgumentParser()
            parser.add_argument('--data_dir', type=str,
            default='/tmp/tensorflow/mnist/input_data',
            help='Directory for storing input data')
            FLAGS, unparsed = parser.parse_known_args()
            tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
            `


            A code to restore the model :



            import tensorflow as tf

            # récupération des poids

            export_dir = "tmp"
            sess = tf.Session()
            tf.saved_model.loader.load(sess,["cnn_mnist"], export_dir)

            #trainable_var = tf.trainable_variables()
            trainable_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
            for var in trainable_var:
            print(var.name)`






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 4 at 13:38









            LaSul

            625421




            625421










            answered Jun 12 '18 at 14:43









            Ismaïla Ismaïla

            11




            11













            • after that you can use the checkpoints to restore the weights properly ...

              – Ismaïla
              Jun 12 '18 at 14:57



















            • after that you can use the checkpoints to restore the weights properly ...

              – Ismaïla
              Jun 12 '18 at 14:57

















            after that you can use the checkpoints to restore the weights properly ...

            – Ismaïla
            Jun 12 '18 at 14:57





            after that you can use the checkpoints to restore the weights properly ...

            – Ismaïla
            Jun 12 '18 at 14:57


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46706888%2fhow-to-predict-values-with-a-trained-tensorflow-model%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas