zoukankan      html  css  js  c++  java
  • tensorflow学习之 Eager execution

      首先tensorflow本身就是一个声明式的编程。而不是命令式的编程。

        1、声明式的编程可以简单理解为先统一列出计算形式或者是表达式,然后最终在会话中进行计算。

        2、而命令式就像是python本身就是。有初始值,再写出计算式的时候,运行到这一步其实就相当于已经的除了结果。

        下面我们可以用斐波那契数列举例:

      「Eager Execution」,它是一个命令式、由运行定义的接口,一旦从 Python 被调用,其操作立即被执行。

      在 TensorFlow 1.X 版本中, 必须 在导入 TensorFlow 库后调用tf.enable_eager_execution()函数以启用 Eager Execution 模式。

    import tensorflow as tf
    #因为我这里是2.0的版本,默认是命令式编程,所以我们需要手动进行取消
    tf.compat.v1.disable_eager_execution()
    state = tf.Variable(tf.zeros([4,5]))
    with tf.compat.v1.Session() as sess:
        sess.run(tf.compat.v1.global_variables_initializer())
        print(sess.run(state))

     


      在 TensorFlow 2.0 版本中,Eager Execution 模式将成为默认模式,无需额外调用 tf.enable_eager_execution() 函数(不过若要关闭 Eager Execution,则需调用 tf.compat.v1.disable_eager_execution() 函数)。  

    import tensorflow as tf
    state = tf.Variable(tf.zeros([4,5]))
    print(state)

      Eager Execution 有啥优点?

      1、快速调试即刻的运行错误并通过 Python 工具进行整合

      2、借助易于使用的 Python 控制流支持动态模型

      3、为自定义和高阶梯度提供强大支持

      4、适用于几乎所有可用的 TensorFlow 运算

      

  • 相关阅读:
    307.区域与检索--数组可修改
    202.快乐数
    263.丑数
    205.同构字符串
    204.计数质数
    40.组合总和Ⅱ
    811.子域名访问计数
    39.组合总和
    udp与tcp
    SQL复习
  • 原文地址:https://www.cnblogs.com/moxihuishou/p/14314552.html
Copyright © 2011-2022 走看看