TensorFlow发布Eager,便于Debug!

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helei001/article/details/78574657

要查看网络中间结果,使用TensorFlow的小伙伴都知道,必须要run一下session,返回对应的值才能查看,这是很不方便的。为此,TensorFlow团队发布了eager,可以实时查看中间结果,便于大家Debug。那么怎么来用呢?今天简单介绍一下。

1. 安装

Eager execution已经包含在最新的1.4版本中,有两个办法可以安装,一是本地编译安装TensorFlow 1.4可以生成eager,二是安装轻量级的库:

pip install tf-nightly # (for CPU-only TensorFlow)
pip install tf-nightly-gpu # (for GPU-enabled TensorFlow)

2. 使用

import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()

在头文件中import eager库,并激活eager execution,后面默认eager一直处于激活状态。这个时候,中间结果像numpy结构的数据一样,可以直接打印输出。



此外,还有一个优势,tensor和numpy可以自动互相调用。

import numpy as np

np_x = np.array(2., dtype=np.float32)
x = tf.constant(np_x)

py_y = 3.
y = tf.constant(py_y)

z = x + y + 1

print(z)
print(z.numpy())

# Output
tf.Tensor(6.0, shape=(), dtype=float32)
6.0
这个真的展示了TF的特点,快速,灵活!



更多资料,请查看:

TensorFlow Eager Execution



猜你喜欢

转载自blog.csdn.net/helei001/article/details/78574657