TensorFlow variable management details, tensorflow variable details

Source: Internet
Author: User

TensorFlow variable management details, tensorflow variable details

I. TensorFlow variable Management

1. TensorFLow also provides the tf. get_variable function to create or obtain variables. When tf. variable is used to create variables, its functions are basically equivalent to tf. Variable. The initialization method (initializer) parameter in tf. get_variable is similar to the initialization process of tf. Variable. the initializer function and the initialization method of tf. Variable are one-to-one. See the following table.

The biggest difference between tf. get_variable and tf. Variable is to specify the Variable name parameters. For tf. variable function. The Variable name is an optional parameter, which is provided in the form of name = "v" for tf. the get_variable function. The variable name is a required parameter, tf. get_variable creates or retrieves variables based on the name.

2. The tf. variable_scope function can be used to control the semantics of the tf. get_variable function. When tf. when the parameter reuse = True of the variable_scope function is used to generate the context manager, all. the get_variable function directly obtains the created variable. If the variable does not exist, an error is returned. When tf. in the context manager created when variable_scope function parameter reuse = False or None, tf. the get_variable function directly creates a new variable. If a variable with the same name already exists, an error is returned.

3. Another tf. variable_scope function can be nested. If the reuse parameter is not declared in the context manager of a layer during nesting, the reuse parameter of the layer context manager is consistent with that of its outer layer.

4. The tf. variable_scope function provides a way to manage variable namespaces. Variables created in tf. variable_scope. Before the name in name. name, The namespace name is added and separated. Tf. get_variable ("foou/baru/u", [1]), you can get the variables in the namespace by using the variable name with the namespace name.

Ii. TensorFlow programming demonstration

Import tensorflow as tf # create a variable named v in the namespace named foo with tf. variable_scope ("foo"): v = tf. get_variable ("v", [1], initializer = tf. constant_initializer (1.0) ''' # because the variable v already exists in the namespace foo, if it is created again, the error with tf is returned. variable_scope ("foo"): v = tf. get_variable ("v", [1]) # ValueError: Variable foo/v already exists, disallowed. # Did you mean to set reuse = True in VarScope? ''' # If you set the reuse parameter to True, tf. get_variable can directly obtain the declared variable with tf. variable_scope ("foo", reuse = True): v1 = tf. get_variable ("v", [1]) print (v = v1) # True ''' # When reuse = True, tf. get_variable can only get the created variable with tf in the specified namespace. variable_scope ("bar", reuse = True): v2 = tf. get_variable ("v", [1]) # ValueError: Variable bar/v does not exist, or was not created with # tf. get_variable (). did you mean to set reuse = None in VarScope? ''' With tf. variable_scope ("root"): # Use tf. get_variable_scope (). the reuse function obtains the reuse parameter value print (tf. get_variable_scope (). reuse) # False with tf. variable_scope ("foo1", reuse = True): print (tf. get_variable_scope (). reuse) # True with tf. variable_scope ("bar1"): # If the reuse parameter is not specified in bar1 nested in context manager foo1, print (tf. get_variable_scope (). reuse) # True print (tf. get_variable_scope (). reuse) # False # tf. the variable_scope function provides a way to manage the variable namespace u1 = tf. get_variable ("u", [1]) print (u1.name) # u: 0 with tf. variable_scope ("foou"): u2 = tf. get_variable ("u", [1]) print (u2.name) # foou/u: 0 with tf. variable_scope ("foou"): with tf. variable_scope ("baru"): u3 = tf. get_variable ("u", [1]) print (u3.name) # foou/baru/u: 0 u4 = tf. get_variable ("u1", [1]) print (u4.name) # foou/u1: 0 # You can directly obtain the variable with tf in The namespace by using the variable name with the namespace name. variable_scope ("", reuse = True): u5 = tf. get_variable ("foou/baru/u", [1]) print (u5.name) # foou/baru/u: 0 print (u5 = u3) # True u6 = tf. get_variable ("foou/u1", [1]) print (u6.name) # foou/u1: 0 print (u6 = u4) # True

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.