Skip to content

Commit 49a8276

Browse files
authored
Update reproducibility_recipes.py (#1941)
* Update reproducibility_recipes.py * Fix formatting and add the generated files
1 parent 5f278fd commit 49a8276

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

examples/keras_recipes/ipynb/reproducibility_recipes.ipynb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,13 @@
252252
" )\n",
253253
"\n",
254254
" model.compile(\n",
255-
" optimizer=\"adam\", loss=\"sparse_categorical_crossentropy\", metrics=[\"accuracy\"]\n",
255+
" optimizer=\"adam\",\n",
256+
" loss=\"sparse_categorical_crossentropy\",\n",
257+
" metrics=[\"accuracy\"],\n",
258+
" jit_compile=False,\n",
256259
" )\n",
260+
" # jit_compile's default value is \"auto\" which will cause some problems in some\n",
261+
" # ops, therefore it's set to False.\n",
257262
"\n",
258263
" # model.fit has a `shuffle` parameter which has a default value of `True`.\n",
259264
" # If you are using array-like objects, this will shuffle the data before\n",

examples/keras_recipes/md/reproducibility_recipes.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,89 +87,89 @@ for initializer in initializers_list:
8787
<div class="k-default-codeblock">
8888
```
8989
Running <class 'keras.src.initializers.random_initializers.RandomNormal'>
90-
Iteration --> 0 // Result --> 0.05609520897269249
91-
Iteration --> 1 // Result --> 0.05609520897269249
90+
Iteration --> 0 // Result --> 0.000790853810030967
91+
Iteration --> 1 // Result --> 0.000790853810030967
9292
```
9393
</div>
9494

9595

9696
<div class="k-default-codeblock">
9797
```
9898
Running <class 'keras.src.initializers.random_initializers.RandomUniform'>
99-
Iteration --> 0 // Result --> 0.03690483793616295
100-
Iteration --> 1 // Result --> 0.03690483793616295
99+
Iteration --> 0 // Result --> -0.02175668440759182
100+
Iteration --> 1 // Result --> -0.02175668440759182
101101
```
102102
</div>
103103

104104

105105
<div class="k-default-codeblock">
106106
```
107107
Running <class 'keras.src.initializers.random_initializers.TruncatedNormal'>
108-
Iteration --> 0 // Result --> 0.05230803042650223
109-
Iteration --> 1 // Result --> 0.05230803042650223
108+
Iteration --> 0 // Result --> 0.000790853810030967
109+
Iteration --> 1 // Result --> 0.000790853810030967
110110
```
111111
</div>
112112

113113

114114
<div class="k-default-codeblock">
115115
```
116116
Running <class 'keras.src.initializers.random_initializers.VarianceScaling'>
117-
Iteration --> 0 // Result --> 1.1893247365951538
118-
Iteration --> 1 // Result --> 1.1893247365951538
117+
Iteration --> 0 // Result --> 0.017981600016355515
118+
Iteration --> 1 // Result --> 0.017981600016355515
119119
```
120120
</div>
121121

122122

123123
<div class="k-default-codeblock">
124124
```
125125
Running <class 'keras.src.initializers.random_initializers.GlorotNormal'>
126-
Iteration --> 0 // Result --> 1.1893247365951538
127-
Iteration --> 1 // Result --> 1.1893247365951538
126+
Iteration --> 0 // Result --> 0.017981600016355515
127+
Iteration --> 1 // Result --> 0.017981600016355515
128128
```
129129
</div>
130130

131131

132132
<div class="k-default-codeblock">
133133
```
134134
Running <class 'keras.src.initializers.random_initializers.GlorotUniform'>
135-
Iteration --> 0 // Result --> 1.2784210443496704
136-
Iteration --> 1 // Result --> 1.2784210443496704
135+
Iteration --> 0 // Result --> -0.7536736726760864
136+
Iteration --> 1 // Result --> -0.7536736726760864
137137
```
138138
</div>
139139

140140

141141
<div class="k-default-codeblock">
142142
```
143143
Running <class 'keras.src.initializers.random_initializers.HeNormal'>
144-
Iteration --> 0 // Result --> 1.6819592714309692
145-
Iteration --> 1 // Result --> 1.6819592714309692
144+
Iteration --> 0 // Result --> 0.025429822504520416
145+
Iteration --> 1 // Result --> 0.025429822504520416
146146
```
147147
</div>
148148

149149

150150
<div class="k-default-codeblock">
151151
```
152152
Running <class 'keras.src.initializers.random_initializers.HeUniform'>
153-
Iteration --> 0 // Result --> 1.8079603910446167
154-
Iteration --> 1 // Result --> 1.8079603910446167
153+
Iteration --> 0 // Result --> -1.065855622291565
154+
Iteration --> 1 // Result --> -1.065855622291565
155155
```
156156
</div>
157157

158158

159159
<div class="k-default-codeblock">
160160
```
161161
Running <class 'keras.src.initializers.random_initializers.LecunNormal'>
162-
Iteration --> 0 // Result --> 1.1893247365951538
163-
Iteration --> 1 // Result --> 1.1893247365951538
162+
Iteration --> 0 // Result --> 0.017981600016355515
163+
Iteration --> 1 // Result --> 0.017981600016355515
164164
```
165165
</div>
166166

167167

168168
<div class="k-default-codeblock">
169169
```
170170
Running <class 'keras.src.initializers.random_initializers.LecunUniform'>
171-
Iteration --> 0 // Result --> 1.2784210443496704
172-
Iteration --> 1 // Result --> 1.2784210443496704
171+
Iteration --> 0 // Result --> -0.7536736726760864
172+
Iteration --> 1 // Result --> -0.7536736726760864
173173
```
174174
</div>
175175

@@ -273,8 +273,13 @@ def train_model(train_data: tf.data.Dataset, test_data: tf.data.Dataset) -> dict
273273
)
274274

