@jalFaizy wrote:
So here I am, trying to prove a point that
tensorflow == numpy
In this needless attempt, I will show you an application of a sorting technique "Bubble Sort" using TensorFlow.A little intro to Bubble Sort; Its the simplest sort you can think of. In this, you are essentially comparing each adjacent value in array again and again till it "sorts itself"
[Image Credits: Bubble Sort wiki Page]Now let's jump on to the code!
We can write a pseudocode of our problem as follows:
for i in range(array_length): for j in range(array_length - i - 1): if current_value > next_value: replace(current_value, next_value) replace(next_value, current_value)
Now, let's see our tensors flow!
# import modules import tensorflow as tf # create interactive session sess = tf.InteractiveSession() # create unsorted array unsorted_array = tf.Variable([1, 4, 2, 3]) # find length of array len_array = unsorted_array.get_shape()[0].value # initialize variables init = tf.initialize_all_variables() sess.run(init) cnt = 0 # bubble sort for i in range(len_array): for j in range(0, len_array - i - 1): if tf.greater(unsorted_array[j], unsorted_array[j+1]).eval(): # extract values temp1 = unsorted_array[j].eval() temp2 = unsorted_array[j + 1].eval() # replace unsorted array unsorted_array = tf.scatter_update(unsorted_array, j, temp2) unsorted_array = tf.scatter_update(unsorted_array, j+1, temp1) print "After iteration", cnt, ':', unsorted_array.eval() cnt += 1
Our output is as follows:
After iteration 0 : [1 4 2 3] After iteration 1 : [1 2 4 3] After iteration 2 : [1 2 3 4] After iteration 3 : [1 2 3 4] After iteration 4 : [1 2 3 4] After iteration 5 : [1 2 3 4]
Looks like our array is sorted! (I knew that already
) Many improvements could still be done in our code, but for display purposes, I think its ok.
So are you convinced that tensorflow is numpy?
Posts: 1
Participants: 1