|
| 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