Skip to content

Commit 9643e7e

Browse files
committed
Fix some issues with TF 2.2 breaking changes
1 parent fe878ea commit 9643e7e

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

12_custom_models_and_training_with_tensorflow.ipynb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,13 @@
25792579
"model.fit(X_train_scaled.astype(np.float32), y_train.astype(np.float32), epochs=2)"
25802580
]
25812581
},
2582+
{
2583+
"cell_type": "markdown",
2584+
"metadata": {},
2585+
"source": [
2586+
"**Warning**: In TF 2.2, tf.keras adds an extra first metric in `model.metrics` at position 0 (see [TF issue #38150](https://github.com/tensorflow/tensorflow/issues/38150)). This forces us to use `model.metrics[-1]` rather than `model.metrics[0]` to access the `HuberMetric`."
2587+
]
2588+
},
25822589
{
25832590
"cell_type": "code",
25842591
"execution_count": 124,
@@ -2596,7 +2603,7 @@
25962603
}
25972604
],
25982605
"source": [
2599-
"model.metrics[0].threshold"
2606+
"model.metrics[-1].threshold"
26002607
]
26012608
},
26022609
{
@@ -2764,6 +2771,13 @@
27642771
"model.fit(X_train_scaled.astype(np.float32), y_train.astype(np.float32), epochs=2)"
27652772
]
27662773
},
2774+
{
2775+
"cell_type": "markdown",
2776+
"metadata": {},
2777+
"source": [
2778+
"**Warning**: In TF 2.2, tf.keras adds an extra first metric in `model.metrics` at position 0 (see [TF issue #38150](https://github.com/tensorflow/tensorflow/issues/38150)). This forces us to use `model.metrics[-1]` rather than `model.metrics[0]` to access the `HuberMetric`."
2779+
]
2780+
},
27672781
{
27682782
"cell_type": "code",
27692783
"execution_count": 134,
@@ -2783,7 +2797,7 @@
27832797
}
27842798
],
27852799
"source": [
2786-
"model.metrics[0].threshold"
2800+
"model.metrics[-1].threshold"
27872801
]
27882802
},
27892803
{
@@ -3124,8 +3138,6 @@
31243138
"class ResidualBlock(keras.layers.Layer):\n",
31253139
" def __init__(self, n_layers, n_neurons, **kwargs):\n",
31263140
" super().__init__(**kwargs)\n",
3127-
" self.n_layers = n_layers # not shown in the book\n",
3128-
" self.n_neurons = n_neurons # not shown\n",
31293141
" self.hidden = [keras.layers.Dense(n_neurons, activation=\"elu\",\n",
31303142
" kernel_initializer=\"he_normal\")\n",
31313143
" for _ in range(n_layers)]\n",
@@ -3134,12 +3146,7 @@
31343146
" Z = inputs\n",
31353147
" for layer in self.hidden:\n",
31363148
" Z = layer(Z)\n",
3137-
" return inputs + Z\n",
3138-
" \n",
3139-
" def get_config(self): # not shown\n",
3140-
" base_config = super().get_config() # not shown\n",
3141-
" return {**base_config, # not shown\n",
3142-
" \"n_layers\": self.n_layers, \"n_neurons\": self.n_neurons} # not shown"
3149+
" return inputs + Z"
31433150
]
31443151
},
31453152
{
@@ -3151,7 +3158,6 @@
31513158
"class ResidualRegressor(keras.models.Model):\n",
31523159
" def __init__(self, output_dim, **kwargs):\n",
31533160
" super().__init__(**kwargs)\n",
3154-
" self.output_dim = output_dim # not shown in the book\n",
31553161
" self.hidden1 = keras.layers.Dense(30, activation=\"elu\",\n",
31563162
" kernel_initializer=\"he_normal\")\n",
31573163
" self.block1 = ResidualBlock(2, 30)\n",
@@ -3163,12 +3169,7 @@
31633169
" for _ in range(1 + 3):\n",
31643170
" Z = self.block1(Z)\n",
31653171
" Z = self.block2(Z)\n",
3166-
" return self.out(Z)\n",
3167-
"\n",
3168-
" def get_config(self): # not shown\n",
3169-
" base_config = super().get_config() # not shown\n",
3170-
" return {**base_config, # not shown\n",
3171-
" \"output_dim\": self.output_dim} # not shown"
3172+
" return self.out(Z)"
31723173
]
31733174
},
31743175
{

0 commit comments

Comments
 (0)