TensorFlow Getting Started----placeholder, constant, and session

Source: Internet
Author: User

After you install TensorFlow, open a Python environment and start running and using TensorFlow.

First give an instance,

#先导入TensorFlow
Import TensorFlow as TF

# Create TensorFlow object called Hello_constant
Hello_constant = Tf.constant (' Hello world! ')

With TF. Session () as Sess:
# Run The tf.constant operation in the session
Output = Sess.run (hello_constant)
Print (output)

Perhaps some wonder why not directly output "Hello world! "Well, this looks like a lot of trouble, isn't it?" Not really.
What is 1.Tensor?
In TensorFlow, data is not present in integers, floating-point numbers, or strings, but is encapsulated in an object called tensor. Tensor is the meaning of tensor, tensor contains 0 to any dimension of the amount, wherein, 0-dimensional is called a constant, 1-dimensional is called a vector, two-dimensional is called a matrix, multi-dimensional directly called tensor amount. In hello_constant = tf.constant (' Hello world! ') code, Hello_constant is a 0-dimensional string tensor,tensors and there are many different sizes:

# Tensor1 is a 0-dimensional int32 tensor
Tensor1 = tf.constant (1234)
# Tensor2 is a 1-dimensional int32 tensor
Tensor2 = Tf.constant ([123,456,789])
# Tensor3 is a two-dimensional int32 tensor
Tensor3 = Tf.constant ([[123,456,789], [222,333,444]])

2.Session is an important concept in TensorFlow.
All the calculations in TensorFlow are constructed in a calculation diagram, which is a visual method of the mathematical operation process. Just like the code just now:

With TF. Session () as Sess:
Output = Sess.run (hello_constant)
This session is responsible for the operation of this graph, the main task of the session is to allocate the GPU or CPU.

3.tf.placeholder ()
Tf.constant (' Hello world! ') appears in the preceding code, and the tf.constant is used to define constants, whose values are constant, but what if you need to use a variable?

Tf.placeholder () and feed_dict need to be used at this time.
First give the Code

x = Tf.placeholder (tf.string)

With TF. Session () as Sess:
Output = Sess.run (x, feed_dict={x: ' Hello World '})

Tf.placeholder represents a placeholder, as to what type, look at its definition, here is defined by the tf.string type, and then, before the session starts run, and then dies before the graph begins to calculate, use Feed_dict to pass the corresponding value to X. This is the placeholder.
The same feed_dict can be set to multiple tensor

x = Tf.placeholder (tf.string)
y = Tf.placeholder (Tf.int32)
z = Tf.placeholder (tf.float32)

With TF. Session () as Sess:
Output = Sess.run (x, feed_dict={x: ' Test String ', y:123, z:45.67})

However, it is important to note that when you set up tensor with feed_dict, you need to give the same value type as the placeholder definition.

TensorFlow Getting Started----placeholder, constant, and session

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.