Skip to content

Instantly share code, notes, and snippets.

@d136o
Last active April 4, 2017 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d136o/4c68d010ecb0abfc7c52 to your computer and use it in GitHub Desktop.
Save d136o/4c68d010ecb0abfc7c52 to your computer and use it in GitHub Desktop.
Getting familiar with TensorFlow

Computation Graph:

Instantiating Variable objects as well as Ops objects adds to a graph of computation that is tracked by tf.

After building your computation graph, where nodes are Variables or Ops or Constants, you can retrieve the executed and resulting value of any of the nodes in the graph.

The difference between Ops and Variables:

  • Ops take Tensor inputs and return Tensor outputs, they have no internal state.
  • Variables can have changing internal state.

So for example:

  • you can instantiate one variable (initialized to 0, and thereby add it to computation graph)
  • you can instantiate one constants (and thereby add it to the computation graph)
  • you can create an op that adds constant and variable
  • you can create an op assigns the previous result to the variable

You can then loop over retrievals of the assignment op and thereby add and store a running sum. Looping over retrievals of the adding op will not cause the result to be stored in the internal state of the variable, as only the computation required to retrieve the value of a node is executed.

import argparse
import tensorflow as tf
import sys
def flowfib(n):
''' f_n = f_n-1 + f_n-2
Fails in a somewhat interesting way, showing that ints passed to constants are cast to 32 bit signed ints,
causing overflow pretty early in fibonacci sequence.
See for example f_48 = f_47 + f_46
numpy.int32(1134903170) + numpy.int32(1836311903)
__main__:1: RuntimeWarning: overflow encountered in int_scalars
-1323752223
'''
f = [tf.constant(0),tf.constant(1)]
if n>2:
for i in range(2,n):
f_i = f[i-1] + f[i-2]
f.append(f_i)
with tf.Session() as sess:
result = sess.run(f)
print result
def constant_test():
i1, i2, i3, i4 = (tf.constant(1.0),
tf.constant(2.0),
tf.constant(3.0),
tf.constant(4.0))
j1, j2 = (tf.mul(i1,i2), tf.mul(i3,i4))
k = tf.mul(j1,j2)
with tf.Session() as sess:
r = sess.run([k ,j1, j2])
print r
def variable_test():
counter_var = tf.Variable(0, name='counter')
one_const = tf.constant(1)
result = tf.add(counter_var, one_const)
update = tf.assign(counter_var, result)
init_operation = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_operation)
for i in range(5):
print "====="
# counter_var is only updated when output of "update" is retrieved
print "counter variable before update:"
print sess.run([counter_var])
print "counter variable retrieved at same time that we retrieve result of update op:"
print sess.run([counter_var,update])
print "variables that feed into result have not changed, thus retrieving result repeatedly will not yield any different value"
print sess.run([result])
print sess.run([result])
print sess.run([result])
def feed_test(data_one, data_two):
data_placeholder_one = tf.placeholder(tf.types.float32)
data_placeholder_two = tf.placeholder(tf.types.float32)
output = tf.mul(data_placeholder_one, data_placeholder_two)
with tf.Session() as sess:
print sess.run(
[output],
feed_dict={
data_placeholder_one : data_one,
data_placeholder_two : data_two
})
def main(args):
parser = argparse.ArgumentParser(
description='Runs some simple TensorFlow examples.'
)
parser.add_argument('--constant-test',action='store_true')
parser.add_argument('--variable-test',action='store_true')
parser.add_argument('--feed-test',action='store_true')
parser.add_argument('--fib-test',action='store_true')
parsed_args = parser.parse_args(args)
if parsed_args.constant_test:
constant_test()
elif parsed_args.variable_test:
variable_test()
elif parsed_args.feed_test:
feed_test(10.0,20.0)
elif parsed_args.fib_test:
flowfib(50)
if __name__=='__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment