Skip to content

Commit 3e74e03

Browse files
cantoniostf-text-github-robot
authored andcommitted
Update TF.Text guide and tutorial notebooks for TensorFlow 2.20+ and Keras 3.
Several TF.Text guide and tutorial notebooks fail on current TensorFlow releases due to outdated package pins, legacy system package workarounds, and Keras 3 API changes. - Unpin legacy package versions, remove obsolete `apt` / `protobuf` workarounds and leftover empty setup cells, and replace `tensorflow-addons` metric usage in `bert_glue.ipynb` with `SparseCategoricalAccuracy`. - Enable legacy Keras (`TF_USE_LEGACY_KERAS=1`) and install `tf-keras` in notebooks that depend on Keras 2 APIs, TensorFlow Hub layers, or `tf-models-official`. - Update `tf.keras.utils.get_file` dataset path handling (`train_dir` and `dataset_dir`) to work with both Keras 2 and Keras 3 return values, and fix a `super().__init__()` call in `text_generation.ipynb`. PiperOrigin-RevId: 1010774895
1 parent a913336 commit 3e74e03

15 files changed

Lines changed: 75 additions & 76 deletions

‎docs/guide/bert_preprocessing_guide.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"cell_type": "code",
120120
"execution_count": null,
121121
"source": [
122-
"!pip install -q -U \"tensorflow-text==2.11.*\""
122+
"!pip install -q -U tensorflow-text"
123123
],
124124
"outputs": [],
125125
"metadata": {

‎docs/guide/subwords_tokenizer.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
},
110110
"outputs": [],
111111
"source": [
112-
"!pip install -q -U \"tensorflow-text==2.11.*\""
112+
"!pip install -q -U tensorflow-text"
113113
]
114114
},
115115
{

‎docs/guide/text_tf_lite.ipynb‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
},
110110
"outputs": [],
111111
"source": [
112-
"!pip install -U \"tensorflow-text==2.11.*\""
112+
"!pip install -q -U tensorflow-text tf-keras"
113113
]
114114
},
115115
{
@@ -120,6 +120,9 @@
120120
},
121121
"outputs": [],
122122
"source": [
123+
"import os\n",
124+
"os.environ[\"TF_USE_LEGACY_KERAS\"] = \"1\"\n",
125+
"\n",
123126
"from absl import app\n",
124127
"import numpy as np\n",
125128
"import tensorflow as tf\n",

‎docs/guide/tokenizers.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
},
9696
"outputs": [],
9797
"source": [
98-
"!pip install -q \"tensorflow-text==2.11.*\""
98+
"!pip install -q -U tensorflow-text"
9999
]
100100
},
101101
{

‎docs/tutorials/bert_glue.ipynb‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
},
117117
"outputs": [],
118118
"source": [
119-
"!pip install -q -U \"tensorflow-text==2.11.*\""
119+
"!pip install -q -U tensorflow-text"
120120
]
121121
},
122122
{
@@ -136,7 +136,7 @@
136136
},
137137
"outputs": [],
138138
"source": [
139-
"!pip install -q -U tf-models-official==2.11.0"
139+
"!pip install -q -U tf-models-official"
140140
]
141141
},
142142
{
@@ -159,11 +159,12 @@
159159
"outputs": [],
160160
"source": [
161161
"import os\n",
162+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
163+
"\n",
162164
"import tensorflow as tf\n",
163165
"import tensorflow_hub as hub\n",
164166
"import tensorflow_datasets as tfds\n",
165167
"import tensorflow_text as text # A dependency of the preprocessing model\n",
166-
"import tensorflow_addons as tfa\n",
167168
"from official.nlp import optimization\n",
168169
"import numpy as np\n",
169170
"\n",
@@ -817,12 +818,8 @@
817818
"def get_configuration(glue_task):\n",
818819
"\n",
819820
" loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n",
820-
"\n",
821-
" if glue_task == 'glue/cola':\n",
822-
" metrics = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=2)\n",
823-
" else:\n",
824-
" metrics = tf.keras.metrics.SparseCategoricalAccuracy(\n",
825-
" 'accuracy', dtype=tf.float32)\n",
821+
" metrics = tf.keras.metrics.SparseCategoricalAccuracy(\n",
822+
" 'accuracy', dtype=tf.float32)\n",
826823
"\n",
827824
" return metrics, loss"
828825
]

‎docs/tutorials/classify_text_with_bert.ipynb‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"outputs": [],
110110
"source": [
111111
"# A dependency of the preprocessing for BERT inputs\n",
112-
"!pip install -U \"tensorflow-text==2.13.*\""
112+
"!pip install -q -U tensorflow-text"
113113
]
114114
},
115115
{
@@ -129,7 +129,7 @@
129129
},
130130
"outputs": [],
131131
"source": [
132-
"!pip install \"tf-models-official==2.13.*\""
132+
"!pip install -q -U tf-models-official"
133133
]
134134
},
135135
{
@@ -143,6 +143,8 @@
143143
"import os\n",
144144
"import shutil\n",
145145
"\n",
146+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
147+
"\n",
146148
"import tensorflow as tf\n",
147149
"import tensorflow_hub as hub\n",
148150
"import tensorflow_text as text\n",
@@ -192,6 +194,8 @@
192194
" cache_subdir='')\n",
193195
"\n",
194196
"dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')\n",
197+
"if not os.path.exists(dataset_dir):\n",
198+
" dataset_dir = os.path.join(dataset, 'aclImdb')\n",
195199
"\n",
196200
"train_dir = os.path.join(dataset_dir, 'train')\n",
197201
"\n",
@@ -226,7 +230,7 @@
226230
"seed = 42\n",
227231
"\n",
228232
"raw_train_ds = tf.keras.utils.text_dataset_from_directory(\n",
229-
" 'aclImdb/train',\n",
233+
" train_dir,\n",
230234
" batch_size=batch_size,\n",
231235
" validation_split=0.2,\n",
232236
" subset='training',\n",
@@ -236,7 +240,7 @@
236240
"train_ds = raw_train_ds.cache().prefetch(buffer_size=AUTOTUNE)\n",
237241
"\n",
238242
"val_ds = tf.keras.utils.text_dataset_from_directory(\n",
239-
" 'aclImdb/train',\n",
243+
" train_dir,\n",
240244
" batch_size=batch_size,\n",
241245
" validation_split=0.2,\n",
242246
" subset='validation',\n",
@@ -245,7 +249,7 @@
245249
"val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)\n",
246250
"\n",
247251
"test_ds = tf.keras.utils.text_dataset_from_directory(\n",
248-
" 'aclImdb/test',\n",
252+
" os.path.join(dataset_dir, 'test'),\n",
249253
" batch_size=batch_size)\n",
250254
"\n",
251255
"test_ds = test_ds.cache().prefetch(buffer_size=AUTOTUNE)"

‎docs/tutorials/image_captioning.ipynb‎

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -146,28 +146,6 @@
146146
"## Setup"
147147
]
148148
},
149-
{
150-
"cell_type": "code",
151-
"execution_count": null,
152-
"metadata": {
153-
"id": "gc06pTaBbl72"
154-
},
155-
"outputs": [],
156-
"source": [
157-
"!apt install --allow-change-held-packages libcudnn8=8.6.0.163-1+cuda11.8"
158-
]
159-
},
160-
{
161-
"cell_type": "code",
162-
"execution_count": null,
163-
"metadata": {
164-
"id": "2R1hQGtZEi8Y"
165-
},
166-
"outputs": [],
167-
"source": [
168-
"!pip uninstall -y tensorflow estimator keras"
169-
]
170-
},
171149
{
172150
"cell_type": "code",
173151
"execution_count": null,
@@ -176,7 +154,7 @@
176154
},
177155
"outputs": [],
178156
"source": [
179-
"!pip install -U tensorflow_text tensorflow tensorflow_datasets"
157+
"!pip install -q -U tensorflow-text tensorflow_datasets tf-keras"
180158
]
181159
},
182160
{
@@ -187,7 +165,7 @@
187165
},
188166
"outputs": [],
189167
"source": [
190-
"!pip install einops"
168+
"!pip install -q einops"
191169
]
192170
},
193171
{
@@ -209,14 +187,16 @@
209187
"outputs": [],
210188
"source": [
211189
"#@title\n",
190+
"import os\n",
191+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
192+
"\n",
212193
"import concurrent.futures\n",
213194
"import collections\n",
214195
"import dataclasses\n",
215196
"import hashlib\n",
216197
"import itertools\n",
217198
"import json\n",
218199
"import math\n",
219-
"import os\n",
220200
"import pathlib\n",
221201
"import random\n",
222202
"import re\n",

‎docs/tutorials/nmt_with_attention.ipynb‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@
130130
},
131131
"outputs": [],
132132
"source": [
133-
"!pip install \"tensorflow-text\u003e=2.11\"\n",
134-
"!pip install einops"
133+
"!pip install -q -U tensorflow-text tf-keras\n",
134+
"!pip install -q einops"
135135
]
136136
},
137137
{
@@ -142,6 +142,9 @@
142142
},
143143
"outputs": [],
144144
"source": [
145+
"import os\n",
146+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
147+
"\n",
145148
"import numpy as np\n",
146149
"\n",
147150
"import typing\n",
@@ -256,7 +259,9 @@
256259
" 'spa-eng.zip', origin='http://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip',\n",
257260
" extract=True)\n",
258261
"\n",
259-
"path_to_file = pathlib.Path(path_to_zip).parent/'spa-eng/spa.txt'"
262+
"path_to_file = pathlib.Path(path_to_zip).parent/'spa-eng/spa.txt'\n",
263+
"if not path_to_file.exists():\n",
264+
" path_to_file = pathlib.Path(path_to_zip)/'spa-eng/spa.txt'"
260265
]
261266
},
262267
{

‎docs/tutorials/text_classification_rnn.ipynb‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
},
8989
"outputs": [],
9090
"source": [
91+
"!pip install -q -U tf-keras\n",
92+
"\n",
93+
"import os\n",
94+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
95+
"\n",
9196
"import numpy as np\n",
9297
"\n",
9398
"import tensorflow_datasets as tfds\n",

‎docs/tutorials/text_generation.ipynb‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,15 @@
156156
},
157157
"outputs": [],
158158
"source": [
159-
"import tensorflow as tf\n",
159+
"!pip install -q -U tf-keras\n",
160160
"\n",
161-
"import numpy as np\n",
162161
"import os\n",
163-
"import time"
162+
"import time\n",
163+
"\n",
164+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
165+
"\n",
166+
"import numpy as np\n",
167+
"import tensorflow as tf"
164168
]
165169
},
166170
{
@@ -673,7 +677,7 @@
673677
"source": [
674678
"class MyModel(tf.keras.Model):\n",
675679
" def __init__(self, vocab_size, embedding_dim, rnn_units):\n",
676-
" super().__init__(self)\n",
680+
" super().__init__()\n",
677681
" self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)\n",
678682
" self.gru = tf.keras.layers.GRU(rnn_units,\n",
679683
" return_sequences=True,\n",

0 commit comments

Comments
 (0)