步履不停:TensorFlow 2.4新功能一覽!

2021-01-15 TechWeb

TensorFlow 2.4 正式發布!隨著對分布式訓練和混合精度提供更多支持,加入新的 Numpy 前端及用於監控和診斷性能瓶頸的工具,這個版本的亮點在於推出新功能,以及對性能和擴展方面的增強。

tf.distribute 的新增功能

參數伺服器策略

在版本 2.4 中,實驗性引入了 tf.distribute 模塊的支持,可通過 ParameterServerStrategy 和自定義訓練循環對 Keras 模型進行異步訓練。與 MultiWorkerMirroredStrategy 一樣,ParameterServerStrategy 是一種多工作器數據並行策略;但其梯度更新方式為異步執行。

 ParameterServerStrategy

        https://tensorflow.google.cn/api_docs/python/tf/distribute/experimental/ParameterServerStrategy

參數伺服器訓練集群包含工作節點和參數伺服器。系統會在參數伺服器上創建變量,然後工作節點會在每個步驟中進行讀取和更新。變量的讀取和更新會在各工作節點上獨立進行,同時無需採取任何同步操作。由於工作節點互不依賴,因此該策略具有工作器容錯的優勢,並會在使用搶佔式伺服器時有所助益。

如要開始使用此策略,請查閱參數伺服器訓練教程。此教程介紹了如何設置 ParameterServerStrategy,並說明了如何使用 ClusterCoordinator 類來創建資源、調度函數和處理任務失敗。

    參數伺服器訓練教程

           https://tensorflow.google.cn/tutorials/distribute/parameter_server_training

    ClusterCoordinator

           https://tensorflow.google.cn/api_docs/python/tf/distribute/experimental/coordinator/ClusterCoordinator

多工作節點鏡像策略

MultiWorkerMirroredStrategy 多工作節點鏡像策略   已順利度過實驗階段,現已成為穩定 API 的組成部分。與單個工作節點副本 MirroredStrategy 一樣,MultiWorkerMirroredStrategy 通過同步數據並行化實現分布式訓練。但利用 MultiWorkerMirroredStrategy,您可以在多臺機器上進行訓練,且每臺機器可以都搭載多個 GPU。

 MultiWorkerMirroredStrategy

         https://tensorflow.google.cn/api_docs/python/tf/distribute/MultiWorkerMirroredStrategy

 MirroredStrategy

         https://tensorflow.google.cn/api_docs/python/tf/distribute/MirroredStrategy

在同步訓練中,每個工作節點會在輸入數據的不同片段上計算正向和反向傳遞次數,並且在每個步驟結束時匯總梯度。對於這種稱為 All Reduce 的匯總, MultiWorkerMirroredStrategy 會使用集合運算保持變量同步。集合運算是 TensorFlow 圖表中的單個算子,可以根據硬體、網絡拓撲和張量大小在 TensorFlow 運行時中自動選擇 All Reduce 算法。集合運算還可實現其他集合運算,例如廣播和 All Gather。

 集合運算

        https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/collective_ops.py

如要開始使用  MultiWorkerMirroredStrategy,請查閱使用 Keras 進行多工作器訓練教程,該教程已更新了有關數據集分片、保存/加載使用分布策略訓練的模型,以及使用 BackupAndRestore 回調進行故障恢復的詳細信息。

 使用 Keras 進行多工作器訓練

        https://tensorflow.google.cn/tutorials/distribute/multi_worker_with_keras

 BackupAndRestore

        https://tensorflow.google.cn/api_docs/python/tf/keras/callbacks/experimental/BackupAndRestore

如果您不熟悉分布式訓練,並希望了解入門方法,或者有興趣在 Google 雲端平臺 (GCP) 上進行分布式訓練,請參閱本博文,以獲取關於關鍵概念和步驟的介紹。

Keras 的相關更新

混合精度

在 TensorFlow 2.4 中,Keras 混合精度 API 已順利度過實驗階段,現已成為穩定的 API。大多數 TensorFlow 模型使用的是 float32 dtype;但也存在使用更少內存的低精度類型(如 float16)。混合精度指在同一模型中通過使用 16 位和 32 位浮點類型,以加快訓練速度。該 API 可使模型在 GPU 上性能提高 3 倍,在 TPU 上提高 60%。

 Keras 混合精度 API

        https://tensorflow.google.cn/api_docs/python/tf/keras/mixed_precision

如要使用混合精度 API,您必須使用 Keras 層和優化工具,但無需使用其他 Keras 類,例如模型或損失。如果您對如何利用此 API 實現性能優化頗有興趣,請查閱混合精度教程。

 混合精度教程

        https://tensorflow.google.cn/guide/mixed_precision

優化工具

