资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

tensorflow的Eagerexecution怎么创建-创新互联

这篇文章主要介绍“tensorflow的Eager execution怎么创建”,在日常操作中,相信很多人在tensorflow的Eager execution怎么创建问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”tensorflow的Eager execution怎么创建”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

我们提供的服务有:成都网站设计、成都做网站、微信公众号开发、网站优化、网站认证、思茅ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的思茅网站制作公司

  一、开始学习 TensorFlow 最简单的方法是使用 Eager Execution,官方提供的教程为Colab notebook,打不开需要梯子,参考其他的吧,比如这个:tensorflow之Eager execution基础

  从tensorflow之Eager execution基础中,我了解到:

  啥是Eager Execution?

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

  这使得入门 TensorFlow 变的更简单,也使研发更直观。

  Eager Execution 有啥优点?

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

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

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

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

  啥是张量?

  张量是一个多维数组。与NumPy ndarray对象类似,Tensor对象具有数据类型和形状。

  此外,Tensors可以驻留在加速器(如GPU)内存中。

  TensorFlow提供了丰富的操作库(tf.add,tf.matmul,tf.linalg.inv等),

  它们使用和生成Tensors。这些操作自动转换本机Python类型。

  张量的基本创建与使用

  # -*- coding: utf-8 -*-

  """

  @File : 191206_test_Eager_execution.py

  @Time : 2019/12/6 11:11

  @Author : Dontla

  @Email : sxana@qq.com

  @Software: PyCharm

  """

  # 导入tensorflow

  import tensorflow as tf

  tf.enable_eager_execution()

  # 创建和使用张量

  print(tf.add(1,2)) # tf.Tensor(3, shape=(), dtype=int32)

  print(tf.add([1, 2], [3, 4])) # tf.Tensor([4 6], shape=(2,), dtype=int32)

  print(tf.square(5)) # tf.Tensor(25, shape=(), dtype=int32)

  print(tf.reduce_sum([1, 2, 3])) # tf.Tensor(6, shape=(), dtype=int32)

  print(tf.encode_base64("hello world")) # tf.Tensor(b'aGVsbG8gd29ybGQ', shape=(), dtype=string)

  print(tf.square(2) + tf.square(3)) # tf.Tensor(13, shape=(), dtype=int32)

  x = tf.matmul([[1]], [[2, 3]])

  print(x) # tf.Tensor([[2 3]], shape=(1, 2), dtype=int32)

  print(x.shape) # (1, 2)

  print(x.dtype) #

  张量的属性

  每个Tensor都有一个形状和数据类型

  x = tf.matmul([[1]], [[2, 3]])

  print(x.shape)

  print(x.dtype)

  NumPy array和TensorFlow张量之间最明显的区别

  张量可以由加速器内存(如GPU,TPU)支持。

  张量是不可改变的。

  TensorFlow张量和NumPy nararrays之间的转换

  TensorFlow操作自动将NumPy ndarrays转换为Tensors。

  NumPy操作自动将Tensors转换为NumPy ndarrays。

  通过在Tensors上调用.numpy()方法,可以将张量显式转换为NumPy ndarrays。这些转换通常很容易,因为如果可能,数组和Tensor共享底层内存表示。但是,共享底层表示并不总是可行的,因为Tensor可能托管在GPU内存中,而NumPy阵列总是由主机内存支持,因此转换将涉及从GPU到主机内存的复制。

  import tensorflow as tf

  import numpy as np

  tf.enable_eager_execution()

  ndarray = np.ones([3, 3])

  print(ndarray)

  # [[1. 1. 1.]

  # [1. 1. 1.]

  print("TensorFlow operations convert numpy arrays to Tensors automatically")

  tensor = tf.multiply(ndarray, 42)

  print(tensor)

  # tf.Tensor(

  # [[42. 42. 42.]

  # [42. 42. 42.]

  # [42. 42. 42.]], shape=(3, 3), dtype=float64)

  print("And NumPy operations convert Tensors to numpy arrays automatically")

  print(np.add(tensor, 1))

  # [[43. 43. 43.]

  # [43. 43. 43.]

  # [43. 43. 43.]]

  print("The .numpy() method explicitly converts a Tensor to a numpy array")

  print(tensor.numpy())

  # [[42. 42. 42.]

  # [42. 42. 42.]

  # [42. 42. 42.]]

  二、GPU加速

  通过使用GPU进行计算,可以加速许多TensorFlow操作。在没有任何注释的情况下,TensorFlow会自动决定是使用GPU还是CPU进行操作(如有必要,还可以复制CPU和GPU内存之间的张量)。由操作产生的张量通常由执行操作的设备的存储器支持。例如:

  # -*- coding: utf-8 -*-

  """

  @File : 191208_test_Eager_execution_once_cls.py

  @Time : 2019/12/8 12:25

  @Author : Dontla

  @Email : sxana@qq.com

  @Software: PyCharm

  """

  import tensorflow as tf

  tf.enable_eager_execution()

  x = tf.random_uniform([3, 3])

  print("Is there a GPU available: ")

  print(tf.test.is_gpu_available()) # True

  print("Is the Tensor on GPU #0: "),

  print(x.device) # /job:localhost/replica:0/task:0/device:GPU:0

  print(x.device.endswith('GPU:0')) # True

  (1)设备名称

  Tensor.device属性提供托管Tensor内容的设备的完全限定字符串名称。此名称对一组详细信息进行编码,例如,正在执行此程序的主机的网络地址的标识符以及该主机中的设备。这是分布式执行TensorFlow程序所必需的,但我们暂时不会这样做。如果张量位于主机上的第N个张量上,则字符串将以GPU:结尾。

  (2)显示设备配置

  TensorFlow中的术语“placement"指的是如何为执行设备分配(放置)各个操作。如上所述,当没有提供明确的指导时,TensorFlow会自动决定执行操作的设备,并在需要时将Tensors复制到该设备。但是,可以使用tf.device上下文管理器将TensorFlow操作显式放置在特定设备上。

到此,关于“tensorflow的Eager execution怎么创建”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联-成都网站建设公司网站,小编会继续努力为大家带来更多实用的文章!


分享题目:tensorflow的Eagerexecution怎么创建-创新互联
文章路径:http://cdkjz.cn/article/didpcp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220