Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating percentage of number with Tensorflow

I have tried this:

>>> import tensorflow as tf
>>> mul = tf.multiply(50,100)
>>> div = tf.divide(mul,50)
>>> mul
<tf.Tensor 'Mul_3:0' shape=() dtype=int32>
>>> div
<tf.Tensor 'truediv_2:0' shape=() dtype=float64>
>>> import tensorflow as tf
>>> x=50
>>> mul = tf.multiply(x,100)
>>> div = tf.divide(mul,50)
>>> mul
<tf.Tensor 'Mul_4:0' shape=() dtype=int32>
>>> div
<tf.Tensor 'truediv_3:0' shape=() dtype=float64>

I am not seeing any numbers. I want to get the percentage done by tensorflow.
Kindly, let me know what I am missing here. Even when I tried evaluating, I got session based error. It's true that I need o establish session, but do not know how I can call it inside.
Please let me know if I missed something.

like image 382
Jaffer Wilson Avatar asked Feb 22 '19 05:02

Jaffer Wilson


2 Answers

In the print statements you get,

<tf.Tensor 'Mul_4:0' shape=() dtype=int32>

And other such statements. This is because Python is printing out the Tensor Objects and not their values. There are two methods to solve this .

  1. Enable eager execution.

    import tensorflow as tf
    tf.enable_eager_execution()
    

This will enable eager mode and you will get values of the tensors instead of the Tensor objects. This initializes the tensors immediately as they are declared ( and hence eager ).

  1. Using tf.Session() A tf.Session() objects runs and evaluates tensors in the graph. It runs on graph mode and not eager mode.

    with tf.Session as session:
        print( session.run( div ) )
    
like image 59
Shubham Panchal Avatar answered Oct 19 '22 22:10

Shubham Panchal


Try this it will certainly help:

>>> import tensorflow as tf
>>> a = tf.placeholder(tf.float32)
>>> b = tf.placeholder(tf.float32)
>>> sess = tf.Session()
>>> percentage = tf.divide(tf.multiply(a,100),b)
>>> sess.run(tf.global_variables_initializer())
>>> sess.run(percentage,feed_dict={a:4,b:20})
20.0
>>> sess.run(percentage,feed_dict={a:50,b:50})
100.0
>>> sess.close()

You can refer to simple example:
https://stackoverflow.com/a/39747526/4948889
Hope this helps.

like image 30
Amazing Things Around You Avatar answered Oct 19 '22 23:10

Amazing Things Around You