Rename variable scope of saved model in TensorFlow

Wesley Tansey :

Is it possible to rename the variable scope of a given model in tensorflow?

For instance, I created a logistic regression model for MNIST digits, based on the tutorial:

with tf.variable_scope('my-first-scope'):
    NUM_IMAGE_PIXELS = 784
    NUM_CLASS_BINS = 10
    x = tf.placeholder(tf.float32, shape=[None, NUM_IMAGE_PIXELS])
    y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASS_BINS])

    W = tf.Variable(tf.zeros([NUM_IMAGE_PIXELS,NUM_CLASS_BINS]))
    b = tf.Variable(tf.zeros([NUM_CLASS_BINS]))

    y = tf.nn.softmax(tf.matmul(x,W) + b)
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
    saver = tf.train.Saver([W, b])

... # some training happens

saver.save(sess, 'my-model')

Now I want to reload the saved model in the 'my-first-scope' variable scope and then save everything again to a new file and under a new variable scope of 'my-second-scope'.

keveman :

You can use tf.contrib.framework.list_variables and tf.contrib.framework.load_variable as follows to achieve your goal :

with tf.Graph().as_default(), tf.Session().as_default() as sess:
  with tf.variable_scope('my-first-scope'):
    NUM_IMAGE_PIXELS = 784
    NUM_CLASS_BINS = 10
    x = tf.placeholder(tf.float32, shape=[None, NUM_IMAGE_PIXELS])
    y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASS_BINS])

    W = tf.Variable(tf.zeros([NUM_IMAGE_PIXELS,NUM_CLASS_BINS]))
    b = tf.Variable(tf.zeros([NUM_CLASS_BINS]))

    y = tf.nn.softmax(tf.matmul(x,W) + b)
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
    saver = tf.train.Saver([W, b])
  sess.run(tf.global_variables_initializer())
  saver.save(sess, 'my-model')

vars = tf.contrib.framework.list_variables('.')
with tf.Graph().as_default(), tf.Session().as_default() as sess:

  new_vars = []
  for name, shape in vars:
    v = tf.contrib.framework.load_variable('.', name)
    new_vars.append(tf.Variable(v, name=name.replace('my-first-scope', 'my-second-scope')))

  saver = tf.train.Saver(new_vars)
  sess.run(tf.global_variables_initializer())
  saver.save(sess, 'my-new-model')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sagemaker and Tensorflow model not saved

Tensorflow train on a saved model

Restoring a single variable tensor saved in one model to variable tensor in another model - Tensorflow

Rename Variable with a string in saved in other variable in R

TensorFlow inference using saved model

Tensorflow : load saved model : UnicodeDecodeError

In Tensorflow how to freeze saved model

Using saved model for prediction in tensorflow

Tensorflow 2.0 : Variables in Saved model

How to restore a saved variable in tensorflow?

Expose saved/static target variable to scope outside

how to load and use a saved model on tensorflow?

Restoring saved model in Tensorflow v1.14

restoring weights of an already saved Tensorflow .pb model

Run a tensorflow saved model in electron app

How to convert or load saved model into TensorFlow or Keras?

Is it possible to restore a tensorflow estimator from saved model?

Running a saved model on tensorflow.rb

How to know the tag name is tensorflow saved model?

Random behavior when restoring saved Tensorflow model

Trained model cannot be saved tensorflow keras

Change dtype policy of tensorflow saved model

How to rename a variable which respects the name scope?

TensorFlow - Getting the variable scope of a tensor?

About names of variable scope in tensorflow

What is the right way to convert saved tensorflow model to tensorflow Lite

How to periodically save tensorflow model using saved_model API?

Tensorflow variable scope: reuse if variable exists

How to load and predict with a tensorflow model saved from save_weights?