Skip to content

Commit 4efd63b

Browse files
lgeigerkpe
authored andcommitted
Reduce usage of tf.contrib (tensorflow#1345)
* tf.contrib.rnn -> tf.nn.rnn_cell * tf.contrib.framework -> tf.train tf.contrib.framework.load_checkpoint -> tf.train.load_checkpoint tf.contrib.framework.list_variables -> tf.train.list_variables tf.contrib.framework.init_from_checkpoint -> tf.train.init_from_checkpoint * tf.contrib.learn.ModeKeys -> tf.estimator.ModeKeys tf.contrib.learn.ModeKeys.TRAIN -> tf.estimator.ModeKeys.TRAIN tf.contrib.learn.ModeKeys.EVAL -> tf.estimator.ModeKeys.EVAL tf.contrib.learn.ModeKeys.INFER -> tf.estimator.ModeKeys.PREDICT * tf.contrib.eager.in_eager_mode -> tf.executing_eagerly * tf.contrib.data.batch_and_drop_remainder -> .batch(..., drop_remainder=True) * tf.contrib.data -> tf.data.experimental * tf.contrib.util.make_tensor_proto -> tf.make_tensor_proto * Simplify tf.contrib.eager imports * tf.initialize_all_variables -> tf.global_variables_initializer
1 parent 8d28945 commit 4efd63b

32 files changed

+80
-80
lines changed

tensor2tensor/bin/t2t_attack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def main(argv):
200200
sur_ch_model.get_probs(inputs)
201201

202202
checkpoint_path = os.path.expanduser(FLAGS.surrogate_output_dir)
203-
tf.contrib.framework.init_from_checkpoint(
203+
tf.train.init_from_checkpoint(
204204
tf.train.latest_checkpoint(checkpoint_path), {"/": "surrogate/"})
205205
sess.run(tf.global_variables_initializer())
206206

tensor2tensor/bin/t2t_avg_all.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def main(_):
6060
for model in bleu_hook.stepfiles_iterator(model_dir, FLAGS.wait_minutes,
6161
FLAGS.min_steps):
6262
if models_processed == 0:
63-
var_list = tf.contrib.framework.list_variables(model.filename)
63+
var_list = tf.train.list_variables(model.filename)
6464
avg_values = {}
6565
for (name, shape) in var_list:
6666
if not (name.startswith("global_step") or
@@ -69,7 +69,7 @@ def main(_):
6969
models_processed += 1
7070

7171
tf.logging.info("Loading [%d]: %s" % (models_processed, model.filename))
72-
reader = tf.contrib.framework.load_checkpoint(model.filename)
72+
reader = tf.train.load_checkpoint(model.filename)
7373
for name in avg_values:
7474
avg_values[name] += reader.get_tensor(name) / FLAGS.n
7575
queue.append(model)
@@ -106,7 +106,7 @@ def main(_):
106106
tf.reset_default_graph()
107107
first_model = queue.popleft()
108108

109-
reader = tf.contrib.framework.load_checkpoint(first_model.filename)
109+
reader = tf.train.load_checkpoint(first_model.filename)
110110
for name in avg_values:
111111
avg_values[name] -= reader.get_tensor(name) / FLAGS.n
112112

tensor2tensor/data_generators/image_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def generate_data(self, data_dir, tmp_dir, task_id=-1):
262262

263263
def encode_images_as_png(images):
264264
"""Yield images encoded as pngs."""
265-
if tf.contrib.eager.in_eager_mode():
265+
if tf.executing_eagerly():
266266
for image in images:
267267
yield tf.image.encode_png(image).numpy()
268268
else:

tensor2tensor/data_generators/problem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def _preprocess(example):
423423

424424
if interleave:
425425
dataset = dataset.apply(
426-
tf.contrib.data.parallel_interleave(
426+
tf.data.experimental.parallel_interleave(
427427
_preprocess, sloppy=True, cycle_length=8))
428428
else:
429429
dataset = dataset.flat_map(_preprocess)
@@ -674,7 +674,7 @@ def _load_records_and_preprocess(filenames):
674674
# Create data-set from files by parsing, pre-processing and interleaving.
675675
if shuffle_files:
676676
dataset = dataset.apply(
677-
tf.contrib.data.parallel_interleave(
677+
tf.data.experimental.parallel_interleave(
678678
_load_records_and_preprocess, sloppy=True, cycle_length=8))
679679
else:
680680
dataset = _load_records_and_preprocess(dataset)
@@ -963,7 +963,7 @@ def define_shapes(example):
963963
batching_scheme["batch_sizes"] = [hparams.batch_size]
964964
batching_scheme["boundaries"] = []
965965
dataset = dataset.apply(
966-
tf.contrib.data.bucket_by_sequence_length(
966+
tf.data.experimental.bucket_by_sequence_length(
967967
data_reader.example_length, batching_scheme["boundaries"],
968968
batching_scheme["batch_sizes"]))
969969

@@ -1040,7 +1040,7 @@ def serving_input_fn(self, hparams):
10401040
tf.shape(serialized_example, out_type=tf.int64)[0],
10411041
dataset.output_shapes)
10421042
dataset = dataset.map(standardize_shapes)
1043-
features = tf.contrib.data.get_single_element(dataset)
1043+
features = tf.data.experimental.get_single_element(dataset)
10441044

10451045
if self.has_inputs:
10461046
features.pop("targets", None)

tensor2tensor/layers/common_image_attention.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def postprocess_image(x, rows, cols, hparams):
501501
use_bias=True,
502502
activation=None,
503503
name="output_conv")
504-
if (hparams.mode == tf.contrib.learn.ModeKeys.INFER and
504+
if (hparams.mode == tf.estimator.ModeKeys.PREDICT and
505505
hparams.block_raster_scan):
506506
y = targets
507507
yshape = common_layers.shape_list(y)
@@ -547,7 +547,7 @@ def prepare_decoder(targets, hparams):
547547

548548
# during training, images are [batch, IMG_LEN, IMG_LEN, 3].
549549
# At inference, they are [batch, curr_infer_length, 1, 1]
550-
if hparams.mode == tf.contrib.learn.ModeKeys.INFER:
550+
if hparams.mode == tf.estimator.ModeKeys.PREDICT:
551551
curr_infer_length = targets_shape[1]
552552
if hparams.block_raster_scan:
553553
assert hparams.img_len*channels % hparams.query_shape[1] == 0

tensor2tensor/layers/common_image_attention_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def testPostProcessImageInferMode(self, likelihood, num_mixtures, depth):
6161
block_raster_scan=True,
6262
hidden_size=2,
6363
likelihood=likelihood,
64-
mode=tf.contrib.learn.ModeKeys.INFER,
64+
mode=tf.estimator.ModeKeys.PREDICT,
6565
num_mixtures=num_mixtures,
6666
query_shape=[block_length, block_width],
6767
)

tensor2tensor/layers/common_layers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def embedding(x,
349349
# On the backwards pass, we want to convert the gradient from
350350
# an indexed-slices to a regular tensor before sending it back to the
351351
# parameter server. This avoids excess computation on the parameter server.
352-
if not tf.contrib.eager.in_eager_mode():
352+
if not tf.executing_eagerly():
353353
embedding_var = convert_gradient_to_tensor(embedding_var)
354354
x = dropout_no_scaling(x, 1.0 - symbol_dropout_rate)
355355
emb_x = gather(embedding_var, x, dtype)
@@ -2868,7 +2868,7 @@ def ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None):
28682868
def reshape_like_all_dims(a, b):
28692869
"""Reshapes a to match the shape of b."""
28702870
ret = tf.reshape(a, tf.shape(b))
2871-
if not tf.contrib.eager.in_eager_mode():
2871+
if not tf.executing_eagerly():
28722872
ret.set_shape(b.get_shape())
28732873
return ret
28742874

@@ -3193,7 +3193,7 @@ def should_generate_summaries():
31933193
def reshape_like(a, b):
31943194
"""Reshapes a to match the shape of b in all but the last dimension."""
31953195
ret = tf.reshape(a, tf.concat([tf.shape(b)[:-1], tf.shape(a)[-1:]], 0))
3196-
if not tf.contrib.eager.in_eager_mode():
3196+
if not tf.executing_eagerly():
31973197
ret.set_shape(b.get_shape().as_list()[:-1] + a.get_shape().as_list()[-1:])
31983198
return ret
31993199

@@ -3205,7 +3205,7 @@ def summarize_video(video, prefix, max_outputs=1):
32053205
raise ValueError("Assuming videos given as tensors in the format "
32063206
"[batch, time, height, width, channels] but got one "
32073207
"of shape: %s" % str(video_shape))
3208-
if tf.contrib.eager.in_eager_mode():
3208+
if tf.executing_eagerly():
32093209
return
32103210
if video.get_shape().as_list()[1] is None:
32113211
tf.summary.image(

tensor2tensor/layers/common_video.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def lstm_cell(inputs,
8282
name=None):
8383
"""Full LSTM cell."""
8484
input_shape = common_layers.shape_list(inputs)
85-
cell = tf.contrib.rnn.LSTMCell(num_units,
85+
cell = tf.nn.rnn_cell.LSTMCell(num_units,
8686
use_peepholes=use_peepholes,
8787
cell_clip=cell_clip,
8888
initializer=initializer,

tensor2tensor/layers/discretization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def gumbel_softmax(x,
473473
d_dev = -tf.reduce_mean(d_variance)
474474
ret = s
475475

476-
if mode != tf.contrib.learn.ModeKeys.TRAIN:
476+
if mode != tf.estimator.ModeKeys.TRAIN:
477477
ret = tf.reshape(maxvhot, common_layers.shape_list(s)) # Just hot @eval.
478478
return m, ret, d_dev * 5.0 + tf.reduce_mean(kl) * 0.002
479479

@@ -822,7 +822,7 @@ def predict_bits_with_lstm(prediction_source, state_size, total_num_bits,
822822

823823
with tf.variable_scope("predict_bits_with_lstm"):
824824
# Layers and cell state creation.
825-
lstm_cell = tf.contrib.rnn.LSTMCell(state_size)
825+
lstm_cell = tf.nn.rnn_cell.LSTMCell(state_size)
826826
discrete_predict = tf.layers.Dense(2**bits_at_once, name="discrete_predict")
827827
discrete_embed = tf.layers.Dense(state_size, name="discrete_embed")
828828
batch_size = common_layers.shape_list(prediction_source)[0]

tensor2tensor/layers/modalities.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _get_weights(self, hidden_dim=None):
9393
else:
9494
ret = tf.concat(shards, 0)
9595
# Convert ret to tensor.
96-
if not tf.contrib.eager.in_eager_mode():
96+
if not tf.executing_eagerly():
9797
ret = common_layers.convert_gradient_to_tensor(ret)
9898
return ret
9999

@@ -226,15 +226,15 @@ class ImageModality(modality.Modality):
226226

227227
def bottom(self, x):
228228
with tf.variable_scope(self.name):
229-
if not tf.contrib.eager.in_eager_mode():
229+
if not tf.executing_eagerly():
230230
tf.summary.image(
231231
"inputs", common_layers.tpu_safe_image_summary(x), max_outputs=2)
232232
return tf.to_float(x)
233233

234234
def targets_bottom(self, x):
235235
inputs = x
236236
with tf.variable_scope(self.name):
237-
if not tf.contrib.eager.in_eager_mode():
237+
if not tf.executing_eagerly():
238238
tf.summary.image(
239239
"targets_bottom",
240240
common_layers.tpu_safe_image_summary(inputs),

0 commit comments

Comments
 (0)