Skip to content

Commit

Permalink
Explicitly set TF_USE_LEGACY_KERAS=1 when importing Keras.
Browse files Browse the repository at this point in the history
Tensorflow Transform is only compatible with Keras 2.

PiperOrigin-RevId: 687967427
  • Loading branch information
tf-transform-team authored and tfx-copybara committed Oct 21, 2024
1 parent 257a24b commit b02397e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tensorflow_transform/keras_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Imports keras 2."""
import os

from absl import logging
import tensorflow as tf

if 'TF_USE_LEGACY_KERAS' not in os.environ:
# Make sure we are using Keras 2.
os.environ['TF_USE_LEGACY_KERAS'] = '1'
elif os.environ['TF_USE_LEGACY_KERAS'] not in ('true', 'True', '1'):
logging.warning(
'TF_USE_LEGACY_KERAS is set to %s, which will not use Keras 2. Tensorflow'
' Transform is only compatible with Keras 2. Please set'
' TF_USE_LEGACY_KERAS=1.',
os.environ['TF_USE_LEGACY_KERAS'],
)

version_fn = getattr(tf.keras, 'version', None)
if version_fn and version_fn().startswith('3.'):
# `tf.keras` points to `keras 3`, so use `tf_keras` package
Expand Down

0 comments on commit b02397e

Please sign in to comment.