Skip to content

Commit

Permalink
Fix notebook failure with Keras 3.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 615189902
  • Loading branch information
MarkDaoust authored and copybara-github committed Mar 12, 2024
1 parent 3688f3c commit b647684
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 131 deletions.
23 changes: 7 additions & 16 deletions site/en/tutorials/images/transfer_learning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@
},
"outputs": [],
"source": [
"prediction_layer = tf.keras.layers.Dense(1)\n",
"prediction_layer = tf.keras.layers.Dense(1, activation='sigmoid')\n",
"prediction_batch = prediction_layer(feature_batch_average)\n",
"print(prediction_batch.shape)"
]
Expand Down Expand Up @@ -667,7 +667,7 @@
"source": [
"### Compile the model\n",
"\n",
"Compile the model before training it. Since there are two classes, use the `tf.keras.losses.BinaryCrossentropy` loss with `from_logits=True` since the model provides a linear output."
"Compile the model before training it. Since there are two classes and a sigmoid oputput, use the `BinaryAccuracy`."
]
},
{
Expand All @@ -680,8 +680,8 @@
"source": [
"base_learning_rate = 0.0001\n",
"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),\n",
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
" metrics=[tf.keras.metrics.BinaryAccuracy(threshold=0, name='accuracy')])"
" loss=tf.keras.losses.BinaryCrossentropy(),\n",
" metrics=[tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='accuracy')])"
]
},
{
Expand Down Expand Up @@ -872,9 +872,9 @@
},
"outputs": [],
"source": [
"model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
"model.compile(loss=tf.keras.losses.BinaryCrossentropy(),\n",
" optimizer = tf.keras.optimizers.RMSprop(learning_rate=base_learning_rate/10),\n",
" metrics=[tf.keras.metrics.BinaryAccuracy(threshold=0, name='accuracy')])"
" metrics=[tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='accuracy')])"
]
},
{
Expand Down Expand Up @@ -1081,22 +1081,13 @@
"\n",
"To learn more, visit the [Transfer learning guide](https://www.tensorflow.org/guide/keras/transfer_learning).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "uKIByL01da8c"
},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"name": "transfer_learning.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
Expand Down
44 changes: 22 additions & 22 deletions site/en/tutorials/keras/text_classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@
"id": "95kkUdRoaeMw"
},
"source": [
"Next, you will use the `text_dataset_from_directory` utility to create a labeled `tf.data.Dataset`. [tf.data](https://www.tensorflow.org/guide/data) is a powerful collection of tools for working with data. \n",
"Next, you will use the `text_dataset_from_directory` utility to create a labeled `tf.data.Dataset`. [tf.data](https://www.tensorflow.org/guide/data) is a powerful collection of tools for working with data.\n",
"\n",
"When running a machine learning experiment, it is a best practice to divide your dataset into three splits: [train](https://developers.google.com/machine-learning/glossary#training_set), [validation](https://developers.google.com/machine-learning/glossary#validation_set), and [test](https://developers.google.com/machine-learning/glossary#test-set). \n",
"When running a machine learning experiment, it is a best practice to divide your dataset into three splits: [train](https://developers.google.com/machine-learning/glossary#training_set), [validation](https://developers.google.com/machine-learning/glossary#validation_set), and [test](https://developers.google.com/machine-learning/glossary#test-set).\n",
"\n",
"The IMDB dataset has already been divided into train and test, but it lacks a validation set. Let's create a validation set using an 80:20 split of the training data by using the `validation_split` argument below."
]
Expand All @@ -286,10 +286,10 @@
"seed = 42\n",
"\n",
"raw_train_ds = tf.keras.utils.text_dataset_from_directory(\n",
" 'aclImdb/train', \n",
" batch_size=batch_size, \n",
" validation_split=0.2, \n",
" subset='training', \n",
" 'aclImdb/train',\n",
" batch_size=batch_size,\n",
" validation_split=0.2,\n",
" subset='training',\n",
" seed=seed)"
]
},
Expand Down Expand Up @@ -322,7 +322,7 @@
"id": "JWq1SUIrp1a-"
},
"source": [
"Notice the reviews contain raw text (with punctuation and occasional HTML tags like `<br/>`). You will show how to handle these in the following section. \n",
"Notice the reviews contain raw text (with punctuation and occasional HTML tags like `<br/>`). You will show how to handle these in the following section.\n",
"\n",
"The labels are 0 or 1. To see which of these correspond to positive and negative movie reviews, you can check the `class_names` property on the dataset.\n"
]
Expand Down Expand Up @@ -366,10 +366,10 @@
"outputs": [],
"source": [
"raw_val_ds = tf.keras.utils.text_dataset_from_directory(\n",
" 'aclImdb/train', \n",
" batch_size=batch_size, \n",
" validation_split=0.2, \n",
" subset='validation', \n",
" 'aclImdb/train',\n",
" batch_size=batch_size,\n",
" validation_split=0.2,\n",
" subset='validation',\n",
" seed=seed)"
]
},
Expand All @@ -382,7 +382,7 @@
"outputs": [],
"source": [
"raw_test_ds = tf.keras.utils.text_dataset_from_directory(\n",
" 'aclImdb/test', \n",
" 'aclImdb/test',\n",
" batch_size=batch_size)"
]
},
Expand All @@ -394,7 +394,7 @@
"source": [
"### Prepare the dataset for training\n",
"\n",
"Next, you will standardize, tokenize, and vectorize the data using the helpful `tf.keras.layers.TextVectorization` layer. \n",
"Next, you will standardize, tokenize, and vectorize the data using the helpful `tf.keras.layers.TextVectorization` layer.\n",
"\n",
"Standardization refers to preprocessing the text, typically to remove punctuation or HTML elements to simplify the dataset. Tokenization refers to splitting strings into tokens (for example, splitting a sentence into individual words, by splitting on whitespace). Vectorization refers to converting tokens into numbers so they can be fed into a neural network. All of these tasks can be accomplished with this layer.\n",
"\n",
Expand Down Expand Up @@ -580,7 +580,7 @@
"\n",
"`.cache()` keeps data in memory after it's loaded off disk. This will ensure the dataset does not become a bottleneck while training your model. If your dataset is too large to fit into memory, you can also use this method to create a performant on-disk cache, which is more efficient to read than many small files.\n",
"\n",
"`.prefetch()` overlaps data preprocessing and model execution while training. \n",
"`.prefetch()` overlaps data preprocessing and model execution while training.\n",
"\n",
"You can learn more about both methods, as well as how to cache data to disk in the [data performance guide](https://www.tensorflow.org/guide/data_performance)."
]
Expand Down Expand Up @@ -635,7 +635,7 @@
" layers.Dropout(0.2),\n",
" layers.GlobalAveragePooling1D(),\n",
" layers.Dropout(0.2),\n",
" layers.Dense(1)])\n",
" layers.Dense(1, activation='sigmoid')])\n",
"\n",
"model.summary()"
]
Expand Down Expand Up @@ -674,9 +674,9 @@
},
"outputs": [],
"source": [
"model.compile(loss=losses.BinaryCrossentropy(from_logits=True),\n",
"model.compile(loss=losses.BinaryCrossentropy(),\n",
" optimizer='adam',\n",
" metrics=tf.metrics.BinaryAccuracy(threshold=0.0))"
" metrics=[tf.metrics.BinaryAccuracy(threshold=0.5)])"
]
},
{
Expand Down Expand Up @@ -884,11 +884,11 @@
},
"outputs": [],
"source": [
"examples = [\n",
"examples = tf.constant([\n",
" \"The movie was great!\",\n",
" \"The movie was okay.\",\n",
" \"The movie was terrible...\"\n",
"]\n",
"])\n",
"\n",
"export_model.predict(examples)"
]
Expand Down Expand Up @@ -916,7 +916,7 @@
"\n",
"This tutorial showed how to train a binary classifier from scratch on the IMDB dataset. As an exercise, you can modify this notebook to train a multi-class classifier to predict the tag of a programming question on [Stack Overflow](http://stackoverflow.com/).\n",
"\n",
"A [dataset](https://storage.googleapis.com/download.tensorflow.org/data/stack_overflow_16k.tar.gz) has been prepared for you to use containing the body of several thousand programming questions (for example, \"How can I sort a dictionary by value in Python?\") posted to Stack Overflow. Each of these is labeled with exactly one tag (either Python, CSharp, JavaScript, or Java). Your task is to take a question as input, and predict the appropriate tag, in this case, Python. \n",
"A [dataset](https://storage.googleapis.com/download.tensorflow.org/data/stack_overflow_16k.tar.gz) has been prepared for you to use containing the body of several thousand programming questions (for example, \"How can I sort a dictionary by value in Python?\") posted to Stack Overflow. Each of these is labeled with exactly one tag (either Python, CSharp, JavaScript, or Java). Your task is to take a question as input, and predict the appropriate tag, in this case, Python.\n",
"\n",
"The dataset you will work with contains several thousand questions extracted from the much larger public Stack Overflow dataset on [BigQuery](https://console.cloud.google.com/marketplace/details/stack-exchange/stack-overflow), which contains more than 17 million posts.\n",
"\n",
Expand Down Expand Up @@ -950,7 +950,7 @@
"\n",
"1. When plotting accuracy over time, change `binary_accuracy` and `val_binary_accuracy` to `accuracy` and `val_accuracy`, respectively.\n",
"\n",
"1. Once these changes are complete, you will be able to train a multi-class classifier. "
"1. Once these changes are complete, you will be able to train a multi-class classifier."
]
},
{
Expand All @@ -968,8 +968,8 @@
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "text_classification.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
Expand Down
20 changes: 10 additions & 10 deletions site/en/tutorials/quickstart/advanced.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
"id": "uGih-c2LgbJu"
},
"source": [
"Choose an optimizer and loss function for training: "
"Choose an optimizer and loss function for training:"
]
},
{
Expand Down Expand Up @@ -311,10 +311,10 @@
"\n",
"for epoch in range(EPOCHS):\n",
" # Reset the metrics at the start of the next epoch\n",
" train_loss.reset_states()\n",
" train_accuracy.reset_states()\n",
" test_loss.reset_states()\n",
" test_accuracy.reset_states()\n",
" train_loss.reset_state()\n",
" train_accuracy.reset_state()\n",
" test_loss.reset_state()\n",
" test_accuracy.reset_state()\n",
"\n",
" for images, labels in train_ds:\n",
" train_step(images, labels)\n",
Expand All @@ -324,10 +324,10 @@
"\n",
" print(\n",
" f'Epoch {epoch + 1}, '\n",
" f'Loss: {train_loss.result()}, '\n",
" f'Accuracy: {train_accuracy.result() * 100}, '\n",
" f'Test Loss: {test_loss.result()}, '\n",
" f'Test Accuracy: {test_accuracy.result() * 100}'\n",
" f'Loss: {train_loss.result():0.2f}, '\n",
" f'Accuracy: {train_accuracy.result() * 100:0.2f}, '\n",
" f'Test Loss: {test_loss.result():0.2f}, '\n",
" f'Test Accuracy: {test_accuracy.result() * 100:0.2f}'\n",
" )"
]
},
Expand All @@ -344,8 +344,8 @@
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "advanced.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
Expand Down
Loading

0 comments on commit b647684

Please sign in to comment.