2019-01-11 12:58:30 -07:00
|
|
|
# Copyright 2019, The TensorFlow Authors.
|
2018-12-04 16:50:21 -07:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
"""Tests for differentially private optimizers."""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from absl.testing import parameterized
|
2018-12-18 15:06:54 -07:00
|
|
|
import mock
|
2018-12-04 16:50:21 -07:00
|
|
|
import numpy as np
|
|
|
|
import tensorflow as tf
|
|
|
|
|
2019-02-08 17:55:29 -07:00
|
|
|
from privacy.analysis import privacy_ledger
|
|
|
|
from privacy.optimizers import dp_optimizer
|
|
|
|
from privacy.optimizers import gaussian_query
|
2018-12-04 16:50:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
class DPOptimizerTest(tf.test.TestCase, parameterized.TestCase):
|
|
|
|
|
2019-03-01 15:48:06 -07:00
|
|
|
def _loss(self, val0, val1):
|
|
|
|
"""Loss function that is minimized at the mean of the input points."""
|
|
|
|
return 0.5 * tf.reduce_sum(tf.squared_difference(val0, val1), axis=1)
|
|
|
|
|
2018-12-18 15:06:54 -07:00
|
|
|
# Parameters for testing: optimizer, num_microbatches, expected answer.
|
2018-12-04 16:50:21 -07:00
|
|
|
@parameterized.named_parameters(
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPGradientDescent 1', dp_optimizer.DPGradientDescentOptimizer, 1,
|
2019-02-13 15:41:52 -07:00
|
|
|
[-2.5, -2.5]),
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPGradientDescent 2', dp_optimizer.DPGradientDescentOptimizer, 2,
|
2019-02-13 15:41:52 -07:00
|
|
|
[-2.5, -2.5]),
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPGradientDescent 4', dp_optimizer.DPGradientDescentOptimizer, 4,
|
|
|
|
[-2.5, -2.5]),
|
2019-02-13 15:41:52 -07:00
|
|
|
('DPAdagrad 1', dp_optimizer.DPAdagradOptimizer, 1, [-2.5, -2.5]),
|
|
|
|
('DPAdagrad 2', dp_optimizer.DPAdagradOptimizer, 2, [-2.5, -2.5]),
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPAdagrad 4', dp_optimizer.DPAdagradOptimizer, 4, [-2.5, -2.5]),
|
2019-02-13 15:41:52 -07:00
|
|
|
('DPAdam 1', dp_optimizer.DPAdamOptimizer, 1, [-2.5, -2.5]),
|
|
|
|
('DPAdam 2', dp_optimizer.DPAdamOptimizer, 2, [-2.5, -2.5]),
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPAdam 4', dp_optimizer.DPAdamOptimizer, 4, [-2.5, -2.5]))
|
|
|
|
def testBaseline(self, cls, num_microbatches, expected_answer):
|
2018-12-04 16:50:21 -07:00
|
|
|
with self.cached_session() as sess:
|
|
|
|
var0 = tf.Variable([1.0, 2.0])
|
|
|
|
data0 = tf.Variable([[3.0, 4.0], [5.0, 6.0], [7.0, 8.0], [-1.0, 0.0]])
|
|
|
|
|
2019-02-08 12:21:20 -07:00
|
|
|
ledger = privacy_ledger.PrivacyLedger(
|
|
|
|
1e6, num_microbatches / 1e6, 50, 50)
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query = gaussian_query.GaussianAverageQuery(
|
2019-02-08 12:21:20 -07:00
|
|
|
1.0e9, 0.0, num_microbatches, ledger)
|
|
|
|
dp_average_query = privacy_ledger.QueryWithLedger(
|
|
|
|
dp_average_query, ledger)
|
2019-01-23 14:51:58 -07:00
|
|
|
|
2018-12-18 15:06:54 -07:00
|
|
|
opt = cls(
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query,
|
2018-12-18 15:06:54 -07:00
|
|
|
num_microbatches=num_microbatches,
|
|
|
|
learning_rate=2.0)
|
|
|
|
|
2018-12-04 16:50:21 -07:00
|
|
|
self.evaluate(tf.global_variables_initializer())
|
|
|
|
# Fetch params to validate initial values
|
|
|
|
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
|
|
|
|
|
|
|
|
# Expected gradient is sum of differences divided by number of
|
|
|
|
# microbatches.
|
2019-03-01 15:48:06 -07:00
|
|
|
gradient_op = opt.compute_gradients(self._loss(data0, var0), [var0])
|
2018-12-04 16:50:21 -07:00
|
|
|
grads_and_vars = sess.run(gradient_op)
|
|
|
|
self.assertAllCloseAccordingToType(expected_answer, grads_and_vars[0][0])
|
|
|
|
|
|
|
|
@parameterized.named_parameters(
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPGradientDescent', dp_optimizer.DPGradientDescentOptimizer),
|
|
|
|
('DPAdagrad', dp_optimizer.DPAdagradOptimizer),
|
|
|
|
('DPAdam', dp_optimizer.DPAdamOptimizer))
|
2018-12-04 16:50:21 -07:00
|
|
|
def testClippingNorm(self, cls):
|
|
|
|
with self.cached_session() as sess:
|
|
|
|
var0 = tf.Variable([0.0, 0.0])
|
|
|
|
data0 = tf.Variable([[3.0, 4.0], [6.0, 8.0]])
|
|
|
|
|
2019-02-08 12:21:20 -07:00
|
|
|
ledger = privacy_ledger.PrivacyLedger(1e6, 1 / 1e6, 50, 50)
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query = gaussian_query.GaussianAverageQuery(1.0, 0.0, 1)
|
2019-02-08 12:21:20 -07:00
|
|
|
dp_average_query = privacy_ledger.QueryWithLedger(
|
|
|
|
dp_average_query, ledger)
|
2019-01-23 14:51:58 -07:00
|
|
|
|
|
|
|
opt = cls(dp_average_query, num_microbatches=1, learning_rate=2.0)
|
2018-12-18 15:06:54 -07:00
|
|
|
|
2018-12-04 16:50:21 -07:00
|
|
|
self.evaluate(tf.global_variables_initializer())
|
|
|
|
# Fetch params to validate initial values
|
|
|
|
self.assertAllClose([0.0, 0.0], self.evaluate(var0))
|
|
|
|
|
|
|
|
# Expected gradient is sum of differences.
|
2019-03-01 15:48:06 -07:00
|
|
|
gradient_op = opt.compute_gradients(self._loss(data0, var0), [var0])
|
2018-12-04 16:50:21 -07:00
|
|
|
grads_and_vars = sess.run(gradient_op)
|
|
|
|
self.assertAllCloseAccordingToType([-0.6, -0.8], grads_and_vars[0][0])
|
|
|
|
|
|
|
|
@parameterized.named_parameters(
|
2018-12-18 15:06:54 -07:00
|
|
|
('DPGradientDescent', dp_optimizer.DPGradientDescentOptimizer),
|
|
|
|
('DPAdagrad', dp_optimizer.DPAdagradOptimizer),
|
|
|
|
('DPAdam', dp_optimizer.DPAdamOptimizer))
|
2018-12-04 16:50:21 -07:00
|
|
|
def testNoiseMultiplier(self, cls):
|
|
|
|
with self.cached_session() as sess:
|
|
|
|
var0 = tf.Variable([0.0])
|
|
|
|
data0 = tf.Variable([[0.0]])
|
|
|
|
|
2019-02-08 12:21:20 -07:00
|
|
|
ledger = privacy_ledger.PrivacyLedger(1e6, 1 / 1e6, 5000, 5000)
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query = gaussian_query.GaussianAverageQuery(4.0, 8.0, 1)
|
2019-02-08 12:21:20 -07:00
|
|
|
dp_average_query = privacy_ledger.QueryWithLedger(
|
|
|
|
dp_average_query, ledger)
|
2019-01-23 14:51:58 -07:00
|
|
|
|
|
|
|
opt = cls(dp_average_query, num_microbatches=1, learning_rate=2.0)
|
2018-12-18 15:06:54 -07:00
|
|
|
|
2018-12-04 16:50:21 -07:00
|
|
|
self.evaluate(tf.global_variables_initializer())
|
|
|
|
# Fetch params to validate initial values
|
|
|
|
self.assertAllClose([0.0], self.evaluate(var0))
|
|
|
|
|
2019-03-01 15:48:06 -07:00
|
|
|
gradient_op = opt.compute_gradients(self._loss(data0, var0), [var0])
|
2018-12-04 16:50:21 -07:00
|
|
|
grads = []
|
2019-01-11 12:58:30 -07:00
|
|
|
for _ in range(1000):
|
2018-12-04 16:50:21 -07:00
|
|
|
grads_and_vars = sess.run(gradient_op)
|
|
|
|
grads.append(grads_and_vars[0][0])
|
|
|
|
|
|
|
|
# Test standard deviation is close to l2_norm_clip * noise_multiplier.
|
|
|
|
self.assertNear(np.std(grads), 2.0 * 4.0, 0.5)
|
|
|
|
|
2018-12-18 15:06:54 -07:00
|
|
|
@mock.patch.object(tf, 'logging')
|
|
|
|
def testComputeGradientsOverrideWarning(self, mock_logging):
|
|
|
|
|
|
|
|
class SimpleOptimizer(tf.train.Optimizer):
|
|
|
|
|
|
|
|
def compute_gradients(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
dp_optimizer.make_optimizer_class(SimpleOptimizer)
|
|
|
|
mock_logging.warning.assert_called_once_with(
|
|
|
|
'WARNING: Calling make_optimizer_class() on class %s that overrides '
|
|
|
|
'method compute_gradients(). Check to ensure that '
|
|
|
|
'make_optimizer_class() does not interfere with overridden version.',
|
|
|
|
'SimpleOptimizer')
|
2018-12-04 16:50:21 -07:00
|
|
|
|
2019-01-11 12:58:30 -07:00
|
|
|
def testEstimator(self):
|
|
|
|
"""Tests that DP optimizers work with tf.estimator."""
|
2019-01-18 11:47:04 -07:00
|
|
|
|
2019-01-11 12:58:30 -07:00
|
|
|
def linear_model_fn(features, labels, mode):
|
|
|
|
preds = tf.keras.layers.Dense(
|
|
|
|
1, activation='linear', name='dense').apply(features['x'])
|
|
|
|
|
|
|
|
vector_loss = tf.squared_difference(labels, preds)
|
|
|
|
scalar_loss = tf.reduce_mean(vector_loss)
|
2019-02-08 12:21:20 -07:00
|
|
|
ledger = privacy_ledger.PrivacyLedger(1e6, 1 / 1e6, 500, 500)
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query = gaussian_query.GaussianAverageQuery(1.0, 0.0, 1)
|
2019-02-08 12:21:20 -07:00
|
|
|
dp_average_query = privacy_ledger.QueryWithLedger(
|
|
|
|
dp_average_query, ledger)
|
2019-01-11 12:58:30 -07:00
|
|
|
optimizer = dp_optimizer.DPGradientDescentOptimizer(
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query,
|
2019-01-11 12:58:30 -07:00
|
|
|
num_microbatches=1,
|
|
|
|
learning_rate=1.0)
|
|
|
|
global_step = tf.train.get_global_step()
|
|
|
|
train_op = optimizer.minimize(loss=vector_loss, global_step=global_step)
|
|
|
|
return tf.estimator.EstimatorSpec(
|
|
|
|
mode=mode, loss=scalar_loss, train_op=train_op)
|
|
|
|
|
|
|
|
linear_regressor = tf.estimator.Estimator(model_fn=linear_model_fn)
|
|
|
|
true_weights = np.array([[-5], [4], [3], [2]]).astype(np.float32)
|
|
|
|
true_bias = 6.0
|
|
|
|
train_data = np.random.normal(scale=3.0, size=(200, 4)).astype(np.float32)
|
|
|
|
|
|
|
|
train_labels = np.matmul(train_data,
|
|
|
|
true_weights) + true_bias + np.random.normal(
|
|
|
|
scale=0.1, size=(200, 1)).astype(np.float32)
|
|
|
|
|
|
|
|
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
|
|
|
x={'x': train_data},
|
|
|
|
y=train_labels,
|
|
|
|
batch_size=20,
|
|
|
|
num_epochs=10,
|
|
|
|
shuffle=True)
|
|
|
|
linear_regressor.train(input_fn=train_input_fn, steps=100)
|
|
|
|
self.assertAllClose(
|
|
|
|
linear_regressor.get_variable_value('dense/kernel'),
|
|
|
|
true_weights,
|
|
|
|
atol=1.0)
|
|
|
|
|
2019-01-18 11:47:04 -07:00
|
|
|
@parameterized.named_parameters(
|
|
|
|
('DPGradientDescent', dp_optimizer.DPGradientDescentOptimizer),
|
|
|
|
('DPAdagrad', dp_optimizer.DPAdagradOptimizer),
|
|
|
|
('DPAdam', dp_optimizer.DPAdamOptimizer))
|
|
|
|
def testUnrollMicrobatches(self, cls):
|
|
|
|
with self.cached_session() as sess:
|
|
|
|
var0 = tf.Variable([1.0, 2.0])
|
|
|
|
data0 = tf.Variable([[3.0, 4.0], [5.0, 6.0], [7.0, 8.0], [-1.0, 0.0]])
|
|
|
|
|
2019-02-08 12:21:20 -07:00
|
|
|
num_microbatches = 4
|
|
|
|
|
|
|
|
ledger = privacy_ledger.PrivacyLedger(
|
|
|
|
1e6, num_microbatches / 1e6, 50, 50)
|
|
|
|
dp_average_query = gaussian_query.GaussianAverageQuery(1.0e9, 0.0, 4)
|
|
|
|
dp_average_query = privacy_ledger.QueryWithLedger(
|
|
|
|
dp_average_query, ledger)
|
|
|
|
|
2019-01-18 11:47:04 -07:00
|
|
|
opt = cls(
|
2019-01-23 14:51:58 -07:00
|
|
|
dp_average_query,
|
2019-02-08 12:21:20 -07:00
|
|
|
num_microbatches=num_microbatches,
|
2019-01-18 11:47:04 -07:00
|
|
|
learning_rate=2.0,
|
|
|
|
unroll_microbatches=True)
|
|
|
|
|
|
|
|
self.evaluate(tf.global_variables_initializer())
|
|
|
|
# Fetch params to validate initial values
|
|
|
|
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
|
|
|
|
|
|
|
|
# Expected gradient is sum of differences divided by number of
|
|
|
|
# microbatches.
|
2019-03-01 15:48:06 -07:00
|
|
|
gradient_op = opt.compute_gradients(self._loss(data0, var0), [var0])
|
2019-01-18 11:47:04 -07:00
|
|
|
grads_and_vars = sess.run(gradient_op)
|
|
|
|
self.assertAllCloseAccordingToType([-2.5, -2.5], grads_and_vars[0][0])
|
|
|
|
|
2019-01-23 14:51:58 -07:00
|
|
|
@parameterized.named_parameters(
|
|
|
|
('DPGradientDescent', dp_optimizer.DPGradientDescentGaussianOptimizer),
|
|
|
|
('DPAdagrad', dp_optimizer.DPAdagradGaussianOptimizer),
|
|
|
|
('DPAdam', dp_optimizer.DPAdamGaussianOptimizer))
|
|
|
|
def testDPGaussianOptimizerClass(self, cls):
|
|
|
|
with self.cached_session() as sess:
|
|
|
|
var0 = tf.Variable([0.0])
|
|
|
|
data0 = tf.Variable([[0.0]])
|
|
|
|
|
|
|
|
opt = cls(
|
|
|
|
l2_norm_clip=4.0,
|
|
|
|
noise_multiplier=2.0,
|
|
|
|
num_microbatches=1,
|
|
|
|
learning_rate=2.0)
|
|
|
|
|
|
|
|
self.evaluate(tf.global_variables_initializer())
|
|
|
|
# Fetch params to validate initial values
|
|
|
|
self.assertAllClose([0.0], self.evaluate(var0))
|
|
|
|
|
2019-03-01 15:48:06 -07:00
|
|
|
gradient_op = opt.compute_gradients(self._loss(data0, var0), [var0])
|
2019-01-23 14:51:58 -07:00
|
|
|
grads = []
|
|
|
|
for _ in range(1000):
|
|
|
|
grads_and_vars = sess.run(gradient_op)
|
|
|
|
grads.append(grads_and_vars[0][0])
|
|
|
|
|
|
|
|
# Test standard deviation is close to l2_norm_clip * noise_multiplier.
|
|
|
|
self.assertNear(np.std(grads), 2.0 * 4.0, 0.5)
|
2019-01-11 12:58:30 -07:00
|
|
|
|
2019-03-01 15:48:06 -07:00
|
|
|
|
2018-12-04 16:50:21 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
tf.test.main()
|