Add optional argument for weighted sum and weighted average queries.
PiperOrigin-RevId: 230021515
This commit is contained in:
parent
047e1eef0e
commit
0b56f7c016
2 changed files with 52 additions and 17 deletions
|
@ -44,10 +44,14 @@ class NoPrivacySumQuery(private_queries.PrivateSumQuery):
|
||||||
del global_state # unused.
|
del global_state # unused.
|
||||||
return nest.map_structure(tf.zeros_like, tensors)
|
return nest.map_structure(tf.zeros_like, tensors)
|
||||||
|
|
||||||
def accumulate_record(self, params, sample_state, record):
|
def accumulate_record(self, params, sample_state, record, weight=1):
|
||||||
"""See base class."""
|
"""See base class. Optional argument for weighted sum queries."""
|
||||||
del params # unused.
|
del params # unused.
|
||||||
return nest.map_structure(tf.add, sample_state, record)
|
|
||||||
|
def add_weighted(state_tensor, record_tensor):
|
||||||
|
return tf.add(state_tensor, weight * record_tensor)
|
||||||
|
|
||||||
|
return nest.map_structure(add_weighted, sample_state, record)
|
||||||
|
|
||||||
def get_noised_sum(self, sample_state, global_state):
|
def get_noised_sum(self, sample_state, global_state):
|
||||||
"""See base class."""
|
"""See base class."""
|
||||||
|
@ -77,11 +81,13 @@ class NoPrivacyAverageQuery(private_queries.PrivateAverageQuery):
|
||||||
"""See base class."""
|
"""See base class."""
|
||||||
return self._numerator.initial_sample_state(global_state, tensors), 0.0
|
return self._numerator.initial_sample_state(global_state, tensors), 0.0
|
||||||
|
|
||||||
def accumulate_record(self, params, sample_state, record):
|
def accumulate_record(self, params, sample_state, record, weight=1):
|
||||||
"""See base class."""
|
"""See base class. Optional argument for weighted average queries."""
|
||||||
sum_sample_state, denominator = sample_state
|
sum_sample_state, denominator = sample_state
|
||||||
return self._numerator.accumulate_record(params, sum_sample_state,
|
return (
|
||||||
record), tf.add(denominator, 1.0)
|
self._numerator.accumulate_record(
|
||||||
|
params, sum_sample_state, record, weight),
|
||||||
|
tf.add(denominator, weight))
|
||||||
|
|
||||||
def get_noised_average(self, sample_state, global_state):
|
def get_noised_average(self, sample_state, global_state):
|
||||||
"""See base class."""
|
"""See base class."""
|
||||||
|
|
|
@ -23,18 +23,14 @@ import tensorflow as tf
|
||||||
|
|
||||||
from privacy.optimizers import no_privacy_query
|
from privacy.optimizers import no_privacy_query
|
||||||
|
|
||||||
try:
|
|
||||||
xrange
|
|
||||||
except NameError:
|
|
||||||
xrange = range
|
|
||||||
|
|
||||||
|
def _run_query(query, records, weights=None):
|
||||||
def _run_query(query, records):
|
|
||||||
"""Executes query on the given set of records as a single sample.
|
"""Executes query on the given set of records as a single sample.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query: A PrivateQuery to run.
|
query: A PrivateQuery to run.
|
||||||
records: An iterable containing records to pass to the query.
|
records: An iterable containing records to pass to the query.
|
||||||
|
weights: An optional iterable containing the weights of the records.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The result of the query.
|
The result of the query.
|
||||||
|
@ -42,8 +38,13 @@ def _run_query(query, records):
|
||||||
global_state = query.initial_global_state()
|
global_state = query.initial_global_state()
|
||||||
params = query.derive_sample_params(global_state)
|
params = query.derive_sample_params(global_state)
|
||||||
sample_state = query.initial_sample_state(global_state, next(iter(records)))
|
sample_state = query.initial_sample_state(global_state, next(iter(records)))
|
||||||
|
if weights is None:
|
||||||
for record in records:
|
for record in records:
|
||||||
sample_state = query.accumulate_record(params, sample_state, record)
|
sample_state = query.accumulate_record(params, sample_state, record)
|
||||||
|
else:
|
||||||
|
for weight, record in zip(weights, records):
|
||||||
|
sample_state = query.accumulate_record(params, sample_state, record,
|
||||||
|
weight)
|
||||||
result, _ = query.get_query_result(sample_state, global_state)
|
result, _ = query.get_query_result(sample_state, global_state)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -61,6 +62,20 @@ class NoPrivacyQueryTest(tf.test.TestCase, parameterized.TestCase):
|
||||||
expected = [1.0, 1.0]
|
expected = [1.0, 1.0]
|
||||||
self.assertAllClose(result, expected)
|
self.assertAllClose(result, expected)
|
||||||
|
|
||||||
|
def test_no_privacy_weighted_sum(self):
|
||||||
|
with self.cached_session() as sess:
|
||||||
|
record1 = tf.constant([2.0, 0.0])
|
||||||
|
record2 = tf.constant([-1.0, 1.0])
|
||||||
|
|
||||||
|
weight1 = 1
|
||||||
|
weight2 = 2
|
||||||
|
|
||||||
|
query = no_privacy_query.NoPrivacySumQuery()
|
||||||
|
query_result = _run_query(query, [record1, record2], [weight1, weight2])
|
||||||
|
result = sess.run(query_result)
|
||||||
|
expected = [0.0, 2.0]
|
||||||
|
self.assertAllClose(result, expected)
|
||||||
|
|
||||||
def test_no_privacy_average(self):
|
def test_no_privacy_average(self):
|
||||||
with self.cached_session() as sess:
|
with self.cached_session() as sess:
|
||||||
record1 = tf.constant([5.0, 0.0])
|
record1 = tf.constant([5.0, 0.0])
|
||||||
|
@ -69,8 +84,22 @@ class NoPrivacyQueryTest(tf.test.TestCase, parameterized.TestCase):
|
||||||
query = no_privacy_query.NoPrivacyAverageQuery()
|
query = no_privacy_query.NoPrivacyAverageQuery()
|
||||||
query_result = _run_query(query, [record1, record2])
|
query_result = _run_query(query, [record1, record2])
|
||||||
result = sess.run(query_result)
|
result = sess.run(query_result)
|
||||||
expected_average = [2.0, 1.0]
|
expected = [2.0, 1.0]
|
||||||
self.assertAllClose(result, expected_average)
|
self.assertAllClose(result, expected)
|
||||||
|
|
||||||
|
def test_no_privacy_weighted_average(self):
|
||||||
|
with self.cached_session() as sess:
|
||||||
|
record1 = tf.constant([4.0, 0.0])
|
||||||
|
record2 = tf.constant([-1.0, 1.0])
|
||||||
|
|
||||||
|
weight1 = 1
|
||||||
|
weight2 = 3
|
||||||
|
|
||||||
|
query = no_privacy_query.NoPrivacyAverageQuery()
|
||||||
|
query_result = _run_query(query, [record1, record2], [weight1, weight2])
|
||||||
|
result = sess.run(query_result)
|
||||||
|
expected = [0.25, 0.75]
|
||||||
|
self.assertAllClose(result, expected)
|
||||||
|
|
||||||
@parameterized.named_parameters(
|
@parameterized.named_parameters(
|
||||||
('type_mismatch', [1.0], (1.0,), TypeError),
|
('type_mismatch', [1.0], (1.0,), TypeError),
|
||||||
|
|
Loading…
Reference in a new issue