0

How to check if a variable exists or not in Tensorflow1.X?

I have get check it out in the programming, and I have googled it for a long time but still get no answer.

WBR
  • 57
  • 1
  • 11
  • Does this answer your question? [How do I check if a variable exists?](https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists) – Andrew Holmgren Jun 16 '20 at 14:58
  • The 'variable' I mentioned above is a Tensorflow variable which couldn't be checked for existence in an ordinary way. – WBR Jun 16 '20 at 15:30

1 Answers1

0

Unless you are passing an explicit value for collections in tf.Variable / tf.get_variable, you should be able to find all the variables in the GLOBAL_VARIABLES collection:

import tensorflow as tf

with tf.Graph().as_default():
    tf.Variable([1, 2, 3], name='my_var')
    print('my_var' in {v.op.name for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)})
    # True

More generally, you can look for an operation with the variable name you are looking for and check the operation type is indeed a variable:

import tensorflow as tf

def variable_exists(var_name, graph=None):
    if graph is None:
        graph = tf.get_default_graph()
    try:
        op = graph.get_operation_by_name(var_name)
    except KeyError:
        return False
    return op.type in {'Variable', 'VariableV2', 'AutoReloadVariable', 'VarHandleOp'}

with tf.Graph().as_default():
    tf.Variable([1, 2, 3], name='my_var')
    print(variable_exists('my_var'))
    # True
    print(variable_exists('my_var2'))
    # False
jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • Wow, how can I become as excellent as you in coding? How do you get so familiar with tensorflow? – WBR Jun 16 '20 at 15:38
  • I have another question. Why a tensor could be got in a 'operation' way? And I remember that the name of a tensor in the net would like this: instead of 'my_var'. But when I try to check if 'my_var:0' exist or not using your code, it comes with Error "Name 'name:0' appears to refer to a Tensor, not a Operation". Could you pleae give me some guide to understand this? – WBR Jun 16 '20 at 15:53
  • @陈绍伍 I'm afraid there's no much "trick" to TensorFlow (or most programming things) beyond spending a lot of time with it, I'm sure you'll get just as fluent with it soon enough :) This is a common confusion, names are associated to operations, operations can have zero or more outputs, most times they have one output, and you refer to each output with `:`. Generally you work with the outputs of operations, which are the tensors. So `my_var:0` is the output `0` of the operation `my_var`, which is the value of your variable. – jdehesa Jun 16 '20 at 16:54
  • @陈绍伍 So you can get the actual variable value either with `tf.get_default_graph().get_operation_by_name('my_var').outputs[0]` or directly with `tf.get_default_graph().get_tensor_by_name('my_var:0')`, it is the same. See [How does TensorFlow name tensors?](https://stackoverflow.com/q/36150834/1782792) for more about this. – jdehesa Jun 16 '20 at 16:56