I am using tf.estimator.Estimator
for developing my model,
I wrote a model_fn
and trained 50,000 iterations, now I want to make a small change in my model_fn
, for example add a new layer.
I don't want to start training from scratch, I want to restore all the old variables from the 50,000 checkpoint, and continue training from this point. When I try to do so I get a NotFoundError
How can this be done with tf.estimator.Estimator
?
TL;DR The easiest way to load variables from a previous checkpoint is to use the function
tf.train.init_from_checkpoint()
. Just one call to this function inside themodel_fn
of your Estimator will override the initializers of the corresponding variables.First model with two hidden layers
In more details, suppose you have trained a first model with two hidden layers on MNIST, named
model_fn_1
. The weights are saved in directorymnist_1
.Second model with three hidden layers
Now we want to train a new model
model_fn_2
with three hidden layers. We want to load the weights for the first two hidden layersh1
andh2
. We usetf.train.init_from_checkpoint()
to do this:The
assignment_map
will load every variable from scopeh1/
in the checkpoint into the new scopeh1/
, and same withh2/
. Don't forget the/
at the end to make TensorFlow know it's a variable scope.I couldn't find a way to make this work using pre-made estimators, since you can't change their
model_fn
.