TensorFlow2.7の新機能

目次

1. tf.function エラーメッセージの改善
2. tf.experimental.ExtensionTypeのリリース
3. TF2の移行が容易になりました
4. 新しいコミュニティがTensorFlow Hubでモデルを提供

関連記事:https://data-analysis-stats.jp/深属学習/tensorflow-2-0-主な変更

 

TensorFlowは2021年11月10日にリリースされました。このリリースでは、エラーメッセージが明確になり、スタックトレースが簡素化されて使いやすさが向上し、TF2に移行するユーザー向けの新しいツールとドキュメントが追加されています。

 

今回はTensorFlow2.7の新機能を紹介したいと思います。

詳細は下記のパスのご参照ください。

https://blog.tensorflow.org/2021/11/whats-new-in-tensorflow-27.html

 

1. tf.function エラーメッセージの改善

スタックトレースがより単純かつ短くなり、コードの問題を理解して修正することが容易になります。

レイヤーをデバッグするときに最初に行うことは、入力の形状とdtype、およびトレーニング引数とマスク引数の値を出力することです。 この情報を、カスタムKerasレイヤーから発生するすべてのスタックトレースに自動的に追加するようになりました。

エラーメッセージは、フレームワークが期待したこと、フレームワークの期待と一致しなかったことを示し、問題を修正するためのヒントを提供する必要があります。

主な違いは、tf.functionの実行中に発生するランタイムエラーに、エラーの原因を示すスタックトレースがユーザーのコードに含まれるようになったことです。

 

# … Python stack trace of the function call …

TypeError: Originated from a graph execution error.

The graph execution error is detected at a node built at (most recent call last):

# … Stack trace of the error within the function …

>>>  File <ipython-input-5-95ca3a98778f>, line 6, in leaky_function

# … More stack trace of the error within the function …

Error detected in node ‘add’ defined at: File “<ipython-input-5-95ca3a98778f>”, line 6, in leaky_function

TypeError: tf.Graph captured an external symbolic tensor. The symbolic tensor ‘add:0’ created by node ‘add’ is captured by the tf.Graph being executed as an input. But a tf.Graph is not allowed to take symbolic tensors from another graph as its inputs. Make sure all captured inputs of the executing tf.Graph are not symbolic tensors. Use return values, explicit Python locals or TensorFlow collections to access it. Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.

 

 

過去エラーメッセージ

# … Python stack trace of the function call …

InvalidArgumentError:  slice index 20 of dimension 0 out of bounds.

[[node strided_slice (defined at <‘ipython-input-8-250c76a76c0e’>:5) ]] [Op:__inference_f_75]

Errors may have originated from an input operation.

Input Source operations connected to node strided_slice:

range (defined at <ipython-input-8-250c76a76c0e >’:4)

Function call stack:

f

新しいエラーメッセージ

# … Python stack trace of the function call …

InvalidArgumentError:  slice index 20 of dimension 0 out of bounds.

[[node strided_slice

(defined at <ipython-input-3-250c76a76c0e>:5)

]] [Op:__inference_f_15]

Errors may have originated from an input operation.

Input Source operations connected to node strided_slice:

In[0] range (defined at <ipython-input-3-250c76a76c0e>:4)

In[1] strided_slice/stack:

In[2] strided_slice/stack_1:

In[3] strided_slice/stack_2:

Operation defined at: (most recent call last)

# … Stack trace of the error within the function …

>>>   File “<ipython-input-3-250c76a76c0e>”, line 7, in <module>

>>>     f()

>>>   File “<ipython-input-3-250c76a76c0e>”, line 5, in f

>>>     return l[20]

 

 

過去エラーメッセージ

# … Python stack trace of the function call …

TypeError: An op outside of the function building code is being passed

a “Graph” tensor. It is possible to have Graph tensors

leak out of the function building context by including a

tf.init_scope in your function building code.

For example, the following function will fail:

@tf.function

def has_init_scope():

my_constant = tf.constant(1.)

with tf.init_scope():

added = my_constant * 2

The graph tensor has name: add:0

新しいエラーメッセージ

# … Python stack trace of the function call …

TypeError: Originated from a graph execution error.

The graph execution error is detected at a node built at (most recent call last):

# … Stack trace of the error within the function …

>>>  File <ipython-input-5-95ca3a98778f>, line 6, in leaky_function

# … More stack trace of the error within the function …

Error detected in node ‘add’ defined at: File “<ipython-input-5-95ca3a98778f>”, line 6, in leaky_function

TypeError: tf.Graph captured an external symbolic tensor. The symbolic tensor ‘add:0’ created by node ‘add’ is captured by the tf.Graph being executed as an input. But a tf.Graph is not allowed to take symbolic tensors from another graph as its inputs. Make sure all captured inputs of the executing tf.Graph are not symbolic tensors. Use return values, explicit Python locals or TensorFlow collections to access it. Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.

 

2. tf.experimental.ExtensionTypeのリリース

ExtensionType APIを使用して、ユーザー定義のオブジェクト指向型を作成できます。ユーザー定義型を使用すると、プロジェクトをより読みやすく、モジュール式で、保守しやすくすることができます。

 

class TensorGraph(tf.experimental.ExtensionType):

“””A collection of labeled nodes connected by weighted edges.”””

edge_weights: tf.Tensor                      # shape=[num_nodes, num_nodes]

node_labels: typing.Mapping[str, tf.Tensor]  # shape=[num_nodes]; dtype=any

 

class MaskedTensor(tf.experimental.ExtensionType):

“””A tensor paired with a boolean mask, indicating which values are valid.”””

values: tf.Tensor

mask: tf.Tensor       # shape=values.shape; false for missing/invalid values.

 

class CSRSparseMatrix(tf.experimental.ExtensionType):

“””Compressed sparse row matrix (https://en.wikipedia.org/wiki/Sparse_matrix).”””

values: tf.Tensor     # shape=[num_nonzero]; dtype=any

col_index: tf.Tensor  # shape=[num_nonzero]; dtype=int64

row_index: tf.Tensor  # shape=[num_rows+1]; dtype=int64

 

拡張タイプは、次のTensorFlowAPIでサポートされています。

 

Keras: Kerasモデルとレイヤーの入力と出力として使用できます。

Dataset:データセットに含めることができ、データセットイテレータによって返されます。

TensorFlowハブ:tf.hubモジュールの入力と出力として使用できます。

SavedModel: SavedModel関数の入力および出力として使用できます。

tf.function: @ tf.functionデコレータでラップされた関数の引数および戻り値として使用できます。

制御フロー:tf.condやtf.while_loopなどの制御フロー操作で使用できます。これには、サインによって追加された制御フロー操作が含まれます。

tf.py_function:拡張タイプは引数として使用でき、tf.py_functionへのfunc引数の値を返します。

Tensor ops:ディスパッチデコレータを使用してTensor入力(tf.matmul、tf.gather、tf.reduce_sumなど)を受け入れるほとんどのTensorFlowopsをサポートできます。

distribution strategy:レプリカごとの値として使用できます。

 

3. TF2の移行が容易になりました

 

TensorFlow Webサイトに新しいMigrateto TF2タブを作成しました。これには、更新されたガイドと、Colabを含まれています。

https://www.tensorflow.org/guide/migrate

 

4. 新しいコミュニティがTensorFlowHubでモデルを提供

前回のTensorFlowリリース以降、コミュニティは実際に集まって、TensorFlowハブで多くの新しいモデルを利用できるようにしました。 これで、MLP-Mixer、Vision Transformers、Wav2Vec2、RoBERTa、ConvMixer、DistillBERT、YoloV5などのモデルを見つけることができます。 これらのモデルはすべて、TensorFlowハブを介して使用する準備ができています。

https://www.tensorflow.org/hub

 

担当者:KW
バンコクのタイ出身 データサイエンティスト
製造、マーケティング、財務、AI研究などの様々な業界にPSI生産管理、在庫予測・最適化分析、顧客ロイヤルティ分析、センチメント分析、SaaS、PaaS、IaaS、AI at the Edge の環境構築などのスペシャリスト