Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Replace .append loop with list comprehension #1451

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions tensor2tensor/data_generators/cnn_dailymail.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ def generate_hash(inp):

all_files_map = {f.split("/")[-1]: f for f in all_files}

urls = []
for line in tf.gfile.Open(url_file):
urls.append(line.strip().encode("utf-8"))
urls = [line.strip().encode("utf-8") for line in tf.gfile.Open(url_file)]

filelist = []
for url in urls:
Expand Down
5 changes: 2 additions & 3 deletions tensor2tensor/data_generators/multi_problem_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,8 @@ def epoch_rates_to_pmf(problems, epoch_rates=None):
"""
if epoch_rates is None:
epoch_rates = [1.0] * len(problems)
example_rates = []
for p, epoch_rate in zip(problems, epoch_rates):
example_rates.append(epoch_rate * p.num_training_examples)
example_rates = [epoch_rate * p.num_training_examples
for p, epoch_rate in zip(problems, epoch_rates)]
return example_rates_to_pmf(example_rates)


Expand Down
9 changes: 2 additions & 7 deletions tensor2tensor/data_generators/video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@


def resize_video_frames(images, size):
resized_images = []
for image in images:
resized_images.append(
tf.to_int64(
tf.image.resize_images(image, [size, size],
tf.image.ResizeMethod.BILINEAR)))
return resized_images
return [tf.to_int64(tf.image.resize_images(
image, [size, size], tf.image.ResizeMethod.BILINEAR)) for image in images]


def video_augmentation(features, hue=False, saturate=False, contrast=False):
Expand Down
12 changes: 5 additions & 7 deletions tensor2tensor/envs/env_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,11 @@ def initialize_environments(self, batch_size=1, max_episode_steps=-1,
assert batch_size >= 1
self._batch_size = batch_size

self._envs = []
for _ in range(batch_size):
self._envs.append(
gym_utils.make_gym_env(
self.base_env_name,
rl_env_max_episode_steps=max_episode_steps,
maxskip_env=max_and_skip_env))
self._envs = [
gym_utils.make_gym_env(
self.base_env_name,
rl_env_max_episode_steps=max_episode_steps,
maxskip_env=max_and_skip_env) for _ in range(batch_size)]

# If self.observation_space and self.action_space aren't None, then it means
# that this is a re-initialization of this class, in that case make sure
Expand Down
12 changes: 5 additions & 7 deletions tensor2tensor/insights/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,11 @@ def list_models(): # pylint: disable=unused-variable
Returns:
JSON for the supported models.
"""
configuration_list = []
for source_code, target_code, label in processors:
configuration_list.append({
"id": label,
"source_language": languages[source_code],
"target_language": languages[target_code],
})
configuration_list = [{
"id": label,
"source_language": languages[source_code],
"target_language": languages[target_code],
} for source_code, target_code, label in processors]
return jsonify({
"configuration": configuration_list
})
Expand Down
14 changes: 5 additions & 9 deletions tensor2tensor/layers/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,8 @@ def bit_to_int(x_bit, num_bits, base=2):
Integer representation of this number.
"""
x_l = tf.stop_gradient(tf.to_int32(tf.reshape(x_bit, [-1, num_bits])))
x_labels = []
for i in range(num_bits):
x_labels.append(x_l[:, i] * tf.to_int32(base)**tf.to_int32(i))
x_labels = [
x_l[:, i] * tf.to_int32(base)**tf.to_int32(i) for i in range(num_bits)]
res = sum(x_labels)
return tf.to_int32(tf.reshape(res, common_layers.shape_list(x_bit)[:-1]))

Expand All @@ -254,12 +253,9 @@ def int_to_bit(x_int, num_bits, base=2):
Corresponding number expressed in base.
"""
x_l = tf.to_int32(tf.expand_dims(x_int, axis=-1))
x_labels = []
for i in range(num_bits):
x_labels.append(
tf.floormod(
tf.floordiv(tf.to_int32(x_l),
tf.to_int32(base)**i), tf.to_int32(base)))
x_labels = [tf.floormod(
tf.floordiv(tf.to_int32(x_l), tf.to_int32(base)**i), tf.to_int32(base))
for i in range(num_bits)]
res = tf.concat(x_labels, axis=-1)
return tf.to_float(res)

