Created
November 30, 2015 18:20
-
-
Save d136o/ce0c54f1d33006402e06 to your computer and use it in GitHub Desktop.
Getting familiar with TensorFlow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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