日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

tensorflow教程 学习笔记 之 Eager execution 急切执行

發(fā)布時(shí)間:2025/3/19 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow教程 学习笔记 之 Eager execution 急切执行 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

鏈接:https://tensorflow.juejin.im/get_started/

學(xué)習(xí)它主要為了解決yunyang tensorflow-yolov-3GPU多線程調(diào)用的問題

文章目錄

      • 坑1
        • 啥是Eager Execution?
        • Eager Execution 有啥優(yōu)點(diǎn)?
        • 啥是張量?
        • 張量的基本創(chuàng)建與使用
        • 張量的屬性
        • NumPy array和TensorFlow張量之間最明顯的區(qū)別
        • TensorFlow張量和NumPy nararrays之間的轉(zhuǎn)換
      • 坑2
        • GPU加速
          • (1)設(shè)備名稱
          • (2)顯示設(shè)備配置
      • 坑3
        • 數(shù)據(jù)集
          • (1)創(chuàng)建源數(shù)據(jù)集
          • (2)應(yīng)用transformations
          • (3)迭代

坑1

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

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

啥是Eager Execution?

「Eager Execution」,它是一個(gè)命令式、由運(yùn)行定義的接口,一旦從 Python 被調(diào)用,其操作立即被執(zhí)行。 這使得入門 TensorFlow 變的更簡單,也使研發(fā)更直觀。

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

1、快速調(diào)試即刻的運(yùn)行錯(cuò)誤并通過 Python 工具進(jìn)行整合 2、借助易于使用的 Python 控制流支持動(dòng)態(tài)模型 3、為自定義和高階梯度提供強(qiáng)大支持 4、適用于幾乎所有可用的 TensorFlow 運(yùn)算

啥是張量?

張量是一個(gè)多維數(shù)組。與NumPy ndarray對象類似,Tensor對象具有數(shù)據(jù)類型和形狀。 此外,Tensors可以駐留在加速器(如GPU)內(nèi)存中。 TensorFlow提供了豐富的操作庫(tf.add,tf.matmul,tf.linalg.inv等), 它們使用和生成Tensors。這些操作自動(dòng)轉(zhuǎn)換本機(jī)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) # <dtype: 'int32'>

張量的屬性

每個(gè)Tensor都有一個(gè)形狀和數(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操作自動(dòng)將NumPy ndarrays轉(zhuǎn)換為Tensors。
  • NumPy操作自動(dòng)將Tensors轉(zhuǎn)換為NumPy ndarrays。

通過在Tensors上調(diào)用.numpy()方法,可以將張量顯式轉(zhuǎn)換為NumPy ndarrays。這些轉(zhuǎn)換通常很容易,因?yàn)槿绻赡?#xff0c;數(shù)組和Tensor共享底層內(nèi)存表示。但是,共享底層表示并不總是可行的,因?yàn)門ensor可能托管在GPU內(nèi)存中,而NumPy陣列總是由主機(jī)內(nèi)存支持,因此轉(zhuǎn)換將涉及從GPU到主機(jī)內(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.] # [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.]]

坑2

GPU加速

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

# -*- 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 tftf.enable_eager_execution()x = tf.random_uniform([3, 3])print("Is there a GPU available: ") print(tf.test.is_gpu_available()) # Trueprint("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í)行此程序的主機(jī)的網(wǎng)絡(luò)地址的標(biāo)識符以及該主機(jī)中的設(shè)備。這是分布式執(zhí)行TensorFlow程序所必需的,但我們暫時(shí)不會(huì)這樣做。如果張量位于主機(jī)上的第N個(gè)張量上,則字符串將以GPU:結(jié)尾。

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

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

Wocalei,我在pycharm上跑半天沒跑起來,最后放到ipython上才跑起來的!!!

