Upload validate_correctness.ipynb

#7
Files changed (1) hide show
  1. validate_correctness.ipynb +2498 -0
validate_correctness.ipynb ADDED
@@ -0,0 +1,2498 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "6bYaCABobL5q"
7
+ },
8
+ "source": [
9
+ "##### Copyright 2021 The TensorFlow Authors."
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 1,
15
+ "metadata": {
16
+ "cellView": "form",
17
+ "execution": {
18
+ "iopub.execute_input": "2022-12-14T03:38:18.997731Z",
19
+ "iopub.status.busy": "2022-12-14T03:38:18.997516Z",
20
+ "iopub.status.idle": "2022-12-14T03:38:19.001439Z",
21
+ "shell.execute_reply": "2022-12-14T03:38:19.000889Z"
22
+ },
23
+ "id": "FlUw7tSKbtg4"
24
+ },
25
+ "outputs": [],
26
+ "source": [
27
+ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
28
+ "# you may not use this file except in compliance with the License.\n",
29
+ "# You may obtain a copy of the License at\n",
30
+ "#\n",
31
+ "# https://www.apache.org/licenses/LICENSE-2.0\n",
32
+ "#\n",
33
+ "# Unless required by applicable law or agreed to in writing, software\n",
34
+ "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
35
+ "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
36
+ "# See the License for the specific language governing permissions and\n",
37
+ "# limitations under the License."
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "markdown",
42
+ "metadata": {
43
+ "id": "MfBg1C5NB3X0"
44
+ },
45
+ "source": [
46
+ "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
47
+ " <td>\n",
48
+ " <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/migrate/validate_correctness\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
49
+ " </td>\n",
50
+ " <td>\n",
51
+ " <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/migrate/validate_correctness.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
52
+ " </td>\n",
53
+ " <td>\n",
54
+ " <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/validate_correctness.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View on GitHub</a>\n",
55
+ " </td>\n",
56
+ " <td>\n",
57
+ " <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/guide/migrate/validate_correctness.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
58
+ " </td>\n",
59
+ "</table>"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "markdown",
64
+ "metadata": {
65
+ "id": "yAMJsAn7NDbc"
66
+ },
67
+ "source": [
68
+ "# Validating correctness & numerical equivalence"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "markdown",
73
+ "metadata": {
74
+ "id": "vyddl2kckpdN"
75
+ },
76
+ "source": [
77
+ "When migrating your TensorFlow code from TF1.x to TF2, it is a good practice to ensure that your migrated code behaves the same way in TF2 as it did in TF1.x. \n",
78
+ "\n",
79
+ "This guide covers migration code examples with the `tf.compat.v1.keras.utils.track_tf1_style_variables` modeling shim applied to `tf.keras.layers.Layer` methods. Read the [model mapping guide](./model_mapping.ipynb) to find out more about the TF2 modeling shims.\n",
80
+ "\n",
81
+ "This guide details approaches you can use to: \n",
82
+ "* Validate the correctness of the results obtained from training models using the migrated code \n",
83
+ "* Validate the numerical equivalence of your code across TensorFlow versions"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "markdown",
88
+ "metadata": {
89
+ "id": "TaYgaekzOAHf"
90
+ },
91
+ "source": [
92
+ "## Setup"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": 2,
98
+ "metadata": {
99
+ "execution": {
100
+ "iopub.execute_input": "2022-12-14T03:38:19.005343Z",
101
+ "iopub.status.busy": "2022-12-14T03:38:19.004839Z",
102
+ "iopub.status.idle": "2022-12-14T03:38:21.539848Z",
103
+ "shell.execute_reply": "2022-12-14T03:38:21.538733Z"
104
+ },
105
+ "id": "FkHX044DzVsd"
106
+ },
107
+ "outputs": [],
108
+ "source": [
109
+ "!pip uninstall -y -q tensorflow"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "execution_count": 3,
115
+ "metadata": {
116
+ "execution": {
117
+ "iopub.execute_input": "2022-12-14T03:38:21.544536Z",
118
+ "iopub.status.busy": "2022-12-14T03:38:21.543790Z",
119
+ "iopub.status.idle": "2022-12-14T03:38:44.840458Z",
120
+ "shell.execute_reply": "2022-12-14T03:38:44.839299Z"
121
+ },
122
+ "id": "M1ZgieHtyzKI"
123
+ },
124
+ "outputs": [],
125
+ "source": [
126
+ "# Install tf-nightly as the DeterministicRandomTestTool is available only in\n",
127
+ "# Tensorflow 2.8\n",
128
+ "!pip install -q tf-nightly"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": 4,
134
+ "metadata": {
135
+ "execution": {
136
+ "iopub.execute_input": "2022-12-14T03:38:44.845262Z",
137
+ "iopub.status.busy": "2022-12-14T03:38:44.844580Z",
138
+ "iopub.status.idle": "2022-12-14T03:38:46.873821Z",
139
+ "shell.execute_reply": "2022-12-14T03:38:46.872724Z"
140
+ },
141
+ "id": "ohYETq4NCX4J"
142
+ },
143
+ "outputs": [],
144
+ "source": [
145
+ "!pip install -q tf_slim"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": 5,
151
+ "metadata": {
152
+ "execution": {
153
+ "iopub.execute_input": "2022-12-14T03:38:46.878200Z",
154
+ "iopub.status.busy": "2022-12-14T03:38:46.877941Z",
155
+ "iopub.status.idle": "2022-12-14T03:38:49.308586Z",
156
+ "shell.execute_reply": "2022-12-14T03:38:49.307853Z"
157
+ },
158
+ "id": "MFey2HxcktP6"
159
+ },
160
+ "outputs": [
161
+ {
162
+ "name": "stderr",
163
+ "output_type": "stream",
164
+ "text": [
165
+ "2022-12-14 03:38:47.140140: E tensorflow/tsl/lib/monitoring/collection_registry.cc:81] Cannot register 2 metrics with the same name: /tensorflow/core/bfc_allocator_delay\n"
166
+ ]
167
+ }
168
+ ],
169
+ "source": [
170
+ "import tensorflow as tf\n",
171
+ "import tensorflow.compat.v1 as v1\n",
172
+ "\n",
173
+ "import numpy as np\n",
174
+ "import tf_slim as slim\n",
175
+ "import sys\n",
176
+ "\n",
177
+ "\n",
178
+ "from contextlib import contextmanager"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": 6,
184
+ "metadata": {
185
+ "execution": {
186
+ "iopub.execute_input": "2022-12-14T03:38:49.312559Z",
187
+ "iopub.status.busy": "2022-12-14T03:38:49.312144Z",
188
+ "iopub.status.idle": "2022-12-14T03:38:53.362680Z",
189
+ "shell.execute_reply": "2022-12-14T03:38:53.361644Z"
190
+ },
191
+ "id": "OriidSSAmRtW"
192
+ },
193
+ "outputs": [
194
+ {
195
+ "name": "stdout",
196
+ "output_type": "stream",
197
+ "text": [
198
+ "Cloning into 'models'...\r\n"
199
+ ]
200
+ },
201
+ {
202
+ "name": "stdout",
203
+ "output_type": "stream",
204
+ "text": [
205
+ "remote: Enumerating objects: 3590, done.\u001b[K\r\n",
206
+ "remote: Counting objects: 0% (1/3590)\u001b[K\r",
207
+ "remote: Counting objects: 1% (36/3590)\u001b[K\r",
208
+ "remote: Counting objects: 2% (72/3590)\u001b[K\r",
209
+ "remote: Counting objects: 3% (108/3590)\u001b[K\r",
210
+ "remote: Counting objects: 4% (144/3590)\u001b[K\r",
211
+ "remote: Counting objects: 5% (180/3590)\u001b[K\r",
212
+ "remote: Counting objects: 6% (216/3590)\u001b[K\r",
213
+ "remote: Counting objects: 7% (252/3590)\u001b[K\r",
214
+ "remote: Counting objects: 8% (288/3590)\u001b[K\r",
215
+ "remote: Counting objects: 9% (324/3590)\u001b[K\r",
216
+ "remote: Counting objects: 10% (359/3590)\u001b[K\r",
217
+ "remote: Counting objects: 11% (395/3590)\u001b[K\r",
218
+ "remote: Counting objects: 12% (431/3590)\u001b[K\r",
219
+ "remote: Counting objects: 13% (467/3590)\u001b[K\r",
220
+ "remote: Counting objects: 14% (503/3590)\u001b[K\r",
221
+ "remote: Counting objects: 15% (539/3590)\u001b[K\r",
222
+ "remote: Counting objects: 16% (575/3590)\u001b[K\r",
223
+ "remote: Counting objects: 17% (611/3590)\u001b[K\r",
224
+ "remote: Counting objects: 18% (647/3590)\u001b[K\r",
225
+ "remote: Counting objects: 19% (683/3590)\u001b[K\r",
226
+ "remote: Counting objects: 20% (718/3590)\u001b[K\r",
227
+ "remote: Counting objects: 21% (754/3590)\u001b[K\r",
228
+ "remote: Counting objects: 22% (790/3590)\u001b[K\r",
229
+ "remote: Counting objects: 23% (826/3590)\u001b[K\r",
230
+ "remote: Counting objects: 24% (862/3590)\u001b[K\r",
231
+ "remote: Counting objects: 25% (898/3590)\u001b[K\r",
232
+ "remote: Counting objects: 26% (934/3590)\u001b[K\r",
233
+ "remote: Counting objects: 27% (970/3590)\u001b[K\r",
234
+ "remote: Counting objects: 28% (1006/3590)\u001b[K\r",
235
+ "remote: Counting objects: 29% (1042/3590)\u001b[K\r",
236
+ "remote: Counting objects: 30% (1077/3590)\u001b[K\r",
237
+ "remote: Counting objects: 31% (1113/3590)\u001b[K\r",
238
+ "remote: Counting objects: 32% (1149/3590)\u001b[K\r",
239
+ "remote: Counting objects: 33% (1185/3590)\u001b[K\r",
240
+ "remote: Counting objects: 34% (1221/3590)\u001b[K\r",
241
+ "remote: Counting objects: 35% (1257/3590)\u001b[K\r",
242
+ "remote: Counting objects: 36% (1293/3590)\u001b[K\r",
243
+ "remote: Counting objects: 37% (1329/3590)\u001b[K\r",
244
+ "remote: Counting objects: 38% (1365/3590)\u001b[K\r",
245
+ "remote: Counting objects: 39% (1401/3590)\u001b[K\r",
246
+ "remote: Counting objects: 40% (1436/3590)\u001b[K\r",
247
+ "remote: Counting objects: 41% (1472/3590)\u001b[K\r",
248
+ "remote: Counting objects: 42% (1508/3590)\u001b[K\r",
249
+ "remote: Counting objects: 43% (1544/3590)\u001b[K\r",
250
+ "remote: Counting objects: 44% (1580/3590)\u001b[K\r",
251
+ "remote: Counting objects: 45% (1616/3590)\u001b[K\r",
252
+ "remote: Counting objects: 46% (1652/3590)\u001b[K\r",
253
+ "remote: Counting objects: 47% (1688/3590)\u001b[K\r",
254
+ "remote: Counting objects: 48% (1724/3590)\u001b[K\r",
255
+ "remote: Counting objects: 49% (1760/3590)\u001b[K\r",
256
+ "remote: Counting objects: 50% (1795/3590)\u001b[K\r",
257
+ "remote: Counting objects: 51% (1831/3590)\u001b[K\r",
258
+ "remote: Counting objects: 52% (1867/3590)\u001b[K\r",
259
+ "remote: Counting objects: 53% (1903/3590)\u001b[K\r",
260
+ "remote: Counting objects: 54% (1939/3590)\u001b[K\r",
261
+ "remote: Counting objects: 55% (1975/3590)\u001b[K\r",
262
+ "remote: Counting objects: 56% (2011/3590)\u001b[K\r",
263
+ "remote: Counting objects: 57% (2047/3590)\u001b[K\r",
264
+ "remote: Counting objects: 58% (2083/3590)\u001b[K\r",
265
+ "remote: Counting objects: 59% (2119/3590)\u001b[K\r",
266
+ "remote: Counting objects: 60% (2154/3590)\u001b[K\r",
267
+ "remote: Counting objects: 61% (2190/3590)\u001b[K\r",
268
+ "remote: Counting objects: 62% (2226/3590)\u001b[K\r",
269
+ "remote: Counting objects: 63% (2262/3590)\u001b[K\r",
270
+ "remote: Counting objects: 64% (2298/3590)\u001b[K\r",
271
+ "remote: Counting objects: 65% (2334/3590)\u001b[K\r",
272
+ "remote: Counting objects: 66% (2370/3590)\u001b[K\r",
273
+ "remote: Counting objects: 67% (2406/3590)\u001b[K\r",
274
+ "remote: Counting objects: 68% (2442/3590)\u001b[K\r",
275
+ "remote: Counting objects: 69% (2478/3590)\u001b[K\r",
276
+ "remote: Counting objects: 70% (2513/3590)\u001b[K\r",
277
+ "remote: Counting objects: 71% (2549/3590)\u001b[K\r",
278
+ "remote: Counting objects: 72% (2585/3590)\u001b[K\r",
279
+ "remote: Counting objects: 73% (2621/3590)\u001b[K\r",
280
+ "remote: Counting objects: 74% (2657/3590)\u001b[K\r",
281
+ "remote: Counting objects: 75% (2693/3590)\u001b[K\r",
282
+ "remote: Counting objects: 76% (2729/3590)\u001b[K\r",
283
+ "remote: Counting objects: 77% (2765/3590)\u001b[K\r",
284
+ "remote: Counting objects: 78% (2801/3590)\u001b[K\r",
285
+ "remote: Counting objects: 79% (2837/3590)\u001b[K\r",
286
+ "remote: Counting objects: 80% (2872/3590)\u001b[K\r",
287
+ "remote: Counting objects: 81% (2908/3590)\u001b[K\r",
288
+ "remote: Counting objects: 82% (2944/3590)\u001b[K\r",
289
+ "remote: Counting objects: 83% (2980/3590)\u001b[K\r",
290
+ "remote: Counting objects: 84% (3016/3590)\u001b[K\r",
291
+ "remote: Counting objects: 85% (3052/3590)\u001b[K\r",
292
+ "remote: Counting objects: 86% (3088/3590)\u001b[K\r",
293
+ "remote: Counting objects: 87% (3124/3590)\u001b[K\r",
294
+ "remote: Counting objects: 88% (3160/3590)\u001b[K\r",
295
+ "remote: Counting objects: 89% (3196/3590)\u001b[K\r",
296
+ "remote: Counting objects: 90% (3231/3590)\u001b[K\r",
297
+ "remote: Counting objects: 91% (3267/3590)\u001b[K\r",
298
+ "remote: Counting objects: 92% (3303/3590)\u001b[K\r",
299
+ "remote: Counting objects: 93% (3339/3590)\u001b[K\r",
300
+ "remote: Counting objects: 94% (3375/3590)\u001b[K\r",
301
+ "remote: Counting objects: 95% (3411/3590)\u001b[K\r",
302
+ "remote: Counting objects: 96% (3447/3590)\u001b[K\r",
303
+ "remote: Counting objects: 97% (3483/3590)\u001b[K\r",
304
+ "remote: Counting objects: 98% (3519/3590)\u001b[K\r",
305
+ "remote: Counting objects: 99% (3555/3590)\u001b[K\r",
306
+ "remote: Counting objects: 100% (3590/3590)\u001b[K\r",
307
+ "remote: Counting objects: 100% (3590/3590), done.\u001b[K\r\n",
308
+ "remote: Compressing objects: 0% (1/3005)\u001b[K\r",
309
+ "remote: Compressing objects: 1% (31/3005)\u001b[K\r",
310
+ "remote: Compressing objects: 2% (61/3005)\u001b[K\r"
311
+ ]
312
+ },
313
+ {
314
+ "name": "stdout",
315
+ "output_type": "stream",
316
+ "text": [
317
+ "remote: Compressing objects: 3% (91/3005)\u001b[K\r",
318
+ "remote: Compressing objects: 4% (121/3005)\u001b[K\r",
319
+ "remote: Compressing objects: 5% (151/3005)\u001b[K\r",
320
+ "remote: Compressing objects: 6% (181/3005)\u001b[K\r"
321
+ ]
322
+ },
323
+ {
324
+ "name": "stdout",
325
+ "output_type": "stream",
326
+ "text": [
327
+ "remote: Compressing objects: 7% (211/3005)\u001b[K\r",
328
+ "remote: Compressing objects: 8% (241/3005)\u001b[K\r",
329
+ "remote: Compressing objects: 9% (271/3005)\u001b[K\r",
330
+ "remote: Compressing objects: 10% (301/3005)\u001b[K\r"
331
+ ]
332
+ },
333
+ {
334
+ "name": "stdout",
335
+ "output_type": "stream",
336
+ "text": [
337
+ "remote: Compressing objects: 11% (331/3005)\u001b[K\r",
338
+ "remote: Compressing objects: 12% (361/3005)\u001b[K\r",
339
+ "remote: Compressing objects: 13% (391/3005)\u001b[K\r"
340
+ ]
341
+ },
342
+ {
343
+ "name": "stdout",
344
+ "output_type": "stream",
345
+ "text": [
346
+ "remote: Compressing objects: 14% (421/3005)\u001b[K\r",
347
+ "remote: Compressing objects: 15% (451/3005)\u001b[K\r",
348
+ "remote: Compressing objects: 16% (481/3005)\u001b[K\r",
349
+ "remote: Compressing objects: 17% (511/3005)\u001b[K\r",
350
+ "remote: Compressing objects: 18% (541/3005)\u001b[K\r"
351
+ ]
352
+ },
353
+ {
354
+ "name": "stdout",
355
+ "output_type": "stream",
356
+ "text": [
357
+ "remote: Compressing objects: 19% (571/3005)\u001b[K\r",
358
+ "remote: Compressing objects: 20% (601/3005)\u001b[K\r",
359
+ "remote: Compressing objects: 21% (632/3005)\u001b[K\r",
360
+ "remote: Compressing objects: 22% (662/3005)\u001b[K\r"
361
+ ]
362
+ },
363
+ {
364
+ "name": "stdout",
365
+ "output_type": "stream",
366
+ "text": [
367
+ "remote: Compressing objects: 23% (692/3005)\u001b[K\r",
368
+ "remote: Compressing objects: 24% (722/3005)\u001b[K\r"
369
+ ]
370
+ },
371
+ {
372
+ "name": "stdout",
373
+ "output_type": "stream",
374
+ "text": [
375
+ "remote: Compressing objects: 25% (752/3005)\u001b[K\r",
376
+ "remote: Compressing objects: 26% (782/3005)\u001b[K\r",
377
+ "remote: Compressing objects: 27% (812/3005)\u001b[K\r"
378
+ ]
379
+ },
380
+ {
381
+ "name": "stdout",
382
+ "output_type": "stream",
383
+ "text": [
384
+ "remote: Compressing objects: 28% (842/3005)\u001b[K\r",
385
+ "remote: Compressing objects: 29% (872/3005)\u001b[K\r"
386
+ ]
387
+ },
388
+ {
389
+ "name": "stdout",
390
+ "output_type": "stream",
391
+ "text": [
392
+ "remote: Compressing objects: 30% (902/3005)\u001b[K\r",
393
+ "remote: Compressing objects: 31% (932/3005)\u001b[K\r",
394
+ "remote: Compressing objects: 32% (962/3005)\u001b[K\r",
395
+ "remote: Compressing objects: 33% (992/3005)\u001b[K\r"
396
+ ]
397
+ },
398
+ {
399
+ "name": "stdout",
400
+ "output_type": "stream",
401
+ "text": [
402
+ "remote: Compressing objects: 34% (1022/3005)\u001b[K\r"
403
+ ]
404
+ },
405
+ {
406
+ "name": "stdout",
407
+ "output_type": "stream",
408
+ "text": [
409
+ "remote: Compressing objects: 35% (1052/3005)\u001b[K\r",
410
+ "remote: Compressing objects: 36% (1082/3005)\u001b[K\r",
411
+ "remote: Compressing objects: 37% (1112/3005)\u001b[K\r",
412
+ "remote: Compressing objects: 38% (1142/3005)\u001b[K\r"
413
+ ]
414
+ },
415
+ {
416
+ "name": "stdout",
417
+ "output_type": "stream",
418
+ "text": [
419
+ "remote: Compressing objects: 39% (1172/3005)\u001b[K\r",
420
+ "remote: Compressing objects: 40% (1202/3005)\u001b[K\r"
421
+ ]
422
+ },
423
+ {
424
+ "name": "stdout",
425
+ "output_type": "stream",
426
+ "text": [
427
+ "remote: Compressing objects: 41% (1233/3005)\u001b[K\r",
428
+ "remote: Compressing objects: 42% (1263/3005)\u001b[K\r",
429
+ "remote: Compressing objects: 43% (1293/3005)\u001b[K\r"
430
+ ]
431
+ },
432
+ {
433
+ "name": "stdout",
434
+ "output_type": "stream",
435
+ "text": [
436
+ "remote: Compressing objects: 44% (1323/3005)\u001b[K\r",
437
+ "remote: Compressing objects: 45% (1353/3005)\u001b[K\r",
438
+ "remote: Compressing objects: 46% (1383/3005)\u001b[K\r"
439
+ ]
440
+ },
441
+ {
442
+ "name": "stdout",
443
+ "output_type": "stream",
444
+ "text": [
445
+ "remote: Compressing objects: 47% (1413/3005)\u001b[K\r",
446
+ "remote: Compressing objects: 48% (1443/3005)\u001b[K\r",
447
+ "remote: Compressing objects: 49% (1473/3005)\u001b[K\r",
448
+ "remote: Compressing objects: 50% (1503/3005)\u001b[K\r"
449
+ ]
450
+ },
451
+ {
452
+ "name": "stdout",
453
+ "output_type": "stream",
454
+ "text": [
455
+ "remote: Compressing objects: 51% (1533/3005)\u001b[K\r",
456
+ "remote: Compressing objects: 52% (1563/3005)\u001b[K\r",
457
+ "remote: Compressing objects: 53% (1593/3005)\u001b[K\r",
458
+ "remote: Compressing objects: 54% (1623/3005)\u001b[K\r"
459
+ ]
460
+ },
461
+ {
462
+ "name": "stdout",
463
+ "output_type": "stream",
464
+ "text": [
465
+ "remote: Compressing objects: 55% (1653/3005)\u001b[K\r",
466
+ "remote: Compressing objects: 56% (1683/3005)\u001b[K\r"
467
+ ]
468
+ },
469
+ {
470
+ "name": "stdout",
471
+ "output_type": "stream",
472
+ "text": [
473
+ "remote: Compressing objects: 57% (1713/3005)\u001b[K\r",
474
+ "remote: Compressing objects: 58% (1743/3005)\u001b[K\r",
475
+ "remote: Compressing objects: 59% (1773/3005)\u001b[K\r"
476
+ ]
477
+ },
478
+ {
479
+ "name": "stdout",
480
+ "output_type": "stream",
481
+ "text": [
482
+ "remote: Compressing objects: 60% (1803/3005)\u001b[K\r",
483
+ "remote: Compressing objects: 61% (1834/3005)\u001b[K\r",
484
+ "remote: Compressing objects: 62% (1864/3005)\u001b[K\r"
485
+ ]
486
+ },
487
+ {
488
+ "name": "stdout",
489
+ "output_type": "stream",
490
+ "text": [
491
+ "remote: Compressing objects: 63% (1894/3005)\u001b[K\r",
492
+ "remote: Compressing objects: 64% (1924/3005)\u001b[K\r",
493
+ "remote: Compressing objects: 65% (1954/3005)\u001b[K\r",
494
+ "remote: Compressing objects: 66% (1984/3005)\u001b[K\r",
495
+ "remote: Compressing objects: 67% (2014/3005)\u001b[K\r",
496
+ "remote: Compressing objects: 68% (2044/3005)\u001b[K\r",
497
+ "remote: Compressing objects: 69% (2074/3005)\u001b[K\r",
498
+ "remote: Compressing objects: 70% (2104/3005)\u001b[K\r",
499
+ "remote: Compressing objects: 71% (2134/3005)\u001b[K\r",
500
+ "remote: Compressing objects: 72% (2164/3005)\u001b[K\r",
501
+ "remote: Compressing objects: 73% (2194/3005)\u001b[K\r",
502
+ "remote: Compressing objects: 73% (2207/3005)\u001b[K\r",
503
+ "remote: Compressing objects: 74% (2224/3005)\u001b[K\r",
504
+ "remote: Compressing objects: 75% (2254/3005)\u001b[K\r"
505
+ ]
506
+ },
507
+ {
508
+ "name": "stdout",
509
+ "output_type": "stream",
510
+ "text": [
511
+ "remote: Compressing objects: 76% (2284/3005)\u001b[K\r",
512
+ "remote: Compressing objects: 77% (2314/3005)\u001b[K\r",
513
+ "remote: Compressing objects: 78% (2344/3005)\u001b[K\r",
514
+ "remote: Compressing objects: 79% (2374/3005)\u001b[K\r",
515
+ "remote: Compressing objects: 80% (2404/3005)\u001b[K\r"
516
+ ]
517
+ },
518
+ {
519
+ "name": "stdout",
520
+ "output_type": "stream",
521
+ "text": [
522
+ "remote: Compressing objects: 81% (2435/3005)\u001b[K\r",
523
+ "remote: Compressing objects: 82% (2465/3005)\u001b[K\r",
524
+ "remote: Compressing objects: 83% (2495/3005)\u001b[K\r",
525
+ "remote: Compressing objects: 84% (2525/3005)\u001b[K\r",
526
+ "remote: Compressing objects: 85% (2555/3005)\u001b[K\r",
527
+ "remote: Compressing objects: 86% (2585/3005)\u001b[K\r",
528
+ "remote: Compressing objects: 87% (2615/3005)\u001b[K\r",
529
+ "remote: Compressing objects: 88% (2645/3005)\u001b[K\r",
530
+ "remote: Compressing objects: 89% (2675/3005)\u001b[K\r",
531
+ "remote: Compressing objects: 90% (2705/3005)\u001b[K\r",
532
+ "remote: Compressing objects: 91% (2735/3005)\u001b[K\r",
533
+ "remote: Compressing objects: 92% (2765/3005)\u001b[K\r",
534
+ "remote: Compressing objects: 93% (2795/3005)\u001b[K\r",
535
+ "remote: Compressing objects: 94% (2825/3005)\u001b[K\r",
536
+ "remote: Compressing objects: 95% (2855/3005)\u001b[K\r",
537
+ "remote: Compressing objects: 96% (2885/3005)\u001b[K\r",
538
+ "remote: Compressing objects: 97% (2915/3005)\u001b[K\r",
539
+ "remote: Compressing objects: 98% (2945/3005)\u001b[K\r",
540
+ "remote: Compressing objects: 99% (2975/3005)\u001b[K\r",
541
+ "remote: Compressing objects: 100% (3005/3005)\u001b[K\r",
542
+ "remote: Compressing objects: 100% (3005/3005), done.\u001b[K\r\n",
543
+ "Receiving objects: 0% (1/3590)\r",
544
+ "Receiving objects: 1% (36/3590)\r",
545
+ "Receiving objects: 2% (72/3590)\r"
546
+ ]
547
+ },
548
+ {
549
+ "name": "stdout",
550
+ "output_type": "stream",
551
+ "text": [
552
+ "Receiving objects: 3% (108/3590)\r",
553
+ "Receiving objects: 4% (144/3590)\r",
554
+ "Receiving objects: 5% (180/3590)\r",
555
+ "Receiving objects: 6% (216/3590)\r",
556
+ "Receiving objects: 7% (252/3590)\r",
557
+ "Receiving objects: 8% (288/3590)\r",
558
+ "Receiving objects: 9% (324/3590)\r"
559
+ ]
560
+ },
561
+ {
562
+ "name": "stdout",
563
+ "output_type": "stream",
564
+ "text": [
565
+ "Receiving objects: 10% (359/3590)\r",
566
+ "Receiving objects: 11% (395/3590)\r",
567
+ "Receiving objects: 12% (431/3590)\r",
568
+ "Receiving objects: 13% (467/3590)\r",
569
+ "Receiving objects: 14% (503/3590)\r",
570
+ "Receiving objects: 15% (539/3590)\r",
571
+ "Receiving objects: 16% (575/3590)\r"
572
+ ]
573
+ },
574
+ {
575
+ "name": "stdout",
576
+ "output_type": "stream",
577
+ "text": [
578
+ "Receiving objects: 17% (611/3590)\r",
579
+ "Receiving objects: 18% (647/3590)\r",
580
+ "Receiving objects: 19% (683/3590)\r",
581
+ "Receiving objects: 20% (718/3590)\r",
582
+ "Receiving objects: 21% (754/3590)\r",
583
+ "Receiving objects: 22% (790/3590)\r",
584
+ "Receiving objects: 23% (826/3590)\r",
585
+ "Receiving objects: 24% (862/3590)\r",
586
+ "Receiving objects: 25% (898/3590)\r",
587
+ "Receiving objects: 26% (934/3590)\r",
588
+ "Receiving objects: 27% (970/3590)\r"
589
+ ]
590
+ },
591
+ {
592
+ "name": "stdout",
593
+ "output_type": "stream",
594
+ "text": [
595
+ "Receiving objects: 28% (1006/3590)\r",
596
+ "Receiving objects: 29% (1042/3590)\r",
597
+ "Receiving objects: 30% (1077/3590)\r",
598
+ "Receiving objects: 31% (1113/3590)\r",
599
+ "Receiving objects: 32% (1149/3590)\r",
600
+ "Receiving objects: 33% (1185/3590)\r",
601
+ "Receiving objects: 34% (1221/3590)\r",
602
+ "Receiving objects: 35% (1257/3590)\r",
603
+ "Receiving objects: 36% (1293/3590)\r",
604
+ "Receiving objects: 37% (1329/3590)\r"
605
+ ]
606
+ },
607
+ {
608
+ "name": "stdout",
609
+ "output_type": "stream",
610
+ "text": [
611
+ "Receiving objects: 38% (1365/3590)\r",
612
+ "Receiving objects: 39% (1401/3590)\r",
613
+ "Receiving objects: 40% (1436/3590)\r"
614
+ ]
615
+ },
616
+ {
617
+ "name": "stdout",
618
+ "output_type": "stream",
619
+ "text": [
620
+ "Receiving objects: 41% (1472/3590), 4.73 MiB | 9.45 MiB/s\r",
621
+ "Receiving objects: 42% (1508/3590), 4.73 MiB | 9.45 MiB/s\r"
622
+ ]
623
+ },
624
+ {
625
+ "name": "stdout",
626
+ "output_type": "stream",
627
+ "text": [
628
+ "Receiving objects: 43% (1544/3590), 4.73 MiB | 9.45 MiB/s\r",
629
+ "Receiving objects: 44% (1580/3590), 4.73 MiB | 9.45 MiB/s\r",
630
+ "Receiving objects: 45% (1616/3590), 4.73 MiB | 9.45 MiB/s\r",
631
+ "Receiving objects: 46% (1652/3590), 4.73 MiB | 9.45 MiB/s\r",
632
+ "Receiving objects: 47% (1688/3590), 4.73 MiB | 9.45 MiB/s\r",
633
+ "Receiving objects: 48% (1724/3590), 4.73 MiB | 9.45 MiB/s\r",
634
+ "Receiving objects: 49% (1760/3590), 4.73 MiB | 9.45 MiB/s\r",
635
+ "Receiving objects: 50% (1795/3590), 4.73 MiB | 9.45 MiB/s\r",
636
+ "Receiving objects: 51% (1831/3590), 4.73 MiB | 9.45 MiB/s\r",
637
+ "Receiving objects: 52% (1867/3590), 4.73 MiB | 9.45 MiB/s\r",
638
+ "Receiving objects: 53% (1903/3590), 4.73 MiB | 9.45 MiB/s\r",
639
+ "Receiving objects: 54% (1939/3590), 4.73 MiB | 9.45 MiB/s\r",
640
+ "Receiving objects: 55% (1975/3590), 4.73 MiB | 9.45 MiB/s\r"
641
+ ]
642
+ },
643
+ {
644
+ "name": "stdout",
645
+ "output_type": "stream",
646
+ "text": [
647
+ "Receiving objects: 56% (2011/3590), 4.73 MiB | 9.45 MiB/s\r"
648
+ ]
649
+ },
650
+ {
651
+ "name": "stdout",
652
+ "output_type": "stream",
653
+ "text": [
654
+ "Receiving objects: 57% (2047/3590), 4.73 MiB | 9.45 MiB/s\r",
655
+ "Receiving objects: 58% (2083/3590), 4.73 MiB | 9.45 MiB/s\r",
656
+ "Receiving objects: 59% (2119/3590), 4.73 MiB | 9.45 MiB/s\r",
657
+ "Receiving objects: 60% (2154/3590), 4.73 MiB | 9.45 MiB/s\r",
658
+ "Receiving objects: 61% (2190/3590), 4.73 MiB | 9.45 MiB/s\r",
659
+ "Receiving objects: 62% (2226/3590), 4.73 MiB | 9.45 MiB/s\r"
660
+ ]
661
+ },
662
+ {
663
+ "name": "stdout",
664
+ "output_type": "stream",
665
+ "text": [
666
+ "Receiving objects: 63% (2262/3590), 4.73 MiB | 9.45 MiB/s\r",
667
+ "Receiving objects: 64% (2298/3590), 4.73 MiB | 9.45 MiB/s\r",
668
+ "Receiving objects: 65% (2334/3590), 4.73 MiB | 9.45 MiB/s\r",
669
+ "Receiving objects: 66% (2370/3590), 4.73 MiB | 9.45 MiB/s\r",
670
+ "Receiving objects: 67% (2406/3590), 4.73 MiB | 9.45 MiB/s\r",
671
+ "Receiving objects: 68% (2442/3590), 4.73 MiB | 9.45 MiB/s\r",
672
+ "Receiving objects: 69% (2478/3590), 4.73 MiB | 9.45 MiB/s\r",
673
+ "Receiving objects: 70% (2513/3590), 4.73 MiB | 9.45 MiB/s\r",
674
+ "Receiving objects: 71% (2549/3590), 4.73 MiB | 9.45 MiB/s\r",
675
+ "Receiving objects: 72% (2585/3590), 4.73 MiB | 9.45 MiB/s\r"
676
+ ]
677
+ },
678
+ {
679
+ "name": "stdout",
680
+ "output_type": "stream",
681
+ "text": [
682
+ "Receiving objects: 73% (2621/3590), 4.73 MiB | 9.45 MiB/s\r",
683
+ "Receiving objects: 74% (2657/3590), 4.73 MiB | 9.45 MiB/s\r",
684
+ "Receiving objects: 75% (2693/3590), 4.73 MiB | 9.45 MiB/s\r",
685
+ "Receiving objects: 76% (2729/3590), 4.73 MiB | 9.45 MiB/s\r",
686
+ "Receiving objects: 76% (2763/3590), 27.43 MiB | 27.42 MiB/s\r",
687
+ "Receiving objects: 77% (2765/3590), 27.43 MiB | 27.42 MiB/s\r",
688
+ "Receiving objects: 78% (2801/3590), 27.43 MiB | 27.42 MiB/s\r"
689
+ ]
690
+ },
691
+ {
692
+ "name": "stdout",
693
+ "output_type": "stream",
694
+ "text": [
695
+ "Receiving objects: 79% (2837/3590), 27.43 MiB | 27.42 MiB/s\r",
696
+ "Receiving objects: 80% (2872/3590), 27.43 MiB | 27.42 MiB/s\r",
697
+ "Receiving objects: 81% (2908/3590), 27.43 MiB | 27.42 MiB/s\r",
698
+ "Receiving objects: 82% (2944/3590), 27.43 MiB | 27.42 MiB/s\r",
699
+ "Receiving objects: 83% (2980/3590), 27.43 MiB | 27.42 MiB/s\r",
700
+ "Receiving objects: 84% (3016/3590), 27.43 MiB | 27.42 MiB/s\r",
701
+ "Receiving objects: 85% (3052/3590), 27.43 MiB | 27.42 MiB/s\r",
702
+ "Receiving objects: 86% (3088/3590), 27.43 MiB | 27.42 MiB/s\r",
703
+ "Receiving objects: 87% (3124/3590), 27.43 MiB | 27.42 MiB/s\r"
704
+ ]
705
+ },
706
+ {
707
+ "name": "stdout",
708
+ "output_type": "stream",
709
+ "text": [
710
+ "Receiving objects: 88% (3160/3590), 27.43 MiB | 27.42 MiB/s\r"
711
+ ]
712
+ },
713
+ {
714
+ "name": "stdout",
715
+ "output_type": "stream",
716
+ "text": [
717
+ "Receiving objects: 89% (3196/3590), 27.43 MiB | 27.42 MiB/s\r",
718
+ "Receiving objects: 90% (3231/3590), 27.43 MiB | 27.42 MiB/s\r"
719
+ ]
720
+ },
721
+ {
722
+ "name": "stdout",
723
+ "output_type": "stream",
724
+ "text": [
725
+ "Receiving objects: 91% (3267/3590), 27.43 MiB | 27.42 MiB/s\r",
726
+ "Receiving objects: 92% (3303/3590), 27.43 MiB | 27.42 MiB/s\r",
727
+ "Receiving objects: 93% (3339/3590), 27.43 MiB | 27.42 MiB/s\r",
728
+ "Receiving objects: 94% (3375/3590), 27.43 MiB | 27.42 MiB/s\r",
729
+ "Receiving objects: 95% (3411/3590), 27.43 MiB | 27.42 MiB/s\r",
730
+ "Receiving objects: 96% (3447/3590), 27.43 MiB | 27.42 MiB/s\r",
731
+ "Receiving objects: 97% (3483/3590), 27.43 MiB | 27.42 MiB/s\r",
732
+ "Receiving objects: 98% (3519/3590), 27.43 MiB | 27.42 MiB/s\r",
733
+ "remote: Total 3590 (delta 943), reused 1501 (delta 531), pack-reused 0\u001b[K\r\n",
734
+ "Receiving objects: 99% (3555/3590), 27.43 MiB | 27.42 MiB/s\r",
735
+ "Receiving objects: 100% (3590/3590), 27.43 MiB | 27.42 MiB/s\r",
736
+ "Receiving objects: 100% (3590/3590), 47.08 MiB | 35.58 MiB/s, done.\r\n",
737
+ "Resolving deltas: 0% (0/943)\r",
738
+ "Resolving deltas: 1% (16/943)\r",
739
+ "Resolving deltas: 3% (31/943)\r",
740
+ "Resolving deltas: 5% (49/943)\r",
741
+ "Resolving deltas: 6% (57/943)\r",
742
+ "Resolving deltas: 7% (68/943)\r",
743
+ "Resolving deltas: 8% (76/943)\r",
744
+ "Resolving deltas: 9% (86/943)\r",
745
+ "Resolving deltas: 10% (95/943)\r",
746
+ "Resolving deltas: 11% (104/943)\r",
747
+ "Resolving deltas: 12% (116/943)\r",
748
+ "Resolving deltas: 13% (127/943)\r",
749
+ "Resolving deltas: 14% (134/943)\r",
750
+ "Resolving deltas: 15% (145/943)\r",
751
+ "Resolving deltas: 16% (152/943)\r",
752
+ "Resolving deltas: 17% (166/943)\r",
753
+ "Resolving deltas: 18% (176/943)\r",
754
+ "Resolving deltas: 19% (183/943)\r",
755
+ "Resolving deltas: 20% (193/943)\r",
756
+ "Resolving deltas: 21% (200/943)\r",
757
+ "Resolving deltas: 22% (208/943)\r",
758
+ "Resolving deltas: 23% (218/943)\r",
759
+ "Resolving deltas: 24% (227/943)\r",
760
+ "Resolving deltas: 25% (237/943)\r",
761
+ "Resolving deltas: 26% (250/943)\r",
762
+ "Resolving deltas: 27% (257/943)\r",
763
+ "Resolving deltas: 29% (281/943)\r",
764
+ "Resolving deltas: 30% (284/943)\r",
765
+ "Resolving deltas: 31% (295/943)\r",
766
+ "Resolving deltas: 32% (303/943)\r",
767
+ "Resolving deltas: 33% (313/943)\r",
768
+ "Resolving deltas: 34% (321/943)\r",
769
+ "Resolving deltas: 35% (335/943)\r",
770
+ "Resolving deltas: 36% (342/943)\r",
771
+ "Resolving deltas: 37% (353/943)\r",
772
+ "Resolving deltas: 38% (359/943)\r",
773
+ "Resolving deltas: 39% (368/943)\r",
774
+ "Resolving deltas: 40% (378/943)\r"
775
+ ]
776
+ },
777
+ {
778
+ "name": "stdout",
779
+ "output_type": "stream",
780
+ "text": [
781
+ "Resolving deltas: 41% (387/943)\r",
782
+ "Resolving deltas: 42% (399/943)\r",
783
+ "Resolving deltas: 43% (406/943)\r",
784
+ "Resolving deltas: 45% (426/943)\r",
785
+ "Resolving deltas: 46% (440/943)\r",
786
+ "Resolving deltas: 47% (447/943)\r",
787
+ "Resolving deltas: 48% (454/943)\r",
788
+ "Resolving deltas: 49% (463/943)\r",
789
+ "Resolving deltas: 50% (472/943)\r",
790
+ "Resolving deltas: 51% (481/943)\r",
791
+ "Resolving deltas: 52% (491/943)\r",
792
+ "Resolving deltas: 53% (500/943)\r",
793
+ "Resolving deltas: 54% (510/943)\r",
794
+ "Resolving deltas: 55% (519/943)\r",
795
+ "Resolving deltas: 56% (533/943)\r",
796
+ "Resolving deltas: 57% (538/943)\r",
797
+ "Resolving deltas: 58% (550/943)\r",
798
+ "Resolving deltas: 59% (557/943)\r",
799
+ "Resolving deltas: 60% (568/943)\r",
800
+ "Resolving deltas: 61% (579/943)\r",
801
+ "Resolving deltas: 62% (585/943)\r",
802
+ "Resolving deltas: 63% (595/943)\r",
803
+ "Resolving deltas: 64% (604/943)\r",
804
+ "Resolving deltas: 65% (613/943)\r",
805
+ "Resolving deltas: 66% (624/943)\r",
806
+ "Resolving deltas: 67% (632/943)\r",
807
+ "Resolving deltas: 68% (644/943)\r",
808
+ "Resolving deltas: 69% (651/943)\r",
809
+ "Resolving deltas: 70% (668/943)\r",
810
+ "Resolving deltas: 72% (680/943)\r",
811
+ "Resolving deltas: 73% (694/943)\r",
812
+ "Resolving deltas: 74% (699/943)\r",
813
+ "Resolving deltas: 75% (708/943)\r",
814
+ "Resolving deltas: 76% (724/943)\r",
815
+ "Resolving deltas: 77% (733/943)\r",
816
+ "Resolving deltas: 78% (741/943)\r",
817
+ "Resolving deltas: 79% (747/943)\r",
818
+ "Resolving deltas: 80% (756/943)\r",
819
+ "Resolving deltas: 81% (764/943)\r",
820
+ "Resolving deltas: 82% (774/943)\r",
821
+ "Resolving deltas: 83% (783/943)\r",
822
+ "Resolving deltas: 84% (793/943)\r",
823
+ "Resolving deltas: 85% (802/943)\r",
824
+ "Resolving deltas: 86% (811/943)\r",
825
+ "Resolving deltas: 87% (826/943)\r",
826
+ "Resolving deltas: 88% (831/943)\r",
827
+ "Resolving deltas: 89% (842/943)\r",
828
+ "Resolving deltas: 90% (852/943)\r",
829
+ "Resolving deltas: 91% (862/943)\r",
830
+ "Resolving deltas: 92% (868/943)\r",
831
+ "Resolving deltas: 93% (877/943)\r",
832
+ "Resolving deltas: 94% (888/943)\r",
833
+ "Resolving deltas: 95% (897/943)\r",
834
+ "Resolving deltas: 96% (908/943)\r",
835
+ "Resolving deltas: 97% (915/943)\r",
836
+ "Resolving deltas: 98% (925/943)\r",
837
+ "Resolving deltas: 99% (936/943)\r"
838
+ ]
839
+ },
840
+ {
841
+ "name": "stdout",
842
+ "output_type": "stream",
843
+ "text": [
844
+ "Resolving deltas: 100% (943/943)\r",
845
+ "Resolving deltas: 100% (943/943), done.\r\n"
846
+ ]
847
+ }
848
+ ],
849
+ "source": [
850
+ "!git clone --depth=1 https://github.com/tensorflow/models.git\n",
851
+ "import models.research.slim.nets.inception_resnet_v2 as inception"
852
+ ]
853
+ },
854
+ {
855
+ "cell_type": "markdown",
856
+ "metadata": {
857
+ "id": "TRacYNxnN-nk"
858
+ },
859
+ "source": [
860
+ "If you're putting a nontrivial chunk of forward pass code into the shim, you want to know that it is behaving the same way as it did in TF1.x. For example, consider trying to put an entire TF-Slim Inception-Resnet-v2 model into the shim as such:"
861
+ ]
862
+ },
863
+ {
864
+ "cell_type": "code",
865
+ "execution_count": 7,
866
+ "metadata": {
867
+ "execution": {
868
+ "iopub.execute_input": "2022-12-14T03:38:53.366962Z",
869
+ "iopub.status.busy": "2022-12-14T03:38:53.366681Z",
870
+ "iopub.status.idle": "2022-12-14T03:38:53.370866Z",
871
+ "shell.execute_reply": "2022-12-14T03:38:53.370295Z"
872
+ },
873
+ "id": "IijQZtxeaErg"
874
+ },
875
+ "outputs": [],
876
+ "source": [
877
+ "# TF1 Inception resnet v2 forward pass based on slim layers\n",
878
+ "def inception_resnet_v2(inputs, num_classes, is_training):\n",
879
+ " with slim.arg_scope(\n",
880
+ " inception.inception_resnet_v2_arg_scope(batch_norm_scale=True)):\n",
881
+ " return inception.inception_resnet_v2(inputs, num_classes, is_training=is_training)"
882
+ ]
883
+ },
884
+ {
885
+ "cell_type": "code",
886
+ "execution_count": 8,
887
+ "metadata": {
888
+ "execution": {
889
+ "iopub.execute_input": "2022-12-14T03:38:53.374046Z",
890
+ "iopub.status.busy": "2022-12-14T03:38:53.373505Z",
891
+ "iopub.status.idle": "2022-12-14T03:38:53.697683Z",
892
+ "shell.execute_reply": "2022-12-14T03:38:53.697035Z"
893
+ },
894
+ "id": "Z_-Oxg9OlSd4"
895
+ },
896
+ "outputs": [
897
+ {
898
+ "name": "stdout",
899
+ "output_type": "stream",
900
+ "text": [
901
+ "WARNING:tensorflow:From /tmpfs/tmp/ipykernel_118303/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.\n",
902
+ "\n"
903
+ ]
904
+ }
905
+ ],
906
+ "source": [
907
+ "class InceptionResnetV2(tf.keras.layers.Layer):\n",
908
+ " \"\"\"Slim InceptionResnetV2 forward pass as a Keras layer\"\"\"\n",
909
+ "\n",
910
+ " def __init__(self, num_classes, **kwargs):\n",
911
+ " super().__init__(**kwargs)\n",
912
+ " self.num_classes = num_classes\n",
913
+ "\n",
914
+ " @tf.compat.v1.keras.utils.track_tf1_style_variables\n",
915
+ " def call(self, inputs, training=None):\n",
916
+ " is_training = training or False \n",
917
+ " \n",
918
+ " # Slim does not accept `None` as a value for is_training,\n",
919
+ " # Keras will still pass `None` to layers to construct functional models\n",
920
+ " # without forcing the layer to always be in training or in inference.\n",
921
+ " # However, `None` is generally considered to run layers in inference.\n",
922
+ " \n",
923
+ " with slim.arg_scope(\n",
924
+ " inception.inception_resnet_v2_arg_scope(batch_norm_scale=True)):\n",
925
+ " return inception.inception_resnet_v2(\n",
926
+ " inputs, self.num_classes, is_training=is_training)\n"
927
+ ]
928
+ },
929
+ {
930
+ "cell_type": "markdown",
931
+ "metadata": {
932
+ "id": "EqFmpktjlvh9"
933
+ },
934
+ "source": [
935
+ "As it so happens, this layer actually works perfectly fine out of the box (complete with accurate regularization loss tracking). \n",
936
+ "\n",
937
+ "However, this is not something you want to take for granted. Follow the below steps to verify that it is actually behaving as it did in TF1.x, down to observing perfect numerical equivalence. These steps can also help you triangulate what part of the forward pass is causing a divergence from TF1.x (identify if the divergence arises in the model forward pass as opposed to a different part of the model)."
938
+ ]
939
+ },
940
+ {
941
+ "cell_type": "markdown",
942
+ "metadata": {
943
+ "id": "mmgubd9vkevp"
944
+ },
945
+ "source": [
946
+ "## Step 1: Verify variables are only created once\n",
947
+ "\n",
948
+ "The very first thing you should verify is that you have correctly built the model in a way that reuses variables in each call rather than accidentally creating and using new variables each time. For example, if your model creates a new Keras layer or calls `tf.Variable` in each forward pass call then it is most likely failing to capture variables and creating new ones each time.\n",
949
+ "\n",
950
+ "Below are two context manager scopes you can use to detect when your model is creating new variables and debug which part of the model is doing it."
951
+ ]
952
+ },
953
+ {
954
+ "cell_type": "code",
955
+ "execution_count": 9,
956
+ "metadata": {
957
+ "execution": {
958
+ "iopub.execute_input": "2022-12-14T03:38:53.701383Z",
959
+ "iopub.status.busy": "2022-12-14T03:38:53.700896Z",
960
+ "iopub.status.idle": "2022-12-14T03:38:53.707635Z",
961
+ "shell.execute_reply": "2022-12-14T03:38:53.707033Z"
962
+ },
963
+ "id": "VMTfTXC0zW97"
964
+ },
965
+ "outputs": [],
966
+ "source": [
967
+ "@contextmanager\n",
968
+ "def assert_no_variable_creations():\n",
969
+ " \"\"\"Assert no variables are created in this context manager scope.\"\"\"\n",
970
+ " def invalid_variable_creator(next_creator, **kwargs):\n",
971
+ " raise ValueError(\"Attempted to create a new variable instead of reusing an existing one. Args: {}\".format(kwargs))\n",
972
+ "\n",
973
+ " with tf.variable_creator_scope(invalid_variable_creator):\n",
974
+ " yield\n",
975
+ "\n",
976
+ "@contextmanager\n",
977
+ "def catch_and_raise_created_variables():\n",
978
+ " \"\"\"Raise all variables created within this context manager scope (if any).\"\"\"\n",
979
+ " created_vars = []\n",
980
+ " def variable_catcher(next_creator, **kwargs):\n",
981
+ " var = next_creator(**kwargs)\n",
982
+ " created_vars.append(var)\n",
983
+ " return var\n",
984
+ "\n",
985
+ " with tf.variable_creator_scope(variable_catcher):\n",
986
+ " yield\n",
987
+ " if created_vars:\n",
988
+ " raise ValueError(\"Created vars:\", created_vars)"
989
+ ]
990
+ },
991
+ {
992
+ "cell_type": "markdown",
993
+ "metadata": {
994
+ "id": "WOKUtciktQqv"
995
+ },
996
+ "source": [
997
+ "The first scope (`assert_no_variable_creations()`) will raise an error immediately once you try creating a variable within the scope. This allows you to inspect the stacktrace (and use interactive debugging) to figure out exactly what lines of code created a variable instead of reusing an existing one.\n",
998
+ "\n",
999
+ "The second scope (`catch_and_raise_created_variables()`) will raise an exception at the end of the scope if any variables ended up being created. This exception will include the list of all variables created in the scope. This is useful for figuring out what the set of all weights your model is creating is in case you can spot general patterns. However, it is less useful for identifying the exact lines of code where those variables got created.\n",
1000
+ "\n",
1001
+ "Use both scopes below to verify that the shim-based InceptionResnetV2 layer does not create any new variables after the first call (presumably reusing them)."
1002
+ ]
1003
+ },
1004
+ {
1005
+ "cell_type": "code",
1006
+ "execution_count": 10,
1007
+ "metadata": {
1008
+ "execution": {
1009
+ "iopub.execute_input": "2022-12-14T03:38:53.711113Z",
1010
+ "iopub.status.busy": "2022-12-14T03:38:53.710699Z",
1011
+ "iopub.status.idle": "2022-12-14T03:39:03.248779Z",
1012
+ "shell.execute_reply": "2022-12-14T03:39:03.248055Z"
1013
+ },
1014
+ "id": "O9FAGotiuLbK"
1015
+ },
1016
+ "outputs": [
1017
+ {
1018
+ "name": "stderr",
1019
+ "output_type": "stream",
1020
+ "text": [
1021
+ "/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.\n",
1022
+ " warnings.warn('`layer.apply` is deprecated and '\n"
1023
+ ]
1024
+ },
1025
+ {
1026
+ "name": "stderr",
1027
+ "output_type": "stream",
1028
+ "text": [
1029
+ "/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.\n",
1030
+ " warnings.warn('`tf.layers.flatten` is deprecated and '\n"
1031
+ ]
1032
+ }
1033
+ ],
1034
+ "source": [
1035
+ "model = InceptionResnetV2(1000)\n",
1036
+ "height, width = 299, 299\n",
1037
+ "num_classes = 1000\n",
1038
+ "\n",
1039
+ "inputs = tf.ones( (1, height, width, 3))\n",
1040
+ "# Create all weights on the first call\n",
1041
+ "model(inputs)\n",
1042
+ "\n",
1043
+ "# Verify that no new weights are created in followup calls\n",
1044
+ "with assert_no_variable_creations():\n",
1045
+ " model(inputs)\n",
1046
+ "with catch_and_raise_created_variables():\n",
1047
+ " model(inputs)"
1048
+ ]
1049
+ },
1050
+ {
1051
+ "cell_type": "markdown",
1052
+ "metadata": {
1053
+ "id": "9ylT-EIhu1lK"
1054
+ },
1055
+ "source": [
1056
+ "In the example below, observe how these decorators work on a layer that incorrectly creates new weights each time instead of reusing existing ones."
1057
+ ]
1058
+ },
1059
+ {
1060
+ "cell_type": "code",
1061
+ "execution_count": 11,
1062
+ "metadata": {
1063
+ "execution": {
1064
+ "iopub.execute_input": "2022-12-14T03:39:03.253141Z",
1065
+ "iopub.status.busy": "2022-12-14T03:39:03.252469Z",
1066
+ "iopub.status.idle": "2022-12-14T03:39:03.256619Z",
1067
+ "shell.execute_reply": "2022-12-14T03:39:03.256047Z"
1068
+ },
1069
+ "id": "gXqhPQWWtMAw"
1070
+ },
1071
+ "outputs": [],
1072
+ "source": [
1073
+ "class BrokenScalingLayer(tf.keras.layers.Layer):\n",
1074
+ " \"\"\"Scaling layer that incorrectly creates new weights each time:\"\"\"\n",
1075
+ "\n",
1076
+ " @tf.compat.v1.keras.utils.track_tf1_style_variables\n",
1077
+ " def call(self, inputs):\n",
1078
+ " var = tf.Variable(initial_value=2.0)\n",
1079
+ " bias = tf.Variable(initial_value=2.0, name='bias')\n",
1080
+ " return inputs * var + bias"
1081
+ ]
1082
+ },
1083
+ {
1084
+ "cell_type": "code",
1085
+ "execution_count": 12,
1086
+ "metadata": {
1087
+ "execution": {
1088
+ "iopub.execute_input": "2022-12-14T03:39:03.260192Z",
1089
+ "iopub.status.busy": "2022-12-14T03:39:03.259627Z",
1090
+ "iopub.status.idle": "2022-12-14T03:39:03.277902Z",
1091
+ "shell.execute_reply": "2022-12-14T03:39:03.277304Z"
1092
+ },
1093
+ "id": "ztUKlMdGvHSq"
1094
+ },
1095
+ "outputs": [
1096
+ {
1097
+ "name": "stderr",
1098
+ "output_type": "stream",
1099
+ "text": [
1100
+ "Traceback (most recent call last):\n",
1101
+ " File \"/tmpfs/tmp/ipykernel_118303/1128777590.py\", line 7, in <module>\n",
1102
+ " model(inputs)\n",
1103
+ " File \"/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/utils/traceback_utils.py\", line 70, in error_handler\n",
1104
+ " raise e.with_traceback(filtered_tb) from None\n",
1105
+ " File \"/tmpfs/tmp/ipykernel_118303/3224979076.py\", line 6, in call\n",
1106
+ " var = tf.Variable(initial_value=2.0)\n",
1107
+ " File \"/tmpfs/tmp/ipykernel_118303/1829430118.py\", line 5, in invalid_variable_creator\n",
1108
+ " raise ValueError(\"Attempted to create a new variable instead of reusing an existing one. Args: {}\".format(kwargs))\n",
1109
+ "ValueError: Exception encountered when calling layer 'broken_scaling_layer' (type BrokenScalingLayer).\n",
1110
+ "\n",
1111
+ "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}\n",
1112
+ "\n",
1113
+ "Call arguments received by layer 'broken_scaling_layer' (type BrokenScalingLayer):\n",
1114
+ " • inputs=tf.Tensor(shape=(1, 299, 299, 3), dtype=float32)\n"
1115
+ ]
1116
+ }
1117
+ ],
1118
+ "source": [
1119
+ "model = BrokenScalingLayer()\n",
1120
+ "inputs = tf.ones( (1, height, width, 3))\n",
1121
+ "model(inputs)\n",
1122
+ "\n",
1123
+ "try:\n",
1124
+ " with assert_no_variable_creations():\n",
1125
+ " model(inputs)\n",
1126
+ "except ValueError as err:\n",
1127
+ " import traceback\n",
1128
+ " traceback.print_exc()\n"
1129
+ ]
1130
+ },
1131
+ {
1132
+ "cell_type": "code",
1133
+ "execution_count": 13,
1134
+ "metadata": {
1135
+ "execution": {
1136
+ "iopub.execute_input": "2022-12-14T03:39:03.281111Z",
1137
+ "iopub.status.busy": "2022-12-14T03:39:03.280539Z",
1138
+ "iopub.status.idle": "2022-12-14T03:39:03.289140Z",
1139
+ "shell.execute_reply": "2022-12-14T03:39:03.288582Z"
1140
+ },
1141
+ "id": "6VyfMJ50vZqZ"
1142
+ },
1143
+ "outputs": [
1144
+ {
1145
+ "name": "stdout",
1146
+ "output_type": "stream",
1147
+ "text": [
1148
+ "('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>])\n"
1149
+ ]
1150
+ }
1151
+ ],
1152
+ "source": [
1153
+ "model = BrokenScalingLayer()\n",
1154
+ "inputs = tf.ones( (1, height, width, 3))\n",
1155
+ "model(inputs)\n",
1156
+ "\n",
1157
+ "try:\n",
1158
+ " with catch_and_raise_created_variables():\n",
1159
+ " model(inputs)\n",
1160
+ "except ValueError as err:\n",
1161
+ " print(err)"
1162
+ ]
1163
+ },
1164
+ {
1165
+ "cell_type": "markdown",
1166
+ "metadata": {
1167
+ "id": "JDaiTArcv49M"
1168
+ },
1169
+ "source": [
1170
+ "You can fix the layer by making sure it only creates the weights once and then reuses them each time."
1171
+ ]
1172
+ },
1173
+ {
1174
+ "cell_type": "code",
1175
+ "execution_count": 14,
1176
+ "metadata": {
1177
+ "execution": {
1178
+ "iopub.execute_input": "2022-12-14T03:39:03.292412Z",
1179
+ "iopub.status.busy": "2022-12-14T03:39:03.291983Z",
1180
+ "iopub.status.idle": "2022-12-14T03:39:03.301456Z",
1181
+ "shell.execute_reply": "2022-12-14T03:39:03.300908Z"
1182
+ },
1183
+ "id": "FN1Oa10iviv8"
1184
+ },
1185
+ "outputs": [],
1186
+ "source": [
1187
+ "class FixedScalingLayer(tf.keras.layers.Layer):\n",
1188
+ " \"\"\"Scaling layer that incorrectly creates new weights each time:\"\"\"\n",
1189
+ " def __init__(self):\n",
1190
+ " super().__init__()\n",
1191
+ " self.var = None\n",
1192
+ " self.bias = None\n",
1193
+ "\n",
1194
+ " @tf.compat.v1.keras.utils.track_tf1_style_variables\n",
1195
+ " def call(self, inputs):\n",
1196
+ " if self.var is None:\n",
1197
+ " self.var = tf.Variable(initial_value=2.0)\n",
1198
+ " self.bias = tf.Variable(initial_value=2.0, name='bias')\n",
1199
+ " return inputs * self.var + self.bias\n",
1200
+ "\n",
1201
+ "model = FixedScalingLayer()\n",
1202
+ "inputs = tf.ones( (1, height, width, 3))\n",
1203
+ "model(inputs)\n",
1204
+ "\n",
1205
+ "with assert_no_variable_creations():\n",
1206
+ " model(inputs)\n",
1207
+ "with catch_and_raise_created_variables():\n",
1208
+ " model(inputs)"
1209
+ ]
1210
+ },
1211
+ {
1212
+ "cell_type": "markdown",
1213
+ "metadata": {
1214
+ "id": "MuiZZ7ktwCcn"
1215
+ },
1216
+ "source": [
1217
+ "### Troubleshooting\n",
1218
+ "\n",
1219
+ "Here are some common reasons why your model might accidentally be creating new weights instead of reusing existing ones:\n",
1220
+ "\n",
1221
+ "1. It uses an explicit `tf.Variable` call without reusing already-created `tf.Variables`. Fix this by first checking if it has not been created then reusing the existing ones.\n",
1222
+ "2. It creates a Keras layer or model directly in the forward pass each time (as opposed to `tf.compat.v1.layers`). Fix this by first checking if it has not been created then reusing the existing ones.\n",
1223
+ "3. It is built on top of `tf.compat.v1.layers` but fails to assign all `compat.v1.layers` an explicit name or to wrap your `compat.v1.layer` usage inside of a named `variable_scope`, causing the autogenerated layer names to increment in each model call. Fix this by putting a named `tf.compat.v1.variable_scope` inside your shim-decorated method that wraps all of your `tf.compat.v1.layers` usage."
1224
+ ]
1225
+ },
1226
+ {
1227
+ "cell_type": "markdown",
1228
+ "metadata": {
1229
+ "id": "V4iZLV9BnwKM"
1230
+ },
1231
+ "source": [
1232
+ "## Step 2: Check that variable counts, names, and shapes match\n",
1233
+ "\n",
1234
+ "The second step is to make sure your layer running in TF2 creates the same number of weights, with the same shapes, as the corresponding code does in TF1.x.\n",
1235
+ "\n",
1236
+ "You can do a mix of manually checking them to see that they match, and doing the checks programmatically in a unit test as shown below."
1237
+ ]
1238
+ },
1239
+ {
1240
+ "cell_type": "code",
1241
+ "execution_count": 15,
1242
+ "metadata": {
1243
+ "execution": {
1244
+ "iopub.execute_input": "2022-12-14T03:39:03.305013Z",
1245
+ "iopub.status.busy": "2022-12-14T03:39:03.304461Z",
1246
+ "iopub.status.idle": "2022-12-14T03:39:21.004861Z",
1247
+ "shell.execute_reply": "2022-12-14T03:39:21.004090Z"
1248
+ },
1249
+ "id": "m_aqag5fpun5"
1250
+ },
1251
+ "outputs": [
1252
+ {
1253
+ "name": "stderr",
1254
+ "output_type": "stream",
1255
+ "text": [
1256
+ "/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.\n",
1257
+ " warnings.warn('`layer.apply` is deprecated and '\n"
1258
+ ]
1259
+ }
1260
+ ],
1261
+ "source": [
1262
+ "# Build the forward pass inside a TF1.x graph, and \n",
1263
+ "# get the counts, shapes, and names of the variables\n",
1264
+ "graph = tf.Graph()\n",
1265
+ "with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
1266
+ " height, width = 299, 299\n",
1267
+ " num_classes = 1000\n",
1268
+ " inputs = tf.ones( (1, height, width, 3))\n",
1269
+ "\n",
1270
+ " out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)\n",
1271
+ "\n",
1272
+ " tf1_variable_names_and_shapes = {\n",
1273
+ " var.name: (var.trainable, var.shape) for var in tf.compat.v1.global_variables()}\n",
1274
+ " num_tf1_variables = len(tf.compat.v1.global_variables())"
1275
+ ]
1276
+ },
1277
+ {
1278
+ "cell_type": "markdown",
1279
+ "metadata": {
1280
+ "id": "WT1-cm99vfNU"
1281
+ },
1282
+ "source": [
1283
+ "Next, do the same for the shim-wrapped layer in TF2.\n",
1284
+ "Notice that the model is also called multiple times before grabbing the weights. This is done to effectively test for variable reuse."
1285
+ ]
1286
+ },
1287
+ {
1288
+ "cell_type": "code",
1289
+ "execution_count": 16,
1290
+ "metadata": {
1291
+ "execution": {
1292
+ "iopub.execute_input": "2022-12-14T03:39:21.008893Z",
1293
+ "iopub.status.busy": "2022-12-14T03:39:21.008637Z",
1294
+ "iopub.status.idle": "2022-12-14T03:39:24.226380Z",
1295
+ "shell.execute_reply": "2022-12-14T03:39:24.225669Z"
1296
+ },
1297
+ "id": "S7ND-lBSqmnE"
1298
+ },
1299
+ "outputs": [],
1300
+ "source": [
1301
+ "height, width = 299, 299\n",
1302
+ "num_classes = 1000\n",
1303
+ "\n",
1304
+ "model = InceptionResnetV2(num_classes)\n",
1305
+ "# The weights will not be created until you call the model\n",
1306
+ "\n",
1307
+ "inputs = tf.ones( (1, height, width, 3))\n",
1308
+ "# Call the model multiple times before checking the weights, to verify variables\n",
1309
+ "# get reused rather than accidentally creating additional variables\n",
1310
+ "out, endpoints = model(inputs, training=False)\n",
1311
+ "out, endpoints = model(inputs, training=False)\n",
1312
+ "\n",
1313
+ "# Grab the name: shape mapping and the total number of variables separately,\n",
1314
+ "# because in TF2 variables can be created with the same name\n",
1315
+ "num_tf2_variables = len(model.variables)\n",
1316
+ "tf2_variable_names_and_shapes = {\n",
1317
+ " var.name: (var.trainable, var.shape) for var in model.variables}"
1318
+ ]
1319
+ },
1320
+ {
1321
+ "cell_type": "code",
1322
+ "execution_count": 17,
1323
+ "metadata": {
1324
+ "execution": {
1325
+ "iopub.execute_input": "2022-12-14T03:39:24.230091Z",
1326
+ "iopub.status.busy": "2022-12-14T03:39:24.229816Z",
1327
+ "iopub.status.idle": "2022-12-14T03:39:24.233961Z",
1328
+ "shell.execute_reply": "2022-12-14T03:39:24.233379Z"
1329
+ },
1330
+ "id": "pY2P_4wqsOYw"
1331
+ },
1332
+ "outputs": [],
1333
+ "source": [
1334
+ "# Verify that the variable counts, names, and shapes all match:\n",
1335
+ "assert num_tf1_variables == num_tf2_variables\n",
1336
+ "assert tf1_variable_names_and_shapes == tf2_variable_names_and_shapes"
1337
+ ]
1338
+ },
1339
+ {
1340
+ "cell_type": "markdown",
1341
+ "metadata": {
1342
+ "id": "N4YKJzSVwWkc"
1343
+ },
1344
+ "source": [
1345
+ "The shim-based InceptionResnetV2 layer passes this test. However, in the case where they don't match, you can run it through a diff (text or other) to see where the differences are.\n",
1346
+ "\n",
1347
+ "This can provide a clue as to what part of the model isn't behaving as expected. With eager execution you can use pdb, interactive debugging, and breakpoints to dig into the parts of the model that seem suspicious, and debug what is going wrong in more depth."
1348
+ ]
1349
+ },
1350
+ {
1351
+ "cell_type": "markdown",
1352
+ "metadata": {
1353
+ "id": "2gYrt-_0xpRM"
1354
+ },
1355
+ "source": [
1356
+ "### Troubleshooting\n",
1357
+ "\n",
1358
+ "* Pay close attention to the names of any variables created directly by explicit `tf.Variable` calls and Keras layers/models as their variable name generation semantics may differ slightly between TF1.x graphs and TF2 functionality such as eager execution and `tf.function` even if everything else is working properly. If this is the case for you, adjust your test to account for any slightly different naming semantics.\n",
1359
+ "\n",
1360
+ "* You may sometimes find that the `tf.Variable`s, `tf.keras.layers.Layer`s, or `tf.keras.Model`s created in your training loop's forward pass are missing from your TF2 variables list even if they were captured by the variables collection in TF1.x. Fix this by assigning the variables/layers/models that your forward pass creates to instance attributes in your model. See [here](https://www.tensorflow.org/guide/keras/custom_layers_and_models) for more info."
1361
+ ]
1362
+ },
1363
+ {
1364
+ "cell_type": "markdown",
1365
+ "metadata": {
1366
+ "id": "fOQJ_hUGnzkq"
1367
+ },
1368
+ "source": [
1369
+ "## Step 3: Reset all variables, check numerical equivalence with all randomness disabled\n",
1370
+ "\n",
1371
+ "The next step is to verify numerical equivalence for both the actual outputs and the regularization loss tracking when you fix the model such that there is no random number generation involved (such as during inference).\n",
1372
+ "\n",
1373
+ "The exact way to do this may depend on your specific model, but in most models (such as this one), you can do this by:\n",
1374
+ "1. Initializing the weights to the same value with no randomness. This can be done by resetting them to a fixed value after they have been created.\n",
1375
+ "2. Running the model in inference mode to avoid triggering any dropout layers which can be sources of randomness.\n",
1376
+ "\n",
1377
+ "The following code demonstrates how you can compare the TF1.x and TF2 results this way."
1378
+ ]
1379
+ },
1380
+ {
1381
+ "cell_type": "code",
1382
+ "execution_count": 18,
1383
+ "metadata": {
1384
+ "execution": {
1385
+ "iopub.execute_input": "2022-12-14T03:39:24.237784Z",
1386
+ "iopub.status.busy": "2022-12-14T03:39:24.237280Z",
1387
+ "iopub.status.idle": "2022-12-14T03:39:49.563479Z",
1388
+ "shell.execute_reply": "2022-12-14T03:39:49.562787Z"
1389
+ },
1390
+ "id": "kL4PzD2Cxzmp"
1391
+ },
1392
+ "outputs": [
1393
+ {
1394
+ "name": "stdout",
1395
+ "output_type": "stream",
1396
+ "text": [
1397
+ "Regularization loss: 0.001182976\n"
1398
+ ]
1399
+ },
1400
+ {
1401
+ "data": {
1402
+ "text/plain": [
1403
+ "array([0.00299837, 0.00299837, 0.00299837, 0.00299837, 0.00299837],\n",
1404
+ " dtype=float32)"
1405
+ ]
1406
+ },
1407
+ "execution_count": 18,
1408
+ "metadata": {},
1409
+ "output_type": "execute_result"
1410
+ }
1411
+ ],
1412
+ "source": [
1413
+ "graph = tf.Graph()\n",
1414
+ "with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
1415
+ " height, width = 299, 299\n",
1416
+ " num_classes = 1000\n",
1417
+ " inputs = tf.ones( (1, height, width, 3))\n",
1418
+ "\n",
1419
+ " out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)\n",
1420
+ "\n",
1421
+ " # Rather than running the global variable initializers,\n",
1422
+ " # reset all variables to a constant value\n",
1423
+ " var_reset = tf.group([var.assign(tf.ones_like(var) * 0.001) for var in tf.compat.v1.global_variables()])\n",
1424
+ " sess.run(var_reset)\n",
1425
+ "\n",
1426
+ " # Grab the outputs & regularization loss\n",
1427
+ " reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
1428
+ " tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
1429
+ " tf1_output = sess.run(out)\n",
1430
+ "\n",
1431
+ "print(\"Regularization loss:\", tf1_regularization_loss)\n",
1432
+ "tf1_output[0][:5]"
1433
+ ]
1434
+ },
1435
+ {
1436
+ "cell_type": "markdown",
1437
+ "metadata": {
1438
+ "id": "IKkoM_x72rUa"
1439
+ },
1440
+ "source": [
1441
+ "Get the TF2 results."
1442
+ ]
1443
+ },
1444
+ {
1445
+ "cell_type": "code",
1446
+ "execution_count": 19,
1447
+ "metadata": {
1448
+ "execution": {
1449
+ "iopub.execute_input": "2022-12-14T03:39:49.567413Z",
1450
+ "iopub.status.busy": "2022-12-14T03:39:49.567128Z",
1451
+ "iopub.status.idle": "2022-12-14T03:39:53.933510Z",
1452
+ "shell.execute_reply": "2022-12-14T03:39:53.932839Z"
1453
+ },
1454
+ "id": "kb086gJwzsNo"
1455
+ },
1456
+ "outputs": [
1457
+ {
1458
+ "name": "stdout",
1459
+ "output_type": "stream",
1460
+ "text": [
1461
+ "Regularization loss: tf.Tensor(0.0011829757, shape=(), dtype=float32)\n"
1462
+ ]
1463
+ },
1464
+ {
1465
+ "data": {
1466
+ "text/plain": [
1467
+ "<tf.Tensor: shape=(5,), dtype=float32, numpy=\n",
1468
+ "array([0.00299837, 0.00299837, 0.00299837, 0.00299837, 0.00299837],\n",
1469
+ " dtype=float32)>"
1470
+ ]
1471
+ },
1472
+ "execution_count": 19,
1473
+ "metadata": {},
1474
+ "output_type": "execute_result"
1475
+ }
1476
+ ],
1477
+ "source": [
1478
+ "height, width = 299, 299\n",
1479
+ "num_classes = 1000\n",
1480
+ "\n",
1481
+ "model = InceptionResnetV2(num_classes)\n",
1482
+ "\n",
1483
+ "inputs = tf.ones((1, height, width, 3))\n",
1484
+ "# Call the model once to create the weights\n",
1485
+ "out, endpoints = model(inputs, training=False)\n",
1486
+ "\n",
1487
+ "# Reset all variables to the same fixed value as above, with no randomness\n",
1488
+ "for var in model.variables:\n",
1489
+ " var.assign(tf.ones_like(var) * 0.001)\n",
1490
+ "tf2_output, endpoints = model(inputs, training=False)\n",
1491
+ "\n",
1492
+ "# Get the regularization loss\n",
1493
+ "tf2_regularization_loss = tf.math.add_n(model.losses)\n",
1494
+ "\n",
1495
+ "print(\"Regularization loss:\", tf2_regularization_loss)\n",
1496
+ "tf2_output[0][:5]"
1497
+ ]
1498
+ },
1499
+ {
1500
+ "cell_type": "code",
1501
+ "execution_count": 20,
1502
+ "metadata": {
1503
+ "execution": {
1504
+ "iopub.execute_input": "2022-12-14T03:39:53.936827Z",
1505
+ "iopub.status.busy": "2022-12-14T03:39:53.936548Z",
1506
+ "iopub.status.idle": "2022-12-14T03:39:53.939905Z",
1507
+ "shell.execute_reply": "2022-12-14T03:39:53.939251Z"
1508
+ },
1509
+ "id": "CUfWqlgIK6ej"
1510
+ },
1511
+ "outputs": [],
1512
+ "source": [
1513
+ "# Create a dict of tolerance values\n",
1514
+ "tol_dict={'rtol':1e-06, 'atol':1e-05}"
1515
+ ]
1516
+ },
1517
+ {
1518
+ "cell_type": "code",
1519
+ "execution_count": 21,
1520
+ "metadata": {
1521
+ "execution": {
1522
+ "iopub.execute_input": "2022-12-14T03:39:53.943322Z",
1523
+ "iopub.status.busy": "2022-12-14T03:39:53.942814Z",
1524
+ "iopub.status.idle": "2022-12-14T03:39:53.947107Z",
1525
+ "shell.execute_reply": "2022-12-14T03:39:53.946575Z"
1526
+ },
1527
+ "id": "R-C07eTo0WTr"
1528
+ },
1529
+ "outputs": [],
1530
+ "source": [
1531
+ "# Verify that the regularization loss and output both match\n",
1532
+ "# when we fix the weights and avoid randomness by running inference:\n",
1533
+ "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
1534
+ "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
1535
+ ]
1536
+ },
1537
+ {
1538
+ "cell_type": "markdown",
1539
+ "metadata": {
1540
+ "id": "5UUq_Fuc2zDO"
1541
+ },
1542
+ "source": [
1543
+ "The numbers match between TF1.x and TF2 when you remove sources of randomness, and the TF2-compatible `InceptionResnetV2` layer passes the test.\n",
1544
+ "\n",
1545
+ "If you are observing the results diverging for your own models, you can use printing or pdb and interactive debugging to identify where and why the results start to diverge. Eager execution can make this significantly easier. You can also use an ablation approach to run only small portions of the model on fixed intermediate inputs and isolate where the divergence happens.\n",
1546
+ "\n",
1547
+ "Conveniently, many slim nets (and other models) also expose intermediate endpoints that you can probe."
1548
+ ]
1549
+ },
1550
+ {
1551
+ "cell_type": "markdown",
1552
+ "metadata": {
1553
+ "id": "btRbak-0ou15"
1554
+ },
1555
+ "source": [
1556
+ "## Step 4: Align random number generation, check numerical equivalence in both training and inference\n",
1557
+ "\n",
1558
+ "The final step is to verify that the TF2 model numerically matches the TF1.x model, even when accounting for random number generation in variable initialization and in the forward pass itself (such as dropout layers during the forward pass).\n",
1559
+ "\n",
1560
+ "You can do this by using the testing tool below to make random number generation semantics match between TF1.x graphs/sessions and eager execution."
1561
+ ]
1562
+ },
1563
+ {
1564
+ "cell_type": "markdown",
1565
+ "metadata": {
1566
+ "id": "jYq-JHiC39QC"
1567
+ },
1568
+ "source": [
1569
+ "TF1 legacy graphs/sessions and TF2 eager execution use different stateful random number generation semantics.\n",
1570
+ "\n",
1571
+ "In `tf.compat.v1.Session`s, if no seeds are specified, the random number generation depends on how many operations are in the graph at the time when the random operation is added, and how many times the graph is run. In eager execution, stateful random number generation depends on the global seed, the operation random seed, and how many times the operation with the operation with the given random seed is run. See \n",
1572
+ "`tf.random.set_seed` for more info."
1573
+ ]
1574
+ },
1575
+ {
1576
+ "cell_type": "markdown",
1577
+ "metadata": {
1578
+ "id": "BQbb8Hyk5YVi"
1579
+ },
1580
+ "source": [
1581
+ "The following [`v1.keras.utils.DeterministicRandomTestTool`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/keras/utils/DeterministicRandomTestTool) class provides a context manager `scope()` that can make stateful random operations use the same seed across both TF1 graphs/sessions and eager execution.\n",
1582
+ "\n",
1583
+ "The tool provides two testing modes: \n",
1584
+ "1. `constant` which uses the same seed for every single operation no matter how many times it has been called and,\n",
1585
+ "2. `num_random_ops` which uses the number of previously-observed stateful random operations as the operation seed.\n",
1586
+ "\n",
1587
+ "This applies both to the stateful random operations used for creating and initializing variables, and to the stateful random operations used in computation (such as for dropout layers)."
1588
+ ]
1589
+ },
1590
+ {
1591
+ "cell_type": "markdown",
1592
+ "metadata": {
1593
+ "id": "MoyZenhGHDA-"
1594
+ },
1595
+ "source": [
1596
+ "Generate three random tensors to show how to use this tool to make stateful random number generation match between sessions and eager execution."
1597
+ ]
1598
+ },
1599
+ {
1600
+ "cell_type": "code",
1601
+ "execution_count": 22,
1602
+ "metadata": {
1603
+ "execution": {
1604
+ "iopub.execute_input": "2022-12-14T03:39:53.950786Z",
1605
+ "iopub.status.busy": "2022-12-14T03:39:53.950284Z",
1606
+ "iopub.status.idle": "2022-12-14T03:39:53.989064Z",
1607
+ "shell.execute_reply": "2022-12-14T03:39:53.988502Z"
1608
+ },
1609
+ "id": "DDFfjrbXEWED"
1610
+ },
1611
+ "outputs": [
1612
+ {
1613
+ "data": {
1614
+ "text/plain": [
1615
+ "(array([[2.5063772],\n",
1616
+ " [2.7488918],\n",
1617
+ " [1.4839486]], dtype=float32),\n",
1618
+ " array([[2.5063772, 2.7488918, 1.4839486],\n",
1619
+ " [1.5633398, 2.1358476, 1.3693532],\n",
1620
+ " [0.3598416, 1.8287641, 2.5314465]], dtype=float32),\n",
1621
+ " array([[2.5063772, 2.7488918, 1.4839486],\n",
1622
+ " [1.5633398, 2.1358476, 1.3693532],\n",
1623
+ " [0.3598416, 1.8287641, 2.5314465]], dtype=float32))"
1624
+ ]
1625
+ },
1626
+ "execution_count": 22,
1627
+ "metadata": {},
1628
+ "output_type": "execute_result"
1629
+ }
1630
+ ],
1631
+ "source": [
1632
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
1633
+ "with random_tool.scope():\n",
1634
+ " graph = tf.Graph()\n",
1635
+ " with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
1636
+ " a = tf.random.uniform(shape=(3,1))\n",
1637
+ " a = a * 3\n",
1638
+ " b = tf.random.uniform(shape=(3,3))\n",
1639
+ " b = b * 3\n",
1640
+ " c = tf.random.uniform(shape=(3,3))\n",
1641
+ " c = c * 3\n",
1642
+ " graph_a, graph_b, graph_c = sess.run([a, b, c])\n",
1643
+ "\n",
1644
+ "graph_a, graph_b, graph_c"
1645
+ ]
1646
+ },
1647
+ {
1648
+ "cell_type": "code",
1649
+ "execution_count": 23,
1650
+ "metadata": {
1651
+ "execution": {
1652
+ "iopub.execute_input": "2022-12-14T03:39:53.992308Z",
1653
+ "iopub.status.busy": "2022-12-14T03:39:53.991806Z",
1654
+ "iopub.status.idle": "2022-12-14T03:39:54.011113Z",
1655
+ "shell.execute_reply": "2022-12-14T03:39:54.010470Z"
1656
+ },
1657
+ "id": "o9bkdPuTFpYr"
1658
+ },
1659
+ "outputs": [
1660
+ {
1661
+ "data": {
1662
+ "text/plain": [
1663
+ "(<tf.Tensor: shape=(3, 1), dtype=float32, numpy=\n",
1664
+ " array([[2.5063772],\n",
1665
+ " [2.7488918],\n",
1666
+ " [1.4839486]], dtype=float32)>,\n",
1667
+ " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
1668
+ " array([[2.5063772, 2.7488918, 1.4839486],\n",
1669
+ " [1.5633398, 2.1358476, 1.3693532],\n",
1670
+ " [0.3598416, 1.8287641, 2.5314465]], dtype=float32)>,\n",
1671
+ " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
1672
+ " array([[2.5063772, 2.7488918, 1.4839486],\n",
1673
+ " [1.5633398, 2.1358476, 1.3693532],\n",
1674
+ " [0.3598416, 1.8287641, 2.5314465]], dtype=float32)>)"
1675
+ ]
1676
+ },
1677
+ "execution_count": 23,
1678
+ "metadata": {},
1679
+ "output_type": "execute_result"
1680
+ }
1681
+ ],
1682
+ "source": [
1683
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
1684
+ "with random_tool.scope():\n",
1685
+ " a = tf.random.uniform(shape=(3,1))\n",
1686
+ " a = a * 3\n",
1687
+ " b = tf.random.uniform(shape=(3,3))\n",
1688
+ " b = b * 3\n",
1689
+ " c = tf.random.uniform(shape=(3,3))\n",
1690
+ " c = c * 3\n",
1691
+ "\n",
1692
+ "a, b, c"
1693
+ ]
1694
+ },
1695
+ {
1696
+ "cell_type": "code",
1697
+ "execution_count": 24,
1698
+ "metadata": {
1699
+ "execution": {
1700
+ "iopub.execute_input": "2022-12-14T03:39:54.014331Z",
1701
+ "iopub.status.busy": "2022-12-14T03:39:54.013797Z",
1702
+ "iopub.status.idle": "2022-12-14T03:39:54.017855Z",
1703
+ "shell.execute_reply": "2022-12-14T03:39:54.017276Z"
1704
+ },
1705
+ "id": "qRJYFydsGIbF"
1706
+ },
1707
+ "outputs": [],
1708
+ "source": [
1709
+ "# Demonstrate that the generated random numbers match\n",
1710
+ "np.testing.assert_allclose(graph_a, a.numpy(), **tol_dict)\n",
1711
+ "np.testing.assert_allclose(graph_b, b.numpy(), **tol_dict)\n",
1712
+ "np.testing.assert_allclose(graph_c, c.numpy(), **tol_dict)"
1713
+ ]
1714
+ },
1715
+ {
1716
+ "cell_type": "markdown",
1717
+ "metadata": {
1718
+ "id": "J8IWCnS-WFrB"
1719
+ },
1720
+ "source": [
1721
+ "However, notice that in `constant` mode, because `b` and `c` were generated with the same seed and have the same shape, they will have exactly the same values."
1722
+ ]
1723
+ },
1724
+ {
1725
+ "cell_type": "code",
1726
+ "execution_count": 25,
1727
+ "metadata": {
1728
+ "execution": {
1729
+ "iopub.execute_input": "2022-12-14T03:39:54.021063Z",
1730
+ "iopub.status.busy": "2022-12-14T03:39:54.020543Z",
1731
+ "iopub.status.idle": "2022-12-14T03:39:54.024066Z",
1732
+ "shell.execute_reply": "2022-12-14T03:39:54.023479Z"
1733
+ },
1734
+ "id": "IdxV89q2WPid"
1735
+ },
1736
+ "outputs": [],
1737
+ "source": [
1738
+ "np.testing.assert_allclose(b.numpy(), c.numpy(), **tol_dict)"
1739
+ ]
1740
+ },
1741
+ {
1742
+ "cell_type": "markdown",
1743
+ "metadata": {
1744
+ "id": "vQTm7joHHh57"
1745
+ },
1746
+ "source": [
1747
+ "### Trace order\n",
1748
+ "If you are worried about some random numbers matching in `constant` mode reducing your confidence in your numerical equivalence test (for example if several weights take on the same initializations), you can use the `num_random_ops` mode to avoid this. In the `num_random_ops` mode, the generated random numbers will depend on the ordering of random ops in the program."
1749
+ ]
1750
+ },
1751
+ {
1752
+ "cell_type": "code",
1753
+ "execution_count": 26,
1754
+ "metadata": {
1755
+ "execution": {
1756
+ "iopub.execute_input": "2022-12-14T03:39:54.027409Z",
1757
+ "iopub.status.busy": "2022-12-14T03:39:54.026970Z",
1758
+ "iopub.status.idle": "2022-12-14T03:39:54.054591Z",
1759
+ "shell.execute_reply": "2022-12-14T03:39:54.054025Z"
1760
+ },
1761
+ "id": "L-AeD148VygJ"
1762
+ },
1763
+ "outputs": [
1764
+ {
1765
+ "data": {
1766
+ "text/plain": [
1767
+ "(array([[2.5063772],\n",
1768
+ " [2.7488918],\n",
1769
+ " [1.4839486]], dtype=float32),\n",
1770
+ " array([[0.45038545, 1.9197761 , 2.4536333 ],\n",
1771
+ " [1.0371652 , 2.9898582 , 1.924583 ],\n",
1772
+ " [0.25679827, 1.6579313 , 2.8418403 ]], dtype=float32),\n",
1773
+ " array([[2.9634383 , 1.0862181 , 2.6042497 ],\n",
1774
+ " [0.70099247, 2.3920312 , 1.0470468 ],\n",
1775
+ " [0.18173039, 0.8359269 , 1.0508587 ]], dtype=float32))"
1776
+ ]
1777
+ },
1778
+ "execution_count": 26,
1779
+ "metadata": {},
1780
+ "output_type": "execute_result"
1781
+ }
1782
+ ],
1783
+ "source": [
1784
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
1785
+ "with random_tool.scope():\n",
1786
+ " graph = tf.Graph()\n",
1787
+ " with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
1788
+ " a = tf.random.uniform(shape=(3,1))\n",
1789
+ " a = a * 3\n",
1790
+ " b = tf.random.uniform(shape=(3,3))\n",
1791
+ " b = b * 3\n",
1792
+ " c = tf.random.uniform(shape=(3,3))\n",
1793
+ " c = c * 3\n",
1794
+ " graph_a, graph_b, graph_c = sess.run([a, b, c])\n",
1795
+ "\n",
1796
+ "graph_a, graph_b, graph_c"
1797
+ ]
1798
+ },
1799
+ {
1800
+ "cell_type": "code",
1801
+ "execution_count": 27,
1802
+ "metadata": {
1803
+ "execution": {
1804
+ "iopub.execute_input": "2022-12-14T03:39:54.057715Z",
1805
+ "iopub.status.busy": "2022-12-14T03:39:54.057212Z",
1806
+ "iopub.status.idle": "2022-12-14T03:39:54.070419Z",
1807
+ "shell.execute_reply": "2022-12-14T03:39:54.069817Z"
1808
+ },
1809
+ "id": "CedD41NuVygK"
1810
+ },
1811
+ "outputs": [
1812
+ {
1813
+ "data": {
1814
+ "text/plain": [
1815
+ "(<tf.Tensor: shape=(3, 1), dtype=float32, numpy=\n",
1816
+ " array([[2.5063772],\n",
1817
+ " [2.7488918],\n",
1818
+ " [1.4839486]], dtype=float32)>,\n",
1819
+ " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
1820
+ " array([[0.45038545, 1.9197761 , 2.4536333 ],\n",
1821
+ " [1.0371652 , 2.9898582 , 1.924583 ],\n",
1822
+ " [0.25679827, 1.6579313 , 2.8418403 ]], dtype=float32)>,\n",
1823
+ " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
1824
+ " array([[2.9634383 , 1.0862181 , 2.6042497 ],\n",
1825
+ " [0.70099247, 2.3920312 , 1.0470468 ],\n",
1826
+ " [0.18173039, 0.8359269 , 1.0508587 ]], dtype=float32)>)"
1827
+ ]
1828
+ },
1829
+ "execution_count": 27,
1830
+ "metadata": {},
1831
+ "output_type": "execute_result"
1832
+ }
1833
+ ],
1834
+ "source": [
1835
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
1836
+ "with random_tool.scope():\n",
1837
+ " a = tf.random.uniform(shape=(3,1))\n",
1838
+ " a = a * 3\n",
1839
+ " b = tf.random.uniform(shape=(3,3))\n",
1840
+ " b = b * 3\n",
1841
+ " c = tf.random.uniform(shape=(3,3))\n",
1842
+ " c = c * 3\n",
1843
+ "\n",
1844
+ "a, b, c"
1845
+ ]
1846
+ },
1847
+ {
1848
+ "cell_type": "code",
1849
+ "execution_count": 28,
1850
+ "metadata": {
1851
+ "execution": {
1852
+ "iopub.execute_input": "2022-12-14T03:39:54.073744Z",
1853
+ "iopub.status.busy": "2022-12-14T03:39:54.073122Z",
1854
+ "iopub.status.idle": "2022-12-14T03:39:54.077214Z",
1855
+ "shell.execute_reply": "2022-12-14T03:39:54.076668Z"
1856
+ },
1857
+ "id": "5We2xSnLVygL"
1858
+ },
1859
+ "outputs": [],
1860
+ "source": [
1861
+ "# Demonstrate that the generated random numbers match\n",
1862
+ "np.testing.assert_allclose(graph_a, a.numpy(), **tol_dict)\n",
1863
+ "np.testing.assert_allclose(graph_b, b.numpy(), **tol_dict )\n",
1864
+ "np.testing.assert_allclose(graph_c, c.numpy(), **tol_dict)"
1865
+ ]
1866
+ },
1867
+ {
1868
+ "cell_type": "code",
1869
+ "execution_count": 29,
1870
+ "metadata": {
1871
+ "execution": {
1872
+ "iopub.execute_input": "2022-12-14T03:39:54.080256Z",
1873
+ "iopub.status.busy": "2022-12-14T03:39:54.079793Z",
1874
+ "iopub.status.idle": "2022-12-14T03:39:54.082990Z",
1875
+ "shell.execute_reply": "2022-12-14T03:39:54.082356Z"
1876
+ },
1877
+ "id": "BBFG1xehWneM"
1878
+ },
1879
+ "outputs": [],
1880
+ "source": [
1881
+ "# Demonstrate that with the 'num_random_ops' mode,\n",
1882
+ "# b & c took on different values even though\n",
1883
+ "# their generated shape was the same\n",
1884
+ "assert not np.allclose(b.numpy(), c.numpy(), **tol_dict)"
1885
+ ]
1886
+ },
1887
+ {
1888
+ "cell_type": "markdown",
1889
+ "metadata": {
1890
+ "id": "OfX_VexcVqSA"
1891
+ },
1892
+ "source": [
1893
+ "However, notice that in this mode random generation is sensitive to program order, and so the following generated random numbers do not match."
1894
+ ]
1895
+ },
1896
+ {
1897
+ "cell_type": "code",
1898
+ "execution_count": 30,
1899
+ "metadata": {
1900
+ "execution": {
1901
+ "iopub.execute_input": "2022-12-14T03:39:54.086042Z",
1902
+ "iopub.status.busy": "2022-12-14T03:39:54.085613Z",
1903
+ "iopub.status.idle": "2022-12-14T03:39:54.102152Z",
1904
+ "shell.execute_reply": "2022-12-14T03:39:54.101559Z"
1905
+ },
1906
+ "id": "cZt__ElEIDl_"
1907
+ },
1908
+ "outputs": [],
1909
+ "source": [
1910
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
1911
+ "with random_tool.scope():\n",
1912
+ " a = tf.random.uniform(shape=(3,1))\n",
1913
+ " a = a * 3\n",
1914
+ " b = tf.random.uniform(shape=(3,3))\n",
1915
+ " b = b * 3\n",
1916
+ "\n",
1917
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
1918
+ "with random_tool.scope():\n",
1919
+ " b_prime = tf.random.uniform(shape=(3,3))\n",
1920
+ " b_prime = b_prime * 3\n",
1921
+ " a_prime = tf.random.uniform(shape=(3,1))\n",
1922
+ " a_prime = a_prime * 3\n",
1923
+ "\n",
1924
+ "assert not np.allclose(a.numpy(), a_prime.numpy())\n",
1925
+ "assert not np.allclose(b.numpy(), b_prime.numpy())"
1926
+ ]
1927
+ },
1928
+ {
1929
+ "cell_type": "markdown",
1930
+ "metadata": {
1931
+ "id": "nHhOLHyQIkAe"
1932
+ },
1933
+ "source": [
1934
+ "To allow for debugging variations due to tracing order, `DeterministicRandomTestTool` in `num_random_ops` mode allows you to see how many random operations have been traced with the `operation_seed` property."
1935
+ ]
1936
+ },
1937
+ {
1938
+ "cell_type": "code",
1939
+ "execution_count": 31,
1940
+ "metadata": {
1941
+ "execution": {
1942
+ "iopub.execute_input": "2022-12-14T03:39:54.105583Z",
1943
+ "iopub.status.busy": "2022-12-14T03:39:54.105101Z",
1944
+ "iopub.status.idle": "2022-12-14T03:39:54.114616Z",
1945
+ "shell.execute_reply": "2022-12-14T03:39:54.114047Z"
1946
+ },
1947
+ "id": "33RCSICuJEyV"
1948
+ },
1949
+ "outputs": [
1950
+ {
1951
+ "name": "stdout",
1952
+ "output_type": "stream",
1953
+ "text": [
1954
+ "0\n",
1955
+ "1\n",
1956
+ "2\n"
1957
+ ]
1958
+ }
1959
+ ],
1960
+ "source": [
1961
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
1962
+ "with random_tool.scope():\n",
1963
+ " print(random_tool.operation_seed)\n",
1964
+ " a = tf.random.uniform(shape=(3,1))\n",
1965
+ " a = a * 3\n",
1966
+ " print(random_tool.operation_seed)\n",
1967
+ " b = tf.random.uniform(shape=(3,3))\n",
1968
+ " b = b * 3\n",
1969
+ " print(random_tool.operation_seed)"
1970
+ ]
1971
+ },
1972
+ {
1973
+ "cell_type": "markdown",
1974
+ "metadata": {
1975
+ "id": "bkQD3NpOMxIv"
1976
+ },
1977
+ "source": [
1978
+ "If you need to account for varying trace order in your tests, you can even set the auto-incrementing `operation_seed` explicitly. For example, you can use this to make random number generation match across two different program orders."
1979
+ ]
1980
+ },
1981
+ {
1982
+ "cell_type": "code",
1983
+ "execution_count": 32,
1984
+ "metadata": {
1985
+ "execution": {
1986
+ "iopub.execute_input": "2022-12-14T03:39:54.117828Z",
1987
+ "iopub.status.busy": "2022-12-14T03:39:54.117272Z",
1988
+ "iopub.status.idle": "2022-12-14T03:39:54.134599Z",
1989
+ "shell.execute_reply": "2022-12-14T03:39:54.134050Z"
1990
+ },
1991
+ "id": "6W4sS_wOM8CH"
1992
+ },
1993
+ "outputs": [
1994
+ {
1995
+ "name": "stdout",
1996
+ "output_type": "stream",
1997
+ "text": [
1998
+ "0\n",
1999
+ "1\n"
2000
+ ]
2001
+ }
2002
+ ],
2003
+ "source": [
2004
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2005
+ "with random_tool.scope():\n",
2006
+ " print(random_tool.operation_seed)\n",
2007
+ " a = tf.random.uniform(shape=(3,1))\n",
2008
+ " a = a * 3\n",
2009
+ " print(random_tool.operation_seed)\n",
2010
+ " b = tf.random.uniform(shape=(3,3))\n",
2011
+ " b = b * 3\n",
2012
+ "\n",
2013
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2014
+ "with random_tool.scope():\n",
2015
+ " random_tool.operation_seed = 1\n",
2016
+ " b_prime = tf.random.uniform(shape=(3,3))\n",
2017
+ " b_prime = b_prime * 3\n",
2018
+ " random_tool.operation_seed = 0\n",
2019
+ " a_prime = tf.random.uniform(shape=(3,1))\n",
2020
+ " a_prime = a_prime * 3\n",
2021
+ "\n",
2022
+ "np.testing.assert_allclose(a.numpy(), a_prime.numpy(), **tol_dict)\n",
2023
+ "np.testing.assert_allclose(b.numpy(), b_prime.numpy(), **tol_dict)\n"
2024
+ ]
2025
+ },
2026
+ {
2027
+ "cell_type": "markdown",
2028
+ "metadata": {
2029
+ "id": "bP5Kx1OcNbvM"
2030
+ },
2031
+ "source": [
2032
+ "However, `DeterministicRandomTestTool` disallows reusing already-used operation seeds, so make sure the auto-incremented sequences cannot overlap. This is because eager execution generates different numbers for follow-on usages of the same operation seed while TF1 graphs and sessions do not, so raising an error helps keep session and eager stateful random number generation in line."
2033
+ ]
2034
+ },
2035
+ {
2036
+ "cell_type": "code",
2037
+ "execution_count": 33,
2038
+ "metadata": {
2039
+ "execution": {
2040
+ "iopub.execute_input": "2022-12-14T03:39:54.138009Z",
2041
+ "iopub.status.busy": "2022-12-14T03:39:54.137470Z",
2042
+ "iopub.status.idle": "2022-12-14T03:39:54.147707Z",
2043
+ "shell.execute_reply": "2022-12-14T03:39:54.147126Z"
2044
+ },
2045
+ "id": "GmBgg5hzNa5H"
2046
+ },
2047
+ "outputs": [
2048
+ {
2049
+ "name": "stdout",
2050
+ "output_type": "stream",
2051
+ "text": [
2052
+ "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.\n"
2053
+ ]
2054
+ }
2055
+ ],
2056
+ "source": [
2057
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2058
+ "with random_tool.scope():\n",
2059
+ " random_tool.operation_seed = 1\n",
2060
+ " b_prime = tf.random.uniform(shape=(3,3))\n",
2061
+ " b_prime = b_prime * 3\n",
2062
+ " random_tool.operation_seed = 0\n",
2063
+ " a_prime = tf.random.uniform(shape=(3,1))\n",
2064
+ " a_prime = a_prime * 3\n",
2065
+ " try:\n",
2066
+ " c = tf.random.uniform(shape=(3,1))\n",
2067
+ " raise RuntimeError(\"An exception should have been raised before this, \" +\n",
2068
+ " \"because the auto-incremented operation seed will \" +\n",
2069
+ " \"overlap an already-used value\")\n",
2070
+ " except ValueError as err:\n",
2071
+ " print(err)\n"
2072
+ ]
2073
+ },
2074
+ {
2075
+ "cell_type": "markdown",
2076
+ "metadata": {
2077
+ "id": "U-bLOeCmOn-4"
2078
+ },
2079
+ "source": [
2080
+ "### Verifying Inference\n",
2081
+ "\n",
2082
+ "You can now use the `DeterministicRandomTestTool` to make sure the `InceptionResnetV2` model matches in inference, even when using the random weight initialization. For a stronger test condition due to matching program order, use the `num_random_ops` mode."
2083
+ ]
2084
+ },
2085
+ {
2086
+ "cell_type": "code",
2087
+ "execution_count": 34,
2088
+ "metadata": {
2089
+ "execution": {
2090
+ "iopub.execute_input": "2022-12-14T03:39:54.151016Z",
2091
+ "iopub.status.busy": "2022-12-14T03:39:54.150495Z",
2092
+ "iopub.status.idle": "2022-12-14T03:40:15.741319Z",
2093
+ "shell.execute_reply": "2022-12-14T03:40:15.740627Z"
2094
+ },
2095
+ "id": "8TWOrflkPa7T"
2096
+ },
2097
+ "outputs": [
2098
+ {
2099
+ "name": "stdout",
2100
+ "output_type": "stream",
2101
+ "text": [
2102
+ "Regularization loss: 1.2254326\n"
2103
+ ]
2104
+ }
2105
+ ],
2106
+ "source": [
2107
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2108
+ "with random_tool.scope():\n",
2109
+ " graph = tf.Graph()\n",
2110
+ " with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
2111
+ " height, width = 299, 299\n",
2112
+ " num_classes = 1000\n",
2113
+ " inputs = tf.ones( (1, height, width, 3))\n",
2114
+ "\n",
2115
+ " out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)\n",
2116
+ "\n",
2117
+ " # Initialize the variables\n",
2118
+ " sess.run(tf.compat.v1.global_variables_initializer())\n",
2119
+ "\n",
2120
+ " # Grab the outputs & regularization loss\n",
2121
+ " reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
2122
+ " tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
2123
+ " tf1_output = sess.run(out)\n",
2124
+ "\n",
2125
+ " print(\"Regularization loss:\", tf1_regularization_loss)"
2126
+ ]
2127
+ },
2128
+ {
2129
+ "cell_type": "code",
2130
+ "execution_count": 35,
2131
+ "metadata": {
2132
+ "execution": {
2133
+ "iopub.execute_input": "2022-12-14T03:40:15.744904Z",
2134
+ "iopub.status.busy": "2022-12-14T03:40:15.744601Z",
2135
+ "iopub.status.idle": "2022-12-14T03:40:18.420342Z",
2136
+ "shell.execute_reply": "2022-12-14T03:40:18.419659Z"
2137
+ },
2138
+ "id": "Qcx6ur4KPMI1"
2139
+ },
2140
+ "outputs": [
2141
+ {
2142
+ "name": "stdout",
2143
+ "output_type": "stream",
2144
+ "text": [
2145
+ "Regularization loss: tf.Tensor(1.2254325, shape=(), dtype=float32)\n"
2146
+ ]
2147
+ }
2148
+ ],
2149
+ "source": [
2150
+ "height, width = 299, 299\n",
2151
+ "num_classes = 1000\n",
2152
+ "\n",
2153
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2154
+ "with random_tool.scope():\n",
2155
+ " model = InceptionResnetV2(num_classes)\n",
2156
+ "\n",
2157
+ " inputs = tf.ones((1, height, width, 3))\n",
2158
+ " tf2_output, endpoints = model(inputs, training=False)\n",
2159
+ "\n",
2160
+ " # Grab the regularization loss as well\n",
2161
+ " tf2_regularization_loss = tf.math.add_n(model.losses)\n",
2162
+ "\n",
2163
+ "print(\"Regularization loss:\", tf2_regularization_loss)"
2164
+ ]
2165
+ },
2166
+ {
2167
+ "cell_type": "code",
2168
+ "execution_count": 36,
2169
+ "metadata": {
2170
+ "execution": {
2171
+ "iopub.execute_input": "2022-12-14T03:40:18.423973Z",
2172
+ "iopub.status.busy": "2022-12-14T03:40:18.423477Z",
2173
+ "iopub.status.idle": "2022-12-14T03:40:18.427976Z",
2174
+ "shell.execute_reply": "2022-12-14T03:40:18.427410Z"
2175
+ },
2176
+ "id": "m_SS2b6qPFl1"
2177
+ },
2178
+ "outputs": [],
2179
+ "source": [
2180
+ "# Verify that the regularization loss and output both match\n",
2181
+ "# when using the DeterministicRandomTestTool:\n",
2182
+ "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
2183
+ "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
2184
+ ]
2185
+ },
2186
+ {
2187
+ "cell_type": "markdown",
2188
+ "metadata": {
2189
+ "id": "TKSktIRaP-5b"
2190
+ },
2191
+ "source": [
2192
+ "### Verifying Training\n",
2193
+ "\n",
2194
+ "Because `DeterministicRandomTestTool` works for *all* stateful random operations (including both weight initialization and computation such as dropout layers), you can use it to verify the models match in training mode as well. You can again use the `num_random_ops` mode because the program order of the stateful random ops matches."
2195
+ ]
2196
+ },
2197
+ {
2198
+ "cell_type": "code",
2199
+ "execution_count": 37,
2200
+ "metadata": {
2201
+ "execution": {
2202
+ "iopub.execute_input": "2022-12-14T03:40:18.431282Z",
2203
+ "iopub.status.busy": "2022-12-14T03:40:18.430734Z",
2204
+ "iopub.status.idle": "2022-12-14T03:40:42.196309Z",
2205
+ "shell.execute_reply": "2022-12-14T03:40:42.195578Z"
2206
+ },
2207
+ "id": "nMBFVa1kQTJH"
2208
+ },
2209
+ "outputs": [
2210
+ {
2211
+ "name": "stdout",
2212
+ "output_type": "stream",
2213
+ "text": [
2214
+ "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.\n",
2215
+ "Instructions for updating:\n",
2216
+ "Colocations handled automatically by placer.\n"
2217
+ ]
2218
+ },
2219
+ {
2220
+ "name": "stdout",
2221
+ "output_type": "stream",
2222
+ "text": [
2223
+ "Regularization loss: 1.22548\n"
2224
+ ]
2225
+ }
2226
+ ],
2227
+ "source": [
2228
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2229
+ "with random_tool.scope():\n",
2230
+ " graph = tf.Graph()\n",
2231
+ " with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
2232
+ " height, width = 299, 299\n",
2233
+ " num_classes = 1000\n",
2234
+ " inputs = tf.ones( (1, height, width, 3))\n",
2235
+ "\n",
2236
+ " out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=True)\n",
2237
+ "\n",
2238
+ " # Initialize the variables\n",
2239
+ " sess.run(tf.compat.v1.global_variables_initializer())\n",
2240
+ "\n",
2241
+ " # Grab the outputs & regularization loss\n",
2242
+ " reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
2243
+ " tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
2244
+ " tf1_output = sess.run(out)\n",
2245
+ "\n",
2246
+ " print(\"Regularization loss:\", tf1_regularization_loss)"
2247
+ ]
2248
+ },
2249
+ {
2250
+ "cell_type": "code",
2251
+ "execution_count": 38,
2252
+ "metadata": {
2253
+ "execution": {
2254
+ "iopub.execute_input": "2022-12-14T03:40:42.199907Z",
2255
+ "iopub.status.busy": "2022-12-14T03:40:42.199657Z",
2256
+ "iopub.status.idle": "2022-12-14T03:40:45.015643Z",
2257
+ "shell.execute_reply": "2022-12-14T03:40:45.014921Z"
2258
+ },
2259
+ "id": "-jlBkwI5QTJI"
2260
+ },
2261
+ "outputs": [
2262
+ {
2263
+ "name": "stdout",
2264
+ "output_type": "stream",
2265
+ "text": [
2266
+ "Regularization loss: tf.Tensor(1.2254798, shape=(), dtype=float32)\n"
2267
+ ]
2268
+ }
2269
+ ],
2270
+ "source": [
2271
+ "height, width = 299, 299\n",
2272
+ "num_classes = 1000\n",
2273
+ "\n",
2274
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
2275
+ "with random_tool.scope():\n",
2276
+ " model = InceptionResnetV2(num_classes)\n",
2277
+ "\n",
2278
+ " inputs = tf.ones((1, height, width, 3))\n",
2279
+ " tf2_output, endpoints = model(inputs, training=True)\n",
2280
+ "\n",
2281
+ " # Grab the regularization loss as well\n",
2282
+ " tf2_regularization_loss = tf.math.add_n(model.losses)\n",
2283
+ "\n",
2284
+ "print(\"Regularization loss:\", tf2_regularization_loss)"
2285
+ ]
2286
+ },
2287
+ {
2288
+ "cell_type": "code",
2289
+ "execution_count": 39,
2290
+ "metadata": {
2291
+ "execution": {
2292
+ "iopub.execute_input": "2022-12-14T03:40:45.019242Z",
2293
+ "iopub.status.busy": "2022-12-14T03:40:45.018962Z",
2294
+ "iopub.status.idle": "2022-12-14T03:40:45.023395Z",
2295
+ "shell.execute_reply": "2022-12-14T03:40:45.022782Z"
2296
+ },
2297
+ "id": "IL9mjTLnQTJJ"
2298
+ },
2299
+ "outputs": [],
2300
+ "source": [
2301
+ "# Verify that the regularization loss and output both match\n",
2302
+ "# when using the DeterministicRandomTestTool\n",
2303
+ "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
2304
+ "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
2305
+ ]
2306
+ },
2307
+ {
2308
+ "cell_type": "markdown",
2309
+ "metadata": {
2310
+ "id": "uJTZvmfnQqZH"
2311
+ },
2312
+ "source": [
2313
+ "You have now verified that the `InceptionResnetV2` model running eagerly with decorators around `tf.keras.layers.Layer` numerically matches the slim network running in TF1 graphs and sessions."
2314
+ ]
2315
+ },
2316
+ {
2317
+ "cell_type": "markdown",
2318
+ "metadata": {
2319
+ "id": "xpOAei5vRAPa"
2320
+ },
2321
+ "source": [
2322
+ "Note: When using the `DeterministicRandomTestTool` in `num_random_ops` mode, it is suggested you directly use and call the `tf.keras.layers.Layer` method decorator when testing for numerical equivalence. Embedding it within a Keras functional model or other Keras models can produce differences in stateful random operation tracing order that can be tricky to reason about or match exactly when comparing TF1.x graphs/sessions and eager execution. \n",
2323
+ "\n",
2324
+ "For example, calling the `InceptionResnetV2` layer directly with `training=True` interleaves variable initialization with the dropout order according to the network creation order.\n",
2325
+ "\n",
2326
+ "On the other hand, first putting the `tf.keras.layers.Layer` decorator in a Keras functional model and only then calling the model with `training=True` is equivalent to initializing all variables then using the dropout layer. This produces a different tracing order and a different set of random numbers.\n",
2327
+ "\n",
2328
+ "However, the default `mode='constant'` is not sensitive to these differences in tracing order and will pass without extra work even when embedding the layer in a Keras functional model."
2329
+ ]
2330
+ },
2331
+ {
2332
+ "cell_type": "code",
2333
+ "execution_count": 40,
2334
+ "metadata": {
2335
+ "execution": {
2336
+ "iopub.execute_input": "2022-12-14T03:40:45.026795Z",
2337
+ "iopub.status.busy": "2022-12-14T03:40:45.026567Z",
2338
+ "iopub.status.idle": "2022-12-14T03:41:08.547454Z",
2339
+ "shell.execute_reply": "2022-12-14T03:41:08.546677Z"
2340
+ },
2341
+ "id": "0dSR4ZNvYNYm"
2342
+ },
2343
+ "outputs": [
2344
+ {
2345
+ "name": "stdout",
2346
+ "output_type": "stream",
2347
+ "text": [
2348
+ "Regularization loss: 1.2239965\n"
2349
+ ]
2350
+ }
2351
+ ],
2352
+ "source": [
2353
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
2354
+ "with random_tool.scope():\n",
2355
+ " graph = tf.Graph()\n",
2356
+ " with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
2357
+ " height, width = 299, 299\n",
2358
+ " num_classes = 1000\n",
2359
+ " inputs = tf.ones( (1, height, width, 3))\n",
2360
+ "\n",
2361
+ " out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=True)\n",
2362
+ "\n",
2363
+ " # Initialize the variables\n",
2364
+ " sess.run(tf.compat.v1.global_variables_initializer())\n",
2365
+ "\n",
2366
+ " # Get the outputs & regularization losses\n",
2367
+ " reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
2368
+ " tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
2369
+ " tf1_output = sess.run(out)\n",
2370
+ "\n",
2371
+ " print(\"Regularization loss:\", tf1_regularization_loss)"
2372
+ ]
2373
+ },
2374
+ {
2375
+ "cell_type": "code",
2376
+ "execution_count": 41,
2377
+ "metadata": {
2378
+ "execution": {
2379
+ "iopub.execute_input": "2022-12-14T03:41:08.551835Z",
2380
+ "iopub.status.busy": "2022-12-14T03:41:08.551188Z",
2381
+ "iopub.status.idle": "2022-12-14T03:41:16.167387Z",
2382
+ "shell.execute_reply": "2022-12-14T03:41:16.166695Z"
2383
+ },
2384
+ "id": "iMPMMnPtYUY7"
2385
+ },
2386
+ "outputs": [
2387
+ {
2388
+ "name": "stderr",
2389
+ "output_type": "stream",
2390
+ "text": [
2391
+ "/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.\n",
2392
+ " warnings.warn('`layer.updates` will be removed in a future version. '\n",
2393
+ "/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.\n",
2394
+ " self.updates, tf.compat.v1.GraphKeys.UPDATE_OPS\n"
2395
+ ]
2396
+ },
2397
+ {
2398
+ "name": "stdout",
2399
+ "output_type": "stream",
2400
+ "text": [
2401
+ "Regularization loss: tf.Tensor(1.2239964, shape=(), dtype=float32)\n"
2402
+ ]
2403
+ }
2404
+ ],
2405
+ "source": [
2406
+ "height, width = 299, 299\n",
2407
+ "num_classes = 1000\n",
2408
+ "\n",
2409
+ "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
2410
+ "with random_tool.scope():\n",
2411
+ " keras_input = tf.keras.Input(shape=(height, width, 3))\n",
2412
+ " layer = InceptionResnetV2(num_classes)\n",
2413
+ " model = tf.keras.Model(inputs=keras_input, outputs=layer(keras_input))\n",
2414
+ "\n",
2415
+ " inputs = tf.ones((1, height, width, 3))\n",
2416
+ " tf2_output, endpoints = model(inputs, training=True)\n",
2417
+ "\n",
2418
+ " # Get the regularization loss\n",
2419
+ " tf2_regularization_loss = tf.math.add_n(model.losses)\n",
2420
+ "\n",
2421
+ "print(\"Regularization loss:\", tf2_regularization_loss)"
2422
+ ]
2423
+ },
2424
+ {
2425
+ "cell_type": "code",
2426
+ "execution_count": 42,
2427
+ "metadata": {
2428
+ "execution": {
2429
+ "iopub.execute_input": "2022-12-14T03:41:16.171428Z",
2430
+ "iopub.status.busy": "2022-12-14T03:41:16.170699Z",
2431
+ "iopub.status.idle": "2022-12-14T03:41:16.175329Z",
2432
+ "shell.execute_reply": "2022-12-14T03:41:16.174712Z"
2433
+ },
2434
+ "id": "jf46KUVyYUY8"
2435
+ },
2436
+ "outputs": [],
2437
+ "source": [
2438
+ "# Verify that the regularization loss and output both match\n",
2439
+ "# when using the DeterministicRandomTestTool\n",
2440
+ "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
2441
+ "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
2442
+ ]
2443
+ },
2444
+ {
2445
+ "cell_type": "markdown",
2446
+ "metadata": {
2447
+ "id": "hWXHjtkiZ09V"
2448
+ },
2449
+ "source": [
2450
+ "## Step 3b or 4b (optional): Testing with pre-existing checkpoints\n",
2451
+ "\n",
2452
+ "After step 3 or step 4 above, it can be useful to run your numerical equivalence tests when starting from pre-existing name-based checkpoints if you have some. This can test both that your legacy checkpoint loading is working correctly and that the model itself is working right. The [Reusing TF1.x checkpoints guide](./reuse_checkpoints.ipynb) covers how to reuse your pre-existing TF1.x checkpoints and transfer them over to TF2 checkpoints.\n"
2453
+ ]
2454
+ },
2455
+ {
2456
+ "cell_type": "markdown",
2457
+ "metadata": {
2458
+ "id": "v6i3MFmGcxYx"
2459
+ },
2460
+ "source": [
2461
+ "## Additional Testing & Troubleshooting\n",
2462
+ "\n",
2463
+ "As you add more numerical equivalence tests, you may also choose to add a test that verifies your gradient computation (or even your optimizer updates) match.\n",
2464
+ "\n",
2465
+ "Backpropagation and gradient computation are more prone to floating point numerical instabilities than model forward passes. This means that as your equivalence tests cover more non-isolated parts of your training, you may begin to see non-trivial numerics differences between running fully eagerly and your TF1 graphs. This may be caused by TensorFlow's graph optimizations that do things such as replace subexpressions in a graph with fewer mathematical operations.\n",
2466
+ "\n",
2467
+ "To isolate whether this is likely to be the case, you can compare your TF1 code to TF2 computation happening inside of a `tf.function` (which applies graph optimization passes like your TF1 graph) rather than to a purely eager computation. Alternatively, you can try using `tf.config.optimizer.set_experimental_options` to disable optimization passes such as `\"arithmetic_optimization\"` before your TF1 computation to see if the result ends up numerically closer to your TF2 computation results. In your actual training runs it is recommended you use `tf.function` with optimization passes enabled for performance reasons, but you may find it useful to disable them in your numerical equivalence unit tests.\n",
2468
+ "\n",
2469
+ "Similarly, you may also find that `tf.compat.v1.train` optimizers and TF2 optimizers have slightly different floating point numerics properties than TF2 optimizers, even if the mathematical formulas they are representing are the same. This is less likely to be an issue in your training runs, but it may require a higher numerical tolerance in equivalence unit tests."
2470
+ ]
2471
+ }
2472
+ ],
2473
+ "metadata": {
2474
+ "colab": {
2475
+ "collapsed_sections": [],
2476
+ "name": "validate_correctness.ipynb",
2477
+ "toc_visible": true
2478
+ },
2479
+ "kernelspec": {
2480
+ "display_name": "Python 3",
2481
+ "name": "python3"
2482
+ },
2483
+ "language_info": {
2484
+ "codemirror_mode": {
2485
+ "name": "ipython",
2486
+ "version": 3
2487
+ },
2488
+ "file_extension": ".py",
2489
+ "mimetype": "text/x-python",
2490
+ "name": "python",
2491
+ "nbconvert_exporter": "python",
2492
+ "pygments_lexer": "ipython3",
2493
+ "version": "3.9.16"
2494
+ }
2495
+ },
2496
+ "nbformat": 4,
2497
+ "nbformat_minor": 0
2498
+ }