语法错误:使用tf.Session()作为sess:File&#34; <ipython-input-20-b0b30aac112f>&#34;,

时间:2018-02-11 16:40:12

标签: python-3.x tensorflow

我尝试运行以下代码,但语法错误

&#13;
&#13;
import tensorflow as tf
p = tf.constant("Hello")
h = tf.constant("world!")
ph = p + h
with tf.Session() as sess:
 sess.run(ph)
print(sess.run(ph))
&#13;
&#13;
&#13;

错误类似于下图中的错误: Syntax Error

1 个答案:

答案 0 :(得分:1)

您应该在with block

中包含print语句

&#13;
&#13;
import tensorflow as tf
p = tf.constant("Hello")
h = tf.constant("world!")
ph = p + h
with tf.Session() as sess:
 print(sess.run(ph))
&#13;
&#13;
&#13;

至于错误,你可能试图在ipython中以交互方式运行代码,并且可能在运行with语句时按下shift + Enter。 Ipython抱怨,因为它在块内找不到任何东西