C:\Users\Dontla>ipython Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.In [1]: import tensorflow as tf...:...:...: def time_matmul(x):...: %timeit tf.matmul(x, x)...:...:...: # Force execution on CPU...: print("On CPU:")...: with tf.device("CPU:0"):...: x = tf.random_uniform([1000, 1000])...: assert x.device.endswith("CPU:0")...: time_matmul(x)...:...: # Force execution on GPU #0 if available...: if tf.test.is_gpu_available():...: with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc....: x = tf.random_uniform([1000, 1000])...: assert x.device.endswith("GPU:0")...: time_matmul(x)...: On CPU: 608 μs ± 40.2 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 2019-12-09 11:33:50.419224: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2019-12-09 11:33:51.356242: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties: name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6575 pciBusID: 0000:0e:00.0 totalMemory: 11.00GiB freeMemory: 9.10GiB 2019-12-09 11:33:51.533039: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 1 with properties: name: GeForce GT 710 major: 3 minor: 5 memoryClockRate(GHz): 0.954 pciBusID: 0000:05:00.0 totalMemory: 2.00GiB freeMemory: 1.67GiB 2019-12-09 11:33:51.546476: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1461] Ignoring visible gpu device (device: 1, name: GeForce GT 710, pci bus id: 0000:05:00.0, compute capability: 3.5) with Cuda compute capability 3.5. The minimum required Cuda capability is 3.7. 2019-12-09 11:33:51.567213: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0 2019-12-09 11:33:53.149081: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-12-09 11:33:53.164773: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] 0 1 2019-12-09 11:33:53.172301: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0: N N 2019-12-09 11:33:53.182373: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 1: N N 2019-12-09 11:33:53.187598: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/device:GPU:0 with 8789 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:0e:00.0, compute capability: 6.1) 620 μs ± 48.5 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)In [2]:

不過,在我搞了半天之后,終于能使程序再pycharm上運(yùn)行了:

# -*- 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 """from timeit import timeit import tensorflow as tfdef func(x):tf.matmul(x, x)def time_matmul(x):print(timeit('func', 'from __main__ import func', number=1))# Force execution on CPU print("On CPU:") with tf.device("CPU:0"):x = tf.random_uniform([1000, 1000])assert x.device.endswith("CPU:0")time_matmul(x)# Force execution on GPU #0 if available if tf.test.is_gpu_available():with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.x = tf.random_uniform([1000, 1000])assert x.device.endswith("GPU:0")time_matmul(x)

結(jié)果:

D:\20191031_tensorflow_yolov3\python\python.exe D:/20191031_tensorflow_yolov3/needed/test_tensorflow-gpu/191208_test_Eager_execution_once_cls.py On CPU: 2019-12-10 09:56:02.601521: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 6.209648925078723e-07 2019-12-10 09:56:03.389699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties: name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6575 pciBusID: 0000:0e:00.0 totalMemory: 11.00GiB freeMemory: 9.10GiB 2019-12-10 09:56:03.537904: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 1 with properties: name: GeForce GT 710 major: 3 minor: 5 memoryClockRate(GHz): 0.954 pciBusID: 0000:05:00.0 totalMemory: 2.00GiB freeMemory: 1.67GiB 2019-12-10 09:56:03.538533: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1461] Ignoring visible gpu device (device: 1, name: GeForce GT 710, pci bus id: 0000:05:00.0, compute capability: 3.5) with Cuda compute capability 3.5. The minimum required Cuda capability is 3.7. 2019-12-10 09:56:03.539099: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0 2019-12-10 09:56:05.750966: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-12-10 09:56:05.751268: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] 0 1 2019-12-10 09:56:05.751452: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0: N N 2019-12-10 09:56:05.751652: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 1: N N 2019-12-10 09:56:05.752012: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/device:GPU:0 with 8789 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:0e:00.0, compute capability: 6.1) 9.314473379262722e-07Process finished with exit code 0

以上我是重新定義了一個(gè)func()函數(shù)了,但并未抓住問題的本質(zhì),以下才是:

# -*- 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 """from timeit import timeit import tensorflow as tfdef time_matmul(x):print(timeit('tf.matmul(x, x)', 'from __main__ import tf,x', number=1))# Force execution on CPU print("On CPU:") with tf.device("CPU:0"):x = tf.random_uniform([1000, 1000])assert x.device.endswith("CPU:0")time_matmul(x)# Force execution on GPU #0 if available if tf.test.is_gpu_available():with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.x = tf.random_uniform([1000, 1000])assert x.device.endswith("GPU:0")time_matmul(x)

結(jié)果:

