tensorflow Getting started with tensorflow Counting to 10


Example

In this example we use Tensorflow to count to 10. Yes this is total overkill, but it is a nice example to show an absolute minimal setup needed to use Tensorflow

import tensorflow as tf

# create a variable, refer to it as 'state' and set it to 0
state = tf.Variable(0)

# set one to a constant set to 1
one = tf.constant(1)

# update phase adds state and one and then assigns to state
addition = tf.add(state, one)
update = tf.assign(state, addition )

# create a session
with tf.Session() as sess:
  # initialize session variables
  sess.run( tf.global_variables_initializer() )

  print "The starting state is",sess.run(state)

  print "Run the update 10 times..."
  for count in range(10):
    # execute the update
    sess.run(update)

  print "The end state is",sess.run(state)

The important thing to realize here is that state, one, addition, and update don't actually contain values. Instead they are references to Tensorflow objects. The final result is not state, but instead is retrieved by using a Tensorflow to evaluate it using sess.run(state)

This example is from https://github.com/panchishin/learn-to-tensorflow . There are several other examples there and a nice graduated learning plan to get acquainted with manipulating the Tensorflow graph in python.