1

I created a file where I create a model and start the training process using tf.train.GradientDescentOptimizer(learning_rate=0.0001).minimize(cost, name='optimizer') and more code.

Can I save this model and in another file continue the training without having to recreate the model?

I would like to do something like:

  • In the new file, load model
  • With the loaded model train.
  • Maybe do an inference at some point in time.

Edit

My hunch tells me that it's not exactly possible. This is what I would do:

  • Save the model using tf.train.Saver
  • In another place, load the model using tf.train.Saver
  • Create a new optimizer to optimize the cost in the model, train again.
loco.loop
  • 1,441
  • 1
  • 15
  • 27
  • You can follow the steps provided in this post's accepted answer: https://stackoverflow.com/questions/33759623/tensorflow-how-to-save-restore-a-model – Pranav Vempati Sep 01 '18 at 22:01

1 Answers1

2

Yes, that is completely possible. Full Tutorial and Documentation

to save:

Tensorflow variables are only alive inside a session. So, you have to save the model inside a session by calling save method on saver object.

import tensorflow as tf
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver.save(sess, 'my_test_model')

For saving the model after 1000 iterations, call save by passing the step count:

saver.save(sess, 'my_test_model',global_step=1000)

To use pre-trained model for fine-tuning:

with tf.Session() as sess:    
  saver = tf.train.import_meta_graph('my-model-1000.meta')
  saver.restore(sess,tf.train.latest_checkpoint('./'))
  print(sess.run('w1:0'))
  ##Model has been restored. Above statement will print the saved value of w1.

To add more operations by adding more layers and then train it:

sess=tf.Session()    
#First let's load meta graph and restore weights
saver = tf.train.import_meta_graph('my_test_model-1000.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))
# Now, let's access and create placeholders variables and
# create feed-dict to feed new data

graph = tf.get_default_graph()
w1 = graph.get_tensor_by_name("w1:0")
w2 = graph.get_tensor_by_name("w2:0")
feed_dict ={w1:13.0,w2:17.0}

#Now, access the op that you want to run. 
op_to_restore = graph.get_tensor_by_name("op_to_restore:0")

#Add more to the current graph
add_on_op = tf.multiply(op_to_restore,2)

print sess.run(add_on_op,feed_dict)
#This will print 120.
Ajay Bisht
  • 585
  • 6
  • 8
  • Thanks for your answer. Do you know when tu use tf.train.Saver() or tf.saved_model.simple_save(...)? From my intuition I would use tf.train.Saver() during training and to store different moments in time. I would use tf.saved_model.simple_save when training is done to use in production. – loco.loop Sep 05 '18 at 02:52
  • I'm commenting now because I didn't mention you before and suspect you didn't receive a notification on the previous comment... – loco.loop Sep 06 '18 at 02:11
  • 1
    Calling the `tf.train.Saver()` method, would save all variables to a file. `SavedModel` builder, and loader functionality wraps the `Saver` class in order to provide a higher-level serialization, which is more suitable for production purposes. `Saver` focuses mainly on variables, `SavedModel` tries to encompass other useful features like Signatures, Assets into one package. – Ajay Bisht Sep 06 '18 at 02:30