此版本支持重構 tf.keras.optimizers.Optimizer 類,使 model.fit 或自定義訓練循環的用戶能夠編寫任何適用於優化工具的訓練代碼。現所有內置的 tf.keras.optimizer.Optimizer 子類均可支持使用 gradient_transformers 和 gradient_aggregator 參數,您可藉此輕鬆定義自定義梯度轉換。

 tf.keras.optimizers.Optimizer

        https://tensorflow.google.cn/api_docs/python/tf/keras/optimizers/Optimizer

通過重構,您現在可以在編寫自定義訓練循環時將損失張量直接傳遞給 Optimizer.minimize:

tape = tf.GradientTape()  with tape:    y_pred = model(x, training=True)    loss = loss_fn(y_pred, y_true)  # 如下所示,在使用損失「張量」時,您可以在「tf.GradientTape」中進行傳遞。  optimizer.minimize(loss, model.trainable_variables, tapetape=tape) 

此類更改旨在使 Model.fit 和自定義訓練循環都能擺脫優化工具細節的限制,從而使您無需修改,即可編寫任何適用於優化工具的訓練代碼。

函數式 API 模型構建的內部改進

最後,在 Keras 中,TensorFlow 2.4 可支持對 Keras Functional API 內部主要結構的重構,從而可降低函數式模型構建的內存消耗並簡化觸發邏輯。開展此類重構操作還能夠確保 TensorFlowOpLayers 行為可預測,並可與 CompositeTensor 類型的籤名一起使用。

隆重推出 tf.experimental.numpy

TensorFlow 2.4 以 tf.experimental.numpy 形式,實驗性引入了對 NumPy API 子集的支持。您可藉此模塊,運行由 TensorFlow 加速的 NumPy 代碼。由於此 API 基於 TensorFlow 構建而成,因此可支持訪問所有 TensorFlow API,與 TensorFlow 實現無縫互操作,並會通過編譯和自動矢量化開展優化。例如,TensorFlow ND 數組可以與 NumPy 函數進行交互,同樣地,TensorFlow NumPy 函數也可以接受包括 tf.Tensor 和 np.ndarray 在內的不同類型的輸入。

import tensorflow.experimental.numpy as tnp ```  # 在輸入流水線中使用 NumPy 代碼  dataset = tf.data.Dataset.from_tensor_slices(      tnp.random.randn(1000, 1024)).map(      lambda z: z.clip(-1,1)).batch(100)  # 通過 NumPy 代碼計算梯度  def grad(x, wt):    with tf.GradientTape() as tape:      tape.watch(wt)      output = tnp.dot(x, wt)      output = tf.sigmoid(output)    return tape.gradient(tnp.sum(output), wt)   tf.experimental.numpy

        https://tensorflow.google.cn/api_docs/python/tf/experimental/numpy

 NumPy API 實驗性支持

        https://github.com/tensorflow/community/blob/master/governance/api-reviews.md#experimental-apis

您可以查閱 TensorFlow 指南上的 NumPy API,了解更多關於使用此 API 的信息。

 TensorFlow 指南上的 NumPy API

        https://tensorflow.google.cn/guide/tf_numpy

全新性能分析器工具

TensorFlow Profiler 中的多工作器支持

TensorFlow Profiler 是一套用於評估 TensorFlow 模型訓練性能和資源消耗情況的工具。TensorFlow Profiler 可幫助您了解模型中算子的硬體資源消耗、診斷瓶頸並最終加快訓練速度。

 TensorFlow Profiler 

         https://tensorflow.google.cn/guide/profiler

之前版本的TensorFlow Profiler 支持監控多 GPU、單主機訓練作業。在現在 2.4 版本中,您可以分析 MultiWorkerMirroredStrategy 訓練作業的性能。例如,您可以使用採樣模型 API 來執行按需分析,並連接到 MultiWorkerMirroredStrategy 工作節點上正在使用的同一伺服器埠: 

# 在模型運行之前啟動性能分析器伺服器。  tf.profiler.experimental.server.start(6009)  # 在此處插入模型代碼……  # 例如,您的工作器 IP 地址是 10.0.0.2、10.0.0.3、10.0.0.4,然後您  # 希望執行 2 秒鐘的性能分析。性能分析數據將  # 保存至 Google Cloud Storage 路徑「your_tb_logdir」。  tf.profiler.experimental.client.trace(      'grpc://10.0.0.2:6009,grpc://10.0.0.3:6009,grpc://10.0.0.4:6009',      'gs://your_tb_logdir',      2000)   採樣模型

        https://tensorflow.google.cn/guide/profiler#sampling_mode

或者,您可以通過向 Capture Profile(捕獲分析結果)工具提供工作節點地址來使用 TensorBoard 配置文件插件。

分析完成後,您可以使用新的 Pod Viewer 工具選擇一個訓練步驟,並查閱所有工作節點的分步時間類別細分。

 Pod Viewer 工具

        https://tensorflow.google.cn/guide/profiler#pod_viewer

有關如何使用 TensorFlow Profiler 的更多信息,請查閱新發布的 GPU 性能指南。此指南介紹了您在對模型訓練作業進行性能分析時可能遇到的常見情況,並提供了調試工作流程來幫助您優化性能,無論您是使用單個 GPU、多個 GPU 還是使用多臺機器進行訓練,均可從中受益。

 GPU 性能指南

        https://tensorflow.google.cn/guide/gpu_performance_analysis

TFLite Profiler

在 2.4 版本中,您亦可在 Android 中啟用對 TFLite 內部結構的跟蹤。現在,您可以使用 Android 版 TFLite Profiler 來識別性能瓶頸。TFLite 性能評估指南介紹了如何使用 Android Studio CPU 性能分析器和系統跟蹤應用添加跟蹤事件,啟用 TFLite 跟蹤以及捕獲跟蹤。

使用 Android 系統跟蹤應用進行跟蹤的示例

 TFLite 性能評估指南

        https://tensorflow.google.cn/lite/performance/measurement#trace_tensorflow_lite_internals_in_android

提供 GPU 支持的新功能

TensorFlow 2.4 可與 CUDA 11 和 cuDNN 8 一起運行,以支持最新上市的 NVIDIA Ampere GPU 架構。如需了解 CUDA 11 功能的更多信息,請查閱此 NVIDIA 開發者博客。

 NVIDIA 開發者博客

        https://developer.nvidia.com/blog/cuda-11-features-revealed/

此外,我們亦會默認在搭載 Ampere 的 GPU 上啟用對 TensorFloat-32 的支持。TensorFloat-32(簡稱為「TF32」)是 NVIDIA Ampere GPU 的一種數學模式,可加快令某些 float32 算子(例如矩陣乘法和卷積)在 Ampere GPU 上的運行速度,但精度降低。如需了解更多信息,請查閱 tf.config.experimental.enable_tensor_float_32_execution 文檔。

 tf.config.experimental.enable_tensor_float_32_execution

        https://tensorflow.google.cn/api_docs/python/tf/config/experimental/enable_tensor_float_32_execution

 

相關焦點

  • TensorFlow 攜手 NVIDIA,使用 TensorRT 優化 TensorFlow Serving...
    HTTP/REST API at:localhost:8501 …$ curl -o /tmp/resnet/resnet_client.py https://raw.githubusercontent.com/tensorflow/serving/master/tensorflow_serving/example/resnet_client.py
  • TensorFlow 2.4來了:上線對分布式訓練和混合精度的新功能支持
    TensorFlow 2.4 的更新包括對於分布式訓練和混合精度的新功能支持,對 NumPy API 子集的試驗性支持以及一些用於監測性能瓶頸的新工具。根據 TensorFlow 官方博客,本次更新的主要內容整理如下:tf.distribute 中的新功能參數伺服器策略在 TensorFlow 2.4 中,tf.distribute 模塊引入了對使用 ParameterServerStrategy 和自定義訓練循環進行模型異步訓練的試驗性支持
  • TensorFlow極速入門
    最後給出了在 tensorflow 中建立一個機器學習模型步驟,並用一個手寫數字識別的例子進行演示。1、tensorflow是什麼?tensorflow 是 google 開源的機器學習工具,在2015年11月其實現正式開源,開源協議Apache 2.0。
  • tensorflow初級必學算子
    在之前的文章中介紹過,tensorflow框架的核心是將各式各樣的神經網絡抽象為一個有向無環圖,圖是由tensor以及tensor變換構成;雖然現在有很多高階API可以讓開發者忽略這層抽象,但對於靈活度要求比較高的算法仍然需要開發者自定義網絡圖,所以建議開發者儘量先學習tf1.x
  • Tensorflow基礎教程15天之創建Tensor
    Tensor是Tensorflow中使用在計算圖中的最基本的數據單位,我們可以聲明Tensor為variable,或者為Tensor提供placeholer。但首先我們必須知道如何創建Tensor。在將Tensor定義為Variable之後,Tensorflow才會將其傳入計算圖。如何操作我們將在這裡介紹創建Tensor的主要方法。
  • TensorFlow 中文資源全集,學習路徑推薦
    https://gitee.com/fendouai/Awesome-TensorFlow-Chinese很多內容下面這個英文項目:Inspired by https://github.com/jtoy/awesome-tensorflow官方網站官網:https://www.tensorflow.org/中文:https://tensorflow.google.cn
  • 終於來了,TensorFlow 新增官方 Windows 支持
    現在你可以使用命令 C:\> pip install tensorflow 安裝 TensorFlow 了。GPU 支持的命令:C:\> pip install tensorflow-gpu有關 TensorFlow Windows 支持的更多細節請閱讀 r0.12 的版本注釋。
  • 玩轉TensorFlow?你需要知道這30功能
    地址是:tensorflow.org/tfx/?網址是:https://www.tensorflow.org/tfx/transform/?網址是:https://www.tensorflow.org/serving/?
  • 在Windows中安裝Tensorflow和Kears深度學習框架
    在命令提示符窗口輸入下列命令: 建立Tensorflow Anaconda虛擬環境 conda create --name tensorflow python=3.5 anaconda 執行後屏界面顯示如圖3-9所示。
  • 5個簡單的步驟掌握Tensorflow的Tensor
    在這篇文章中,我們將深入研究Tensorflow Tensor的細節。我們將在以下五個簡單步驟中介紹與Tensorflow的Tensor中相關的所有主題:第一步:張量的定義→什麼是張量?我們經常將NumPy與TensorFlow一起使用,因此我們還可以使用以下行導入NumPy:import tensorflow as tfimport numpy as np張量的創建:創建張量對象有幾種方法可以創建tf.Tensor對象。讓我們從幾個例子開始。
  • TensorFlow 2.0開源工具書,30天「無痛」上手
    開源電子書地址:https://lyhue1991.github.io/eat_tensorflow2_in_30_days/GitHub 項目地址:https://github.com/lyhue1991/eat_tensorflow2_in_30_days為什麼一定要學
  • 深度解讀TensorFlow,了解它的最新發展!
    Tensorboard是tensorflow內置的一個可視化工具,它通過將tensorflow程序輸出的日誌文件的信息可視化,使得tensorflow程序的理解、調試和優化更加簡單高效。Tensorboard的可視化依賴於tensorflow程序運行輸出的日誌文件,因而tensorboard和tensorflow程序在不同的進程中運行。
  • Tensorflow 2.0 即將入場
    通過閱讀這篇文章,熟悉Tensorflow的老用戶可以儘早轉變思維,適應新版本的變化。而新手也可以直接以Tensorflow 2.0的方式思考,至少目前沒有必要急著去學習別的框架。Tensorflow 2.0的開發初衷是製作一個更簡單易用的Tensorflow。
  • 最簡單的深度學習TensorFlow應用舉例!
    小編我的電腦很一般,沒有32G內存,也沒有1080,就windows上直接裝了23333windows+python 3.6+pycharm+tensorflow cpu話不多說,直接線性回歸,上圖。代碼截圖#接下來貼代碼#辰星樹洞import numpy as np #這是Python的一種開源的數值計算擴展,非常強大import tensorflow
  • Tensorflow 全網最全學習資料匯總之Tensorflow 的入門與安裝【2】
    《TensorFlow學習筆記1:入門》連結:http://www.jeyzhang.com/tensorflow-learning-notes.html本文與上一篇的行文思路基本一致,首先概括了TensorFlow的特性,然後介紹了graph、session、variable 等基本概念的含義,以具體代碼的形式針對每個概念給出了進一步的解釋
  • 基於RTX2060構建TensorFlow-gpu(keras)學習平臺
    開始菜單運行anaconda navigator檢查是否安裝了notebook(默認有安裝)三、安裝tensorflow/keras在激活的環境中安裝:1. 如果機器上有gpu,則安裝gpu版本,沒有GPU就安裝cpu版。
  • 如何使用TensorFlow Hub的ESRGAN模型來在安卓app中生成超分圖片
    file_path=org%2Ftensorflow%2Ftensorflow-lite%2F2.3.0%2Ftensorflow-lite-2.3.0.aar" dest "${project.rootDir}/libraries/tensorflow-lite-2.3.0.aar" overwrite false retries 5 } } task downloadTFLiteGPUDelegateAARFile
  • TensorFlow極簡教程:創建、保存和恢復機器學習模型
    繼續之前,也可以閱讀這個 Tensorflow 小入門:https://blog.metaflow.fr/tensorflow-a-primer-4b3fa0978be3#.wxlmweb8h你有必要了解這些信息,因為了解如何保存不同級別的代碼是非常重要的,這可以避免混亂無序。
  • 機器學習中的embedding原理及tensorflow 相關API的理解
    # 概述本文主要講解tensorflow中涉及embedding的API。之前看了一些文章,寫的雲山霧繞,花了好長時間才搞懂,太笨了。embedding 算法主要用於處理稀疏特徵,應用於NLP、推薦、廣告等領域。所以word2vec 只是embbeding 思想的一個應用,而不是全部。
  • 分享TensorFlow Lite應用案例
    不支持的 op 主要集中有兩大類情況:   包括控制流 (control flow) 的 op   相對於 TF mobile,TF Lite 的部分 op 只支持最簡單的 case   目前的一個好的消息就是 TensorFlow 項目組一直在持續的推進對 RNN 系列的支持。   3.