TensorFlow 2.7正式版上線,支持Jax模型到TensorFlow Lite轉換
「調試代碼(debug)是框架用戶體驗的關鍵部分,更輕松的調試意味著更快的迭代周期。在此版本中,我們通過三個主要更改對 TF/Keras 調試體驗進行了廣泛的重新設計,使其更高效、更有趣……」谷歌科學家,Keras 發(fā)明者 François Chollet 說道。
11 月 5 日,TensorFlow 官方發(fā)布了 2.7 正式版,距離上次 2.6.1 的更新剛過去一個月時間。

在 TensorFlow 博客上,官方簡述了新版本的主要升級內容:
TensorFlow 2.7 主要變化
tf.keras:
- Model.fit()、 Model.predict() 和 Model.evaluate()方法將不再把 (batch_size, ) 的輸入數(shù)據(jù)上升為 (batch_size, 1)。這使得 Model 子類能夠在其 train_step()/ test_step()/ predict_step()方法中處理標量數(shù)據(jù);
- Model.to_yaml()和 keras.models.model_from_yaml 方法已被替換為引發(fā) RuntimeError,因為它們可能被濫用以導致任意代碼執(zhí)行。建議使用 JSON 序列化而不是 YAML,或者,一個更好的替代方案,序列化到 H5;
- LinearModel 和 WideDeepModel 被移至 tf.compat.v1.keras.models. 命名空間( tf.compat.v1.keras.models.LinearModel 和 tf.compat.v1.keras.models.WideDeepModel),其 experimental 端點 tf.keras.experimental.models.LinearModel 和 tf.keras.experimental.models.WideDeepModel 被棄用;
- 所有 tf.keras.initializers 類的 RNG 行為改變,這一變化將使初始化行為在 v1 和 v2 之間保持一致。
tf.lite:
- 重命名 schema 中的 SignatureDef 表,以最大化與 TF SavedModel Signature 概念的奇偶校驗。
- 棄用 Makefile 構建,Makefile 用戶需要將他們的構建遷移到 CMake 或 Bazel。
- 棄用 tflite::OpResolver::GetDelegates。TfLite 的 BuiltinOpResolver::GetDelegates 所返回的列表現(xiàn)在總是空的。相反,建議使用新方法 tflite::OpResolver::GetDelegateCreators。
TF Core:
- tf.Graph.get_name_scope() 現(xiàn)在總是返回一個字符串。之前當在 name_scope("") 或 name_scope(None) 上下文中調用時,它返回 None, 現(xiàn)在它返回空字符串;
- tensorflow/core/ir/ 包含一個新的基于 MLIR 的 Graph dialect,它與 GraphDef 同構,并將用于替換基于 GraphDef(例如 Grappler)的優(yōu)化;
- 棄用并刪除了形狀推理中的 attrs() 函數(shù),現(xiàn)在所有的屬性都應該通過名字來查詢。
- 以下 Python 符號是在 TensorFlow 的早期版本中意外添加的,現(xiàn)在已被刪除。每個符號都有一個替換項,但請注意替換項的參數(shù)名稱是不同的:
-
- tf.quantize_and_dequantize_v4(在 TensorFlow 2.4 中意外引入):改用 tf.quantization.quantize_and_dequantize_v2;
- tf.batch_mat_mul_v3(在 TensorFlow 2.6 中意外引入):改用 tf.linalg.matmul;
- tf.sparse_segment_sum_grad(在 TensorFlow 2.6 中意外引入):改用 tf.raw_ops.SparseSegmentSumGrad。
-
將 tensorflow::int64 重命名為 int_64_t(前者是后者的別名)。
模塊化文件系統(tǒng)的遷移:
對 S3 和 HDFS 文件系統(tǒng)的支持已經(jīng)遷移到一個基于模塊化文件系統(tǒng)的方法,現(xiàn)在可以在 https://github.com/tensorflow/io 中使用。用戶需要安裝 tensorflow-io python 包,以支持 S3 和 HDFS。
TensorFlow 2.7 主要功能和改進
對 TensorFlow 調試經(jīng)驗的改進:以前,TensorFlow 錯誤堆棧跟蹤涉及許多內部幀,讀出這些幀可能具有挑戰(zhàn)性,而且對最終用戶而言不可操作。從 TF 2.7 開始,TensorFlow 在它引發(fā)的大多數(shù)錯誤中過濾內部幀,以保持堆棧跟蹤簡短、可讀,并專注于最終用戶可操作的內容。
通過在每個異常中添加傳遞給該層的參數(shù)值的完整列表,提高由 Keras Layer.__call__()引發(fā)的錯誤消息信息量。
tf.data:tf.data 服務現(xiàn)在支持自動分片(auto-sharding)。用戶通過 tf.data.experimental.service.ShardingPolicy 枚舉指定分片策略;tf.data.experimental.service.register_dataset 現(xiàn)在接受可選的 compression 參數(shù)。
Keras:tf.keras.layers.Conv 現(xiàn)在包含一個公共的 convolution_op 方法。此方法可用于簡化 Conv 子類的實現(xiàn),有兩種方式使用這個新方法,第一種方法如下:
- class StandardizedConv2D(tf.keras.layers.Conv2D):
- def call(self, inputs):
- mean, var = tf.nn.moments(self.kernel, axes=[0, 1, 2], keepdims=True)
- return self.convolution_op(inputs, (self.kernel - mean) / tf.sqrt(var + 1e-10))
你也可以采用如下方法:
- class StandardizedConv2D(tf.keras.Layer):
- def convolution_op(self, inputs, kernel):
- mean, var = tf.nn.moments(kernel, axes=[0, 1, 2], keepdims=True)
- # Author code uses std + 1e-5
- return super().convolution_op(inputs, (kernel - mean) / tf.sqrt(var + 1e-10))
- 向 tf.keras.metrics.Metric 添加了 merge_state() 方法以用于分布式計算;
- 向 tf.keras.layers.TextVectorization 添加了 sparse 和 ragged 的選項,以允許來自層的 SparseTensor 和 RaggedTensor 輸出。
tf.lite:添加 experimental API Experiment_from_jax 以支持從 Jax 模型到 TensorFlow Lite 的轉換;支持 uint32 數(shù)據(jù)類型;添加實驗量化調試器 tf.lite.QuantizationDebugger。
更多詳情可查看:
https://github.com/tensorflow/tensorflow/releases/tag/v2.7.0