How to use local variables in a saved Tensorflow model

How do I use local variables in a saved Tensorflow model? In particular, I want to use tf.contrib.metrics.streaming_auc

. Usually used sess.run(tf.local_variables_initializer())

before this op can be used (it has local variables for true positives, etc.). This works great when I just created the model. However, when I reload the model, the launch sess.run(tf.local_variables_initializer())

gives me AttributeError: 'Tensor' object has no attribute 'initializer'

. If I try to use auc op without initializing local variables, I get FailedPreconditionError: Attempting to use uninitialized value auc/true_positives

. Do I need to get / store references to local variables used in the auc op and initialize them directly when I reload the model?

Here's an example that fails:

inputs = tf.random_normal([10, 2])
labels = tf.reshape(tf.constant([1] * 5 + [0] * 5), (-1, 1))
logits = tf.layers.dense(inputs, 1)
pred = tf.sigmoid(logits)
auc_val, auc_op = tf.contrib.metrics.streaming_auc(pred, labels)

sess = tf.Session()
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

print(sess.run(auc_op)) # some terrible AUC, but it works
saver = tf.train.Saver()
saver.save(sess, 'test.model')

      

Then in a new process:

sess = tf.Session()

saver = tf.train.import_meta_graph('test.model.meta')
saver.restore(sess, 'test.model')
auc_op = tf.get_default_graph().get_operation_by_name('auc/update_op')
print(sess.run(auc_op))
# FailedPreconditionError: Attempting to use uninitialized value auc/true_positives

      

Note that if you execute sess.run(tf.local_variables_initializer())

to try to initialize, you will get the other error mentioned above.

It is possible to execute a new auc op after loading, but it would be better not to. Also, for this new auc op, you have to initialize local variables, so you need to filter them out of the non-intuitive tensors left over local_variables

from the previous auc op.

+3


source to share





All Articles