Last active
October 22, 2017 12:17
-
-
Save akimach/ecd187659801768fa69653c99429c758 to your computer and use it in GitHub Desktop.
Esoteric TensorFlow ref: http://qiita.com/akimach/items/46fc21431876b191972a
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
| from __future__ import print_function | |
| import tensorflow as tf | |
| class FizzBuzz(): | |
| def __init__(self, length=30): | |
| self.length = length | |
| self.array = tf.Variable([str(i) for i in range(1, length+1)], dtype=tf.string, trainable=False) | |
| self.graph = tf.while_loop(self.cond, self.body, [1, self.array], | |
| shape_invariants=[tf.TensorShape([]), tf.TensorShape(self.length)], | |
| back_prop=False) | |
| def run(self): | |
| with tf.Session() as sess: | |
| tf.global_variables_initializer().run() | |
| return sess.run(self.graph) | |
| def cond(self, i, _): | |
| return (tf.less(i, self.length+1)) | |
| def body(self, i, _): | |
| r = tf.cond( | |
| tf.logical_and( # tf.equal(tf.mod(i, 15), 0) | |
| tf.equal(tf.mod(i, 3), 0), | |
| tf.equal(tf.mod(i, 5), 0)), | |
| lambda: tf.assign(self.array[i - 1], 'FizzBuzz'), | |
| lambda: tf.cond(tf.equal(tf.mod(i, 3), 0), | |
| lambda: tf.assign(self.array[i - 1], 'Fizz'), | |
| lambda: tf.cond(tf.equal(tf.mod(i, 5), 0), | |
| lambda: tf.assign(self.array[i - 1], 'Buzz'), | |
| lambda: self.array | |
| ) | |
| ) | |
| ) | |
| return (tf.add(i, 1), r) | |
| if __name__ == '__main__': | |
| fizzbuzz = FizzBuzz(length=50) | |
| ix, array = fizzbuzz.run() | |
| print(array) |
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
| ['1' '2' 'Fizz' '4' 'Buzz' 'Fizz' '7' '8' 'Fizz' 'Buzz' '11' 'Fizz' '13' | |
| '14' 'FizzBuzz' '16' '17' 'Fizz' '19' 'Buzz' 'Fizz' '22' '23' 'Fizz' | |
| 'Buzz' '26' 'Fizz' '28' '29' 'FizzBuzz' '31' '32' 'Fizz' '34' 'Buzz' | |
| 'Fizz' '37' '38' 'Fizz' 'Buzz' '41' 'Fizz' '43' '44' 'FizzBuzz' '46' '47' | |
| 'Fizz' '49' 'Buzz'] |
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 tensorflow as tf | |
| tf.InteractiveSession() | |
| i = tf.constant(0) | |
| c = lambda i: tf.less(i, 10) | |
| b = lambda i: tf.add(i, 1) | |
| r = tf.while_loop(c, b, [i]) | |
| r.eval() #=> 10 |
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
| tf.cond( | |
| pred,# Condition | |
| true_fn=None, # Process to be executed when cond is True | |
| false_fn=None, # Process to be executed when cond is False | |
| ) |
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 tensorflow as tf | |
| import numpy as np | |
| x = tf.Variable(np.arange(5)) #=> [0, 1, 2, 3, 4] | |
| i = tf.constant(1) | |
| j = tf.constant(4) | |
| # インデックス1と4の要素を交換する | |
| swap = tf.scatter_update(x, [i, j], [x[j], x[i]]) | |
| tf.InteractiveSession() | |
| tf.global_variables_initializer().run() | |
| swap.eval() #=> [0, 4, 2, 3, 1] |
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
| from __future__ import print_function | |
| import numpy as np | |
| import tensorflow as tf | |
| np.random.seed(123) | |
| class BubbleSort(): | |
| def __init__(self, array): | |
| self.i = tf.constant(0) | |
| self.j = tf.constant(len(array)-1) | |
| self.array = tf.Variable(array, trainable=False) | |
| self.length = len(array) | |
| cond = lambda i, j, _: tf.less(i-1, self.length-1) | |
| self.graph = tf.while_loop(cond, self.outer_loop, loop_vars=[self.i, self.j, self.array], | |
| shape_invariants=[self.i.get_shape(), self.j.get_shape(), tf.TensorShape(self.length)], | |
| parallel_iterations=1, | |
| back_prop=False) | |
| def run(self): | |
| with tf.Session() as sess: | |
| tf.global_variables_initializer().run() | |
| return sess.run(self.graph) | |
| def outer_loop(self, i, j, _): | |
| cond = lambda i, j, _: tf.greater(j, i) | |
| loop = tf.while_loop(cond, self.inner_loop, loop_vars=[i, self.length-1, self.array], | |
| shape_invariants=[i.get_shape(), j.get_shape(), tf.TensorShape(self.length)], | |
| parallel_iterations=1, | |
| back_prop=False) | |
| return tf.add(i, 1), loop[1], loop[2] | |
| def inner_loop(self, i, j, _): | |
| body = tf.cond(tf.greater(self.array[j-1], self.array[j]), | |
| lambda: tf.scatter_nd_update(self.array, [[j-1],[j]], [self.array[j],self.array[j-1]]), | |
| lambda: self.array) | |
| return i, tf.subtract(j, 1), body | |
| if __name__ == '__main__': | |
| x = np.array([1.,7.,3.,8.]) | |
| _, _, sorted_array = BubbleSort(x).run() | |
| print(x) | |
| print(sorted_array) | |
| y = np.random.rand(20) | |
| print(y) | |
| _, _, sorted_array = BubbleSort(y).run() | |
| print(sorted_array) |
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
| [ 1. 7. 3. 8.] | |
| [ 1. 3. 7. 8.] | |
| [ 0.69646919 0.28613933 0.22685145 0.55131477 0.71946897 0.42310646 | |
| 0.9807642 0.68482974 0.4809319 0.39211752 0.34317802 0.72904971 | |
| 0.43857224 0.0596779 0.39804426 0.73799541 0.18249173 0.17545176 | |
| 0.53155137 0.53182759] | |
| [ 0.0596779 0.17545176 0.18249173 0.22685145 0.28613933 0.34317802 | |
| 0.39211752 0.39804426 0.42310646 0.43857224 0.4809319 0.53155137 | |
| 0.53182759 0.55131477 0.68482974 0.69646919 0.71946897 0.72904971 | |
| 0.73799541 0.9807642 ] |
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
| # Code abridged | |
| if __name__ == '__main__': | |
| src_A = '++++++[> ++++++++++ < -]> +++++.' | |
| pc, tape, cur, jumps, output = BrainFuck(src_A).run() | |
| print(output) #=> A | |
| src_helloworld =''' | |
| +++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-. | |
| ------------.<++++++++.--------.+++.------.--------.>+. | |
| ''' | |
| pc, tape, cur, jumps, output = BrainFuck(src_helloworld).run() | |
| print(output) #=> Hello, world! |
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 tensorflow as tf | |
| import tfgraphviz as tfg | |
| x = tf.Variable(0, name="x") | |
| y = tf.Variable(1, name="y") | |
| a = tf.constant(3, name="a") | |
| b = tf.constant(4, name="b") | |
| mul_op = tf.multiply(a, b, name="mul_op") | |
| assign_x_op = tf.assign(x, mul_op, name="assign_x") | |
| assign_y_1_op = tf.assign_add(y, 1, name="assign_add_y_1") | |
| assign_y_2_op = tf.assign_add(assign_y_1_op, 1, name="assign_add_y_2") | |
| assign_y_3_op = tf.assign_add(assign_y_2_op, 1, name="assign_add_y_3") | |
| assign_y_4_op = tf.assign_add(assign_y_3_op, 1, name="assign_add_y_4") | |
| sum_op = tf.add(assign_x_op, assign_y_2_op, name="sum_op") | |
| sess = tf.Session() | |
| tfg.board(sess.graph) |
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
| int main() { | |
| int x = 0, y = 1; | |
| int mul, sum; | |
| const int a = 3, b = 4; | |
| mul = a * b; | |
| x = mul; | |
| y++; | |
| y++; | |
| sum = x + y; | |
| y++; | |
| y++; | |
| return 0; | |
| } |
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
| tf.cond( | |
| pred,# 条件判定 | |
| true_fn=None, # Trueの場合に呼び出される関数 | |
| false_fn=None, # Falseの場合に呼び出される関数 | |
| ) |
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 tensorflow as tf | |
| tf.InteractiveSession() | |
| a = tf.constant(3) | |
| b = tf.constant(7) | |
| r = tf.cond(tf.greater(a, b), lambda: a, lambda: b) | |
| r.eval() #=> 7 |
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
| tf.while_loop( | |
| cond, # 条件判定 | |
| body, # condがTrueの場合に実行される処理 | |
| loop_vars, # condとbodyに渡されるノード | |
| shape_invariants=None, # tf.while_loop(...)の返り値のshape | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment