"TensorFlow" Prints all variables in TensorFlow graph--tf.trainable_variables ()

Source: Internet
Author: User

In general, there are two functions for printing tensorflow variables:
tf.trainable_variables () and Tf.all_variables ()
The difference is:
Tf.trainable_variables () refers to the variables that need to be trained
Tf.all_variables () refers to all variables

In general, we are more concerned with training variables that need to be trained:
It is important to note that the entire graph is initialized when the variable name is output

First, print the name of the variable you need to train

Sess.run (Tf.global_varibales_initializer ())
variable_name = [v.name for C in Tf.trainable_variables ()]
print (Variable_names)

Second, print the variable name and variable value to be trained

Variable_names = [V.name for V ' tf.trainable_variables ()]
values = Sess.run (variable_names) for
k,v in Zip (vari Able_names, values): Print ("
    Variable:", k) print (
    "Shape:", V.shape)
    print (v)

here is a function to print the variable name, shape and the number of its variables

def print_num_of_total_parameters (Output_detail=false, output_to_logging=false): total_parameters = 0 parameters_s Tring = "" For variable in Tf.trainable_variables (): Shape = Variable.get_shape () variable_parameter s = 1 for Dim in Shape:variable_parameters *= dim.value total_parameters + = Variable_paramete
        RS If Len (shape) = = 1:parameters_string + = ("%s%d,"% (Variable.name, variable_parameters)) Else:parameters_string + = ("%s%s=%d,"% (Variable.name, str (SHAPE), variable_parameters)) if output_t  O_logging:if output_detail:logging.info (parameters_string) logging.info ("Total%d variables,
            %s params "% (Len (Tf.trainable_variables ())," {:,} ". Format (total_parameters))) Else:if Output_detail: Print (parameters_string) print ("Total%d variables,%s params"% (Len (Tf.trainable_variables ()), "{:,}". Form at (total_parameters)))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.