Last active
July 12, 2018 09:27
-
-
Save NaxAlpha/0fea37487b394689f1ff36dbd6d72551 to your computer and use it in GitHub Desktop.
[VX] Tensorflow-Fun: Build Fibonacci Series using Tensorflow
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
| # Pure TF implm. | |
| # Fibonacci series using tensorflow | |
| import tensorflow as tf | |
| import numpy as np | |
| import time | |
| def build_graph2(max_count): | |
| init = np.zeros([max_count], dtype=int) | |
| m0 = tf.Variable(init,dtype=tf.int32) | |
| i0 = tf.constant(2) | |
| cond = lambda i, op: i<max_count | |
| body = lambda i, op: [i+1, tf.assign(m0[i], m0[i-1]+m0[i-2])] | |
| loop = tf.while_loop(cond, body, loop_vars=[i0, m0]) | |
| return loop | |
| def build_graph(max_count): | |
| m0 = tf.constant([0, 1]) | |
| i0 = tf.constant(2) | |
| cond = lambda i, m: tf.less(i, max_count) | |
| body = lambda i, m: [tf.add(i, 1), tf.concat([m, [m[i-1] + m[i-2]]], 0)] | |
| loop = tf.while_loop(cond, body, | |
| loop_vars=[i0, m0], | |
| shape_invariants=[i0.get_shape(), tf.TensorShape([None])]) | |
| return loop | |
| def build_series(sess, max_count): | |
| loop = build_graph(max_count) | |
| sess.run(tf.global_variables_initializer()) | |
| now = time.time() | |
| i, out = sess.run(loop) | |
| print(time.time() - now) | |
| return out | |
| def fab_cpu(count): | |
| now = time.time() | |
| series = np.zeros([count], dtype=int); series[1] = 1 | |
| for i in range(2, count): | |
| series[i] = series[i-1] + series[i-2] | |
| print(time.time() - now) | |
| if __name__ == '__main__': | |
| sess = tf.InteractiveSession() | |
| build_series(sess, 10000) | |
| fab_cpu(10000) |
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
| # Fibonacci series using tensorflow | |
| import tensorflow as tf | |
| import numpy as np | |
| def build_graph(max_count): | |
| init = np.zeros([max_count], dtype=np.int64); init[1] = 1 | |
| series = tf.Variable(init) | |
| idx = tf.placeholder(dtype=tf.int32, shape=None) | |
| first_last = series[idx-1] | |
| second_last = series[idx-2] | |
| new_val = tf.add(first_last, second_last) | |
| step = tf.assign(series[idx], new_val) | |
| return idx, step, series | |
| def build_series(sess, max_count): | |
| idx, step, series = build_graph(max_count) | |
| sess.run(tf.global_variables_initializer()) | |
| for i in range(2, max_count): | |
| sess.run(step, {idx: i}) | |
| return sess.run(series) | |
| if __name__ == '__main__': | |
| sess = tf.InteractiveSession() | |
| print(build_series(sess, 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment