diff --git a/tensorflow_privacy/__init__.py b/tensorflow_privacy/__init__.py index 1e6e42d..bf0e8e4 100644 --- a/tensorflow_privacy/__init__.py +++ b/tensorflow_privacy/__init__.py @@ -26,6 +26,9 @@ from tensorflow_privacy.version import __version__ # pylint: disable=g-bad-impo if hasattr(sys, 'skip_tf_privacy_import'): # Useful for standalone scripts. pass else: + # TensorFlow v1 imports + import tensorflow_privacy.v1 + # Analysis from tensorflow_privacy.privacy.analysis.compute_dp_sgd_privacy_lib import compute_dp_sgd_privacy from tensorflow_privacy.privacy.analysis.privacy_ledger import GaussianSumQueryEntry @@ -54,7 +57,6 @@ else: # Estimators from tensorflow_privacy.privacy.estimators.dnn import DNNClassifier - from tensorflow_privacy.privacy.estimators.v1.dnn import DNNClassifier as DNNClassifierV1 # Keras Models from tensorflow_privacy.privacy.keras_models.dp_keras_model import DPModel @@ -62,14 +64,6 @@ else: from tensorflow_privacy.privacy.keras_models.dp_keras_model import make_dp_model_class # Optimizers - from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradGaussianOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamGaussianOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentGaussianOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer import make_optimizer_class - from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras import DPKerasAdagradOptimizer from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras import DPKerasAdamOptimizer from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras import DPKerasSGDOptimizer @@ -80,15 +74,6 @@ else: from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras_vectorized import VectorizedDPKerasSGDOptimizer from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras_vectorized import make_vectorized_keras_optimizer_class - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdagradOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdamOptimizer - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPSGDOptimizer - - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdagrad - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdam - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPSGD - from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import make_vectorized_optimizer_class - try: from tensorflow_privacy.privacy.bolt_on.models import BoltOnModel from tensorflow_privacy.privacy.bolt_on.optimizers import BoltOn diff --git a/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy.py b/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy.py index e4a0efb..e9d999d 100644 --- a/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy.py +++ b/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy.py @@ -32,16 +32,11 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import sys - from absl import app from absl import flags from tensorflow_privacy.privacy.analysis.compute_dp_sgd_privacy_lib import compute_dp_sgd_privacy -# Opting out of loading all sibling packages and their dependencies. -sys.skip_tf_privacy_import = True - FLAGS = flags.FLAGS flags.DEFINE_integer('N', None, 'Total number of examples') diff --git a/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy_lib.py b/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy_lib.py index a69d7db..8d081c5 100644 --- a/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy_lib.py +++ b/tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy_lib.py @@ -19,13 +19,9 @@ from __future__ import division from __future__ import print_function import math -import sys from absl import app -# Opting out of loading all sibling packages and their dependencies. -sys.skip_tf_privacy_import = True - from tensorflow_privacy.privacy.analysis.rdp_accountant import compute_rdp # pylint: disable=g-import-not-at-top from tensorflow_privacy.privacy.analysis.rdp_accountant import get_privacy_spent diff --git a/tensorflow_privacy/privacy/analysis/compute_noise_from_budget.py b/tensorflow_privacy/privacy/analysis/compute_noise_from_budget.py index bb23eea..731c9ac 100644 --- a/tensorflow_privacy/privacy/analysis/compute_noise_from_budget.py +++ b/tensorflow_privacy/privacy/analysis/compute_noise_from_budget.py @@ -34,16 +34,11 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import sys - from absl import app from absl import flags from tensorflow_privacy.privacy.analysis.compute_noise_from_budget_lib import compute_noise -# Opting out of loading all sibling packages and their dependencies. -sys.skip_tf_privacy_import = True - FLAGS = flags.FLAGS flags.DEFINE_integer('N', None, 'Total number of examples') diff --git a/tensorflow_privacy/privacy/analysis/compute_noise_from_budget_lib.py b/tensorflow_privacy/privacy/analysis/compute_noise_from_budget_lib.py index 1325cb2..ccf1f1c 100644 --- a/tensorflow_privacy/privacy/analysis/compute_noise_from_budget_lib.py +++ b/tensorflow_privacy/privacy/analysis/compute_noise_from_budget_lib.py @@ -19,7 +19,6 @@ from __future__ import division from __future__ import print_function import math -import sys from absl import app from scipy.optimize import bisect @@ -27,9 +26,6 @@ from scipy.optimize import bisect from tensorflow_privacy.privacy.analysis.rdp_accountant import compute_rdp # pylint: disable=g-import-not-at-top from tensorflow_privacy.privacy.analysis.rdp_accountant import get_privacy_spent -# Opting out of loading all sibling packages and their dependencies. -sys.skip_tf_privacy_import = True - def apply_dp_sgd_analysis(q, sigma, steps, orders, delta): """Compute and print results of DP-SGD analysis.""" diff --git a/tensorflow_privacy/v1/__init__.py b/tensorflow_privacy/v1/__init__.py new file mode 100644 index 0000000..7bce1a9 --- /dev/null +++ b/tensorflow_privacy/v1/__init__.py @@ -0,0 +1,50 @@ +# Copyright 2020, The TensorFlow Privacy Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""TensorFlow Privacy library v1 imports. + +This module includes classes designed to be compatible with TF1, based on +`tf.compat.v1.train.Optimizer` and `tf.estimator.Estimator`. +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import sys + +# pylint: disable=g-import-not-at-top + +if hasattr(sys, 'skip_tf_privacy_import'): # Useful for standalone scripts. + pass +else: + # Estimators + from tensorflow_privacy.privacy.estimators.v1.dnn import DNNClassifier as DNNClassifierV1 + + # Optimizers + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradGaussianOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamGaussianOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentGaussianOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import make_optimizer_class + + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdagradOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdamOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPSGDOptimizer + + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdagrad + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPAdam + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import VectorizedDPSGD + from tensorflow_privacy.privacy.optimizers.dp_optimizer_vectorized import make_vectorized_optimizer_class