Skip to content

Gracefully handle missing tf-estimator on TensorFlow 2.16+ (fixes #611) - #651

Open
laasyapriya2007 wants to merge 1 commit into
tensorflow:masterfrom
laasyapriya2007:fix-tf-estimator-compatibility
Open

laasyapriya2007 wants to merge 1 commit into
tensorflow:masterfrom
laasyapriya2007:fix-tf-estimator-compatibility

Conversation

@laasyapriya2007

Copy link
Copy Markdown

Summary

Fixes #611 (and resolves #597).

In TensorFlow 2.16+, tf-estimator has been deprecated and its distributor hooks (estimator_training) removed from tensorflow.python.distribute.

Previously, tensorflow_privacy/__init__.py and tensorflow_privacy/v1/__init__.py unconditionally imported DNNClassifier and DNNClassifierV1, which caused import tensorflow_privacy to crash immediately on TensorFlow 2.16+ (including 2.18.0) even when the user only intended to use DP-SGD, Keras DPModel, DP queries, or privacy analysis tools.

Changes

  1. Added try...except (ImportError, AttributeError) fallback logic when importing DNNClassifier and DNNClassifierV1.
  2. When tf-estimator is unavailable, instantiating the stub provides an informative RuntimeError explaining the deprecation and guiding users to migrate to Keras-based DP models (DPModel / DPSequential).
  3. Preserves backwards compatibility for existing environments while unblocking usage on modern TensorFlow installations.

Testing

  • Verified import tensorflow_privacy and import tensorflow_privacy.v1 succeed in both standard environments and when tensorflow_estimator is simulated as missing/deprecated.
  • Verified all 30 tests in compute_dp_sgd_privacy_test.py continue to pass.

@google-cla

google-cla Bot commented Oct 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tensorflow privacy not compatible with tensorflow version 2.18.0 ImportError: cannot import name 'estimator' from 'tensorflow' (unknown location)

1 participant