python - I cannot understand Tensorflow system -


i cannot understand tensorflow system. first,i wrote

#coding:utf-8  __future__ import absolute_import __future__ import division __future__ import print_function  import tensorflow tf  const1 = tf.constant(2) const2 = tf.constant(3) add_op = tf.add(const1,const2)  tf.session() sess:     result = sess.run(add_op)     print(result) 

and print out 5. second,i wrote

#coding:utf-8  __future__ import absolute_import __future__ import division __future__ import print_function  import tensorflow tf  const1 = tf.constant(2) const2 = tf.constant(3) add_op = tf.add(const1,const2) print(add_op) 

and print out tensor("add:0", shape=(), dtype=int32). cannot understand system. use python , other languages, think tf.add() method add method.however,in case of tensorflow,it seems different. why part

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

necessary? functions part have?

i suggest read official getting started tensorflow guide of tensorflow know core concepts of library, such 1 seems problem here:

every tensorflow program consists of 2 parts:

  1. building computational graph.
  2. running computational graph.

now, "computational graph"? in tensorflow, specify series of operations executed on input. series of operations "computational graph". understand that, lets @ examples:

  • simple addition: let's @ example, code

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

    this creates 2 constant nodes in graph, , creates second node adds them. graphically, looks like:

    graph 2+3

  • to make little bit more complex, lets have input x , want add constant 3 it. code be:

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

    and graph is

    graph x+3

in both examples, first part of program. far, defined how our computational graph should look, i.e. inputs have, outputs, , calculations needed.

but: no calculations have been done far! in second example, don't know input x - float32. if have gpu, you'll notice tensorflow hasn't touched gpu yet. if have huge neural network millions of training images, step runs in milliseconds, no "real" work has done.

now comes part two: running graph defined above. here's work happens! fire tensorflow creating tf.session, , can run calling sess.run().

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

in second example, have tell tensorflow our value x should be:

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

tl;dr: every tensorflow program has 2 parts: 1. building computational graph, , 2. running graph. tf.add define graph, no addition performed yet. run graph, use sess.run() in first piece code.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -