简体   繁体   中英

Tensorflow wrong ouput in print

I have started learning tensorflow , Tried to execute a code and continously getting wrong result

import tensorflow as tf

# Immutable constants
a = tf.constant(6,name='constant_a')
b = tf.constant(3,name='contant_b')
c = tf.constant(10,name='contant_c')
d = tf.constant(5,name='contant_d')

mul = tf.multiply(a,b,name='mul')
div = tf.div(c,d,name="div")
# Output of the multiplication what needs to be added
addn = tf.add_n([mul,div],name="addn")
# Print out the result
print (addn)

Result is just

 Tensor("addn:0", shape=(), dtype=int32) 

Strange output wanted the value of addn after it has performed all the computation

Problem is

print (addn)

Printing data just gives the name of the

 Tensor("addn:0", shape=(), dtype=int32) 

Tensor ,shape and its data type

doesn't give value it hold any point of time. This is because above code is not run/executed. It has just constructed the Graph in tensorflow but haven't executed to get the result To Execute it session is required

you can just add few lines ,create a session then print

sess = tf.Session()
print(sess.run(addn))

output you will get output 20

a*b + c/d = 6*3 + 10/5 = 18 + 2 = 20

Complete Code

d = tf.constant(5,name='contant_d')

mul = tf.multiply(a,b,name='mul')
div = tf.div(c,d,name="div")

# Output of the multiplication what needs to be added
addn = tf.add_n([mul,div],name="addn")
print (addn)

"""
Printing data just gives the name of the Tensor ,shape and its data type
doesn't give  value it hold anypoint of time
This is because above code is not run
It has just constructed the Graph in tensorflow but haven't executed to get the result
To Execute it session is required  
"""
sess = tf.Session()
print(sess.run(addn))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM