選自TensorFlow Blog
機器之心編譯
編輯:小舟、蛋醬
tape = tf.GradientTape()with tape: y_pred = model(x, training=True) loss = loss_fn(y_pred, y_true)
optimizer.minimize(loss, model.trainable_variables, tape=tape)這些更新的目標是讓 Model.fit 和自定義訓練循環與優化器細節更加不相關,從而讓使用者無需修改即可編寫出與任何優化器共同使用的訓練代碼。最後,TensorFlow 2.4 的更新還包括 Keras Functional API 內部的重構,改善了函數式模型構造所產生的內存消耗並簡化了觸發邏輯。這種重構可以保證 TensorFlowOpLayers 的行為可預測,並且可以使用 CompositeTensor 類型籤名(type signature)。TensorFlow 2.4 引入了對 NumPy API 子集的試驗性支持。該模塊可以運行由 TensorFlow 加速的 NumPy 代碼,由於這一 API 是基於 TensorFlow 構建的,因此可與 TensorFlow 無縫銜接,允許訪問所有 TensorFlow API 並通過編譯和自動矢量化提供優化後的運行。例如,TensorFlow ND 數組可以與 NumPy 函數互通,類似地,TensorFlow NumPy 函數可以接受包括 tf.Tensor 和 np.ndarray 在內的不同類型輸入。import tensorflow.experimental.numpy as tnp# Use NumPy code in input pipelines
dataset = tf.data.Dataset.from_tensor_slices( tnp.random.randn(1000, 1024)).map(lambda z: z.clip(-1,1)).batch(100)# Compute gradients through NumPy codedef 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)TensorFlow Profiler 是度量 TensorFlow 模型的訓練性能和資源消耗情況的工具,用來診斷性能瓶頸,最終加快訓練速度。此前,TensorFlow Profiler 支持多 GPU 單主機訓練。到了 2.4 版本,使用者可以測試 MultiWorkerMirroredStrategy 的訓練工作了,比如使用採樣模式 API 按需配置,並連接到 MultiWorkerMirroredStrategy 工作器正在使用的同一伺服器。# Start a profiler server before your model runs.
tf.profiler.experimental.server.start(6009)# Model code goes here....# E.g. your worker IP addresses are 10.0.0.2, 10.0.0.3, 10.0.0.4, and you# would like to profile for a duration of 2 seconds. The profiling data will# be saved to the Google Cloud Storage path 「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)另外,你可以通過向捕獲配置文件工具提供工作器地址來使用 TensorBoard 配置文件插件。配置之後,你可以使用新的 Pod Viewer tool 來選擇訓練步驟,並查看所有工作器上該步驟的 step-time 的細分。TFLite Profiler 則支持在 Android 中跟蹤 TFLite 內部信息,以識別性能瓶頸。TensorFlow 2.4 與 CUDA 11 和 cuDNN 8 配合運行,支持最新發布的英偉達安培架構 GPU,對於 CUDA 11 的新特性,可以參考英偉達開發者博客:https://developer.nvidia.com/blog/cuda-11-features-revealed/在新版本中,默認情況下會啟用安培 GPU 的新特性——對 TensorFloat-32 的支持。TensorFloat-32 又簡稱為 TF32,是英偉達 Ampere GPU 的一種數學精度模式,可導致某些 float32 運算(如矩陣乘法和卷積)在安培架構 GPU 上運行得更快,但精度略有降低。https://www.tensorflow.org/api_docs/python/tf/config/experimental/enable_tensor_float_32_execution「三小時AI開發進階」公開課上線!本周四(12月17日)20:00,百度高級研發工程師可樂老師將在第一課《小目標檢測技術詳解》中介紹:技術講解:小目標檢測場景定義、難點分析與相應算法講解現場實戰:基於EasyDL完成物體檢測模型開發與部署掃碼進群聽課,還有機會贏取100元京東卡、《智能經濟》實體書、限量百度滑鼠墊多重好獎!© THE END
轉載請聯繫本公眾號獲得授權
投稿或尋求報導:content@jiqizhixin.com