繁体   English   中英

Tensorflow错误输出

[英]Tensorflow wrong ouput in print

我已经开始学习tensorflow,试图执行代码并不断得到错误的结果

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)

结果就是

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

奇怪的输出执行完所有计算后想要addn的值

问题是

print (addn)

打印数据仅给出了名称

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

张量,形状及其数据类型

没有赋予它任何时间点的价值。 这是因为上面的代码没有运行/执行。 它只是在tensorflow中构造了Graph,但尚未执行以获取结果。要执行该session

您可以添加几行,创建一个会话,然后打印

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

输出,您将获得输出20

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

完整的代码

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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