TensorFlow: getting variable by name

后端 未结 4 463
后悔当初
后悔当初 2020-12-08 09:36

When using the TensorFlow Python API, I created a variable (without specifying its name in the constructor), and its name property had the value

4条回答
  •  生来不讨喜
    2020-12-08 09:56

    Based on @mrry 's answer, I think it would be better to create and use the following function, since there's also local variables, and other variables that are not in global variables (they are in different collections):

    def get_var_by_name(query_name, var_list):
        """
        Get Variable by name
    
        e.g.
        local_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES)
        the_var = get_var_by_name(local_vars, 'accuracy/total:0')
        """
        target_var = None
        for var in var_list:
            if var.name==query_name:
                target_var = var
                break
        return target_var
    

提交回复
热议问题