Skip to content

Instantly share code, notes, and snippets.

@InnerPeace-Wu
Created October 4, 2017 08:28
Show Gist options
  • Save InnerPeace-Wu/837164549a98bdc202f457260af056fa to your computer and use it in GitHub Desktop.
Save InnerPeace-Wu/837164549a98bdc202f457260af056fa to your computer and use it in GitHub Desktop.
get variables in tensorflow
import tensorflow as tf
def get_v_names():
tf.reset_default_graph()
with tf.variable_scope('test'):
a = tf.Variable(tf.ones([2]),tf.float32, name='a')
b = tf.Variable(tf.zeros([3]), dtype=tf.float32, name='b',trainable=False)
# GLOBAL_VARIABLES: get all variable, e.g. a & b
# GLOBAL_TRAINABLE: get trainable variable, e.g. a
for i in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):#TRAINABLE
print(i.name) # i.name if you want just a name
# another way to get trainable variables
for i in tf.trainable_variables():
print(i.name)
if __name__ == '__main__':
get_v_names()
'''out:
test/a:0
test/b:0
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment