美文网首页
# TF Eager Execution 阅读笔记

# TF Eager Execution 阅读笔记

作者: tsiic | 来源:发表于2018-07-03 10:47 被阅读0次

TF Eager Execution 阅读笔记

@[TensonFlow]

看了半天不知道Eager 是啥,这哪能看下去。所以Google了一下,在知乎发现如下解释:

......就开启了Eager模式,这时,TensorFlow会从原先的声明式(declarative)编程形式变成命令式(imperative)编程形式。当写下语句"c = tf.matmul(a, b)"后(以及其他任何tf开头的函数),就会直接执行相应的操作并得到值,而不再像之前那样,生成一个Tensor,通过sess.run()才能拿到值。注意:这种Eager模式一旦被开启就不能被关闭。

好处

使用Eager模式的好处大家应当都很清楚,可以直接参考PyTorch的相关问题来看:PyTorch到底好用在哪里?。这里就简单说一说,大概有以下几点:

  1. 搭模型更方便了:之前搭模型通常要认真记下每一步Tensor的shape和意义,然后再操作。现在可以轻松点,边搭边写,忘记形状或者含义的时候可以直接打出来看。另外流程控制可以使用Python的内建语法,更加直观。
  2. 调试时no more sess.run() ! 之前在调试时必须要加上sess.run(),很麻烦,现在可以直接把变量print出来,亦可使用IDE的监控工具单步调试。
  3. 最后,如果之前我们想在自己的程序中用tf开头的函数,需要手动开启Session将结果的Tensor转换成Numpy数组,或者使用官方提供的函数修饰器。现在只需要用开启这个Eager模式,就可以直接把tf开头的函数当普通函数用了。
    作者:何之源
    链接:https://www.zhihu.com/question/67471378/answer/253549074
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

OK。主要优势是立即执行,对模型搭建调试友好。

Since there isn't a computational graph to build and run later in a session, it's easy to inspect results using print() or a debugger. Evaluating, printing, and checking tensor values does not break the flow for computing gradients.

GradientTape

又是一个不知道的东西,感觉上这个Eager模式是某种进阶模式,在我对术语和流程掌握都不够的时候,阅读起来有点障碍,而且感触也很少。

首先根据术语表,Gradient是梯度。

官方的解释有以下几点:

  1. It records operations to use to compute gradients.
  2. Since different operations can occur during each call, all forward-pass operations get recorded to a "tape".
  3. To compute the gradient, play the tape backwards and then discard.
  4. A particular tfe.GradientTape can only be computed once, subsequent calls throw a runtime error.

嗯就是记录操作并用来反向运算计算梯度的,不能连续调用。

示例

示例用的是MNIST识别手写数字的问题,代码见https://github.com/tensorflow/models/blob/master/official/mnist/mnist_eager.py

参考资料

  1. https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/eager
  2. https://www.zhihu.com/question/67471378/answer/253549074
  3. https://www.hksilicon.com/articles/1465947
  4. TensorFlow入门6 -- 鸢尾(Iris)花分类,训练模型:https://www.jianshu.com/p/b74955fa1b16

相关文章

网友评论

      本文标题:# TF Eager Execution 阅读笔记

      本文链接:https://www.haomeiwen.com/subject/xejduftx.html