只能在SPYDER IDE中的程序启动时调用tf.enable_eager_execution

2024-10-06 13:52:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着对一个简单的代码执行一个急切的执行。

我在Jupyter笔记本和Spyder IDE上都试过。 对于Jupyter,我没有问题,但是当我在Spyder中执行代码时,它会返回一个错误:

File "C:\...\lib\site-packages\tensorflow\python\framework\ops.py", line 5496, in enable_eager_execution "tf.enable_eager_execution must be called at program startup.")
ValueError: tf.enable_eager_execution must be called at program startup.

代码如下:

import tensorflow as tf
tf.enable_eager_execution ()
import tensorflow.contrib.eager as tfe
def square (x):
     return tf.multiply (x, x)

grad = tfe.gradients_function (square)

print (grad (3.))

Tags: 代码importenabletftensorflowjupyterbeprogram