Skip to content

Commit 183c35a

Browse files
authored
Lint and update the Overfit and underfit tutorial
1 parent f78339c commit 183c35a

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

site/en/tutorials/keras/overfit_and_underfit.ipynb

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@
102102
"source": [
103103
"As always, the code in this example will use the `tf.keras` API, which you can learn more about in the TensorFlow [Keras guide](https://www.tensorflow.org/guide/keras).\n",
104104
"\n",
105-
"In both of the previous examples—[classifying text](https://www.tensorflow.org/tutorials/keras/text_classification_with_hub) and [predicting fuel efficiency](https://www.tensorflow.org/tutorials/keras/regression) — we saw that the accuracy of our model on the validation data would peak after training for a number of epochs, and would then stagnate or start decreasing.\n",
105+
"In both of the previous examples—[classifying text](text_classification_with_hub.ipynb) and [predicting fuel efficiency](regression.ipynb)—the accuracy of models on the validation data would peak after training for a number of epochs and then stagnate or start decreasing.\n",
106106
"\n",
107-
"In other words, our model would *overfit* to the training data. Learning how to deal with overfitting is important. Although it's often possible to achieve high accuracy on the *training set*, what we really want is to develop models that generalize well to a *testing set* (or data they haven't seen before).\n",
107+
"In other words, your model would *overfit* to the training data. Learning how to deal with overfitting is important. Although it's often possible to achieve high accuracy on the *training set*, what you really want is to develop models that generalize well to a *testing set* (or data they haven't seen before).\n",
108108
"\n",
109109
"The opposite of overfitting is *underfitting*. Underfitting occurs when there is still room for improvement on the train data. This can happen for a number of reasons: If the model is not powerful enough, is over-regularized, or has simply not been trained long enough. This means the network has not learned the relevant patterns in the training data.\n",
110110
"\n",
111-
"If you train for too long though, the model will start to overfit and learn patterns from the training data that don't generalize to the test data. We need to strike a balance. Understanding how to train for an appropriate number of epochs as we'll explore below is a useful skill.\n",
111+
"If you train for too long though, the model will start to overfit and learn patterns from the training data that don't generalize to the test data. You need to strike a balance. Understanding how to train for an appropriate number of epochs as we'll explore below is a useful skill.\n",
112112
"\n",
113113
"To prevent overfitting, the best solution is to use more complete training data. The dataset should cover the full range of inputs that the model is expected to handle. Additional data may only be useful if it covers new and interesting cases.\n",
114114
"\n",
@@ -202,9 +202,9 @@
202202
"id": "1cweoTiruj8O"
203203
},
204204
"source": [
205-
"## The Higgs Dataset\n",
205+
"## The Higgs dataset\n",
206206
"\n",
207-
"The goal of this tutorial is not to do particle physics, so don't dwell on the details of the dataset. It contains 11 000 000 examples, each with 28 features, and a binary class label."
207+
"The goal of this tutorial is not to do particle physics, so don't dwell on the details of the dataset. It contains 11,000,000 examples, each with 28 features, and a binary class label."
208208
]
209209
},
210210
{
@@ -280,7 +280,7 @@
280280
"source": [
281281
"TensorFlow is most efficient when operating on large batches of data.\n",
282282
"\n",
283-
"So instead of repacking each row individually make a new `Dataset` that takes batches of 10000-examples, applies the `pack_row` function to each batch, and then splits the batches back up into individual records:"
283+
"So, instead of repacking each row individually make a new `tf.data.Dataset` that takes batches of 10,000 examples, applies the `pack_row` function to each batch, and then splits the batches back up into individual records:"
284284
]
285285
},
286286
{
@@ -300,7 +300,7 @@
300300
"id": "lUbxc5bxNSXV"
301301
},
302302
"source": [
303-
"Have a look at some of the records from this new `packed_ds`.\n",
303+
"Inspect some of the records from this new `packed_ds`.\n",
304304
"\n",
305305
"The features are not perfectly normalized, but this is sufficient for this tutorial."
306306
]
@@ -324,7 +324,7 @@
324324
"id": "ICKZRY7gN-QM"
325325
},
326326
"source": [
327-
"To keep this tutorial relatively short use just the first 1000 samples for validation, and the next 10 000 for training:"
327+
"To keep this tutorial relatively short, use just the first 1,000 samples for validation, and the next 10,000 for training:"
328328
]
329329
},
330330
{
@@ -382,7 +382,7 @@
382382
"id": "6PMliHoVO3OL"
383383
},
384384
"source": [
385-
"These datasets return individual examples. Use the `.batch` method to create batches of an appropriate size for training. Before batching also remember to `.shuffle` and `.repeat` the training set."
385+
"These datasets return individual examples. Use the `Dataset.batch` method to create batches of an appropriate size for training. Before batching, also remember to use `Dataset.shuffle` and `Dataset.repeat` on the training set."
386386
]
387387
},
388388
{
@@ -417,7 +417,7 @@
417417
"\n",
418418
"To find an appropriate model size, it's best to start with relatively few layers and parameters, then begin increasing the size of the layers or adding new layers until you see diminishing returns on the validation loss.\n",
419419
"\n",
420-
"Start with a simple model using only `layers.Dense` as a baseline, then create larger versions, and compare them."
420+
"Start with a simple model using only densely-connected layers (`tf.keras.layers.Dense`) as a baseline, then create larger models, and compare them."
421421
]
422422
},
423423
{
@@ -435,7 +435,7 @@
435435
"id": "pNzkSkkXSP5l"
436436
},
437437
"source": [
438-
"Many models train better if you gradually reduce the learning rate during training. Use `optimizers.schedules` to reduce the learning rate over time:"
438+
"Many models train better if you gradually reduce the learning rate during training. Use `tf.keras.optimizers.schedules` to reduce the learning rate over time:"
439439
]
440440
},
441441
{
@@ -462,7 +462,7 @@
462462
"id": "kANLx6OYTQ8B"
463463
},
464464
"source": [
465-
"The code above sets a `schedules.InverseTimeDecay` to hyperbolically decrease the learning rate to 1/2 of the base rate at 1000 epochs, 1/3 at 2000 epochs and so on."
465+
"The code above sets a `tf.keras.optimizers.schedules.InverseTimeDecay` to hyperbolically decrease the learning rate to 1/2 of the base rate at 1,000 epochs, 1/3 at 2,000 epochs, and so on."
466466
]
467467
},
468468
{
@@ -492,7 +492,7 @@
492492
"\n",
493493
"The training for this tutorial runs for many short epochs. To reduce the logging noise use the `tfdocs.EpochDots` which simply prints a `.` for each epoch, and a full set of metrics every 100 epochs.\n",
494494
"\n",
495-
"Next include `callbacks.EarlyStopping` to avoid long and unnecessary training times. Note that this callback is set to monitor the `val_binary_crossentropy`, not the `val_loss`. This difference will be important later.\n",
495+
"Next include `tf.keras.callbacks.EarlyStopping` to avoid long and unnecessary training times. Note that this callback is set to monitor the `val_binary_crossentropy`, not the `val_loss`. This difference will be important later.\n",
496496
"\n",
497497
"Use `callbacks.TensorBoard` to generate TensorBoard logs for the training.\n"
498498
]
@@ -643,7 +643,7 @@
643643
"id": "YjMb6E72f2pN"
644644
},
645645
"source": [
646-
"To see if you can beat the performance of the small model, progressively train some larger models.\n",
646+
"To check if you can beat the performance of the small model, progressively train some larger models.\n",
647647
"\n",
648648
"Try two hidden layers with 16 units each:"
649649
]
@@ -690,7 +690,7 @@
690690
"id": "SrfoVQheYSO5"
691691
},
692692
"source": [
693-
"Now try 3 hidden layers with 64 units each:"
693+
"Now try three hidden layers with 64 units each:"
694694
]
695695
},
696696
{
@@ -737,7 +737,7 @@
737737
"source": [
738738
"### Large model\n",
739739
"\n",
740-
"As an exercise, you can create an even larger model, and see how quickly it begins overfitting. Next, let's add to this benchmark a network that has much more capacity, far more than the problem would warrant:"
740+
"As an exercise, you can create an even larger model and check how quickly it begins overfitting. Next, add to this benchmark a network that has much more capacity, far more than the problem would warrant:"
741741
]
742742
},
743743
{
@@ -803,7 +803,7 @@
803803
"source": [
804804
"While building a larger model gives it more power, if this power is not constrained somehow it can easily overfit to the training set.\n",
805805
"\n",
806-
"In this example, typically, only the `\"Tiny\"` model manages to avoid overfitting altogether, and each of the larger models overfit the data more quickly. This becomes so severe for the `\"large\"` model that you need to switch the plot to a log-scale to really see what's happening.\n",
806+
"In this example, typically, only the `\"Tiny\"` model manages to avoid overfitting altogether, and each of the larger models overfit the data more quickly. This becomes so severe for the `\"large\"` model that you need to switch the plot to a log-scale to really figure out what's happening.\n",
807807
"\n",
808808
"This is apparent if you plot and compare the validation metrics to the training metrics.\n",
809809
"\n",
@@ -969,15 +969,15 @@
969969
"source": [
970970
"You may be familiar with Occam's Razor principle: given two explanations for something, the explanation most likely to be correct is the \"simplest\" one, the one that makes the least amount of assumptions. This also applies to the models learned by neural networks: given some training data and a network architecture, there are multiple sets of weights values (multiple models) that could explain the data, and simpler models are less likely to overfit than complex ones.\n",
971971
"\n",
972-
"A \"simple model\" in this context is a model where the distribution of parameter values has less entropy (or a model with fewer parameters altogether, as we saw in the section above). Thus a common way to mitigate overfitting is to put constraints on the complexity of a network by forcing its weights only to take small values, which makes the distribution of weight values more \"regular\". This is called \"weight regularization\", and it is done by adding to the loss function of the network a cost associated with having large weights. This cost comes in two flavors:\n",
972+
"A \"simple model\" in this context is a model where the distribution of parameter values has less entropy (or a model with fewer parameters altogether, as demonstrated in the section above). Thus a common way to mitigate overfitting is to put constraints on the complexity of a network by forcing its weights only to take small values, which makes the distribution of weight values more \"regular\". This is called \"weight regularization\", and it is done by adding to the loss function of the network a cost associated with having large weights. This cost comes in two flavors:\n",
973973
"\n",
974974
"* [L1 regularization](https://developers.google.com/machine-learning/glossary/#L1_regularization), where the cost added is proportional to the absolute value of the weights coefficients (i.e. to what is called the \"L1 norm\" of the weights).\n",
975975
"\n",
976976
"* [L2 regularization](https://developers.google.com/machine-learning/glossary/#L2_regularization), where the cost added is proportional to the square of the value of the weights coefficients (i.e. to what is called the squared \"L2 norm\" of the weights). L2 regularization is also called weight decay in the context of neural networks. Don't let the different name confuse you: weight decay is mathematically the exact same as L2 regularization.\n",
977977
"\n",
978-
"L1 regularization pushes weights towards exactly zero encouraging a sparse model. L2 regularization will penalize the weights parameters without making them sparse since the penalty goes to zero for small weights-one reason why L2 is more common.\n",
978+
"L1 regularization pushes weights towards exactly zero, encouraging a sparse model. L2 regularization will penalize the weights parameters without making them sparse since the penalty goes to zero for small weightsone reason why L2 is more common.\n",
979979
"\n",
980-
"In `tf.keras`, weight regularization is added by passing weight regularizer instances to layers as keyword arguments. Let's add L2 weight regularization now."
980+
"In `tf.keras`, weight regularization is added by passing weight regularizer instances to layers as keyword arguments. Add L2 weight regularization:"
981981
]
982982
},
983983
{
@@ -1035,7 +1035,7 @@
10351035
"id": "Kx1YHMsVxWjP"
10361036
},
10371037
"source": [
1038-
"As you can see, the `\"L2\"` regularized model is now much more competitive with the the `\"Tiny\"` model. This `\"L2\"` model is also much more resistant to overfitting than the `\"Large\"` model it was based on despite having the same number of parameters."
1038+
"As demonstrated in the diagram above, the `\"L2\"` regularized model is now much more competitive with the `\"Tiny\"` model. This `\"L2\"` model is also much more resistant to overfitting than the `\"Large\"` model it was based on despite having the same number of parameters."
10391039
]
10401040
},
10411041
{
@@ -1046,9 +1046,9 @@
10461046
"source": [
10471047
"#### More info\n",
10481048
"\n",
1049-
"There are two important things to note about this sort of regularization.\n",
1049+
"There are two important things to note about this sort of regularization:\n",
10501050
"\n",
1051-
"**First:** if you are writing your own training loop, then you need to be sure to ask the model for its regularization losses."
1051+
"1. If you are writing your own training loop, then you need to be sure to ask the model for its regularization losses."
10521052
]
10531053
},
10541054
{
@@ -1069,9 +1069,9 @@
10691069
"id": "MLhG6fMSjE-J"
10701070
},
10711071
"source": [
1072-
"**Second:** This implementation works by adding the weight penalties to the model's loss, and then applying a standard optimization procedure after that.\n",
1072+
"2. This implementation works by adding the weight penalties to the model's loss, and then applying a standard optimization procedure after that.\n",
10731073
"\n",
1074-
"There is a second approach that instead only runs the optimizer on the raw loss, and then while applying the calculated step the optimizer also applies some weight decay. This \"Decoupled Weight Decay\" is seen in optimizers like `optimizers.FTRL` and `optimizers.AdamW`."
1074+
"There is a second approach that instead only runs the optimizer on the raw loss, and then while applying the calculated step the optimizer also applies some weight decay. This \"decoupled weight decay\" is used in optimizers like `tf.keras.optimizers.Ftrl` and `tfa.optimizers.AdamW`."
10751075
]
10761076
},
10771077
{
@@ -1086,14 +1086,13 @@
10861086
"\n",
10871087
"The intuitive explanation for dropout is that because individual nodes in the network cannot rely on the output of the others, each node must output features that are useful on their own.\n",
10881088
"\n",
1089-
"Dropout, applied to a layer, consists of randomly \"dropping out\" (i.e. set to zero) a number of output features of the layer during training. Let's say a given layer would normally have returned a vector [0.2, 0.5, 1.3, 0.8, 1.1] for a given input sample during training; after applying dropout, this vector will have a few zero entries distributed at random, e.g. [0, 0.5,\n",
1090-
"1.3, 0, 1.1].\n",
1089+
"Dropout, applied to a layer, consists of randomly \"dropping out\" (i.e. set to zero) a number of output features of the layer during training. For example, a given layer would normally have returned a vector `[0.2, 0.5, 1.3, 0.8, 1.1]` for a given input sample during training; after applying dropout, this vector will have a few zero entries distributed at random, e.g. `[0, 0.5, 1.3, 0, 1.1]`.\n",
10911090
"\n",
10921091
"The \"dropout rate\" is the fraction of the features that are being zeroed-out; it is usually set between 0.2 and 0.5. At test time, no units are dropped out, and instead the layer's output values are scaled down by a factor equal to the dropout rate, so as to balance for the fact that more units are active than at training time.\n",
10931092
"\n",
1094-
"In `tf.keras` you can introduce dropout in a network via the Dropout layer, which gets applied to the output of layer right before.\n",
1093+
"In Keras, you can introduce dropout in a network via the `tf.keras.layers.Dropout` layer, which gets applied to the output of layer right before.\n",
10951094
"\n",
1096-
"Let's add two Dropout layers in our network to see how well they do at reducing overfitting:"
1095+
"Add two dropout layers to your network to check how well they do at reducing overfitting:"
10971096
]
10981097
},
10991098
{
@@ -1269,7 +1268,7 @@
12691268
"id": "gjfnkEeQyAFG"
12701269
},
12711270
"source": [
1272-
"To recap: here are the most common ways to prevent overfitting in neural networks:\n",
1271+
"To recap, here are the most common ways to prevent overfitting in neural networks:\n",
12731272
"\n",
12741273
"* Get more training data.\n",
12751274
"* Reduce the capacity of the network.\n",
@@ -1278,8 +1277,8 @@
12781277
"\n",
12791278
"Two important approaches not covered in this guide are:\n",
12801279
"\n",
1281-
"* data-augmentation\n",
1282-
"* batch normalization\n",
1280+
"* [Data augmentation](../images/data_augmentation.ipynb)\n",
1281+
"* Batch normalization (`tf.keras.layers.BatchNormalization`)\n",
12831282
"\n",
12841283
"Remember that each method can help on its own, but often combining them can be even more effective."
12851284
]

0 commit comments

Comments
 (0)