D:\20191031_tensorflow_yolov3\python\python.exe D:/20191031_tensorflow_yolov3/needed/test_tensorflow-gpu/191208_test_Eager_execution_once_cls.py On CPU: 2019-12-10 10:45:10.788751: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 0.0006731259434785336 2019-12-10 10:45:11.520722: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties: name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6575 pciBusID: 0000:0e:00.0 totalMemory: 11.00GiB freeMemory: 9.10GiB 2019-12-10 10:45:11.647470: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 1 with properties: name: GeForce GT 710 major: 3 minor: 5 memoryClockRate(GHz): 0.954 pciBusID: 0000:05:00.0 totalMemory: 2.00GiB freeMemory: 1.67GiB 2019-12-10 10:45:11.648053: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1461] Ignoring visible gpu device (device: 1, name: GeForce GT 710, pci bus id: 0000:05:00.0, compute capability: 3.5) with Cuda compute capability 3.5. The minimum required Cuda capability is 3.7. 2019-12-10 10:45:11.648577: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0 2019-12-10 10:45:13.033777: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-12-10 10:45:13.034072: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] 0 1 2019-12-10 10:45:13.034252: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0: N N 2019-12-10 10:45:13.034427: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 1: N N 2019-12-10 10:45:13.034762: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/device:GPU:0 with 8789 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:0e:00.0, compute capability: 6.1) 0.0015999160455466566Process finished with exit code 0

坑3

數(shù)據(jù)集

演示如何使用tf.data.Dataset API構(gòu)建pipelines以將數(shù)據(jù)提供給模型。它涵蓋:

  • 創(chuàng)建數(shù)據(jù)集。
  • 在啟用了eager execution的情況下對數(shù)據(jù)集進(jìn)行迭代。
    我們建議使用數(shù)據(jù)集API從簡單,可重復(fù)使用的部分構(gòu)建高性能,復(fù)雜的pipelines,這些部分將為模型的訓(xùn)練或評估循環(huán)提供支持。

如果您熟悉TensorFlow圖,則在啟用eager execution時(shí),構(gòu)建數(shù)據(jù)集對象的API保持完全相同,但迭代數(shù)據(jù)集元素的過程稍微簡單一些。您可以對tf.data.Dataset對象使用Python迭代,而不需要顯式創(chuàng)建tf.data.Iterator對象。因此,在啟用eager執(zhí)行時(shí),TensorFlow指南中對迭代器的討論無關(guān)緊要。

(1)創(chuàng)建源數(shù)據(jù)集

使用其中一個(gè)工廠函數(shù)(如Dataset.from_tensors,Dataset.from_tensor_slices)或使用從TextLineDataset或TFRecordDataset等文件讀取的對象創(chuàng)建源數(shù)據(jù)集。

# -*- 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 tftf.enable_eager_execution()ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6]) # print(type(ds_tensors)) # <class 'tensorflow.python.data.ops.dataset_ops.TensorSliceDataset'> # print(ds_tensors) # TensorSliceDataset shapes: (), types: tf.int32># Create a CSV file import tempfile# tempfile.mkstemp()返回os.open返回的文件描述符和文件名 _, filename = tempfile.mkstemp(dir='./') # print(_) # 3 # print(filename) # D:\20191031_tensorflow_yolov3\needed\test_tensorflow-gpu\tmpjrwgapybwith open(filename, 'w') as f:f.write("""Line 1 Line 2 Line 3""")ds_file = tf.data.TextLineDataset(filename)with open(filename, 'r') as f:print(f.read()) # Line 1 # Line 2 # Line 3
(2)應(yīng)用transformations

使用map,batch,shuffle等轉(zhuǎn)換函數(shù)將轉(zhuǎn)換應(yīng)用于數(shù)據(jù)集的記錄。(啥意思??)

ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2)ds_file = ds_file.batch(2)
(3)迭代

啟用eager執(zhí)行時(shí),Dataset對象支持迭代。如果您熟悉TensorFlow圖中數(shù)據(jù)集的使用,請注意不需要調(diào)用Dataset.make_one_shot_iterator()或get_next()。

print('Elements of ds_tensors:') for x in ds_tensors:print(x) # Elements of ds_tensors: # tf.Tensor([4 9], shape=(2,), dtype=int32) # tf.Tensor([16 25], shape=(2,), dtype=int32) # tf.Tensor([ 1 36], shape=(2,), dtype=int32)print('\nElements in ds_file:') for x in ds_file:print(x) # Elements in ds_file: # tf.Tensor([b'Line 1 ' b'Line 2'], shape=(2,), dtype=string) # tf.Tensor([b'Line 3'], shape=(1,), dtype=string)

引用文章:tensorflow之Eager execution基礎(chǔ)

總結(jié)

以上是生活随笔為你收集整理的tensorflow教程 学习笔记 之 Eager execution 急切执行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。