tensorflow的Eagerexecution怎么創(chuàng)建-創(chuàng)新互聯(lián)

這篇文章主要介紹“tensorflow的Eager execution怎么創(chuàng)建”,在日常操作中,相信很多人在tensorflow的Eager execution怎么創(chuàng)建問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”tensorflow的Eager execution怎么創(chuàng)建”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、思茅ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的思茅網(wǎng)站制作公司

  一、開始學(xué)習(xí) TensorFlow 最簡單的方法是使用 Eager Execution,官方提供的教程為Colab notebook,打不開需要梯子,參考其他的吧,比如這個:tensorflow之Eager execution基礎(chǔ)

  從tensorflow之Eager execution基礎(chǔ)中,我了解到:

  啥是Eager Execution?

  「Eager Execution」,它是一個命令式、由運行定義的接口,一旦從 Python 被調(diào)用,其操作立即被執(zhí)行。

  這使得入門 TensorFlow 變的更簡單,也使研發(fā)更直觀。

  Eager Execution 有啥優(yōu)點?

  1、快速調(diào)試即刻的運行錯誤并通過 Python 工具進(jìn)行整合

  2、借助易于使用的 Python 控制流支持動態(tài)模型

  3、為自定義和高階梯度提供強大支持

  4、適用于幾乎所有可用的 TensorFlow 運算

  啥是張量?

  張量是一個多維數(shù)組。與NumPy ndarray對象類似,Tensor對象具有數(shù)據(jù)類型和形狀。

  此外,Tensors可以駐留在加速器(如GPU)內(nèi)存中。

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

  它們使用和生成Tensors。這些操作自動轉(zhuǎn)換本機Python類型。

  張量的基本創(chuàng)建與使用

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

  """

  @File : 191206_test_Eager_execution.py

  @Time : 2019/12/6 11:11

  @Author : Dontla

  @Email : sxana@qq.com

  @Software: PyCharm

  """

  # 導(dǎo)入tensorflow

  import tensorflow as tf

  tf.enable_eager_execution()

  # 創(chuàng)建和使用張量

  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都有一個形狀和數(shù)據(jù)類型

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

  print(x.shape)

  print(x.dtype)

  NumPy array和TensorFlow張量之間最明顯的區(qū)別

  張量可以由加速器內(nèi)存(如GPU,TPU)支持。

  張量是不可改變的。

  TensorFlow張量和NumPy nararrays之間的轉(zhuǎn)換

  TensorFlow操作自動將NumPy ndarrays轉(zhuǎn)換為Tensors。

  NumPy操作自動將Tensors轉(zhuǎn)換為NumPy ndarrays。

  通過在Tensors上調(diào)用.numpy()方法,可以將張量顯式轉(zhuǎn)換為NumPy ndarrays。這些轉(zhuǎn)換通常很容易,因為如果可能,數(shù)組和Tensor共享底層內(nèi)存表示。但是,共享底層表示并不總是可行的,因為Tensor可能托管在GPU內(nèi)存中,而NumPy陣列總是由主機內(nèi)存支持,因此轉(zhuǎn)換將涉及從GPU到主機內(nèi)存的復(fù)制。

  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進(jìn)行計算,可以加速許多TensorFlow操作。在沒有任何注釋的情況下,TensorFlow會自動決定是使用GPU還是CPU進(jìn)行操作(如有必要,還可以復(fù)制CPU和GPU內(nèi)存之間的張量)。由操作產(chǎn)生的張量通常由執(zhí)行操作的設(shè)備的存儲器支持。例如:

  # -*- 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)設(shè)備名稱

  Tensor.device屬性提供托管Tensor內(nèi)容的設(shè)備的完全限定字符串名稱。此名稱對一組詳細(xì)信息進(jìn)行編碼,例如,正在執(zhí)行此程序的主機的網(wǎng)絡(luò)地址的標(biāo)識符以及該主機中的設(shè)備。這是分布式執(zhí)行TensorFlow程序所必需的,但我們暫時不會這樣做。如果張量位于主機上的第N個張量上,則字符串將以GPU:結(jié)尾。

  (2)顯示設(shè)備配置

  TensorFlow中的術(shù)語“placement"指的是如何為執(zhí)行設(shè)備分配(放置)各個操作。如上所述,當(dāng)沒有提供明確的指導(dǎo)時,TensorFlow會自動決定執(zhí)行操作的設(shè)備,并在需要時將Tensors復(fù)制到該設(shè)備。但是,可以使用tf.device上下文管理器將TensorFlow操作顯式放置在特定設(shè)備上。

到此,關(guān)于“tensorflow的Eager execution怎么創(chuàng)建”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

網(wǎng)頁標(biāo)題:tensorflow的Eagerexecution怎么創(chuàng)建-創(chuàng)新互聯(lián)
文章URL:http://bm7419.com/article28/didpcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、營銷型網(wǎng)站建設(shè)、微信公眾號、網(wǎng)站改版、服務(wù)器托管、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)