Vig*_*mar 1 keras tensorflow eager-execution
我有一个安装了 Tensorflow 2.0.0-beta1 的 conda 环境。但是,每当我导入 tensorflow 并尝试启用急切执行时,我都会收到错误消息:
AttributeError: module 'tensorflow' has no attribute 'enable_eager_execution'
Run Code Online (Sandbox Code Playgroud)
我为此运行的唯一代码是:
import tensorflow as tf
print(tf.__version__)
tf.enable_eager_execution()
Run Code Online (Sandbox Code Playgroud)
这是 tensorflow 2.0 beta 模块的错误还是我的安装问题?
在 ternsorflow 2.0 中,该enable_eager_execution方法移至tf.compat.v1模块。以下适用于 tensorflow-2.0.0-beta1
tf.compat.v1.enable_eager_execution()
Run Code Online (Sandbox Code Playgroud)
在 tensorflow 2.0 中,默认情况下启用了急切执行。您不需要在程序中启用它。
例如
import tensorflow as tf
t = tf.constant([5.0])
Run Code Online (Sandbox Code Playgroud)
现在你可以直接查看张量的值,而无需使用会话对象。
print(t)
# tf.Tensor([5.], shape=(1,), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
您还可以将张量值更改为 numpy 数组
numpy_array = t.numpy()
print(numpy_array)
# [5.]
Run Code Online (Sandbox Code Playgroud)
您还可以在 tensorflow-2 中禁用 Eager Execution(在 tensorflow-2.0.0-beta1 上测试。这可能不适用于未来版本。)
tf.compat.v1.disable_eager_execution()
t2 = tf.constant([5.0])
print(t2)
# Tensor("Const:0", shape=(1,), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
禁用急切执行后在张量上调用 numpy() 方法会引发错误
AttributeError: 'Tensor' object has no attribute 'numpy'
Run Code Online (Sandbox Code Playgroud)
禁用急切执行时应该考虑的一个问题是,一旦禁用急切执行,就不能在同一程序中启用它,因为tf.enable_eager_execution应该在程序启动时调用,并且在禁用急切执行后调用此方法会引发错误:
ValueError: tf.enable_eager_execution must be called at program startup.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5139 次 |
| 最近记录: |