Expand Down
16 changes: 7 additions & 9 deletions tensor2tensor/layers/vq_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ def bit_to_int(self, x_bit, num_bits, base=2):
Integer representation of this number.
"""
x_l = tf.stop_gradient(tf.to_int32(tf.reshape(x_bit, [-1, num_bits])))
x_labels = []
for i in range(num_bits):
x_labels.append(x_l[:, i] * tf.to_int32(base)**tf.to_int32(i))
x_labels = [
x_l[:, i] * tf.to_int32(base)**tf.to_int32(i) for i in range(num_bits)]
res = sum(x_labels)
return tf.to_int32(tf.reshape(res, common_layers.shape_list(x_bit)[:-1]))

Expand All @@ -177,12 +176,11 @@ def int_to_bit(self, x_int, num_bits, base=2):
Corresponding number expressed in base.
"""
x_l = tf.to_int32(tf.expand_dims(x_int, axis=-1))
x_labels = []
for i in range(num_bits):
x_labels.append(
tf.floormod(
tf.floordiv(tf.to_int32(x_l),
tf.to_int32(base)**i), tf.to_int32(base)))
x_labels = [
tf.floormod(
tf.floordiv(tf.to_int32(x_l),
tf.to_int32(base)**i), tf.to_int32(base))
for i in range(num_bits)]
res = tf.concat(x_labels, axis=-1)
return tf.to_float(res)

Expand Down
7 changes: 3 additions & 4 deletions tensor2tensor/models/video/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,9 @@ def get_scheduled_sample_inputs(self,
def sample():
"""Calculate the scheduled sampling params based on iteration number."""
with tf.variable_scope("scheduled_sampling", reuse=tf.AUTO_REUSE):
output_items = []
for item_gt, item_gen in zip(groundtruth_items, generated_items):
output_items.append(scheduled_sampling_func(item_gt, item_gen))
return output_items
return [
scheduled_sampling_func(item_gt, item_gen)
for item_gt, item_gen in zip(groundtruth_items, generated_items)]

cases = [
(tf.logical_not(done_warm_start), lambda: groundtruth_items),
Expand Down
9 changes: 3 additions & 6 deletions tensor2tensor/rl/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,9 @@ def get_keys_to_action(self):
keys_to_action = {}

for action_id, action_meaning in enumerate(self.action_meanings):
keys = []
for keyword, key in keyword_to_key.items():
if keyword in action_meaning:
keys.append(key)
keys_tuple = tuple(sorted(keys))
del keys
keys_tuple = tuple(sorted([
key for keyword, key in keyword_to_key.items()
if keyword in action_meaning]))
assert keys_tuple not in keys_to_action
keys_to_action[keys_tuple] = action_id

Expand Down
4 changes: 1 addition & 3 deletions tensor2tensor/utils/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,7 @@ def _pad_batch(features, batch_multiple):
padded_features = {}
for k, feature in features.items():
rank = len(feature.shape)
paddings = []
for _ in range(rank):
paddings.append([0, 0])
paddings = [[0, 0] for _ in range(rank)]
paddings[0][1] = batch_padding
padded_feature = tf.pad(feature, paddings)
padded_features[k] = padded_feature
Expand Down
6 changes: 1 addition & 5 deletions tensor2tensor/visualization/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ def get_out_out_attention(layer):

def get_attentions(get_attention_fn):
num_layers = len(enc_atts)
attentions = []
for i in range(num_layers):
attentions.append(get_attention_fn(i))

return attentions
return [get_attention_fn(i) for i in range(num_layers)]

attentions = {
'all': {
Expand Down