How to get reference by name of variable/placeholder?

前端 未结 1 1011
野的像风
野的像风 2020-12-30 11:57

By names I\'m referring to:

tf.placeholder(tf.float32, name=\'NAME\')
tf.get_variable(\"W\", [n_in, n_out],initializer=w_init())

I have sev

相关标签:
1条回答
  • 2020-12-30 12:49

    First of all, you can get the placeholder using tf.Graph.get_tensor_by_name(). For example, assuming that you are working with the default graph:

    placeholder1 = tf.placeholder(tf.float32, name='NAME')
    placeholder2 = tf.get_default_graph().get_tensor_by_name('NAME:0')
    assert placeholder1 == placeholder2
    

    Secondly, I would use the following function to get all variables with a given name (no matter their scope):

    def get_all_variables_with_name(var_name):
        name = var_name + ':0'
        return [var for var in tf.all_variables() if var.name.endswith(name)]
    
    0 讨论(0)
提交回复
热议问题