分享好友 人工智能首页 频道列表

tensorflow学习之 Eager execution

tensorflow教程  2023-02-09 19:199660

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

tensorflow学习之 Eager execution

 

 

 

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

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

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

tensorflow学习之 Eager execution

 

 

 

  「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学习之 Eager execution

 

 tensorflow学习之 Eager execution

 

 


  在 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)

 

tensorflow学习之 Eager execution

 

 

 

 

 

 

  Eager Execution 有啥优点?

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

 

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

 

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

 

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

 

  

查看更多关于【tensorflow教程】的文章

展开全文
相关推荐
反对 0
举报 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
tensorflow2.0——LSTM,GRU(Sequential层版)
前面都是写的cell版本的GRU和LSTM,比较底层,便于理解原理。下面的Sequential版不用自定义state参数的形状,使用更简便: import tensorflow as tfimport osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'assert tf.__version__.startswith('2.')# 设置相关底层

0评论2023-02-10907

TensorFlow基础笔记(7) 图像风格化效果与性能优化进展
参考 http://hacker.duanshishi.com/?p=1693http://blog.csdn.net/hungryof/article/details/53981959http://blog.csdn.net/hungryof/article/details/61195783http://blog.csdn.net/wyl1987527/article/details/70245214https://www.ctolib.com/AdaIN-style.

0评论2023-02-09559

Tensorflow报错总结 TensorFlow文档
输入不对应报错内容:WARNING:tensorflow:Model was constructed with shape (None, 79) for input Tensor("genres:0", shape=(None, 79), dtype=float32), but it was called on an input with incompatible shape (128, 5).定义模型的输入和训练时候传入的in

0评论2023-02-09636

深度学习框架之TensorFlow的概念及安装(ubuntu下基于pip的安装,IDE为Pycharm)
2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源。TensorFlow 是使用数据流图进行数值计算的开源软件库。也就是说,TensorFlow 使用图(graph)来表示计算任务。图中的节点表示数学运算,边表示运算之间用来交流的多维数组(也就是tensor,张量)

0评论2023-02-09857

TensorFlow源码分析——Tensor与Eigen tensorflow 开源
TensorFlow底层操作的数据结构是Tensor(张量),可以表示多维的数据,其实现在core/framework/tensor.h中,对于tensor的理解主要分两大块:1.Tensor的组成成分2.Tensor是如何进行数学运算的(TensorFlow本质就是处理大量训练数据集,在底层要实现深度学习常

0评论2023-02-09693

tensorflow scope的作用
  我们在使用tensorflow的时候,当你想复用一个函数的模块,调试时候回提示你变量已经出现,提示你是否重用。那我们当然是不重用的,因为每一个变量都是我们需要的。  要体现不同,就在不同的变量中使用name scope限定,那么其中的重复名字就不会出现问题

0评论2023-02-09697

TensorFlow——LinearRegression简单模型代码
代码函数详解tf.random.truncated_normal()函数tf.truncated_normal函数随机生成正态分布的数据,生成的数据是截断的正态分布,截断的标准是2倍的stddev。zip()函数zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些

0评论2023-02-09381

tensorFlow2.1下的tf.data.Dataset.from_tensor_slices()的用法
一、总结一句话总结:将输入的张量的第一个维度看做样本的个数,沿其第一个维度将tensor切片,得到的每个切片是一个样本数据。实现了输入张量的自动切片。# from_tensor_slices 为输入张量的每一行创建一个带有单独元素的数据集ts = tf.constant([[1, 2], [3,

0评论2023-02-09789

TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例
http://blog.csdn.net/huachao1001/article/details/78502910http://blog.csdn.net/u014432647/article/details/75276718https://zhuanlan.zhihu.com/p/32887066#coding:utf-8#http://blog.csdn.net/zhuiqiuk/article/details/53376283#http://blog.csdn.net/

0评论2023-02-091030

更多推荐