275275
model.compile(
276-
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
276+
optimizer="adam",
277+
loss="sparse_categorical_crossentropy",
278+
metrics=["accuracy"],
279+
jit_compile=False,
277280
)
281+
# jit_compile's default value is "auto" which will cause some problems in some
282+
# ops, therefore it's set to False.
278283

279284
# model.fit has a `shuffle` parameter which has a default value of `True`.
280285
# If you are using array-like objects, this will shuffle the data before
@@ -298,6 +303,13 @@ train_ds = tf.data.Dataset.from_tensor_slices((train_images, train_labels))
298303
test_ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels))
299304
```
300305

306+
<div class="k-default-codeblock">
307+
```
308+
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
309+
11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
310+
311+
```
312+
</div>
301313
Remember we called `tf.config.experimental.enable_op_determinism()` at the
302314
beginning of the function. This makes the `tf.data` operations deterministic.
303315
However, making `tf.data` operations deterministic comes with a performance
@@ -375,11 +387,11 @@ history = train_model(train_data, test_data)
375387
<div class="k-default-codeblock">
376388
```
377389
Epoch 1/2
378-
938/938 ━━━━━━━━━━━━━━━━━━━━ 26s 27ms/step - accuracy: 0.5418 - loss: 1.2867 - val_accuracy: 0.9291 - val_loss: 0.2303
390+
938/938 ━━━━━━━━━━━━━━━━━━━━ 73s 73ms/step - accuracy: 0.5726 - loss: 1.2175 - val_accuracy: 0.9401 - val_loss: 0.1924
379391
Epoch 2/2
380-
938/938 ━━━━━━━━━━━━━━━━━━━━ 25s 26ms/step - accuracy: 0.9075 - loss: 0.2983 - val_accuracy: 0.9583 - val_loss: 0.1343
381-
157/157 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.9512 - loss: 0.1559
382-
Model accuracy on test data: 95.83%
392+
938/938 ━━━━━━━━━━━━━━━━━━━━ 89s 81ms/step - accuracy: 0.9105 - loss: 0.2885 - val_accuracy: 0.9630 - val_loss: 0.1131
393+
157/157 ━━━━━━━━━━━━━━━━━━━━ 3s 17ms/step - accuracy: 0.9553 - loss: 0.1353
394+
Model accuracy on test data: 96.30%
383395
384396
```
385397
</div>

examples/keras_recipes/reproducibility_recipes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,13 @@ def train_model(train_data: tf.data.Dataset, test_data: tf.data.Dataset) -> dict
151151
)
152152

153153
model.compile(
154-
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
154+
optimizer="adam",
155+
loss="sparse_categorical_crossentropy",
156+
metrics=["accuracy"],
157+
jit_compile=False,
155158
)
159+
# jit_compile's default value is "auto" which will cause some problems in some
160+
# ops, therefore it's set to False.
156161

157162
# model.fit has a `shuffle` parameter which has a default value of `True`.
158163
# If you are using array-like objects, this will shuffle the data before

0 commit comments

Comments
 (0)