Python Tensorflow打印输出错误

Python Tensorflow打印输出错误,python,python-3.x,tensorflow,Python,Python 3.x,Tensorflow,我已经开始学习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

我已经开始学习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中构建了图形,但尚未执行以获得执行它的结果
需要会话

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

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))
问题是

print (addn)
打印数据只是给出了数据的名称

 Tensor("addn:0", shape=(), dtype=int32) 
张量、形状及其数据类型

不给它任何价值,它持有任何时间点。 这是因为上面的代码没有运行/执行。 它刚刚在tensorflow中构建了图形,但尚未执行以获得执行它的结果
需要会话

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

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

您需要实例化一个会话:
sess=tf.session()
,然后
sess.run(addn)
才能实际运行计算。奇怪的行为欢迎使用Tensorflow:P图形构建和执行之间的分离是您需要掌握的一个关键概念。这比听起来容易。看看Tensorflow的网站上的教程(但不要停留在那些只向你展示如何使用预制代码的简单教程上)这是什么-1?如果你保持耐心并仔细阅读,你可能会得到答案你需要实例化一个会话:
sess=tf.session()
然后
sess.run(addn)
以实际运行计算。奇怪的行为欢迎来到Tensorflow:P图形构建和执行之间的分离是您需要掌握的一个关键概念。这比听起来容易。请查看Tensorflow的网站以获取教程(但不要停留在那些只向你展示如何使用预制代码的简单的书上)这是什么-1?如果你保持耐心并仔细阅读,你可能会得到答案