Created
June 6, 2017 20:36
-
-
Save armancohan/e756784df5b5d218af6f4f3843f2147b to your computer and use it in GitHub Desktop.
Calculate the n-th Fibonacci number using tensorflow's FIFOQueue data structure
This file contains hidden or 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 sys | |
import tensorflow as tf | |
def main(n): | |
""" Calculate the n-th Fibonacci number """ | |
if n < 1: | |
print('n should be greater than 0') | |
sys.exit(1) | |
elif n == 1 or n == 2: | |
return 1 | |
else: | |
n = n - 2 | |
q = tf.FIFOQueue(2, tf.float32, shapes=[], name='QUEUE') | |
init = q.enqueue_many([[1., 1.], ], 'initialization') | |
out = q.dequeue_many(2, name='deque') | |
y = tf.reduce_sum(out) | |
fib_op = q.enqueue_many([[out[1], y], ], name='fib-op') | |
res = q.dequeue_many(2)[1] | |
writer = tf.summary.FileWriter('/tmp/tf-example/1/') | |
with tf.Session() as sess: | |
writer.add_graph(sess.graph) | |
sess.run(init) | |
for _ in range(n): | |
sess.run(fib_op) | |
return int(sess.run(res)) | |
if __name__ == '__main__': | |
n = int(sys.argv[1]) | |
print(main(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment