2019-06-11 19:58:33 -06:00
|
|
|
# Copyright 2019, The TensorFlow 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.
|
|
|
|
"""DP Logistic Regression on MNIST.
|
|
|
|
|
|
|
|
DP Logistic Regression on MNIST with support for privacy-by-iteration analysis.
|
2019-06-24 13:49:48 -06:00
|
|
|
Vitaly Feldman, Ilya Mironov, Kunal Talwar, and Abhradeep Thakurta.
|
2019-06-11 19:58:33 -06:00
|
|
|
"Privacy amplification by iteration."
|
|
|
|
In 2018 IEEE 59th Annual Symposium on Foundations of Computer Science (FOCS),
|
|
|
|
pp. 521-532. IEEE, 2018.
|
|
|
|
https://arxiv.org/abs/1808.06651.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
from absl import app
|
|
|
|
from absl import flags
|
2022-02-01 16:28:37 -07:00
|
|
|
from absl import logging
|
2022-07-08 13:06:47 -06:00
|
|
|
import dp_accounting
|
2019-06-11 19:58:33 -06:00
|
|
|
import numpy as np
|
2022-02-07 17:05:45 -07:00
|
|
|
import tensorflow as tf
|
2022-03-30 17:04:17 -06:00
|
|
|
from tensorflow import estimator as tf_estimator
|
|
|
|
from tensorflow.compat.v1 import estimator as tf_compat_v1_estimator
|
2019-10-14 16:29:21 -06:00
|
|
|
from tensorflow_privacy.privacy.optimizers import dp_optimizer
|
2019-06-11 19:58:33 -06:00
|
|
|
|
2022-02-07 17:05:45 -07:00
|
|
|
GradientDescentOptimizer = tf.compat.v1.train.GradientDescentOptimizer
|
2019-06-11 19:58:33 -06:00
|
|
|
|
|
|
|
FLAGS = flags.FLAGS
|
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
flags.DEFINE_boolean(
|
|
|
|
'dpsgd', True, 'If True, train with DP-SGD. If False, '
|
|
|
|
'train with vanilla SGD.')
|
2019-06-11 19:58:33 -06:00
|
|
|
flags.DEFINE_float('learning_rate', 0.001, 'Learning rate for training')
|
2019-06-24 13:49:48 -06:00
|
|
|
flags.DEFINE_float('noise_multiplier', 0.05,
|
2019-06-11 19:58:33 -06:00
|
|
|
'Ratio of the standard deviation to the clipping norm')
|
2019-06-24 13:49:48 -06:00
|
|
|
flags.DEFINE_integer('batch_size', 5, 'Batch size')
|
2019-06-11 19:58:33 -06:00
|
|
|
flags.DEFINE_integer('epochs', 5, 'Number of epochs')
|
|
|
|
flags.DEFINE_float('regularizer', 0, 'L2 regularizer coefficient')
|
|
|
|
flags.DEFINE_string('model_dir', None, 'Model directory')
|
2019-06-24 13:49:48 -06:00
|
|
|
flags.DEFINE_float('data_l2_norm', 8, 'Bound on the L2 norm of normalized data')
|
2019-06-11 19:58:33 -06:00
|
|
|
|
|
|
|
|
|
|
|
def lr_model_fn(features, labels, mode, nclasses, dim):
|
|
|
|
"""Model function for logistic regression."""
|
|
|
|
input_layer = tf.reshape(features['x'], tuple([-1]) + dim)
|
2020-10-27 15:15:49 -06:00
|
|
|
logits = tf.keras.layers.Dense(
|
2019-06-24 13:49:48 -06:00
|
|
|
units=nclasses,
|
2020-10-27 15:15:49 -06:00
|
|
|
kernel_regularizer=tf.keras.regularizers.L2(l2=FLAGS.regularizer),
|
2022-07-25 13:07:06 -06:00
|
|
|
bias_regularizer=tf.keras.regularizers.L2(l2=FLAGS.regularizer))(
|
|
|
|
input_layer)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
|
|
|
# Calculate loss as a vector (to support microbatches in DP-SGD).
|
|
|
|
vector_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
|
2022-07-25 13:07:06 -06:00
|
|
|
labels, logits) + tf.compat.v1.losses.get_regularization_loss()
|
2019-06-11 19:58:33 -06:00
|
|
|
# Define mean of loss across minibatch (for reporting through tf.Estimator).
|
|
|
|
scalar_loss = tf.reduce_mean(vector_loss)
|
|
|
|
|
|
|
|
# Configure the training op (for TRAIN mode).
|
2022-03-30 17:04:17 -06:00
|
|
|
if mode == tf_estimator.ModeKeys.TRAIN:
|
2019-06-11 19:58:33 -06:00
|
|
|
if FLAGS.dpsgd:
|
|
|
|
# The loss function is L-Lipschitz with L = sqrt(2*(||x||^2 + 1)) where
|
|
|
|
# ||x|| is the norm of the data.
|
2019-06-24 13:49:48 -06:00
|
|
|
# We don't use microbatches (thus speeding up computation), since no
|
|
|
|
# clipping is necessary due to data normalization.
|
2019-06-11 19:58:33 -06:00
|
|
|
optimizer = dp_optimizer.DPGradientDescentGaussianOptimizer(
|
2019-06-24 13:49:48 -06:00
|
|
|
l2_norm_clip=math.sqrt(2 * (FLAGS.data_l2_norm**2 + 1)),
|
2019-06-11 19:58:33 -06:00
|
|
|
noise_multiplier=FLAGS.noise_multiplier,
|
2019-06-24 13:49:48 -06:00
|
|
|
num_microbatches=1,
|
2019-06-11 19:58:33 -06:00
|
|
|
learning_rate=FLAGS.learning_rate)
|
|
|
|
opt_loss = vector_loss
|
|
|
|
else:
|
|
|
|
optimizer = GradientDescentOptimizer(learning_rate=FLAGS.learning_rate)
|
|
|
|
opt_loss = scalar_loss
|
2022-02-07 17:05:45 -07:00
|
|
|
global_step = tf.compat.v1.train.get_global_step()
|
2019-06-11 19:58:33 -06:00
|
|
|
train_op = optimizer.minimize(loss=opt_loss, global_step=global_step)
|
|
|
|
# In the following, we pass the mean of the loss (scalar_loss) rather than
|
|
|
|
# the vector_loss because tf.estimator requires a scalar loss. This is only
|
|
|
|
# used for evaluation and debugging by tf.estimator. The actual loss being
|
|
|
|
# minimized is opt_loss defined above and passed to optimizer.minimize().
|
2022-03-30 17:04:17 -06:00
|
|
|
return tf_estimator.EstimatorSpec(
|
2019-06-24 13:49:48 -06:00
|
|
|
mode=mode, loss=scalar_loss, train_op=train_op)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
|
|
|
# Add evaluation metrics (for EVAL mode).
|
2022-03-30 17:04:17 -06:00
|
|
|
elif mode == tf_estimator.ModeKeys.EVAL:
|
2019-06-11 19:58:33 -06:00
|
|
|
eval_metric_ops = {
|
|
|
|
'accuracy':
|
2022-07-25 13:07:06 -06:00
|
|
|
tf.compat.v1.metrics.accuracy(
|
2019-06-24 13:49:48 -06:00
|
|
|
labels=labels, predictions=tf.argmax(input=logits, axis=1))
|
2019-06-11 19:58:33 -06:00
|
|
|
}
|
2022-03-30 17:04:17 -06:00
|
|
|
return tf_estimator.EstimatorSpec(
|
2019-06-24 13:49:48 -06:00
|
|
|
mode=mode, loss=scalar_loss, eval_metric_ops=eval_metric_ops)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
|
|
|
|
|
|
|
def normalize_data(data, data_l2_norm):
|
|
|
|
"""Normalizes data such that each samples has bounded L2 norm.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: the dataset. Each row represents one samples.
|
|
|
|
data_l2_norm: the target upper bound on the L2 norm.
|
|
|
|
"""
|
|
|
|
|
|
|
|
for i in range(data.shape[0]):
|
|
|
|
norm = np.linalg.norm(data[i])
|
|
|
|
if norm > data_l2_norm:
|
|
|
|
data[i] = data[i] / norm * data_l2_norm
|
|
|
|
|
|
|
|
|
|
|
|
def load_mnist(data_l2_norm=float('inf')):
|
|
|
|
"""Loads MNIST and preprocesses to combine training and validation data."""
|
|
|
|
train, test = tf.keras.datasets.mnist.load_data()
|
|
|
|
train_data, train_labels = train
|
|
|
|
test_data, test_labels = test
|
|
|
|
|
|
|
|
train_data = np.array(train_data, dtype=np.float32) / 255
|
|
|
|
test_data = np.array(test_data, dtype=np.float32) / 255
|
|
|
|
|
|
|
|
train_data = train_data.reshape(train_data.shape[0], -1)
|
|
|
|
test_data = test_data.reshape(test_data.shape[0], -1)
|
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
idx = np.random.permutation(len(train_data)) # shuffle data once
|
2019-06-11 19:58:33 -06:00
|
|
|
train_data = train_data[idx]
|
|
|
|
train_labels = train_labels[idx]
|
|
|
|
|
|
|
|
normalize_data(train_data, data_l2_norm)
|
|
|
|
normalize_data(test_data, data_l2_norm)
|
|
|
|
|
|
|
|
train_labels = np.array(train_labels, dtype=np.int32)
|
|
|
|
test_labels = np.array(test_labels, dtype=np.int32)
|
|
|
|
|
|
|
|
return train_data, train_labels, test_data, test_labels
|
|
|
|
|
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
def print_privacy_guarantees(epochs, batch_size, samples, noise_multiplier):
|
|
|
|
"""Tabulating position-dependent privacy guarantees."""
|
|
|
|
if noise_multiplier == 0:
|
|
|
|
print('No differential privacy (additive noise is 0).')
|
|
|
|
return
|
|
|
|
|
|
|
|
print('In the conditions of Theorem 34 (https://arxiv.org/abs/1808.06651) '
|
|
|
|
'the training procedure results in the following privacy guarantees.')
|
|
|
|
|
|
|
|
print('Out of the total of {} samples:'.format(samples))
|
|
|
|
|
|
|
|
steps_per_epoch = samples // batch_size
|
|
|
|
orders = np.concatenate(
|
|
|
|
[np.linspace(2, 20, num=181),
|
|
|
|
np.linspace(20, 100, num=81)])
|
|
|
|
delta = 1e-5
|
|
|
|
for p in (.5, .9, .99):
|
|
|
|
steps = math.ceil(steps_per_epoch * p) # Steps in the last epoch.
|
|
|
|
coef = 2 * (noise_multiplier * batch_size)**-2 * (
|
|
|
|
# Accounting for privacy loss
|
|
|
|
(epochs - 1) / steps_per_epoch + # ... from all-but-last epochs
|
|
|
|
1 / (steps_per_epoch - steps + 1)) # ... due to the last epoch
|
|
|
|
# Using RDP accountant to compute eps. Doing computation analytically is
|
|
|
|
# an option.
|
|
|
|
rdp = [order * coef for order in orders]
|
2022-07-25 13:07:06 -06:00
|
|
|
eps, _ = dp_accounting.rdp.compute_epsilon(orders, rdp, delta)
|
2022-01-28 12:56:55 -07:00
|
|
|
print('\t{:g}% enjoy at least ({:.2f}, {})-DP'.format(p * 100, eps, delta))
|
2019-06-24 13:49:48 -06:00
|
|
|
|
2022-07-01 11:33:52 -06:00
|
|
|
accountant = dp_accounting.rdp.RdpAccountant(orders)
|
|
|
|
event = dp_accounting.SelfComposedDpEvent(
|
|
|
|
dp_accounting.PoissonSampledDpEvent(
|
|
|
|
batch_size / samples,
|
|
|
|
dp_accounting.GaussianDpEvent(noise_multiplier)),
|
2022-05-05 16:29:04 -06:00
|
|
|
epochs * steps_per_epoch)
|
|
|
|
accountant.compose(event)
|
|
|
|
eps_sgm = accountant.get_epsilon(target_delta=delta)
|
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
print('By comparison, DP-SGD analysis for training done with the same '
|
|
|
|
'parameters and random shuffling in each epoch guarantees '
|
|
|
|
'({:.2f}, {})-DP for all samples.'.format(eps_sgm, delta))
|
|
|
|
|
|
|
|
|
2019-06-11 19:58:33 -06:00
|
|
|
def main(unused_argv):
|
2022-07-25 11:48:07 -06:00
|
|
|
logging.set_verbosity(logging.INFO)
|
2022-02-01 16:28:37 -07:00
|
|
|
|
2019-06-11 19:58:33 -06:00
|
|
|
if FLAGS.data_l2_norm <= 0:
|
2019-06-24 13:49:48 -06:00
|
|
|
raise ValueError('data_l2_norm must be positive.')
|
|
|
|
if FLAGS.dpsgd and FLAGS.learning_rate > 8 / FLAGS.data_l2_norm**2:
|
|
|
|
raise ValueError('The amplification-by-iteration analysis requires'
|
2019-06-11 19:58:33 -06:00
|
|
|
'learning_rate <= 2 / beta, where beta is the smoothness'
|
|
|
|
'of the loss function and is upper bounded by ||x||^2 / 4'
|
|
|
|
'with ||x|| being the largest L2 norm of the samples.')
|
|
|
|
|
|
|
|
# Load training and test data.
|
|
|
|
# Smoothness = ||x||^2 / 4 where ||x|| is the largest L2 norm of the samples.
|
|
|
|
# To get bounded smoothness, we normalize the data such that each sample has a
|
|
|
|
# bounded L2 norm.
|
|
|
|
train_data, train_labels, test_data, test_labels = load_mnist(
|
|
|
|
data_l2_norm=FLAGS.data_l2_norm)
|
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
# Instantiate tf.Estimator.
|
2019-06-11 19:58:33 -06:00
|
|
|
# pylint: disable=g-long-lambda
|
2019-06-24 13:49:48 -06:00
|
|
|
model_fn = lambda features, labels, mode: lr_model_fn(
|
|
|
|
features, labels, mode, nclasses=10, dim=train_data.shape[1:])
|
2022-03-30 17:04:17 -06:00
|
|
|
mnist_classifier = tf_estimator.Estimator(
|
2019-06-24 13:49:48 -06:00
|
|
|
model_fn=model_fn, model_dir=FLAGS.model_dir)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
|
|
|
# Create tf.Estimator input functions for the training and test data.
|
|
|
|
# To analyze the per-user privacy loss, we keep the same orders of samples in
|
|
|
|
# each epoch by setting shuffle=False.
|
2022-03-30 17:04:17 -06:00
|
|
|
train_input_fn = tf_compat_v1_estimator.inputs.numpy_input_fn(
|
2019-06-11 19:58:33 -06:00
|
|
|
x={'x': train_data},
|
|
|
|
y=train_labels,
|
|
|
|
batch_size=FLAGS.batch_size,
|
|
|
|
num_epochs=FLAGS.epochs,
|
|
|
|
shuffle=False)
|
2022-03-30 17:04:17 -06:00
|
|
|
eval_input_fn = tf_compat_v1_estimator.inputs.numpy_input_fn(
|
2019-06-24 13:49:48 -06:00
|
|
|
x={'x': test_data}, y=test_labels, num_epochs=1, shuffle=False)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
# Train the model.
|
|
|
|
num_samples = train_data.shape[0]
|
|
|
|
steps_per_epoch = num_samples // FLAGS.batch_size
|
2019-06-11 19:58:33 -06:00
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
mnist_classifier.train(
|
|
|
|
input_fn=train_input_fn, steps=steps_per_epoch * FLAGS.epochs)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
2019-06-24 13:49:48 -06:00
|
|
|
# Evaluate the model and print results.
|
|
|
|
eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
|
|
|
|
print('Test accuracy after {} epochs is: {:.2f}'.format(
|
|
|
|
FLAGS.epochs, eval_results['accuracy']))
|
|
|
|
|
|
|
|
if FLAGS.dpsgd:
|
|
|
|
print_privacy_guarantees(
|
|
|
|
epochs=FLAGS.epochs,
|
|
|
|
batch_size=FLAGS.batch_size,
|
|
|
|
samples=num_samples,
|
|
|
|
noise_multiplier=FLAGS.noise_multiplier,
|
|
|
|
)
|
2019-06-11 19:58:33 -06:00
|
|
|
|
2022-01-28 12:56:55 -07:00
|
|
|
|
2019-06-11 19:58:33 -06:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(main)
|