TensorFlow Varibale usage, tensorflowvaribale

Source: Internet
Author: User

TensorFlow Varibale usage, tensorflowvaribale

-------------------------------------------

Reprinted Please note: from blog

Xiuyuxuanchen

Address: http://www.cnblogs.com/greentomlee/

-------------------------------------------

Varibale usage

Instance:

Example:

First:

#! /Usr/bin/env python

This statement specifies the python runtime environment. There are two ways to specify this method. One is to specify the python path ---#! /Usr/bin/python (here we need to note that "/usr/bin/python" is the python installation path). I use ubuntu14.0.4 which contains the env variable, the environment variables are recorded, so you can write them like this.

#-*-Coding: UTF-8 -*-

This statement specifies the encoding method of *. py. If the file contains Chinese characters, it is necessary to write this statement. Of course you can also write: encoding: UTF-8

Import tensorflow as tf

This statement is used to import the tensorflow module.

State = tf. Variable (0, name = 'counter ')

Use tensorflow to create a node in the default graph. This node is a variable.

One = tf. constant (1)

A function of td is called to create constants.

New_value = tf. add (state, one)

Perform simple addition operations on constants and variables. Note that in TensoorFlow, all op and variables are regarded as nodes and tf. add () means to add an op in the default graph of tf, which is used for addition operations.

Update = tf. assign (state, new_value)

This operation is a value assignment operation. Assign the value of new_value to the update variable.

Okay, so far. Our "Graph flow" has been built.

It is roughly like this:

(Pay attention to the flow direction)

Here, we need to explain again: we have just defined the graph, and no variables are not initialized. Currently, only the value of state is 1.

Init = tf. initialize_all_variables ()

This is used to initialize variables. However, this statement will not be executed immediately. Data streams need to be moved through sess.

Remember: All operations should be performed in the session:

With tf. Session () as sess:

A session is automatically started here.

Sess. run (init)

Initialize the variable and execute the (run) init statement.

For _ in range (3 ):
Sess. run (update)
Print (sess. run (state ))

Loop 3 times and print the output.

Summary and experiences:

1. TensorFlow is slightly different from our normal programming thinking: the statements in TensorFlow are not executed immediately. Instead, the statements in session. run () are executed only when the session is started. If other nodes are involved in the run operation, they are also executed.

2. All nodes in the Tesorflow model can be considered as Operation op or tensor

Output result:

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.