Skip to content

Commit 2301931

Browse files
Fix issue with importing tensorflow.compat.v1.
PiperOrigin-RevId: 300175680
1 parent 6541960 commit 2301931

17 files changed

+87
-88
lines changed

tensorflow_privacy/privacy/analysis/privacy_ledger.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def record_sum_query(self, l2_norm_bound, noise_stddev):
111111

112112
def _do_record_query():
113113
with tf.control_dependencies(
114-
[tf.compat.v1.assign(self._query_count, self._query_count + 1)]):
114+
[tf.assign(self._query_count, self._query_count + 1)]):
115115
return self._query_buffer.append(
116116
[self._sample_count, l2_norm_bound, noise_stddev])
117117

@@ -120,14 +120,14 @@ def _do_record_query():
120120
def finalize_sample(self):
121121
"""Finalizes sample and records sample ledger entry."""
122122
with tf.control_dependencies([
123-
tf.compat.v1.assign(self._sample_var, [
123+
tf.assign(self._sample_var, [
124124
self._population_size, self._selection_probability,
125125
self._query_count
126126
])
127127
]):
128128
with tf.control_dependencies([
129-
tf.compat.v1.assign(self._sample_count, self._sample_count + 1),
130-
tf.compat.v1.assign(self._query_count, 0)
129+
tf.assign(self._sample_count, self._sample_count + 1),
130+
tf.assign(self._query_count, 0)
131131
]):
132132
return self._sample_buffer.append(self._sample_var)
133133

tensorflow_privacy/privacy/analysis/privacy_ledger_test.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from tensorflow_privacy.privacy.dp_query import nested_query
2626
from tensorflow_privacy.privacy.dp_query import test_utils
2727

28-
tf.compat.v1.enable_eager_execution()
28+
tf.enable_eager_execution()
2929

3030

3131
class PrivacyLedgerTest(tf.test.TestCase):
@@ -63,8 +63,8 @@ def test_sum_query(self):
6363
query, population_size, selection_probability)
6464

6565
# First sample.
66-
tf.compat.v1.assign(population_size, 10)
67-
tf.compat.v1.assign(selection_probability, 0.1)
66+
tf.assign(population_size, 10)
67+
tf.assign(selection_probability, 0.1)
6868
test_utils.run_query(query, [record1, record2])
6969

7070
expected_queries = [[10.0, 0.0]]
@@ -75,8 +75,8 @@ def test_sum_query(self):
7575
self.assertAllClose(sample_1.queries, expected_queries)
7676

7777
# Second sample.
78-
tf.compat.v1.assign(population_size, 20)
79-
tf.compat.v1.assign(selection_probability, 0.2)
78+
tf.assign(population_size, 20)
79+
tf.assign(selection_probability, 0.2)
8080
test_utils.run_query(query, [record1, record2])
8181

8282
formatted = query.ledger.get_formatted_ledger_eager()
@@ -106,8 +106,8 @@ def test_nested_query(self):
106106
record2 = [5.0, [1.0, 2.0]]
107107

108108
# First sample.
109-
tf.compat.v1.assign(population_size, 10)
110-
tf.compat.v1.assign(selection_probability, 0.1)
109+
tf.assign(population_size, 10)
110+
tf.assign(selection_probability, 0.1)
111111
test_utils.run_query(query, [record1, record2])
112112

113113
expected_queries = [[4.0, 2.0], [5.0, 1.0]]
@@ -118,8 +118,8 @@ def test_nested_query(self):
118118
self.assertAllClose(sorted(sample_1.queries), sorted(expected_queries))
119119

120120
# Second sample.
121-
tf.compat.v1.assign(population_size, 20)
122-
tf.compat.v1.assign(selection_probability, 0.2)
121+
tf.assign(population_size, 20)
122+
tf.assign(selection_probability, 0.2)
123123
test_utils.run_query(query, [record1, record2])
124124

125125
formatted = query.ledger.get_formatted_ledger_eager()

tensorflow_privacy/privacy/analysis/tensor_buffer.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def __init__(self, capacity, shape, dtype=tf.int32, name=None):
5050
raise ValueError('Shape cannot be scalar.')
5151
shape = [capacity] + shape
5252

53-
with tf.compat.v1.variable_scope(self._name):
53+
with tf.variable_scope(self._name):
5454
# We need to use a placeholder as the initial value to allow resizing.
55-
self._buffer = tf.compat.v1.Variable(
56-
initial_value=tf.compat.v1.placeholder_with_default(
55+
self._buffer = tf.Variable(
56+
initial_value=tf.placeholder_with_default(
5757
tf.zeros(shape, dtype), shape=None),
5858
trainable=False,
5959
name='buffer',
@@ -82,18 +82,18 @@ def _double_capacity():
8282
padding = tf.zeros_like(self._buffer, self._buffer.dtype)
8383
new_buffer = tf.concat([self._buffer, padding], axis=0)
8484
if tf.executing_eagerly():
85-
with tf.compat.v1.variable_scope(self._name, reuse=True):
86-
self._buffer = tf.compat.v1.get_variable(
85+
with tf.variable_scope(self._name, reuse=True):
86+
self._buffer = tf.get_variable(
8787
name='buffer',
8888
dtype=self._dtype,
8989
initializer=new_buffer,
9090
trainable=False)
91-
return self._buffer, tf.compat.v1.assign(
91+
return self._buffer, tf.assign(
9292
self._capacity, tf.multiply(self._capacity, 2))
9393
else:
94-
return tf.compat.v1.assign(
94+
return tf.assign(
9595
self._buffer, new_buffer,
96-
validate_shape=False), tf.compat.v1.assign(
96+
validate_shape=False), tf.assign(
9797
self._capacity, tf.multiply(self._capacity, 2))
9898

9999
update_buffer, update_capacity = tf.cond(
@@ -103,18 +103,18 @@ def _double_capacity():
103103

104104
with tf.control_dependencies([update_buffer, update_capacity]):
105105
with tf.control_dependencies([
106-
tf.compat.v1.assert_less(
106+
tf.assert_less(
107107
self._current_size,
108108
self._capacity,
109109
message='Appending past end of TensorBuffer.'),
110-
tf.compat.v1.assert_equal(
110+
tf.assert_equal(
111111
tf.shape(input=value),
112112
tf.shape(input=self._buffer)[1:],
113113
message='Appending value of inconsistent shape.')
114114
]):
115115
with tf.control_dependencies(
116-
[tf.compat.v1.assign(self._buffer[self._current_size, :], value)]):
117-
return tf.compat.v1.assign_add(self._current_size, 1)
116+
[tf.assign(self._buffer[self._current_size, :], value)]):
117+
return tf.assign_add(self._current_size, 1)
118118

119119
@property
120120
def values(self):

tensorflow_privacy/privacy/analysis/tensor_buffer_test_eager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from tensorflow_privacy.privacy.analysis import tensor_buffer
2323

24-
tf.compat.v1.enable_eager_execution()
24+
tf.enable_eager_execution()
2525

2626

2727
class TensorBufferTest(tf.test.TestCase):

tensorflow_privacy/privacy/analysis/tensor_buffer_test_graph.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_noresize(self):
3838
values = my_buffer.values
3939
current_size = my_buffer.current_size
4040
capacity = my_buffer.capacity
41-
self.evaluate(tf.compat.v1.global_variables_initializer())
41+
self.evaluate(tf.global_variables_initializer())
4242

4343
v, cs, cap = sess.run([values, current_size, capacity])
4444
self.assertAllEqual(v, [value1, value2])
@@ -60,7 +60,7 @@ def test_resize(self):
6060
values = my_buffer.values
6161
current_size = my_buffer.current_size
6262
capacity = my_buffer.capacity
63-
self.evaluate(tf.compat.v1.global_variables_initializer())
63+
self.evaluate(tf.global_variables_initializer())
6464

6565
v, cs, cap = sess.run([values, current_size, capacity])
6666
self.assertAllEqual(v, [value1, value2, value3])

tensorflow_privacy/privacy/dp_query/gaussian_query.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def add_noise(v):
9696
return v + tf.random.normal(
9797
tf.shape(input=v), stddev=global_state.stddev)
9898
else:
99-
random_normal = tf.compat.v1.random_normal_initializer(
99+
random_normal = tf.random_normal_initializer(
100100
stddev=global_state.stddev)
101101

102102
def add_noise(v):

tensorflow_privacy/privacy/dp_query/gaussian_query_test.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@ def test_gaussian_sum_with_changing_clip_no_noise(self):
5959
record2 = tf.constant([4.0, -3.0]) # Not clipped.
6060

6161
l2_norm_clip = tf.Variable(5.0)
62-
l2_norm_clip_placeholder = tf.compat.v1.placeholder(tf.float32)
63-
assign_l2_norm_clip = tf.compat.v1.assign(l2_norm_clip,
64-
l2_norm_clip_placeholder)
62+
l2_norm_clip_placeholder = tf.placeholder(tf.float32)
63+
assign_l2_norm_clip = tf.assign(l2_norm_clip, l2_norm_clip_placeholder)
6564
query = gaussian_query.GaussianSumQuery(
6665
l2_norm_clip=l2_norm_clip, stddev=0.0)
6766
query_result, _ = test_utils.run_query(query, [record1, record2])
6867

69-
self.evaluate(tf.compat.v1.global_variables_initializer())
68+
self.evaluate(tf.global_variables_initializer())
7069
result = sess.run(query_result)
7170
expected = [1.0, 1.0]
7271
self.assertAllClose(result, expected)

tensorflow_privacy/privacy/dp_query/quantile_adaptive_clip_sum_query_test.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from tensorflow_privacy.privacy.dp_query import quantile_adaptive_clip_sum_query
2828
from tensorflow_privacy.privacy.dp_query import test_utils
2929

30-
tf.compat.v1.enable_eager_execution()
30+
tf.enable_eager_execution()
3131

3232

3333
class QuantileAdaptiveClipSumQueryTest(
@@ -323,7 +323,7 @@ def test_adaptation_all_equal(self, start_low, geometric):
323323
global_state = query.initial_global_state()
324324

325325
for t in range(50):
326-
tf.compat.v1.assign(learning_rate, 1.0 / np.sqrt(t + 1))
326+
tf.assign(learning_rate, 1.0 / np.sqrt(t + 1))
327327
_, global_state = test_utils.run_query(query, records, global_state)
328328

329329
actual_clip = global_state.sum_state.l2_norm_clip
@@ -350,8 +350,8 @@ def test_ledger(self):
350350
query, population_size, selection_probability)
351351

352352
# First sample.
353-
tf.compat.v1.assign(population_size, 10)
354-
tf.compat.v1.assign(selection_probability, 0.1)
353+
tf.assign(population_size, 10)
354+
tf.assign(selection_probability, 0.1)
355355
_, global_state = test_utils.run_query(query, [record1, record2])
356356

357357
expected_queries = [[10.0, 10.0], [0.5, 0.0]]
@@ -362,8 +362,8 @@ def test_ledger(self):
362362
self.assertAllClose(sample_1.queries, expected_queries)
363363

364364
# Second sample.
365-
tf.compat.v1.assign(population_size, 20)
366-
tf.compat.v1.assign(selection_probability, 0.2)
365+
tf.assign(population_size, 20)
366+
tf.assign(selection_probability, 0.2)
367367
test_utils.run_query(query, [record1, record2], global_state)
368368

369369
formatted = query.ledger.get_formatted_ledger_eager()

tensorflow_privacy/privacy/optimizers/dp_optimizer.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
def make_optimizer_class(cls):
2929
"""Constructs a DP optimizer class from an existing one."""
30-
parent_code = tf.compat.v1.train.Optimizer.compute_gradients.__code__
30+
parent_code = tf.train.Optimizer.compute_gradients.__code__
3131
child_code = cls.compute_gradients.__code__
32-
GATE_OP = tf.compat.v1.train.Optimizer.GATE_OP # pylint: disable=invalid-name
32+
GATE_OP = tf.train.Optimizer.GATE_OP # pylint: disable=invalid-name
3333
if child_code is not parent_code:
3434
logging.warning(
3535
'WARNING: Calling make_optimizer_class() on class %s that overrides '
@@ -146,8 +146,8 @@ def process_microbatch(i, sample_state):
146146

147147
if var_list is None:
148148
var_list = (
149-
tf.compat.v1.trainable_variables() + tf.compat.v1.get_collection(
150-
tf.compat.v1.GraphKeys.TRAINABLE_RESOURCE_VARIABLES))
149+
tf.trainable_variables() + tf.get_collection(
150+
tf.GraphKeys.TRAINABLE_RESOURCE_VARIABLES))
151151

152152
sample_state = self._dp_sum_query.initial_sample_state(var_list)
153153

@@ -213,9 +213,9 @@ def ledger(self):
213213

214214
return DPGaussianOptimizerClass
215215

216-
AdagradOptimizer = tf.compat.v1.train.AdagradOptimizer
217-
AdamOptimizer = tf.compat.v1.train.AdamOptimizer
218-
GradientDescentOptimizer = tf.compat.v1.train.GradientDescentOptimizer
216+
AdagradOptimizer = tf.train.AdagradOptimizer
217+
AdamOptimizer = tf.train.AdamOptimizer
218+
GradientDescentOptimizer = tf.train.GradientDescentOptimizer
219219

220220
DPAdagradOptimizer = make_optimizer_class(AdagradOptimizer)
221221
DPAdamOptimizer = make_optimizer_class(AdamOptimizer)

tensorflow_privacy/privacy/optimizers/dp_optimizer_eager_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
class DPOptimizerEagerTest(tf.test.TestCase, parameterized.TestCase):
3131

3232
def setUp(self):
33-
tf.compat.v1.enable_eager_execution()
33+
tf.enable_eager_execution()
3434
super(DPOptimizerEagerTest, self).setUp()
3535

3636
def _loss_fn(self, val0, val1):
@@ -64,7 +64,7 @@ def testBaseline(self, cls, num_microbatches, expected_answer):
6464
num_microbatches=num_microbatches,
6565
learning_rate=2.0)
6666

67-
self.evaluate(tf.compat.v1.global_variables_initializer())
67+
self.evaluate(tf.global_variables_initializer())
6868
# Fetch params to validate initial values
6969
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
7070

@@ -89,7 +89,7 @@ def testClippingNorm(self, cls):
8989

9090
opt = cls(dp_sum_query, num_microbatches=1, learning_rate=2.0)
9191

92-
self.evaluate(tf.compat.v1.global_variables_initializer())
92+
self.evaluate(tf.global_variables_initializer())
9393
# Fetch params to validate initial values
9494
self.assertAllClose([0.0, 0.0], self.evaluate(var0))
9595

@@ -113,7 +113,7 @@ def testNoiseMultiplier(self, cls):
113113

114114
opt = cls(dp_sum_query, num_microbatches=1, learning_rate=2.0)
115115

116-
self.evaluate(tf.compat.v1.global_variables_initializer())
116+
self.evaluate(tf.global_variables_initializer())
117117
# Fetch params to validate initial values
118118
self.assertAllClose([0.0], self.evaluate(var0))
119119

tensorflow_privacy/privacy/optimizers/dp_optimizer_test.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def testBaseline(self, cls, num_microbatches, expected_answer):
6363
num_microbatches=num_microbatches,
6464
learning_rate=2.0)
6565

66-
self.evaluate(tf.compat.v1.global_variables_initializer())
66+
self.evaluate(tf.global_variables_initializer())
6767
# Fetch params to validate initial values
6868
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
6969

@@ -87,7 +87,7 @@ def testClippingNorm(self, cls):
8787

8888
opt = cls(dp_sum_query, num_microbatches=1, learning_rate=2.0)
8989

90-
self.evaluate(tf.compat.v1.global_variables_initializer())
90+
self.evaluate(tf.global_variables_initializer())
9191
# Fetch params to validate initial values
9292
self.assertAllClose([0.0, 0.0], self.evaluate(var0))
9393

@@ -110,7 +110,7 @@ def testNoiseMultiplier(self, cls):
110110

111111
opt = cls(dp_sum_query, num_microbatches=1, learning_rate=2.0)
112112

113-
self.evaluate(tf.compat.v1.global_variables_initializer())
113+
self.evaluate(tf.global_variables_initializer())
114114
# Fetch params to validate initial values
115115
self.assertAllClose([0.0], self.evaluate(var0))
116116

@@ -126,7 +126,7 @@ def testNoiseMultiplier(self, cls):
126126
@mock.patch('absl.logging.warning')
127127
def testComputeGradientsOverrideWarning(self, mock_logging):
128128

129-
class SimpleOptimizer(tf.compat.v1.train.Optimizer):
129+
class SimpleOptimizer(tf.train.Optimizer):
130130

131131
def compute_gradients(self):
132132
return 0
@@ -153,7 +153,7 @@ def linear_model_fn(features, labels, mode):
153153
dp_sum_query,
154154
num_microbatches=1,
155155
learning_rate=1.0)
156-
global_step = tf.compat.v1.train.get_global_step()
156+
global_step = tf.train.get_global_step()
157157
train_op = optimizer.minimize(loss=vector_loss, global_step=global_step)
158158
return tf.estimator.EstimatorSpec(
159159
mode=mode, loss=scalar_loss, train_op=train_op)
@@ -167,7 +167,7 @@ def linear_model_fn(features, labels, mode):
167167
true_weights) + true_bias + np.random.normal(
168168
scale=0.1, size=(200, 1)).astype(np.float32)
169169

170-
train_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn(
170+
train_input_fn = tf.estimator.inputs.numpy_input_fn(
171171
x={'x': train_data},
172172
y=train_labels,
173173
batch_size=20,
@@ -200,7 +200,7 @@ def testUnrollMicrobatches(self, cls):
200200
learning_rate=2.0,
201201
unroll_microbatches=True)
202202

203-
self.evaluate(tf.compat.v1.global_variables_initializer())
203+
self.evaluate(tf.global_variables_initializer())
204204
# Fetch params to validate initial values
205205
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
206206

@@ -225,7 +225,7 @@ def testDPGaussianOptimizerClass(self, cls):
225225
num_microbatches=1,
226226
learning_rate=2.0)
227227

228-
self.evaluate(tf.compat.v1.global_variables_initializer())
228+
self.evaluate(tf.global_variables_initializer())
229229
# Fetch params to validate initial values
230230
self.assertAllClose([0.0], self.evaluate(var0))
231231

0 commit comments

Comments
 (0)