Skip to content

Commit 1ce8cd4

Browse files
schien1729tensorflower-gardener
authored andcommitted
Restructure TF Privacy to be more in line with other repos in the TF ecosystem.
PiperOrigin-RevId: 274674077
1 parent c0e05f6 commit 1ce8cd4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6849
-23
lines changed

tensorflow_privacy/__init__.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2019, The TensorFlow Privacy Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""TensorFlow Privacy library."""
15+
16+
from __future__ import absolute_import
17+
from __future__ import division
18+
from __future__ import print_function
19+
20+
import sys
21+
22+
# pylint: disable=g-import-not-at-top
23+
24+
if hasattr(sys, 'skip_tf_privacy_import'): # Useful for standalone scripts.
25+
pass
26+
else:
27+
from tensorflow_privacy.privacy.analysis.privacy_ledger import GaussianSumQueryEntry
28+
from tensorflow_privacy.privacy.analysis.privacy_ledger import PrivacyLedger
29+
from tensorflow_privacy.privacy.analysis.privacy_ledger import QueryWithLedger
30+
from tensorflow_privacy.privacy.analysis.privacy_ledger import SampleEntry
31+
32+
from tensorflow_privacy.privacy.dp_query.dp_query import DPQuery
33+
from tensorflow_privacy.privacy.dp_query.gaussian_query import GaussianAverageQuery
34+
from tensorflow_privacy.privacy.dp_query.gaussian_query import GaussianSumQuery
35+
from tensorflow_privacy.privacy.dp_query.nested_query import NestedQuery
36+
from tensorflow_privacy.privacy.dp_query.no_privacy_query import NoPrivacyAverageQuery
37+
from tensorflow_privacy.privacy.dp_query.no_privacy_query import NoPrivacySumQuery
38+
from tensorflow_privacy.privacy.dp_query.normalized_query import NormalizedQuery
39+
from tensorflow_privacy.privacy.dp_query.quantile_adaptive_clip_sum_query import QuantileAdaptiveClipSumQuery
40+
from tensorflow_privacy.privacy.dp_query.quantile_adaptive_clip_sum_query import QuantileAdaptiveClipAverageQuery
41+
42+
from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradGaussianOptimizer
43+
from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradOptimizer
44+
from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamGaussianOptimizer
45+
from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamOptimizer
46+
from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentGaussianOptimizer
47+
from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentOptimizer
48+
49+
try:
50+
from tensorflow_privacy.privacy.bolt_on.models import BoltOnModel
51+
from tensorflow_privacy.privacy.bolt_on.optimizers import BoltOn
52+
from tensorflow_privacy.privacy.bolt_on.losses import StrongConvexMixin
53+
from tensorflow_privacy.privacy.bolt_on.losses import StrongConvexBinaryCrossentropy
54+
from tensorflow_privacy.privacy.bolt_on.losses import StrongConvexHuber
55+
except ImportError:
56+
# module `bolt_on` not yet available in this version of TF Privacy
57+
pass

tensorflow_privacy/privacy/BUILD

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
licenses(["notice"])
4+
5+
exports_files(["LICENSE"])
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2019, The TensorFlow Privacy Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

tensorflow_privacy/privacy/analysis/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
r"""Command-line script for computing privacy of a model trained with DP-SGD.
16+
17+
The script applies the RDP accountant to estimate privacy budget of an iterated
18+
Sampled Gaussian Mechanism. The mechanism's parameters are controlled by flags.
19+
20+
Example:
21+
compute_dp_sgd_privacy
22+
--N=60000 \
23+
--batch_size=256 \
24+
--noise_multiplier=1.12 \
25+
--epochs=60 \
26+
--delta=1e-5
27+
28+
The output states that DP-SGD with these parameters satisfies (2.92, 1e-5)-DP.
29+
"""
30+
31+
from __future__ import absolute_import
32+
from __future__ import division
33+
from __future__ import print_function
34+
35+
import math
36+
import sys
37+
38+
from absl import app
39+
from absl import flags
40+
41+
# Opting out of loading all sibling packages and their dependencies.
42+
sys.skip_tf_privacy_import = True
43+
44+
from tensorflow_privacy.privacy.analysis.rdp_accountant import compute_rdp # pylint: disable=g-import-not-at-top
45+
from tensorflow_privacy.privacy.analysis.rdp_accountant import get_privacy_spent
46+
47+
FLAGS = flags.FLAGS
48+
49+
flags.DEFINE_integer('N', None, 'Total number of examples')
50+
flags.DEFINE_integer('batch_size', None, 'Batch size')
51+
flags.DEFINE_float('noise_multiplier', None, 'Noise multiplier for DP-SGD')
52+
flags.DEFINE_float('epochs', None, 'Number of epochs (may be fractional)')
53+
flags.DEFINE_float('delta', 1e-6, 'Target delta')
54+
55+
flags.mark_flag_as_required('N')
56+
flags.mark_flag_as_required('batch_size')
57+
flags.mark_flag_as_required('noise_multiplier')
58+
flags.mark_flag_as_required('epochs')
59+
60+
61+
def apply_dp_sgd_analysis(q, sigma, steps, orders, delta):
62+
"""Compute and print results of DP-SGD analysis."""
63+
64+
# compute_rdp requires that sigma be the ratio of the standard deviation of
65+
# the Gaussian noise to the l2-sensitivity of the function to which it is
66+
# added. Hence, sigma here corresponds to the `noise_multiplier` parameter
67+
# in the DP-SGD implementation found in privacy.optimizers.dp_optimizer
68+
rdp = compute_rdp(q, sigma, steps, orders)
69+
70+
eps, _, opt_order = get_privacy_spent(orders, rdp, target_delta=delta)
71+
72+
print('DP-SGD with sampling rate = {:.3g}% and noise_multiplier = {} iterated'
73+
' over {} steps satisfies'.format(100 * q, sigma, steps), end=' ')
74+
print('differential privacy with eps = {:.3g} and delta = {}.'.format(
75+
eps, delta))
76+
print('The optimal RDP order is {}.'.format(opt_order))
77+
78+
if opt_order == max(orders) or opt_order == min(orders):
79+
print('The privacy estimate is likely to be improved by expanding '
80+
'the set of orders.')
81+
82+
83+
def main(argv):
84+
del argv # argv is not used.
85+
86+
q = FLAGS.batch_size / FLAGS.N # q - the sampling ratio.
87+
if q > 1:
88+
raise app.UsageError('N must be larger than the batch size.')
89+
orders = ([1.25, 1.5, 1.75, 2., 2.25, 2.5, 3., 3.5, 4., 4.5] +
90+
list(range(5, 64)) + [128, 256, 512])
91+
steps = int(math.ceil(FLAGS.epochs * FLAGS.N / FLAGS.batch_size))
92+
93+
apply_dp_sgd_analysis(q, FLAGS.noise_multiplier, steps, orders, FLAGS.delta)
94+
95+
96+
if __name__ == '__main__':
97+
app.run(main)

0 commit comments

Comments
 (0)