Skip to content

Commit 68df6ef

Browse files
Johannes Ball?copybara-github
authored andcommitted
Fixes deprecated names.
- In Keras layers, replaces add_variable() with add_weight(). - In Keras layers, removes inputs= argument in add_update(). - In unit tests, replaces test_session() with cached_session(). PiperOrigin-RevId: 281393590 Change-Id: I7c66f654d3828a657a5f55c06fdd00f510ef1043
1 parent 09a34e6 commit 68df6ef

File tree

10 files changed

+48
-48
lines changed

10 files changed

+48
-48
lines changed

tensorflow_compression/python/layers/entropy_models.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,21 @@ def build(self, input_shape):
467467
self._factors = []
468468
for i in range(len(self.filters) + 1):
469469
init = np.log(np.expm1(1 / scale / filters[i + 1]))
470-
matrix = self.add_variable(
470+
matrix = self.add_weight(
471471
"matrix_{}".format(i), dtype=self.dtype,
472472
shape=(channels, filters[i + 1], filters[i]),
473473
initializer=tf.initializers.constant(init))
474474
matrix = tf.nn.softplus(matrix)
475475
self._matrices.append(matrix)
476476

477-
bias = self.add_variable(
477+
bias = self.add_weight(
478478
"bias_{}".format(i), dtype=self.dtype,
479479
shape=(channels, filters[i + 1], 1),
480480
initializer=tf.initializers.random_uniform(-.5, .5))
481481
self._biases.append(bias)
482482

483483
if i < len(self.filters):
484-
factor = self.add_variable(
484+
factor = self.add_weight(
485485
"factor_{}".format(i), dtype=self.dtype,
486486
shape=(channels, filters[i + 1], 1),
487487
initializer=tf.initializers.zeros())
@@ -510,7 +510,7 @@ def quantiles_initializer(shape, dtype=None, partition_info=None):
510510
[[[-self.init_scale, 0, self.init_scale]]], dtype=dtype)
511511
return tf.tile(init, (shape[0], 1, 1))
512512

513-
quantiles = self.add_variable(
513+
quantiles = self.add_weight(
514514
"quantiles", shape=(channels, 1, 3), dtype=self.dtype,
515515
initializer=quantiles_initializer)
516516
logits = self._logits_cumulative(quantiles, stop_gradient=True)
@@ -572,13 +572,13 @@ def cdf_initializer(shape, dtype=None, partition_info=None):
572572
# We need to supply an initializer without fully defined static shape
573573
# here, or the variable will return the wrong dynamic shape later. A
574574
# placeholder with default gets the trick done (see initializer above).
575-
quantized_cdf = self.add_variable(
575+
quantized_cdf = self.add_weight(
576576
"quantized_cdf",
577577
shape=(channels, None),
578578
dtype=tf.int32,
579579
trainable=False,
580580
initializer=cdf_initializer)
581-
cdf_length = self.add_variable(
581+
cdf_length = self.add_weight(
582582
"cdf_length", shape=(channels,), dtype=tf.int32, trainable=False,
583583
initializer=tf.initializers.constant(3))
584584
# Works around a weird TF issue with reading variables inside a loop.
@@ -593,7 +593,7 @@ def cdf_initializer(shape, dtype=None, partition_info=None):
593593
cdf_length,
594594
pmf_length + 2)
595595
update_op = tf.group(update_cdf, update_length)
596-
self.add_update(update_op, inputs=None)
596+
self.add_update(update_op)
597597

598598
super(EntropyBottleneck, self).build(input_shape)
599599

@@ -853,10 +853,10 @@ def cdf_initializer(shape, dtype=None, partition_info=None):
853853
pmf, tail_mass,
854854
tf.constant(pmf_length, dtype=tf.int32), max_length)
855855

856-
quantized_cdf = self.add_variable(
856+
quantized_cdf = self.add_weight(
857857
"quantized_cdf", shape=(len(pmf_length), max_length + 2),
858858
initializer=cdf_initializer, dtype=tf.int32, trainable=False)
859-
cdf_length = self.add_variable(
859+
cdf_length = self.add_weight(
860860
"cdf_length", shape=(len(pmf_length),),
861861
initializer=tf.initializers.constant(pmf_length + 2),
862862
dtype=tf.int32, trainable=False)

tensorflow_compression/python/layers/entropy_models_test.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_noise(self):
3232
inputs = tf.placeholder(tf.float32, (None, 1))
3333
layer = entropy_models.EntropyBottleneck()
3434
noisy, _ = layer(inputs, training=True)
35-
with self.test_session() as sess:
35+
with self.cached_session() as sess:
3636
sess.run(tf.global_variables_initializer())
3737
values = np.linspace(-50, 50, 100)[:, None]
3838
noisy, = sess.run([noisy], {inputs: values})
@@ -45,7 +45,7 @@ def test_quantization_init(self):
4545
inputs = tf.placeholder(tf.float32, (None, 1))
4646
layer = entropy_models.EntropyBottleneck()
4747
quantized, _ = layer(inputs, training=False)
48-
with self.test_session() as sess:
48+
with self.cached_session() as sess:
4949
sess.run(tf.global_variables_initializer())
5050
values = np.linspace(-50, 50, 100)[:, None]
5151
quantized, = sess.run([quantized], {inputs: values})
@@ -61,7 +61,7 @@ def test_quantization(self):
6161
opt = tf.train.GradientDescentOptimizer(learning_rate=1)
6262
self.assertEqual(1, len(layer.losses))
6363
step = opt.minimize(layer.losses[0])
64-
with self.test_session() as sess:
64+
with self.cached_session() as sess:
6565
sess.run(tf.global_variables_initializer())
6666
sess.run(step)
6767
values = np.linspace(-50, 50, 100)[:, None]
@@ -79,7 +79,7 @@ def test_codec_init(self):
7979
data_format="channels_last", init_scale=30)
8080
bitstrings = layer.compress(inputs)
8181
decoded = layer.decompress(bitstrings, tf.shape(inputs)[1:])
82-
with self.test_session() as sess:
82+
with self.cached_session() as sess:
8383
sess.run(tf.global_variables_initializer())
8484
values = np.linspace(-50, 50, 100)[None, :, None]
8585
decoded, = sess.run([decoded], {inputs: values})
@@ -98,7 +98,7 @@ def test_codec(self):
9898
opt = tf.train.GradientDescentOptimizer(learning_rate=1)
9999
self.assertEqual(1, len(layer.losses))
100100
step = opt.minimize(layer.losses[0])
101-
with self.test_session() as sess:
101+
with self.cached_session() as sess:
102102
sess.run(tf.global_variables_initializer())
103103
sess.run(step)
104104
self.assertEqual(1, len(layer.updates))
@@ -120,7 +120,7 @@ def test_channels_last(self):
120120
quantized, _ = layer(inputs, training=False)
121121
bitstrings = layer.compress(inputs)
122122
decoded = layer.decompress(bitstrings, tf.shape(inputs)[1:])
123-
with self.test_session() as sess:
123+
with self.cached_session() as sess:
124124
sess.run(tf.global_variables_initializer())
125125
self.assertEqual(1, len(layer.updates))
126126
sess.run(layer.updates[0])
@@ -141,7 +141,7 @@ def test_channels_first(self):
141141
quantized, _ = layer(inputs, training=False)
142142
bitstrings = layer.compress(inputs)
143143
decoded = layer.decompress(bitstrings, tf.shape(inputs)[1:])
144-
with self.test_session() as sess:
144+
with self.cached_session() as sess:
145145
sess.run(tf.global_variables_initializer())
146146
self.assertEqual(1, len(layer.updates))
147147
sess.run(layer.updates[0])
@@ -161,7 +161,7 @@ def test_compress(self):
161161
data_format="channels_first", filters=(), init_scale=2)
162162
bitstrings = layer.compress(inputs)
163163
decoded = layer.decompress(bitstrings, tf.shape(inputs)[1:])
164-
with self.test_session() as sess:
164+
with self.cached_session() as sess:
165165
values = 8 * np.random.uniform(size=(2, 3, 9)) - 4
166166
sess.run(tf.global_variables_initializer())
167167
self.assertEqual(1, len(layer.updates))
@@ -213,7 +213,7 @@ def test_decompress(self):
213213
layer._quantized_cdf = quantized_cdf
214214
layer._cdf_length = cdf_length
215215
decoded = layer.decompress(bitstrings, input_shape[1:])
216-
with self.test_session() as sess:
216+
with self.cached_session() as sess:
217217
sess.run(tf.global_variables_initializer())
218218
decoded, = sess.run([decoded], {
219219
bitstrings: self.bitstrings, input_shape: self.expected.shape,
@@ -233,7 +233,7 @@ def test_normalization(self):
233233
inputs = tf.placeholder(tf.float32, (None, 1))
234234
layer = entropy_models.EntropyBottleneck(filters=(2,))
235235
_, likelihood = layer(inputs, training=True)
236-
with self.test_session() as sess:
236+
with self.cached_session() as sess:
237237
sess.run(tf.global_variables_initializer())
238238
x = np.repeat(np.arange(-200, 201), 2000)[:, None]
239239
likelihood, = sess.run([likelihood], {inputs: x})
@@ -251,7 +251,7 @@ def test_entropy_estimates(self):
251251
_, likelihood = layer(inputs, training=False)
252252
disc_entropy = tf.reduce_sum(tf.log(likelihood)) / -np.log(2)
253253
bitstrings = layer.compress(inputs)
254-
with self.test_session() as sess:
254+
with self.cached_session() as sess:
255255
sess.run(tf.global_variables_initializer())
256256
self.assertEqual(1, len(layer.updates))
257257
sess.run(layer.updates[0])
@@ -272,7 +272,7 @@ def test_noise(self):
272272
scale = tf.placeholder(tf.float32, [None])
273273
layer = self.subclass(scale, [1])
274274
noisy, _ = layer(inputs, training=True)
275-
with self.test_session() as sess:
275+
with self.cached_session() as sess:
276276
sess.run(tf.global_variables_initializer())
277277
values = np.linspace(-50, 50, 100)
278278
noisy, = sess.run([noisy], {
@@ -288,7 +288,7 @@ def test_quantization(self):
288288
scale = tf.placeholder(tf.float32, [None])
289289
layer = self.subclass(scale, [1], mean=None)
290290
quantized, _ = layer(inputs, training=False)
291-
with self.test_session() as sess:
291+
with self.cached_session() as sess:
292292
sess.run(tf.global_variables_initializer())
293293
values = np.linspace(-50, 50, 100)
294294
quantized, = sess.run([quantized], {
@@ -305,7 +305,7 @@ def test_quantization_mean(self):
305305
mean = tf.placeholder(tf.float32, [None])
306306
layer = self.subclass(scale, [1], mean=mean)
307307
quantized, _ = layer(inputs, training=False)
308-
with self.test_session() as sess:
308+
with self.cached_session() as sess:
309309
sess.run(tf.global_variables_initializer())
310310
values = np.linspace(-50, 50, 100)
311311
mean_values = np.random.normal(size=values.shape)
@@ -327,7 +327,7 @@ def test_codec(self):
327327
scale, [2 ** x for x in range(-10, 10)], mean=None)
328328
bitstrings = layer.compress(inputs)
329329
decoded = layer.decompress(bitstrings)
330-
with self.test_session() as sess:
330+
with self.cached_session() as sess:
331331
sess.run(tf.global_variables_initializer())
332332
values = np.linspace(-50, 50, 100)[None]
333333
decoded, = sess.run([decoded], {
@@ -346,7 +346,7 @@ def test_codec_mean(self):
346346
scale, [2 ** x for x in range(-10, 10)], mean=mean)
347347
bitstrings = layer.compress(inputs)
348348
decoded = layer.decompress(bitstrings)
349-
with self.test_session() as sess:
349+
with self.cached_session() as sess:
350350
sess.run(tf.global_variables_initializer())
351351
values = np.linspace(-50, 50, 100)[None]
352352
mean_values = np.random.normal(size=values.shape)
@@ -369,7 +369,7 @@ def test_multiple_dimensions(self):
369369
quantized, _ = layer(inputs, training=False)
370370
bitstrings = layer.compress(inputs)
371371
decoded = layer.decompress(bitstrings)
372-
with self.test_session() as sess:
372+
with self.cached_session() as sess:
373373
sess.run(tf.global_variables_initializer())
374374
values = 10 * np.random.normal(size=(2, 5, 3, 7))
375375
noisy, quantized, decoded = sess.run(
@@ -391,7 +391,7 @@ def test_compress(self):
391391
layer = self.subclass(scale, scale_table, indexes=indexes)
392392
bitstrings = layer.compress(inputs)
393393
decoded = layer.decompress(bitstrings)
394-
with self.test_session() as sess:
394+
with self.cached_session() as sess:
395395
values = 8 * np.random.uniform(size=shape) - 4
396396
indexes = np.random.randint(
397397
0, len(scale_table), size=shape, dtype=np.int32)
@@ -415,7 +415,7 @@ def test_decompress(self):
415415
layer = self.subclass(
416416
scale, scale_table, indexes=indexes, dtype=tf.float32)
417417
decoded = layer.decompress(bitstrings)
418-
with self.test_session() as sess:
418+
with self.cached_session() as sess:
419419
sess.run(tf.global_variables_initializer())
420420
decoded, = sess.run([decoded], {
421421
bitstrings: self.bitstrings,
@@ -437,7 +437,7 @@ def test_quantile_function(self):
437437
# Test that quantile function inverts cumulative.
438438
scale = tf.placeholder(tf.float64, [None])
439439
layer = self.subclass(scale, [1], dtype=tf.float64)
440-
with self.test_session() as sess:
440+
with self.cached_session() as sess:
441441
sess.run(tf.global_variables_initializer())
442442
quantiles = np.array([1e-5, 1e-2, .1, .5, .6, .8])
443443
locations = layer._standardized_quantile(quantiles)
@@ -452,7 +452,7 @@ def test_distribution(self):
452452
scale = tf.placeholder(tf.float32, [None, None])
453453
layer = self.subclass(scale, [1], scale_bound=0, mean=None)
454454
_, likelihood = layer(inputs, training=False)
455-
with self.test_session() as sess:
455+
with self.cached_session() as sess:
456456
sess.run(tf.global_variables_initializer())
457457
values = np.arange(-5, 1)[:, None] # must be integers due to quantization
458458
scales = 2 ** np.linspace(-3, 3, 10)[None, :]
@@ -476,7 +476,7 @@ def test_entropy_estimates(self):
476476
disc_entropy = tf.reduce_mean(tf.log(likelihood), axis=1)
477477
disc_entropy /= -np.log(2)
478478
bitstrings = layer.compress(inputs)
479-
with self.test_session() as sess:
479+
with self.cached_session() as sess:
480480
sess.run(tf.global_variables_initializer())
481481
scales = np.repeat([layer.scale_table], 10000, axis=0).T
482482
values = self.scipy_class.rvs(scale=scales, size=scales.shape)

tensorflow_compression/python/layers/gdn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ def build(self, input_shape):
158158
# pylint:disable=not-callable
159159
self.beta = self.beta_parameterizer(
160160
name="beta", shape=[num_channels], dtype=self.dtype,
161-
getter=self.add_variable, initializer=tf.initializers.ones())
161+
getter=self.add_weight, initializer=tf.initializers.ones())
162162

163163
self.gamma = self.gamma_parameterizer(
164164
name="gamma", shape=[num_channels, num_channels], dtype=self.dtype,
165-
getter=self.add_variable,
165+
getter=self.add_weight,
166166
initializer=tf.initializers.identity(gain=self._gamma_init))
167167
# pylint:enable=not-callable
168168

tensorflow_compression/python/layers/gdn_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _run_gdn(self, x, shape, inverse, rectify, data_format):
3131
layer = gdn.GDN(
3232
inverse=inverse, rectify=rectify, data_format=data_format)
3333
outputs = layer(inputs)
34-
with self.test_session() as sess:
34+
with self.cached_session() as sess:
3535
tf.global_variables_initializer().run()
3636
y, = sess.run([outputs], {inputs: x})
3737
return y

tensorflow_compression/python/layers/parameterizers_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _test_parameterizer(self, param, init, shape):
3030
var = param(
3131
getter=tf.get_variable, name="test", shape=shape, dtype=tf.float32,
3232
initializer=init, regularizer=None)
33-
with self.test_session() as sess:
33+
with self.cached_session() as sess:
3434
tf.global_variables_initializer().run()
3535
var, = sess.run([var])
3636
return var

tensorflow_compression/python/layers/signal_conv.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,10 @@ def build(self, input_shape):
417417

418418
kernel_parameterizer = self.kernel_parameterizer
419419
if kernel_parameterizer is None:
420-
getter = self.add_variable
420+
getter = self.add_weight
421421
else:
422422
getter = functools.partial(
423-
kernel_parameterizer, getter=self.add_variable)
423+
kernel_parameterizer, getter=self.add_weight)
424424
self._kernel = getter(
425425
name="kernel", shape=kernel_shape, dtype=self.dtype,
426426
initializer=self.kernel_initializer,
@@ -429,10 +429,10 @@ def build(self, input_shape):
429429
if self.use_bias:
430430
bias_parameterizer = self.bias_parameterizer
431431
if bias_parameterizer is None:
432-
getter = self.add_variable
432+
getter = self.add_weight
433433
else:
434434
getter = functools.partial(
435-
bias_parameterizer, getter=self.add_variable)
435+
bias_parameterizer, getter=self.add_weight)
436436
self._bias = getter(
437437
name="bias", shape=(output_channels,), dtype=self.dtype,
438438
initializer=self.bias_initializer, regularizer=self.bias_regularizer)

tensorflow_compression/python/layers/signal_conv_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def run_valid(self, batch, input_support, channels, filters, kernel_support,
110110
activation=activation, use_bias=use_bias,
111111
kernel_parameterizer=tf_kernel)
112112
tf_outputs = layer(tf_inputs)
113-
with self.test_session() as sess:
113+
with self.cached_session() as sess:
114114
sess.run(tf.global_variables_initializer())
115115
outputs = sess.run(tf_outputs)
116116

@@ -160,7 +160,7 @@ def run_same(self, batch, input_support, channels, filters, kernel_support,
160160
activation=activation, use_bias=use_bias,
161161
kernel_parameterizer=tf_kernel)
162162
tf_outputs = layer(tf_inputs)
163-
with self.test_session() as sess:
163+
with self.cached_session() as sess:
164164
sess.run(tf.global_variables_initializer())
165165
outputs = sess.run(tf_outputs)
166166

tensorflow_compression/python/ops/math_ops_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _test_upper_bound(self, gradient):
4343
pgrads_expected = [1, 1]
4444
ngrads_expected = [-1, 0]
4545

46-
with self.test_session() as sess:
46+
with self.cached_session() as sess:
4747
outputs, pgrads, ngrads = sess.run(
4848
[outputs, pgrads, ngrads], {inputs: inputs_feed})
4949
self.assertAllEqual(outputs, outputs_expected)
@@ -81,7 +81,7 @@ def _test_lower_bound(self, gradient):
8181
pgrads_expected = [0, 1]
8282
ngrads_expected = [-1, -1]
8383

84-
with self.test_session() as sess:
84+
with self.cached_session() as sess:
8585
outputs, pgrads, ngrads = sess.run(
8686
[outputs, pgrads, ngrads], {inputs: inputs_feed})
8787
self.assertAllEqual(outputs, outputs_expected)

tensorflow_compression/python/ops/padding_ops_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_same_padding_corr(self):
3737
tf.reshape(inputs, (1, 1, -1, 1)),
3838
tf.reshape(kernel, (1, -1, 1, 1)),
3939
padding="VALID", data_format="NHWC")
40-
with self.test_session() as sess:
40+
with self.cached_session() as sess:
4141
outputs = np.squeeze(sess.run(outputs))
4242
pos_inp = np.squeeze(np.nonzero(inputs))
4343
pos_out = np.squeeze(np.nonzero(outputs))
@@ -57,7 +57,7 @@ def test_same_padding_conv(self):
5757
(1, 1, ishape[0] + kshape[0] - 1, 1),
5858
strides=(1, 1, 1, 1), padding="VALID", data_format="NHWC")
5959
outputs = outputs[:, :, (kshape[0] - 1):-(kshape[0] - 1), :]
60-
with self.test_session() as sess:
60+
with self.cached_session() as sess:
6161
outputs = np.squeeze(sess.run(outputs))
6262
pos_inp = np.squeeze(np.nonzero(inputs))
6363
pos_out = np.squeeze(np.nonzero(outputs))

0 commit comments

Comments
 (0)