Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow: Saver has 5 models limit

I wanted to save multiple models for my experiment but I noticed that tf.train.Saver() constructor could not save more than 5 models. Here is a simple code:

import tensorflow as tf 

x = tf.Variable(tf.zeros([1]))
saver = tf.train.Saver()
sess = tf.Session()

for i in range(10):
  sess.run(tf.initialize_all_variables())
  saver.save( sess, '/home/eneskocabey/Desktop/model' + str(i) )

When I ran this code, I saw only 5 models on my Desktop. Why is this? How can I save more than 5 models with the same tf.train.Saver() constructor?

like image 210
Transcendental Avatar asked Aug 08 '16 19:08

Transcendental


1 Answers

The tf.train.Saver() constructor takes an optional argument called max_to_keep, which defaults to keeping the 5 most recent checkpoints of your model. To save more models, simply specify a value for that argument:

import tensorflow as tf 

x = tf.Variable(tf.zeros([1]))
saver = tf.train.Saver(max_to_keep=10)
sess = tf.Session()

for i in range(10):
  sess.run(tf.initialize_all_variables())
  saver.save(sess, '/home/eneskocabey/Desktop/model' + str(i))

To keep all checkpoints, pass the argument max_to_keep=None to the saver constructor.

like image 160
mrry Avatar answered Sep 20 '22 16:09

mrry