Skip to content

Commit 1f3c829

Browse files
cantoniostf-text-github-robot
authored andcommitted
Fix failing TF.Text guide and tutorial notebooks for TF 2.20+ and Keras 3.
- Unpin legacy `tensorflow-text==2.11.*` / `2.13.*`, `tf-models-official==2.11.0` / `2.13.*`, and `tensorflow>2.10` pip installs across guide and tutorial notebooks so they do not downgrade TensorFlow against NumPy 2.x. - Remove obsolete `!apt install ... libcudnn8=...`, `!pip uninstall -y tensorflow ...`, and `protobuf~=3.20.3` commands in `transformer.ipynb`, `image_captioning.ipynb`, and `uncertainty_quantification_with_sngp_bert.ipynb`. - Pin `"setuptools<81"` in notebooks that use `tensorflow_hub` or `tf_text.HubModuleTokenizer` so `import pkg_resources` in `tensorflow_hub` succeeds when `setuptools>=82.0.0` is preinstalled. - Set `os.environ["TF_USE_LEGACY_KERAS"] = "1"` before importing TensorFlow in notebooks that rely on Keras 2 (`tf_keras`) APIs, `tensorflow_hub.KerasLayer`, `tf-models-official`, `warmstart_embedding_matrix`, `MobileNetV3Small(include_preprocessing=True)`, or NumPy string array inputs. - Fix `super().__init__(self)` -> `super().__init__()` in `MyModel.__init__` in `text_generation.ipynb`. - Make dataset path resolution resilient to both Keras 2 and Keras 3 `tf.keras.utils.get_file` return paths in `nmt_with_attention.ipynb`, `classify_text_with_bert.ipynb`, `warmstart_embedding_matrix.ipynb`, and `word_embeddings.ipynb`. PiperOrigin-RevId: 1009121307
1 parent c8d37cd commit 1f3c829

15 files changed

Lines changed: 71 additions & 32 deletions

‎docs/guide/bert_preprocessing_guide.ipynb‎

Lines changed: 4 additions & 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": {
@@ -130,6 +130,9 @@
130130
"cell_type": "code",
131131
"execution_count": null,
132132
"source": [
133+
"import os\n",
134+
"os.environ[\"TF_USE_LEGACY_KERAS\"] = \"1\"\n",
135+
"\n",
133136
"import tensorflow as tf\n",
134137
"import tensorflow_text as text\n",
135138
"import functools\n",

‎docs/guide/subwords_tokenizer.ipynb‎

Lines changed: 3 additions & 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
{
@@ -140,6 +140,8 @@
140140
"import tempfile\n",
141141
"import time\n",
142142
"\n",
143+
"os.environ[\"TF_USE_LEGACY_KERAS\"] = \"1\"\n",
144+
"\n",
143145
"import numpy as np\n",
144146
"import matplotlib.pyplot as plt\n",
145147
"\n",

‎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 -U tensorflow-text"
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: 4 additions & 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 \"setuptools<81\""
9999
]
100100
},
101101
{
@@ -106,6 +106,9 @@
106106
},
107107
"outputs": [],
108108
"source": [
109+
"import os\n",
110+
"os.environ[\"TF_USE_LEGACY_KERAS\"] = \"1\"\n",
111+
"\n",
109112
"import requests\n",
110113
"import tensorflow as tf\n",
111114
"import tensorflow_text as tf_text"

‎docs/tutorials/bert_glue.ipynb‎

Lines changed: 4 additions & 2 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 \"setuptools<81\""
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,6 +159,8 @@
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",

‎docs/tutorials/classify_text_with_bert.ipynb‎

Lines changed: 6 additions & 2 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 \"setuptools<81\""
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",

‎docs/tutorials/image_captioning.ipynb‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
},
155155
"outputs": [],
156156
"source": [
157-
"!apt install --allow-change-held-packages libcudnn8=8.6.0.163-1+cuda11.8"
157+
"# Install packages"
158158
]
159159
},
160160
{
@@ -165,7 +165,7 @@
165165
},
166166
"outputs": [],
167167
"source": [
168-
"!pip uninstall -y tensorflow estimator keras"
168+
"# Install packages"
169169
]
170170
},
171171
{
@@ -176,7 +176,7 @@
176176
},
177177
"outputs": [],
178178
"source": [
179-
"!pip install -U tensorflow_text tensorflow tensorflow_datasets"
179+
"!pip install -q -U tensorflow-text tensorflow_datasets"
180180
]
181181
},
182182
{
@@ -187,7 +187,7 @@
187187
},
188188
"outputs": [],
189189
"source": [
190-
"!pip install einops"
190+
"!pip install -q einops \"setuptools<81\""
191191
]
192192
},
193193
{
@@ -209,14 +209,16 @@
209209
"outputs": [],
210210
"source": [
211211
"#@title\n",
212+
"import os\n",
213+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
214+
"\n",
212215
"import concurrent.futures\n",
213216
"import collections\n",
214217
"import dataclasses\n",
215218
"import hashlib\n",
216219
"import itertools\n",
217220
"import json\n",
218221
"import math\n",
219-
"import os\n",
220222
"import pathlib\n",
221223
"import random\n",
222224
"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\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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
},
8989
"outputs": [],
9090
"source": [
91+
"import os\n",
92+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
93+
"\n",
9194
"import numpy as np\n",
9295
"\n",
9396
"import tensorflow_datasets as tfds\n",

‎docs/tutorials/text_generation.ipynb‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,13 @@
156156
},
157157
"outputs": [],
158158
"source": [
159-
"import tensorflow as tf\n",
159+
"import os\n",
160+
"import time\n",
161+
"\n",
162+
"os.environ['TF_USE_LEGACY_KERAS'] = '1'\n",
160163
"\n",
161164
"import numpy as np\n",
162-
"import os\n",
163-
"import time"
165+
"import tensorflow as tf"
164166
]
165167
},
166168
{
@@ -673,7 +675,7 @@
673675
"source": [
674676
"class MyModel(tf.keras.Model):\n",
675677
" def __init__(self, vocab_size, embedding_dim, rnn_units):\n",
676-
" super().__init__(self)\n",
678+
" super().__init__()\n",
677679
" self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)\n",
678680
" self.gru = tf.keras.layers.GRU(rnn_units,\n",
679681
" return_sequences=True,\n",

0 commit comments

Comments
 (0)