简体   繁体   English

我无法理解Tensorflow系统

[英]I cannot understand Tensorflow system

I cannot understand Tensorflow system. 我无法理解Tensorflow系统。 First,I wrote 首先,我写了

#coding:UTF-8

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

const1 = tf.constant(2)
const2 = tf.constant(3)
add_op = tf.add(const1,const2)

with tf.Session() as sess:
    result = sess.run(add_op)
    print(result)

and it print out 5. Second,I wrote 然后打印出5。第二,我写道

#coding:UTF-8

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

const1 = tf.constant(2)
const2 = tf.constant(3)
add_op = tf.add(const1,const2)
print(add_op)

and it print out Tensor("Add:0", shape=(), dtype=int32). 然后打印出Tensor(“ Add:0”,shape =(),dtype = int32)。 I cannot understand this system. 我无法理解这个系统。 I use Python and other languages, so I think tf.add() method is add method.However,in the case of Tensorflow,it seems different. 我使用Python和其他语言,所以我认为tf.add()方法是add方法。但是,在Tensorflow的情况下,它似乎有所不同。 why is this part 为什么这部分

with tf.Session() as sess:
    result = sess.run(add_op)
    print(result)

necessary? 必要? What functions does this part have? 这部分有什么功能?

I would suggest to read the official Getting Started with TensorFlow guide of TensorFlow to get to know the core concepts of the library, such as the one which seems to be the problem here: 我建议阅读TensorFlow的官方TensorFlow入门指南,以了解该库的核心概念,例如这里似乎是问题所在的那个概念:

Every TensorFlow program consists of two parts: 每个TensorFlow程序均包含两个部分:

  1. Building the computational graph. 建立计算图。
  2. Running the computational graph. 运行计算图。

Now, what is a "computational graph"? 现在,什么是“计算图”? In TensorFlow, you specify a series of operations which are executed on your input. 在TensorFlow中,您可以指定在输入上执行的一系列操作。 This series of operations is your "computational graph". 这一系列的操作就是您的“计算图”。 To understand that, lets look at some examples: 为了理解这一点,让我们看一些例子:

  • Simple addition: let's look at your example, your code is 简单的补充:让我们看一下您的示例,您的代码是

     const1 = tf.constant(2) const2 = tf.constant(3) add_op = tf.add(const1,const2) 

    This creates two constant nodes in the graph, and creates a second node which adds them. 这将在图形中创建两个常数节点,并创建第二个节点并将它们相加。 Graphically, this looks like: 在图形上,这看起来像:

    2 + 3的图形

  • To make it a little bit more complex, lets say you have an input x and want to add a constant 3 to it. 为了使其更复杂一点,假设您有一个输入x并想为其添加常数3 Then your code would be: 那么您的代码将是:

     const1 = tf.constant(2) x = tf.placeholder(tf.float32) add_op = tf.add(const1,x) 

    and your graph is 你的图是

    x + 3的图形

In both examples, this was the first part of the program. 在两个示例中,这都是程序的第一部分 So far, we only defined how our computational graph should look, ie what inputs we have, what outputs, and all calculations needed. 到目前为止,我们仅定义了计算图的外观,即我们拥有哪些输入,哪些输出以及所需的所有计算。

But: no calculations have been done so far! 但是: 到目前为止,尚未进行任何计算! In the second example, you don't even know what your input x is - only that it will be a float32 . 在第二个示例中,您甚至不知道输入x是什么-只是它将是float32 If you have a GPU, you'll notice that TensorFlow hasn't even touched the GPU yet. 如果您有GPU,您会注意到TensorFlow甚至还没有接触过GPU。 Even if you have a huge neural network with millions of training images, this step runs in milliseconds, as no "real" work has to be done. 即使您拥有包含数百万个训练图像的庞大神经网络,该步骤也将以毫秒为单位运行,因为无需执行任何“实际”工作。

Now comes part two: running the graph we defined above. 现在来第二部分: 运行上面定义的图。 Here's where the work happens! 这是工作发生的地方! We fire up TensorFlow by creating a tf.Session , and then we can run anything by calling sess.run() . 我们通过创建一个tf.Session启动TensorFlow,然后我们可以通过调用sess.run()运行任何东西。

with tf.Session() as sess:
    result = sess.run(add_op)
    print(result)

In the second example, we now have to tell TensorFlow what our value x should be: 在第二个示例中,我们现在必须告诉TensorFlow我们的值x应该是:

with tf.Session() as sess:
    result = sess.run(add_op, {x: 5.0})
    print(result)

tl;dr: every TensorFlow program has two parts: 1. building a computational graph, and 2. running this graph. tl; dr:每个TensorFlow程序都有两个部分:1.构建计算图,以及2.运行该图。 With tf.add you only define the graph, but no addition is performed yet. 使用tf.add仅定义图,但尚未执行任何加法。 To run this graph, use sess.run() as in your first piece code. 要运行此图,请在您的第一段代码中使用sess.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM