TensorFlow.org で表示
|
Google Colab で実行
|
GitHub でソースを表示 |
ノートブックをダウンロード |
TensorFlow コードを TF1.x から TF2 に移行する場合、移行したコードが TF2 でも TF1.x と同じように動作することを確認することを推薦します。
このガイドでは、tf.keras.layers.Layer メソッドに適用された tf.compat.v1.keras.utils.track_tf1_style_variables モデリング shim を使用した移行コードの例について説明します。TF2 モデリング shim の詳細については、モデルマッピングガイドを参照してください。
このガイドでは、次の目的で使用できるアプローチについて詳しく説明します。
- 移行されたコードを使用してトレーニングモデルから得られた結果の正確性を検証する
- TensorFlow バージョン間でコードの数値的等価性を検証する
セットアップ
pip uninstall -y -q tensorflow# Install tf-nightly as the DeterministicRandomTestTool is available only in# Tensorflow 2.8pip install -q tf-nightly
pip install -q tf_slimimport tensorflow as tf
import tensorflow.compat.v1 as v1
import numpy as np
import tf_slim as slim
import sys
from contextlib import contextmanager
2022-12-14 22:00:19.013155: E tensorflow/tsl/lib/monitoring/collection_registry.cc:81] Cannot register 2 metrics with the same name: /tensorflow/core/bfc_allocator_delay
!git clone --depth=1 https://github.com/tensorflow/models.git
import models.research.slim.nets.inception_resnet_v2 as inception
Cloning into 'models'... remote: Enumerating objects: 3590, done. remote: Counting objects: 0% (1/3590) remote: Counting objects: 1% (36/3590) remote: Counting objects: 2% (72/3590) remote: Counting objects: 3% (108/3590) remote: Counting objects: 4% (144/3590) remote: Counting objects: 5% (180/3590) remote: Counting objects: 6% (216/3590) remote: Counting objects: 7% (252/3590) remote: Counting objects: 8% (288/3590) remote: Counting objects: 9% (324/3590) remote: Counting objects: 10% (359/3590) remote: Counting objects: 11% (395/3590) remote: Counting objects: 12% (431/3590) remote: Counting objects: 13% (467/3590) remote: Counting objects: 14% (503/3590) remote: Counting objects: 15% (539/3590) remote: Counting objects: 16% (575/3590) remote: Counting objects: 17% (611/3590) remote: Counting objects: 18% (647/3590) remote: Counting objects: 19% (683/3590) remote: Counting objects: 20% (718/3590) remote: Counting objects: 21% (754/3590) remote: Counting objects: 22% (790/3590) remote: Counting objects: 23% (826/3590) remote: Counting objects: 24% (862/3590) remote: Counting objects: 25% (898/3590) remote: Counting objects: 26% (934/3590) remote: Counting objects: 27% (970/3590) remote: Counting objects: 28% (1006/3590) remote: Counting objects: 29% (1042/3590) remote: Counting objects: 30% (1077/3590) remote: Counting objects: 31% (1113/3590) remote: Counting objects: 32% (1149/3590) remote: Counting objects: 33% (1185/3590) remote: Counting objects: 34% (1221/3590) remote: Counting objects: 35% (1257/3590) remote: Counting objects: 36% (1293/3590) remote: Counting objects: 37% (1329/3590) remote: Counting objects: 38% (1365/3590) remote: Counting objects: 39% (1401/3590) remote: Counting objects: 40% (1436/3590) remote: Counting objects: 41% (1472/3590) remote: Counting objects: 42% (1508/3590) remote: Counting objects: 43% (1544/3590) remote: Counting objects: 44% (1580/3590) remote: Counting objects: 45% (1616/3590) remote: Counting objects: 46% (1652/3590) remote: Counting objects: 47% (1688/3590) remote: Counting objects: 48% (1724/3590) remote: Counting objects: 49% (1760/3590) remote: Counting objects: 50% (1795/3590) remote: Counting objects: 51% (1831/3590) remote: Counting objects: 52% (1867/3590) remote: Counting objects: 53% (1903/3590) remote: Counting objects: 54% (1939/3590) remote: Counting objects: 55% (1975/3590) remote: Counting objects: 56% (2011/3590) remote: Counting objects: 57% (2047/3590) remote: Counting objects: 58% (2083/3590) remote: Counting objects: 59% (2119/3590) remote: Counting objects: 60% (2154/3590) remote: Counting objects: 61% (2190/3590) remote: Counting objects: 62% (2226/3590) remote: Counting objects: 63% (2262/3590) remote: Counting objects: 64% (2298/3590) remote: Counting objects: 65% (2334/3590) remote: Counting objects: 66% (2370/3590) remote: Counting objects: 67% (2406/3590) remote: Counting objects: 68% (2442/3590) remote: Counting objects: 69% (2478/3590) remote: Counting objects: 70% (2513/3590) remote: Counting objects: 71% (2549/3590) remote: Counting objects: 72% (2585/3590) remote: Counting objects: 73% (2621/3590) remote: Counting objects: 74% (2657/3590) remote: Counting objects: 75% (2693/3590) remote: Counting objects: 76% (2729/3590) remote: Counting objects: 77% (2765/3590) remote: Counting objects: 78% (2801/3590) remote: Counting objects: 79% (2837/3590) remote: Counting objects: 80% (2872/3590) remote: Counting objects: 81% (2908/3590) remote: Counting objects: 82% (2944/3590) remote: Counting objects: 83% (2980/3590) remote: Counting objects: 84% (3016/3590) remote: Counting objects: 85% (3052/3590) remote: Counting objects: 86% (3088/3590) remote: Counting objects: 87% (3124/3590) remote: Counting objects: 88% (3160/3590) remote: Counting objects: 89% (3196/3590) remote: Counting objects: 90% (3231/3590) remote: Counting objects: 91% (3267/3590) remote: Counting objects: 92% (3303/3590) remote: Counting objects: 93% (3339/3590) remote: Counting objects: 94% (3375/3590) remote: Counting objects: 95% (3411/3590) remote: Counting objects: 96% (3447/3590) remote: Counting objects: 97% (3483/3590) remote: Counting objects: 98% (3519/3590) remote: Counting objects: 99% (3555/3590) remote: Counting objects: 100% (3590/3590) remote: Counting objects: 100% (3590/3590), done. remote: Compressing objects: 0% (1/3006) remote: Compressing objects: 1% (31/3006) remote: Compressing objects: 2% (61/3006) remote: Compressing objects: 3% (91/3006) remote: Compressing objects: 4% (121/3006) remote: Compressing objects: 5% (151/3006) remote: Compressing objects: 6% (181/3006) remote: Compressing objects: 7% (211/3006) remote: Compressing objects: 8% (241/3006) remote: Compressing objects: 9% (271/3006) remote: Compressing objects: 10% (301/3006) remote: Compressing objects: 11% (331/3006) remote: Compressing objects: 12% (361/3006) remote: Compressing objects: 13% (391/3006) remote: Compressing objects: 14% (421/3006) remote: Compressing objects: 15% (451/3006) remote: Compressing objects: 16% (481/3006) remote: Compressing objects: 17% (512/3006) remote: Compressing objects: 18% (542/3006) remote: Compressing objects: 19% (572/3006) remote: Compressing objects: 20% (602/3006) remote: Compressing objects: 21% (632/3006) remote: Compressing objects: 22% (662/3006) remote: Compressing objects: 23% (692/3006) remote: Compressing objects: 24% (722/3006) remote: Compressing objects: 25% (752/3006) remote: Compressing objects: 26% (782/3006) remote: Compressing objects: 27% (812/3006) remote: Compressing objects: 28% (842/3006) remote: Compressing objects: 29% (872/3006) remote: Compressing objects: 30% (902/3006) remote: Compressing objects: 31% (932/3006) remote: Compressing objects: 32% (962/3006) remote: Compressing objects: 33% (992/3006) remote: Compressing objects: 34% (1023/3006) remote: Compressing objects: 35% (1053/3006) remote: Compressing objects: 36% (1083/3006) remote: Compressing objects: 37% (1113/3006) remote: Compressing objects: 38% (1143/3006) remote: Compressing objects: 39% (1173/3006) remote: Compressing objects: 40% (1203/3006) remote: Compressing objects: 41% (1233/3006) remote: Compressing objects: 42% (1263/3006) remote: Compressing objects: 43% (1293/3006) remote: Compressing objects: 44% (1323/3006) remote: Compressing objects: 45% (1353/3006) remote: Compressing objects: 46% (1383/3006) remote: Compressing objects: 47% (1413/3006) remote: Compressing objects: 48% (1443/3006) remote: Compressing objects: 49% (1473/3006) remote: Compressing objects: 50% (1503/3006) remote: Compressing objects: 51% (1534/3006) remote: Compressing objects: 52% (1564/3006) remote: Compressing objects: 53% (1594/3006) remote: Compressing objects: 54% (1624/3006) remote: Compressing objects: 55% (1654/3006) remote: Compressing objects: 56% (1684/3006) remote: Compressing objects: 57% (1714/3006) remote: Compressing objects: 58% (1744/3006) remote: Compressing objects: 59% (1774/3006) remote: Compressing objects: 60% (1804/3006) remote: Compressing objects: 61% (1834/3006) remote: Compressing objects: 62% (1864/3006) remote: Compressing objects: 63% (1894/3006) remote: Compressing objects: 63% (1903/3006) remote: Compressing objects: 64% (1924/3006) remote: Compressing objects: 65% (1954/3006) remote: Compressing objects: 66% (1984/3006) remote: Compressing objects: 67% (2015/3006) remote: Compressing objects: 68% (2045/3006) remote: Compressing objects: 69% (2075/3006) remote: Compressing objects: 70% (2105/3006) remote: Compressing objects: 71% (2135/3006) remote: Compressing objects: 72% (2165/3006) remote: Compressing objects: 73% (2195/3006) remote: Compressing objects: 74% (2225/3006) remote: Compressing objects: 75% (2255/3006) remote: Compressing objects: 76% (2285/3006) remote: Compressing objects: 77% (2315/3006) remote: Compressing objects: 78% (2345/3006) remote: Compressing objects: 79% (2375/3006) remote: Compressing objects: 80% (2405/3006) remote: Compressing objects: 81% (2435/3006) remote: Compressing objects: 82% (2465/3006) remote: Compressing objects: 83% (2495/3006) remote: Compressing objects: 84% (2526/3006) remote: Compressing objects: 85% (2556/3006) remote: Compressing objects: 86% (2586/3006) remote: Compressing objects: 87% (2616/3006) remote: Compressing objects: 88% (2646/3006) remote: Compressing objects: 89% (2676/3006) remote: Compressing objects: 90% (2706/3006) remote: Compressing objects: 91% (2736/3006) remote: Compressing objects: 92% (2766/3006) remote: Compressing objects: 93% (2796/3006) remote: Compressing objects: 94% (2826/3006) remote: Compressing objects: 95% (2856/3006) remote: Compressing objects: 96% (2886/3006) remote: Compressing objects: 97% (2916/3006) remote: Compressing objects: 98% (2946/3006) remote: Compressing objects: 99% (2976/3006) remote: Compressing objects: 100% (3006/3006) remote: Compressing objects: 100% (3006/3006), done. Receiving objects: 0% (1/3590) Receiving objects: 1% (36/3590) Receiving objects: 2% (72/3590) Receiving objects: 3% (108/3590) Receiving objects: 4% (144/3590) Receiving objects: 5% (180/3590) Receiving objects: 6% (216/3590) Receiving objects: 7% (252/3590) Receiving objects: 8% (288/3590) Receiving objects: 9% (324/3590) Receiving objects: 10% (359/3590) Receiving objects: 11% (395/3590) Receiving objects: 12% (431/3590) Receiving objects: 13% (467/3590) Receiving objects: 14% (503/3590) Receiving objects: 15% (539/3590) Receiving objects: 16% (575/3590) Receiving objects: 17% (611/3590) Receiving objects: 18% (647/3590) Receiving objects: 19% (683/3590) Receiving objects: 20% (718/3590) Receiving objects: 21% (754/3590) Receiving objects: 22% (790/3590) Receiving objects: 23% (826/3590) Receiving objects: 24% (862/3590) Receiving objects: 25% (898/3590) Receiving objects: 26% (934/3590) Receiving objects: 27% (970/3590) Receiving objects: 28% (1006/3590) Receiving objects: 29% (1042/3590) Receiving objects: 30% (1077/3590) Receiving objects: 31% (1113/3590) Receiving objects: 32% (1149/3590) Receiving objects: 33% (1185/3590) Receiving objects: 34% (1221/3590) Receiving objects: 35% (1257/3590) Receiving objects: 36% (1293/3590) Receiving objects: 37% (1329/3590) Receiving objects: 38% (1365/3590) Receiving objects: 39% (1401/3590) Receiving objects: 40% (1436/3590) Receiving objects: 41% (1472/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 42% (1508/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 43% (1544/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 44% (1580/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 45% (1616/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 46% (1652/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 47% (1688/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 48% (1724/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 49% (1760/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 50% (1795/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 51% (1831/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 52% (1867/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 53% (1903/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 54% (1939/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 55% (1975/3590), 3.79 MiB | 7.49 MiB/s Receiving objects: 55% (2004/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 56% (2011/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 57% (2047/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 58% (2083/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 59% (2119/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 60% (2154/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 61% (2190/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 62% (2226/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 63% (2262/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 64% (2298/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 65% (2334/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 66% (2370/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 67% (2406/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 68% (2442/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 69% (2478/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 70% (2513/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 71% (2549/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 72% (2585/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 73% (2621/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 74% (2657/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 75% (2693/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 76% (2729/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 77% (2765/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 78% (2801/3590), 17.57 MiB | 17.47 MiB/s Receiving objects: 79% (2837/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 80% (2872/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 81% (2908/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 82% (2944/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 83% (2980/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 84% (3016/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 85% (3052/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 86% (3088/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 87% (3124/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 88% (3160/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 89% (3196/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 90% (3231/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 91% (3267/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 92% (3303/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 93% (3339/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 94% (3375/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 95% (3411/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 96% (3447/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 97% (3483/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 98% (3519/3590), 35.19 MiB | 23.37 MiB/s remote: Total 3590 (delta 942), reused 1502 (delta 530), pack-reused 0 Receiving objects: 99% (3555/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 100% (3590/3590), 35.19 MiB | 23.37 MiB/s Receiving objects: 100% (3590/3590), 47.08 MiB | 25.56 MiB/s, done. Resolving deltas: 0% (0/942) Resolving deltas: 3% (31/942) Resolving deltas: 4% (47/942) Resolving deltas: 5% (48/942) Resolving deltas: 6% (57/942) Resolving deltas: 7% (66/942) Resolving deltas: 8% (77/942) Resolving deltas: 9% (86/942) Resolving deltas: 10% (95/942) Resolving deltas: 11% (104/942) Resolving deltas: 12% (115/942) Resolving deltas: 13% (126/942) Resolving deltas: 14% (132/942) Resolving deltas: 15% (142/942) Resolving deltas: 16% (153/942) Resolving deltas: 17% (161/942) Resolving deltas: 18% (170/942) Resolving deltas: 19% (183/942) Resolving deltas: 20% (191/942) Resolving deltas: 21% (199/942) Resolving deltas: 22% (209/942) Resolving deltas: 23% (218/942) Resolving deltas: 24% (227/942) Resolving deltas: 25% (238/942) Resolving deltas: 26% (246/942) Resolving deltas: 27% (257/942) Resolving deltas: 28% (266/942) Resolving deltas: 29% (274/942) Resolving deltas: 30% (284/942) Resolving deltas: 31% (293/942) Resolving deltas: 32% (303/942) Resolving deltas: 33% (313/942) Resolving deltas: 34% (321/942) Resolving deltas: 35% (331/942) Resolving deltas: 36% (342/942) Resolving deltas: 37% (349/942) Resolving deltas: 38% (359/942) Resolving deltas: 39% (368/942) Resolving deltas: 40% (377/942) Resolving deltas: 41% (387/942) Resolving deltas: 42% (400/942) Resolving deltas: 43% (406/942) Resolving deltas: 45% (426/942) Resolving deltas: 46% (440/942) Resolving deltas: 47% (448/942) Resolving deltas: 48% (454/942) Resolving deltas: 49% (462/942) Resolving deltas: 50% (471/942) Resolving deltas: 51% (481/942) Resolving deltas: 52% (490/942) Resolving deltas: 53% (500/942) Resolving deltas: 54% (510/942) Resolving deltas: 55% (519/942) Resolving deltas: 56% (532/942) Resolving deltas: 57% (537/942) Resolving deltas: 58% (549/942) Resolving deltas: 59% (556/942) Resolving deltas: 60% (567/942) Resolving deltas: 61% (578/942) Resolving deltas: 62% (585/942) Resolving deltas: 63% (594/942) Resolving deltas: 64% (603/942) Resolving deltas: 65% (620/942) Resolving deltas: 66% (623/942) Resolving deltas: 67% (637/942) Resolving deltas: 68% (643/942) Resolving deltas: 69% (650/942) Resolving deltas: 70% (667/942) Resolving deltas: 72% (679/942) Resolving deltas: 73% (693/942) Resolving deltas: 74% (698/942) Resolving deltas: 75% (707/942) Resolving deltas: 76% (724/942) Resolving deltas: 77% (733/942) Resolving deltas: 78% (740/942) Resolving deltas: 79% (746/942) Resolving deltas: 80% (755/942) Resolving deltas: 81% (767/942) Resolving deltas: 82% (773/942) Resolving deltas: 83% (783/942) Resolving deltas: 84% (792/942) Resolving deltas: 85% (801/942) Resolving deltas: 86% (813/942) Resolving deltas: 87% (824/942) Resolving deltas: 88% (829/942) Resolving deltas: 89% (841/942) Resolving deltas: 90% (851/942) Resolving deltas: 91% (861/942) Resolving deltas: 92% (867/942) Resolving deltas: 93% (877/942) Resolving deltas: 94% (887/942) Resolving deltas: 95% (895/942) Resolving deltas: 96% (911/942) Resolving deltas: 97% (914/942) Resolving deltas: 98% (924/942) Resolving deltas: 99% (935/942) Resolving deltas: 100% (942/942) Resolving deltas: 100% (942/942), done.
重要なフォワードパスコードのチャンクを shim に入れる場合は、TF1.x と同じように動作していることを確認する必要があります。たとえば、TF-Slim Inception-Resnet-v2 モデル全体を次のように shim に入れることを検討してください。
# TF1 Inception resnet v2 forward pass based on slim layers
def inception_resnet_v2(inputs, num_classes, is_training):
with slim.arg_scope(
inception.inception_resnet_v2_arg_scope(batch_norm_scale=True)):
return inception.inception_resnet_v2(inputs, num_classes, is_training=is_training)
class InceptionResnetV2(tf.keras.layers.Layer):
"""Slim InceptionResnetV2 forward pass as a Keras layer"""
def __init__(self, num_classes, **kwargs):
super().__init__(**kwargs)
self.num_classes = num_classes
@tf.compat.v1.keras.utils.track_tf1_style_variables
def call(self, inputs, training=None):
is_training = training or False
# Slim does not accept `None` as a value for is_training,
# Keras will still pass `None` to layers to construct functional models
# without forcing the layer to always be in training or in inference.
# However, `None` is generally considered to run layers in inference.
with slim.arg_scope(
inception.inception_resnet_v2_arg_scope(batch_norm_scale=True)):
return inception.inception_resnet_v2(
inputs, self.num_classes, is_training=is_training)
WARNING:tensorflow:From /tmpfs/tmp/ipykernel_143457/2131234657.py:8: The name tf.keras.utils.track_tf1_style_variables is deprecated. Please use tf.compat.v1.keras.utils.track_tf1_style_variables instead.
ここでは、このレイヤーは実際にはそのまますぐに完全に機能します(正確な正則化損失トラッキングを備えています)。
ただし、これは当たり前のことではありません。以下のステップに従って、実際に TF1.x と同じように動作していることを確認し、数値的に完全に等価であることを確認します。これらのステップは、フォワードパスのどの部分が TF1.x からの分岐を引き起こしているかを三角測量するのにも役立ちます(モデルの別の部分ではなく、モデルのフォワードパスで分岐が発生しているかどうかを特定します)。
ステップ 1: 変数が 1 回だけ作成されることを確認する
最初に、各呼び出しで変数が再利用され、毎回新しい変数が誤って作成されて使用されないようにモデルが正しく構築されていることを検証する必要があります。たとえば、モデルが新しい Keras レイヤーを作成したり、各フォワードパス呼び出しで tf.Variable を呼び出す場合、変数のキャプチャに失敗し、毎回新しい変数を作成する可能性が高くなります。
以下は、モデルが新しい変数を作成している場合に、そのことを検出し、モデルのどの部分がそれを行っているかをデバッグするために使用できる 2 つのコンテキストマネージャースコープです。
@contextmanager
def assert_no_variable_creations():
"""Assert no variables are created in this context manager scope."""
def invalid_variable_creator(next_creator, **kwargs):
raise ValueError("Attempted to create a new variable instead of reusing an existing one. Args: {}".format(kwargs))
with tf.variable_creator_scope(invalid_variable_creator):
yield
@contextmanager
def catch_and_raise_created_variables():
"""Raise all variables created within this context manager scope (if any)."""
created_vars = []
def variable_catcher(next_creator, **kwargs):
var = next_creator(**kwargs)
created_vars.append(var)
return var
with tf.variable_creator_scope(variable_catcher):
yield
if created_vars:
raise ValueError("Created vars:", created_vars)
スコープ内で変数を作成しようとすると、最初のスコープ(assert_no_variable_creations())は、すぐにエラーを発生します。これにより、スタックトレースを調べて(対話型デバッグを使用して)、既存の変数を再利用する代わりに、変数を作成したコード行を正確に把握できます。
2 番目のスコープ(catch_and_raise_created_variables())は、変数が作成された場合、スコープの最後で例外を発生させます。この例外には、スコープで作成されたすべての変数のリストが含まれます。これは、一般的なパターンを見つけることができる場合に、モデルが作成しているすべての重みのセットが何であるかを把握するのに役立ちます。ただし、これらの変数が作成された正確なコード行を特定するにはあまり役に立ちません。
以下の両方のスコープを使用して、shim ベースの InceptionResnetV2 レイヤーが最初の呼び出し後に新しい変数を作成せずに再利用していることを確認します。
model = InceptionResnetV2(1000)
height, width = 299, 299
num_classes = 1000
inputs = tf.ones( (1, height, width, 3))
# Create all weights on the first call
model(inputs)
# Verify that no new weights are created in followup calls
with assert_no_variable_creations():
model(inputs)
with catch_and_raise_created_variables():
model(inputs)
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py:2212: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
warnings.warn('`layer.apply` is deprecated and '
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/legacy_tf_layers/core.py:332: UserWarning: `tf.layers.flatten` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Flatten` instead.
warnings.warn('`tf.layers.flatten` is deprecated and '
以下の例では、既存の重みを再利用する代わりに、毎回誤って新しい重みを作成するレイヤーで、これらのデコレータがどのように機能するかを観察できます。
class BrokenScalingLayer(tf.keras.layers.Layer):
"""Scaling layer that incorrectly creates new weights each time:"""
@tf.compat.v1.keras.utils.track_tf1_style_variables
def call(self, inputs):
var = tf.Variable(initial_value=2.0)
bias = tf.Variable(initial_value=2.0, name='bias')
return inputs * var + bias
model = BrokenScalingLayer()
inputs = tf.ones( (1, height, width, 3))
model(inputs)
try:
with assert_no_variable_creations():
model(inputs)
except ValueError as err:
import traceback
traceback.print_exc()
Traceback (most recent call last):
File "/tmpfs/tmp/ipykernel_143457/1128777590.py", line 7, in <module>
model(inputs)
File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/tmpfs/tmp/ipykernel_143457/3224979076.py", line 6, in call
var = tf.Variable(initial_value=2.0)
File "/tmpfs/tmp/ipykernel_143457/1829430118.py", line 5, in invalid_variable_creator
raise ValueError("Attempted to create a new variable instead of reusing an existing one. Args: {}".format(kwargs))
ValueError: Exception encountered when calling layer 'broken_scaling_layer' (type BrokenScalingLayer).
Attempted to create a new variable instead of reusing an existing one. Args: {'initial_value': 2.0, 'trainable': None, 'validate_shape': True, 'caching_device': None, 'name': None, 'variable_def': None, 'dtype': None, 'import_scope': None, 'constraint': None, 'synchronization': <VariableSynchronization.AUTO: 0>, 'aggregation': <VariableAggregation.NONE: 0>, 'shape': None, 'experimental_enable_variable_lifting': None}
Call arguments received by layer 'broken_scaling_layer' (type BrokenScalingLayer):
• inputs=tf.Tensor(shape=(1, 299, 299, 3), dtype=float32)
model = BrokenScalingLayer()
inputs = tf.ones( (1, height, width, 3))
model(inputs)
try:
with catch_and_raise_created_variables():
model(inputs)
except ValueError as err:
print(err)
('Created vars:', [<tf.Variable 'broken_scaling_layer_1/Variable:0' shape=() dtype=float32, numpy=2.0>, <tf.Variable 'broken_scaling_layer_1/bias:0' shape=() dtype=float32, numpy=2.0>])
重みを一度だけ作成し、毎回再利用するようにすることで、レイヤーを修正できます。
class FixedScalingLayer(tf.keras.layers.Layer):
"""Scaling layer that incorrectly creates new weights each time:"""
def __init__(self):
super().__init__()
self.var = None
self.bias = None
@tf.compat.v1.keras.utils.track_tf1_style_variables
def call(self, inputs):
if self.var is None:
self.var = tf.Variable(initial_value=2.0)
self.bias = tf.Variable(initial_value=2.0, name='bias')
return inputs * self.var + self.bias
model = FixedScalingLayer()
inputs = tf.ones( (1, height, width, 3))
model(inputs)
with assert_no_variable_creations():
model(inputs)
with catch_and_raise_created_variables():
model(inputs)
トラブルシューティング
以下は、モデルが既存の重みを再利用せずに誤って新しい重みを作成してしまう一般的な理由です。
- 既に作成された
tf.Variablesを再利用せずに、明示的なtf.Variable呼び出しを使用してしまう場合、最初に作成されていないかどうかを確認してから、既存のものを再利用します。 - (
tf.compat.v1.layersとは対照的に)毎回フォワードパスで Keras レイヤーまたはモデルを直接作成してしまう場合、最初に作成されていないかどうかを確認して、既存のものを再利用します。 tf.compat.v1.layersの上に構築されていて、すべてのcompat.v1.layersに明示的な名前を割り当てたり、名前付きvariable_scope内でcompat.v1 .layerの使用をラップできず、自動生成されたレイヤー名が各モデル呼び出しでインクリメントされてしまう場合、tf.compat.v1.layersの使用をすべてラップする shim でデコレートされたメソッド内に名前付きのtf.compat.v1.variable_scopeを配置します。
ステップ 2: 変数の数、名前、形状が一致していることを確認する
2 番目のステップは、TF2 で実行されているレイヤーが対応するコードが TF1.x と同じ形状で同じ数の重みを作成することを確認することです。
以下に示すように、これらが一致することを確認するために手動での確認と、単体テストでのプログラムによる確認を組み合わせることができます。
# Build the forward pass inside a TF1.x graph, and
# get the counts, shapes, and names of the variables
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
height, width = 299, 299
num_classes = 1000
inputs = tf.ones( (1, height, width, 3))
out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)
tf1_variable_names_and_shapes = {
var.name: (var.trainable, var.shape) for var in tf.compat.v1.global_variables()}
num_tf1_variables = len(tf.compat.v1.global_variables())
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer_v1.py:1694: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
warnings.warn('`layer.apply` is deprecated and '
次に、TF2 の shim によりラップされたレイヤーに対して同じことを実行します。重みを取得する前に、モデルも複数回呼び出されることに注意してください。これは、変数の再利用を効果的にテストするために行われます。
height, width = 299, 299
num_classes = 1000
model = InceptionResnetV2(num_classes)
# The weights will not be created until you call the model
inputs = tf.ones( (1, height, width, 3))
# Call the model multiple times before checking the weights, to verify variables
# get reused rather than accidentally creating additional variables
out, endpoints = model(inputs, training=False)
out, endpoints = model(inputs, training=False)
# Grab the name: shape mapping and the total number of variables separately,
# because in TF2 variables can be created with the same name
num_tf2_variables = len(model.variables)
tf2_variable_names_and_shapes = {
var.name: (var.trainable, var.shape) for var in model.variables}
# Verify that the variable counts, names, and shapes all match:
assert num_tf1_variables == num_tf2_variables
assert tf1_variable_names_and_shapes == tf2_variable_names_and_shapes
Shim ベースの InceptionResnetV2 レイヤーは、このテストに合格しています。ただし、一致しない場合は、差分(テキストまたはその他)を実行して、差分がどこにあるかを確認できます。
これにより、モデルのどの部分が期待どおりに動作していないかが分かります。Eager execution では、pdb、インタラクティブなデバッグ、およびブレークポイントを使用して、疑わしいと思われるモデルの部分を掘り下げ、問題が何なのかをより深くデバッグできます。
トラブルシューティング
明示的な
tf.Variable呼び出しと Keras レイヤー/モデルによって直接作成された変数の名前に細心の注意を払ってください。他のすべてが正常に機能している場合でもそれらの変数名生成セマンティクスは、TF1.x Graph と Eager execution およびtf.functionなどの TF2 関数との間でわずかに異なる可能性があるためです。このような場合は、わずかに異なる命名セマンティクスを考慮してテストを調整してください。TF1.x の変数コレクションによってキャプチャされた場合でも、
tf.Variable、tf.keras.layers.Layer、またはtf.keras.Modelがトレーニングループのフォワードパスが TF2 変数リストにない場合があります。これを修正するには、フォワードパスが作成する変数/レイヤー/モデルをモデルのインスタンス属性に割り当てます。詳細については、こちらを参照してください。
ステップ 3: すべての変数をリセットし、ランダム性をすべて無効にして数値の等価性を確認する
次のステップでは、(推論中などに)乱数生成が含まれないようにモデルを修正するときに、実際の出力と正則化損失トラッキングの両方の数値的等価性を検証します。
正確な方法は、特定のモデルに依存する場合がありますが、ほとんどのモデル(このモデルなど)では、次の方法でこれを行うことができます。
- 重みをランダム性なしで同じ値に初期化します。そのためには、作成後に固定値にリセットします。
- モデルを推論モードで実行して、ランダム性の原因となる可能性のあるドロップアウトレイヤーがトリガーされないようにします。
次のコードは、この方法で TF1.x と TF2 の結果を比較する方法を示しています。
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
height, width = 299, 299
num_classes = 1000
inputs = tf.ones( (1, height, width, 3))
out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)
# Rather than running the global variable initializers,
# reset all variables to a constant value
var_reset = tf.group([var.assign(tf.ones_like(var) * 0.001) for var in tf.compat.v1.global_variables()])
sess.run(var_reset)
# Grab the outputs & regularization loss
reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)
tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))
tf1_output = sess.run(out)
print("Regularization loss:", tf1_regularization_loss)
tf1_output[0][:5]
Regularization loss: 0.001182976
array([0.00299837, 0.00299837, 0.00299837, 0.00299837, 0.00299837],
dtype=float32)
TF2 の結果を取得します。
height, width = 299, 299
num_classes = 1000
model = InceptionResnetV2(num_classes)
inputs = tf.ones((1, height, width, 3))
# Call the model once to create the weights
out, endpoints = model(inputs, training=False)
# Reset all variables to the same fixed value as above, with no randomness
for var in model.variables:
var.assign(tf.ones_like(var) * 0.001)
tf2_output, endpoints = model(inputs, training=False)
# Get the regularization loss
tf2_regularization_loss = tf.math.add_n(model.losses)
print("Regularization loss:", tf2_regularization_loss)
tf2_output[0][:5]
Regularization loss: tf.Tensor(0.0011829757, shape=(), dtype=float32)
<tf.Tensor: shape=(5,), dtype=float32, numpy=
array([0.00299837, 0.00299837, 0.00299837, 0.00299837, 0.00299837],
dtype=float32)>
# Create a dict of tolerance values
tol_dict={'rtol':1e-06, 'atol':1e-05}
# Verify that the regularization loss and output both match
# when we fix the weights and avoid randomness by running inference:
np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)
np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)
ランダム性のソースを削除すると、TF1.x と TF2 の間で数値が一致し、TF2 互換の InceptionResnetV2 レイヤーがテストに合格します。
独自のモデルで結果が分岐しているのを観察した場合は、出力または pdb と対話型デバッグを使用して、結果が分岐し始める場所と理由を特定できます。Eager execution を使用すると、これが大幅に容易になります。また、アブレーションアプローチを使用して、モデルのごく一部のみを固定中間入力で実行し、分岐が発生する場所を分離することもできます。
便利なことに、多くのスリムネット(およびその他のモデル)は、プローブ可能な中間エンドポイントも公開しています。
ステップ 4: 乱数生成を調整し、トレーニングと推論における数値等価性をチェックする
最後のステップは、TF2 モデルが TF1.x モデルと数値的に一致することを確認することです。これは、変数の初期化およびフォワードパス自体(フォワードパス中のドロップアウトレイヤーなど)での乱数生成を考慮する場合でも同様です。
これを行うには、以下のテストツールを使用して、乱数生成のセマンティクスを TF1.x Graph/Session と Eager execution の間で一致させます。
以前の TF1 Graph/Session と TF2 Eager execution は、異なるステートフルな乱数生成セマンティクスを使用します
tf.compat.v1.Session で、シードが指定されていない場合、乱数の生成は、ランダムな演算が追加された時点で Graph にある演算の数と、その Graph の実行回数に依存します。Eager execution では、ステートフルな乱数の生成は、グローバルシード、演算のランダムシード、および指定されたランダムシードを使用した演算が実行される回数に依存します。詳細については、tf.random.set_seed を参照してください。
次の v1.keras.utils.DeterministicRandomTestTool クラスは、コンテキストマネージャ scope() を提供し、 TF1 Graphs/Session と Eager execution の両方でステートフルなランダム演算が同じシードを使用できるようになります。
このツールには、次の 2 つのテストモードがあります。
constantは、呼び出された回数に関係なく、1 つの演算ごとに同じシードを使用します。num_random_opsは、以前に観測されたステートフルなランダム演算の数を演算シードとして使用します。
これは、変数の作成と初期化に使用されるステートフルなランダム演算と、計算で使用されるステートフルなランダム演算(ドロップアウトレイヤーなど)の両方に適用されます。
このツールを使用して、Session と Eager execution の間でステートフルな乱数生成を一致させる方法を示すために、3 つのランダムテンソルを生成します。
random_tool = v1.keras.utils.DeterministicRandomTestTool()
with random_tool.scope():
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
a = tf.random.uniform(shape=(3,1))
a = a * 3
b = tf.random.uniform(shape=(3,3))
b = b * 3
c = tf.random.uniform(shape=(3,3))
c = c * 3
graph_a, graph_b, graph_c = sess.run([a, b, c])
graph_a, graph_b, graph_c
(array([[2.5063772],
[2.7488918],
[1.4839486]], dtype=float32),
array([[2.5063772, 2.7488918, 1.4839486],
[1.5633398, 2.1358476, 1.3693532],
[0.3598416, 1.8287641, 2.5314465]], dtype=float32),
array([[2.5063772, 2.7488918, 1.4839486],
[1.5633398, 2.1358476, 1.3693532],
[0.3598416, 1.8287641, 2.5314465]], dtype=float32))
random_tool = v1.keras.utils.DeterministicRandomTestTool()
with random_tool.scope():
a = tf.random.uniform(shape=(3,1))
a = a * 3
b = tf.random.uniform(shape=(3,3))
b = b * 3
c = tf.random.uniform(shape=(3,3))
c = c * 3
a, b, c
(<tf.Tensor: shape=(3, 1), dtype=float32, numpy=
array([[2.5063772],
[2.7488918],
[1.4839486]], dtype=float32)>,
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[2.5063772, 2.7488918, 1.4839486],
[1.5633398, 2.1358476, 1.3693532],
[0.3598416, 1.8287641, 2.5314465]], dtype=float32)>,
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[2.5063772, 2.7488918, 1.4839486],
[1.5633398, 2.1358476, 1.3693532],
[0.3598416, 1.8287641, 2.5314465]], dtype=float32)>)
# Demonstrate that the generated random numbers match
np.testing.assert_allclose(graph_a, a.numpy(), **tol_dict)
np.testing.assert_allclose(graph_b, b.numpy(), **tol_dict)
np.testing.assert_allclose(graph_c, c.numpy(), **tol_dict)
ただし、constant モードでは、b と c は同じシードで同じ形状で生成されているため、正確に同じ値になります。
np.testing.assert_allclose(b.numpy(), c.numpy(), **tol_dict)
順位トレース
constant モードで一部の乱数が一致して数値的等価性テストの信頼性が低下することが懸念される場合(たとえば、複数の重みが同じ初期化を行う場合)、これを回避するには num_random_ops モードを使用します。num_random_ops モードでは、生成される乱数は、プログラム内のランダム演算の順位に依存します。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
a = tf.random.uniform(shape=(3,1))
a = a * 3
b = tf.random.uniform(shape=(3,3))
b = b * 3
c = tf.random.uniform(shape=(3,3))
c = c * 3
graph_a, graph_b, graph_c = sess.run([a, b, c])
graph_a, graph_b, graph_c
(array([[2.5063772],
[2.7488918],
[1.4839486]], dtype=float32),
array([[0.45038545, 1.9197761 , 2.4536333 ],
[1.0371652 , 2.9898582 , 1.924583 ],
[0.25679827, 1.6579313 , 2.8418403 ]], dtype=float32),
array([[2.9634383 , 1.0862181 , 2.6042497 ],
[0.70099247, 2.3920312 , 1.0470468 ],
[0.18173039, 0.8359269 , 1.0508587 ]], dtype=float32))
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
a = tf.random.uniform(shape=(3,1))
a = a * 3
b = tf.random.uniform(shape=(3,3))
b = b * 3
c = tf.random.uniform(shape=(3,3))
c = c * 3
a, b, c
(<tf.Tensor: shape=(3, 1), dtype=float32, numpy=
array([[2.5063772],
[2.7488918],
[1.4839486]], dtype=float32)>,
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0.45038545, 1.9197761 , 2.4536333 ],
[1.0371652 , 2.9898582 , 1.924583 ],
[0.25679827, 1.6579313 , 2.8418403 ]], dtype=float32)>,
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[2.9634383 , 1.0862181 , 2.6042497 ],
[0.70099247, 2.3920312 , 1.0470468 ],
[0.18173039, 0.8359269 , 1.0508587 ]], dtype=float32)>)
# Demonstrate that the generated random numbers match
np.testing.assert_allclose(graph_a, a.numpy(), **tol_dict)
np.testing.assert_allclose(graph_b, b.numpy(), **tol_dict )
np.testing.assert_allclose(graph_c, c.numpy(), **tol_dict)
# Demonstrate that with the 'num_random_ops' mode,
# b & c took on different values even though
# their generated shape was the same
assert not np.allclose(b.numpy(), c.numpy(), **tol_dict)
ただし、このモードでの乱数生成はプログラムの順位に影響されるため、次の生成された乱数は一致しないことに注意してください。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
a = tf.random.uniform(shape=(3,1))
a = a * 3
b = tf.random.uniform(shape=(3,3))
b = b * 3
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
b_prime = tf.random.uniform(shape=(3,3))
b_prime = b_prime * 3
a_prime = tf.random.uniform(shape=(3,1))
a_prime = a_prime * 3
assert not np.allclose(a.numpy(), a_prime.numpy())
assert not np.allclose(b.numpy(), b_prime.numpy())
順位トレースによるデバッグのバリエーションを可能にするために、num_random_ops モードで DeterministicRandomTestTool を使用すると、operation_seed プロパティでトレースされたランダム演算の数を確認できます。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
print(random_tool.operation_seed)
a = tf.random.uniform(shape=(3,1))
a = a * 3
print(random_tool.operation_seed)
b = tf.random.uniform(shape=(3,3))
b = b * 3
print(random_tool.operation_seed)
0 1 2
テストでさまざまな順位トレースを考慮する必要がある場合は、自動インクリメント operation_seed を明示的に設定することもできます。たとえば、これを使用して、2 つの異なるプログラムの順位間で乱数生成を一致させることができます。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
print(random_tool.operation_seed)
a = tf.random.uniform(shape=(3,1))
a = a * 3
print(random_tool.operation_seed)
b = tf.random.uniform(shape=(3,3))
b = b * 3
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
random_tool.operation_seed = 1
b_prime = tf.random.uniform(shape=(3,3))
b_prime = b_prime * 3
random_tool.operation_seed = 0
a_prime = tf.random.uniform(shape=(3,1))
a_prime = a_prime * 3
np.testing.assert_allclose(a.numpy(), a_prime.numpy(), **tol_dict)
np.testing.assert_allclose(b.numpy(), b_prime.numpy(), **tol_dict)
0 1
ただし、DeterministicRandomTestTool では、すでに使用されている演算シードの再利用が許可されていないため、自動インクリメントシーケンスが重複しないようにしてください。これは、Eager execution では同じ演算シードの後続の使用に対して異なる数が生成されるためです。TF1 Graphs と Session では異なる数は生成されません。そのため、エラーを発生させると、Session と Eager ステートフル乱数の生成を一致させることができます。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
random_tool.operation_seed = 1
b_prime = tf.random.uniform(shape=(3,3))
b_prime = b_prime * 3
random_tool.operation_seed = 0
a_prime = tf.random.uniform(shape=(3,1))
a_prime = a_prime * 3
try:
c = tf.random.uniform(shape=(3,1))
raise RuntimeError("An exception should have been raised before this, " +
"because the auto-incremented operation seed will " +
"overlap an already-used value")
except ValueError as err:
print(err)
This `DeterministicRandomTestTool` object is trying to re-use the already-used operation seed 1. It cannot guarantee random numbers will match between eager and sessions when an operation seed is reused. You most likely set `operation_seed` explicitly but used a value that caused the naturally-incrementing operation seed sequences to overlap with an already-used seed.
推論の検証
DeterministicRandomTestTool を使用して、ランダムな重みの初期化を使用している場合でも、InceptionResnetV2 モデルが推論で一致することを確認できるようになりました。プログラムの順位が一致するより強力なテスト条件を得るには、num_random_ops モードを使用します。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
height, width = 299, 299
num_classes = 1000
inputs = tf.ones( (1, height, width, 3))
out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)
# Initialize the variables
sess.run(tf.compat.v1.global_variables_initializer())
# Grab the outputs & regularization loss
reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)
tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))
tf1_output = sess.run(out)
print("Regularization loss:", tf1_regularization_loss)
Regularization loss: 1.2254326
height, width = 299, 299
num_classes = 1000
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
model = InceptionResnetV2(num_classes)
inputs = tf.ones((1, height, width, 3))
tf2_output, endpoints = model(inputs, training=False)
# Grab the regularization loss as well
tf2_regularization_loss = tf.math.add_n(model.losses)
print("Regularization loss:", tf2_regularization_loss)
Regularization loss: tf.Tensor(1.2254325, shape=(), dtype=float32)
# Verify that the regularization loss and output both match
# when using the DeterministicRandomTestTool:
np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)
np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)
トレーニングの検証
DeterministicRandomTestTool は、すべてのステートフルなランダム演算(重みの初期化とドロップアウトレイヤーなどの計算を含む)で機能するため、これを使用して、トレーニングモードでもモデルが一致することを確認できます。ステートフルなランダム演算のプログラムの順位が一致するため、num_random_ops モードを再度使用できます。
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
height, width = 299, 299
num_classes = 1000
inputs = tf.ones( (1, height, width, 3))
out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=True)
# Initialize the variables
sess.run(tf.compat.v1.global_variables_initializer())
# Grab the outputs & regularization loss
reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)
tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))
tf1_output = sess.run(out)
print("Regularization loss:", tf1_regularization_loss)
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/layers/normalization/batch_normalization.py:581: _colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. Regularization loss: 1.22548
height, width = 299, 299
num_classes = 1000
random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')
with random_tool.scope():
model = InceptionResnetV2(num_classes)
inputs = tf.ones((1, height, width, 3))
tf2_output, endpoints = model(inputs, training=True)
# Grab the regularization loss as well
tf2_regularization_loss = tf.math.add_n(model.losses)
print("Regularization loss:", tf2_regularization_loss)
Regularization loss: tf.Tensor(1.2254798, shape=(), dtype=float32)
# Verify that the regularization loss and output both match
# when using the DeterministicRandomTestTool
np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)
np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)
これで、tf.keras.layers.Layer の周りのデコレータで Eager execution されている InceptionResnetV2 モデルが、TF1 Graph と Session で実行されているスリムネットワークと数値的に一致することを確認できました。
注意: num_random_ops モードで DeterministicRandomTestTool を使用する場合、数値的等価性のためにテスト時に tf.keras.layers.Layer メソッドデコレータを直接使用して呼び出すことを推薦します。Keras functional モデルまたは他の Keras モデル内に埋め込むと、TF1.x Graph/Session と Eager execution を比較するときに、ステートフルなランダム演算の順位トレースに違いが生じ、正確に一致させるのが難しくなる可能性があります。
たとえば、InceptionResnetV2 レイヤーを training=True で直接呼び出すと、変数の初期化がネットワークの作成順位に従ってドロップアウト順位でインターリーブされます。
一方、最初に tf.keras.layers.Layer デコレータを Keras functional モデルに配置してから、そのモデルを training=True で呼び出すことは、すべての変数を初期化し、ドロップアウトレイヤーを使用することと同じです。これにより、異なる順位トレースと異なる乱数セットが生成されます。
ただし、デフォルトの mode='constant' は、これらの順位トレースの違いに影響されず、レイヤーを Keras functional モデルに埋め込む場合でも、追加の作業なしで渡せます。
random_tool = v1.keras.utils.DeterministicRandomTestTool()
with random_tool.scope():
graph = tf.Graph()
with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:
height, width = 299, 299
num_classes = 1000
inputs = tf.ones( (1, height, width, 3))
out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=True)
# Initialize the variables
sess.run(tf.compat.v1.global_variables_initializer())
# Get the outputs & regularization losses
reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)
tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))
tf1_output = sess.run(out)
print("Regularization loss:", tf1_regularization_loss)
Regularization loss: 1.2239965
height, width = 299, 299
num_classes = 1000
random_tool = v1.keras.utils.DeterministicRandomTestTool()
with random_tool.scope():
keras_input = tf.keras.Input(shape=(height, width, 3))
layer = InceptionResnetV2(num_classes)
model = tf.keras.Model(inputs=keras_input, outputs=layer(keras_input))
inputs = tf.ones((1, height, width, 3))
tf2_output, endpoints = model(inputs, training=True)
# Get the regularization loss
tf2_regularization_loss = tf.math.add_n(model.losses)
print("Regularization loss:", tf2_regularization_loss)
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py:1345: UserWarning: `layer.updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
warnings.warn('`layer.updates` will be removed in a future version. '
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/legacy_tf_layers/base.py:627: UserWarning: `layer.updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
self.updates, tf.compat.v1.GraphKeys.UPDATE_OPS
Regularization loss: tf.Tensor(1.2239964, shape=(), dtype=float32)
# Verify that the regularization loss and output both match
# when using the DeterministicRandomTestTool
np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)
np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)
ステップ 3b 、4b(オプション): 既存のチェックポイントを使用したテスト
上記のステップ 3 またはステップ 4 の後、既存の名前ベースのチェックポイントがある場合は、そこから開始するときに数値的等価性テストを実行すると便利です。これにより、レガシーチェックポイントの読み込みが正しく機能していることと、モデル自体が正しく機能していることの両方をテストできます。TF1.x チェックポイントの再利用ガイドでは、既存の TF1.x チェックポイントを再利用して TF2 チェックポイントに移行する方法について説明されています。
追加のテストとトラブルシューティング
数値的等価性テストをさらに追加する場合、勾配計算(またはオプティマイザーの更新)の一致を検証するテストを追加することもできます。
バックプロパゲーションと勾配の計算は、モデルのフォワードパスよりも浮動小数点の数値が不安定になる傾向があります。これは、トレーニングの分離されていない部分の等価性をテストすると、完全に Eager execution を実行した場合と TF1 Graph との間に大きな数値上の違いが見られる可能性があることを意味します。これは、Graph 内の部分式をより少ない数学的演算に置き換えたりする TensorFlow Graph の最適化が原因である可能性があります。
これが当てはまる可能性があるかどうかを特定するには、TF1 コードを、純粋な Eager 計算ではなく、tf.function(TF1 Graph のようなグラフ最適化パスを適用する)内で行われている TF2 計算と比較できます。または、TF1 計算の前に tf.config.optimizer.set_experimental_options を使用して "arithmetic_optimization" などの最適化パスを無効にして、結果が TF2 計算結果と数値的に近い値になるかどうかを確認することもできます。実際のトレーニングの実行では、パフォーマンス上の理由から最適化パスを有効にして tf.function を使用することを推薦しますが、数値等価性の単体テストではそれらを無効にすることが役立つ場合があります。
同様に、tf.compat.v1.train オプティマイザーと TF2 オプティマイザーは、それらが表す数式が同じであっても、TF2 オプティマイザーにはわずかに異なる浮動小数点数値プロパティがあります。これがトレーニングの実行で問題になる可能性は低いですが、等価性単体テストではより高い数値許容誤差が必要になる場合があります。
TensorFlow.org で表示
Google Colab で実行
GitHub でソースを表示
ノートブックをダウンロード