repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
tensorflow/tensor2tensor
tensor2tensor/layers/transformer_memory.py
TransformerMemory.read
def read(self, x): """Read from the memory. An external component can use the results via a simple MLP, e.g., fn(x W_x + retrieved_mem W_m). Args: x: a tensor in the shape of [batch_size, length, depth]. Returns: access_logits: the logits for accessing the memory in shape of [batch_size, length, memory_size]. retrieved_mem: the retrieved results in the shape of [batch_size, length, val_depth]. """ access_logits = self._address_content(x) weights = tf.nn.softmax(access_logits) retrieved_mem = tf.reduce_sum( tf.multiply(tf.expand_dims(weights, 3), tf.expand_dims(self.mem_vals, axis=1)), axis=2) return access_logits, retrieved_mem
python
def read(self, x): """Read from the memory. An external component can use the results via a simple MLP, e.g., fn(x W_x + retrieved_mem W_m). Args: x: a tensor in the shape of [batch_size, length, depth]. Returns: access_logits: the logits for accessing the memory in shape of [batch_size, length, memory_size]. retrieved_mem: the retrieved results in the shape of [batch_size, length, val_depth]. """ access_logits = self._address_content(x) weights = tf.nn.softmax(access_logits) retrieved_mem = tf.reduce_sum( tf.multiply(tf.expand_dims(weights, 3), tf.expand_dims(self.mem_vals, axis=1)), axis=2) return access_logits, retrieved_mem
[ "def", "read", "(", "self", ",", "x", ")", ":", "access_logits", "=", "self", ".", "_address_content", "(", "x", ")", "weights", "=", "tf", ".", "nn", ".", "softmax", "(", "access_logits", ")", "retrieved_mem", "=", "tf", ".", "reduce_sum", "(", "tf", ".", "multiply", "(", "tf", ".", "expand_dims", "(", "weights", ",", "3", ")", ",", "tf", ".", "expand_dims", "(", "self", ".", "mem_vals", ",", "axis", "=", "1", ")", ")", ",", "axis", "=", "2", ")", "return", "access_logits", ",", "retrieved_mem" ]
Read from the memory. An external component can use the results via a simple MLP, e.g., fn(x W_x + retrieved_mem W_m). Args: x: a tensor in the shape of [batch_size, length, depth]. Returns: access_logits: the logits for accessing the memory in shape of [batch_size, length, memory_size]. retrieved_mem: the retrieved results in the shape of [batch_size, length, val_depth].
[ "Read", "from", "the", "memory", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/transformer_memory.py#L251-L270
train
tensorflow/tensor2tensor
tensor2tensor/layers/transformer_memory.py
TransformerMemory.write
def write(self, x, access_logits): """Write to the memory based on a combination of similarity and least used. Based on arXiv:1607.00036v2 [cs.LG]. Args: x: a tensor in the shape of [batch_size, length, depth]. access_logits: the logits for accessing the memory. Returns: the update op. """ gamma = tf.layers.dense(x, 1, activation=tf.sigmoid, name="gamma") write_logits = access_logits - gamma * tf.expand_dims(self.mean_logits, 1) candidate_value = tf.layers.dense(x, self.val_depth, activation=tf.nn.relu, name="candidate_value") erase_gates = tf.layers.dense(x, self.memory_size, activation=tf.nn.sigmoid, name="erase") write_weights = tf.nn.softmax(write_logits) erase_weights = tf.expand_dims(1 - erase_gates * write_weights, 3) erase = tf.multiply(erase_weights, tf.expand_dims(self.mem_vals, 1)) addition = tf.multiply( tf.expand_dims(write_weights, 3), tf.expand_dims(candidate_value, 2)) update_value_op = self.mem_vals.assign( tf.reduce_mean(erase + addition, axis=1)) with tf.control_dependencies([update_value_op]): write_op = self.mean_logits.assign( self.mean_logits * 0.1 + tf.reduce_mean(write_logits * 0.9, axis=1)) return write_op
python
def write(self, x, access_logits): """Write to the memory based on a combination of similarity and least used. Based on arXiv:1607.00036v2 [cs.LG]. Args: x: a tensor in the shape of [batch_size, length, depth]. access_logits: the logits for accessing the memory. Returns: the update op. """ gamma = tf.layers.dense(x, 1, activation=tf.sigmoid, name="gamma") write_logits = access_logits - gamma * tf.expand_dims(self.mean_logits, 1) candidate_value = tf.layers.dense(x, self.val_depth, activation=tf.nn.relu, name="candidate_value") erase_gates = tf.layers.dense(x, self.memory_size, activation=tf.nn.sigmoid, name="erase") write_weights = tf.nn.softmax(write_logits) erase_weights = tf.expand_dims(1 - erase_gates * write_weights, 3) erase = tf.multiply(erase_weights, tf.expand_dims(self.mem_vals, 1)) addition = tf.multiply( tf.expand_dims(write_weights, 3), tf.expand_dims(candidate_value, 2)) update_value_op = self.mem_vals.assign( tf.reduce_mean(erase + addition, axis=1)) with tf.control_dependencies([update_value_op]): write_op = self.mean_logits.assign( self.mean_logits * 0.1 + tf.reduce_mean(write_logits * 0.9, axis=1)) return write_op
[ "def", "write", "(", "self", ",", "x", ",", "access_logits", ")", ":", "gamma", "=", "tf", ".", "layers", ".", "dense", "(", "x", ",", "1", ",", "activation", "=", "tf", ".", "sigmoid", ",", "name", "=", "\"gamma\"", ")", "write_logits", "=", "access_logits", "-", "gamma", "*", "tf", ".", "expand_dims", "(", "self", ".", "mean_logits", ",", "1", ")", "candidate_value", "=", "tf", ".", "layers", ".", "dense", "(", "x", ",", "self", ".", "val_depth", ",", "activation", "=", "tf", ".", "nn", ".", "relu", ",", "name", "=", "\"candidate_value\"", ")", "erase_gates", "=", "tf", ".", "layers", ".", "dense", "(", "x", ",", "self", ".", "memory_size", ",", "activation", "=", "tf", ".", "nn", ".", "sigmoid", ",", "name", "=", "\"erase\"", ")", "write_weights", "=", "tf", ".", "nn", ".", "softmax", "(", "write_logits", ")", "erase_weights", "=", "tf", ".", "expand_dims", "(", "1", "-", "erase_gates", "*", "write_weights", ",", "3", ")", "erase", "=", "tf", ".", "multiply", "(", "erase_weights", ",", "tf", ".", "expand_dims", "(", "self", ".", "mem_vals", ",", "1", ")", ")", "addition", "=", "tf", ".", "multiply", "(", "tf", ".", "expand_dims", "(", "write_weights", ",", "3", ")", ",", "tf", ".", "expand_dims", "(", "candidate_value", ",", "2", ")", ")", "update_value_op", "=", "self", ".", "mem_vals", ".", "assign", "(", "tf", ".", "reduce_mean", "(", "erase", "+", "addition", ",", "axis", "=", "1", ")", ")", "with", "tf", ".", "control_dependencies", "(", "[", "update_value_op", "]", ")", ":", "write_op", "=", "self", ".", "mean_logits", ".", "assign", "(", "self", ".", "mean_logits", "*", "0.1", "+", "tf", ".", "reduce_mean", "(", "write_logits", "*", "0.9", ",", "axis", "=", "1", ")", ")", "return", "write_op" ]
Write to the memory based on a combination of similarity and least used. Based on arXiv:1607.00036v2 [cs.LG]. Args: x: a tensor in the shape of [batch_size, length, depth]. access_logits: the logits for accessing the memory. Returns: the update op.
[ "Write", "to", "the", "memory", "based", "on", "a", "combination", "of", "similarity", "and", "least", "used", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/transformer_memory.py#L272-L303
train
tensorflow/tensor2tensor
tensor2tensor/layers/transformer_memory.py
TransformerMemory.reset
def reset(self, entries_to_reset): """Reset the entries in the memory. Args: entries_to_reset: a 1D tensor. Returns: the reset op. """ num_updates = tf.size(entries_to_reset) update_vals = tf.scatter_update( self.mem_vals, entries_to_reset, tf.tile(tf.expand_dims( tf.fill([self.memory_size, self.val_depth], .0), 0), [num_updates, 1, 1])) update_logits = tf.scatter_update( self.mean_logits, entries_to_reset, tf.tile(tf.expand_dims( tf.fill([self.memory_size], .0), 0), [num_updates, 1])) reset_op = tf.group([update_vals, update_logits]) return reset_op
python
def reset(self, entries_to_reset): """Reset the entries in the memory. Args: entries_to_reset: a 1D tensor. Returns: the reset op. """ num_updates = tf.size(entries_to_reset) update_vals = tf.scatter_update( self.mem_vals, entries_to_reset, tf.tile(tf.expand_dims( tf.fill([self.memory_size, self.val_depth], .0), 0), [num_updates, 1, 1])) update_logits = tf.scatter_update( self.mean_logits, entries_to_reset, tf.tile(tf.expand_dims( tf.fill([self.memory_size], .0), 0), [num_updates, 1])) reset_op = tf.group([update_vals, update_logits]) return reset_op
[ "def", "reset", "(", "self", ",", "entries_to_reset", ")", ":", "num_updates", "=", "tf", ".", "size", "(", "entries_to_reset", ")", "update_vals", "=", "tf", ".", "scatter_update", "(", "self", ".", "mem_vals", ",", "entries_to_reset", ",", "tf", ".", "tile", "(", "tf", ".", "expand_dims", "(", "tf", ".", "fill", "(", "[", "self", ".", "memory_size", ",", "self", ".", "val_depth", "]", ",", ".0", ")", ",", "0", ")", ",", "[", "num_updates", ",", "1", ",", "1", "]", ")", ")", "update_logits", "=", "tf", ".", "scatter_update", "(", "self", ".", "mean_logits", ",", "entries_to_reset", ",", "tf", ".", "tile", "(", "tf", ".", "expand_dims", "(", "tf", ".", "fill", "(", "[", "self", ".", "memory_size", "]", ",", ".0", ")", ",", "0", ")", ",", "[", "num_updates", ",", "1", "]", ")", ")", "reset_op", "=", "tf", ".", "group", "(", "[", "update_vals", ",", "update_logits", "]", ")", "return", "reset_op" ]
Reset the entries in the memory. Args: entries_to_reset: a 1D tensor. Returns: the reset op.
[ "Reset", "the", "entries", "in", "the", "memory", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/transformer_memory.py#L317-L337
train
tensorflow/tensor2tensor
tensor2tensor/layers/transformer_memory.py
TransformerMemory.pre_attention
def pre_attention(self, segment_number, query_antecedent, memory_antecedent, bias): """Called prior to self-attention, to incorporate memory items. Args: segment_number: an integer Tensor with shape [batch] query_antecedent: a Tensor with shape [batch, length_q, channels] memory_antecedent: must be None. Attention normally allows this to be a Tensor with shape [batch, length_m, channels], but we currently only support memory for decoder-side self-attention. bias: bias Tensor (see attention_bias()) Returns: (data, new_query_antecedent, new_memory_antecedent, new_bias) """ with tf.variable_scope(self.name + "/pre_attention", reuse=tf.AUTO_REUSE): assert memory_antecedent is None, "We only support language modeling" with tf.control_dependencies([ tf.assert_greater_equal(self.batch_size, tf.size(segment_number))]): difference = self.batch_size - tf.size(segment_number) segment_number = tf.pad(segment_number, [[0, difference]]) reset_op = self.reset(tf.reshape(tf.where( tf.less(segment_number, self.segment_number)), [-1])) memory_results = {} with tf.control_dependencies([reset_op]): with tf.control_dependencies([ self.update_segment_number(segment_number)]): x = tf.pad(query_antecedent, [ [0, difference], [0, 0], [0, 0]]) access_logits, retrieved_mem = self.read(x) memory_results["x"] = x memory_results["access_logits"] = access_logits memory_results["retrieved_mem"] = retrieved_mem return memory_results, query_antecedent, memory_antecedent, bias
python
def pre_attention(self, segment_number, query_antecedent, memory_antecedent, bias): """Called prior to self-attention, to incorporate memory items. Args: segment_number: an integer Tensor with shape [batch] query_antecedent: a Tensor with shape [batch, length_q, channels] memory_antecedent: must be None. Attention normally allows this to be a Tensor with shape [batch, length_m, channels], but we currently only support memory for decoder-side self-attention. bias: bias Tensor (see attention_bias()) Returns: (data, new_query_antecedent, new_memory_antecedent, new_bias) """ with tf.variable_scope(self.name + "/pre_attention", reuse=tf.AUTO_REUSE): assert memory_antecedent is None, "We only support language modeling" with tf.control_dependencies([ tf.assert_greater_equal(self.batch_size, tf.size(segment_number))]): difference = self.batch_size - tf.size(segment_number) segment_number = tf.pad(segment_number, [[0, difference]]) reset_op = self.reset(tf.reshape(tf.where( tf.less(segment_number, self.segment_number)), [-1])) memory_results = {} with tf.control_dependencies([reset_op]): with tf.control_dependencies([ self.update_segment_number(segment_number)]): x = tf.pad(query_antecedent, [ [0, difference], [0, 0], [0, 0]]) access_logits, retrieved_mem = self.read(x) memory_results["x"] = x memory_results["access_logits"] = access_logits memory_results["retrieved_mem"] = retrieved_mem return memory_results, query_antecedent, memory_antecedent, bias
[ "def", "pre_attention", "(", "self", ",", "segment_number", ",", "query_antecedent", ",", "memory_antecedent", ",", "bias", ")", ":", "with", "tf", ".", "variable_scope", "(", "self", ".", "name", "+", "\"/pre_attention\"", ",", "reuse", "=", "tf", ".", "AUTO_REUSE", ")", ":", "assert", "memory_antecedent", "is", "None", ",", "\"We only support language modeling\"", "with", "tf", ".", "control_dependencies", "(", "[", "tf", ".", "assert_greater_equal", "(", "self", ".", "batch_size", ",", "tf", ".", "size", "(", "segment_number", ")", ")", "]", ")", ":", "difference", "=", "self", ".", "batch_size", "-", "tf", ".", "size", "(", "segment_number", ")", "segment_number", "=", "tf", ".", "pad", "(", "segment_number", ",", "[", "[", "0", ",", "difference", "]", "]", ")", "reset_op", "=", "self", ".", "reset", "(", "tf", ".", "reshape", "(", "tf", ".", "where", "(", "tf", ".", "less", "(", "segment_number", ",", "self", ".", "segment_number", ")", ")", ",", "[", "-", "1", "]", ")", ")", "memory_results", "=", "{", "}", "with", "tf", ".", "control_dependencies", "(", "[", "reset_op", "]", ")", ":", "with", "tf", ".", "control_dependencies", "(", "[", "self", ".", "update_segment_number", "(", "segment_number", ")", "]", ")", ":", "x", "=", "tf", ".", "pad", "(", "query_antecedent", ",", "[", "[", "0", ",", "difference", "]", ",", "[", "0", ",", "0", "]", ",", "[", "0", ",", "0", "]", "]", ")", "access_logits", ",", "retrieved_mem", "=", "self", ".", "read", "(", "x", ")", "memory_results", "[", "\"x\"", "]", "=", "x", "memory_results", "[", "\"access_logits\"", "]", "=", "access_logits", "memory_results", "[", "\"retrieved_mem\"", "]", "=", "retrieved_mem", "return", "memory_results", ",", "query_antecedent", ",", "memory_antecedent", ",", "bias" ]
Called prior to self-attention, to incorporate memory items. Args: segment_number: an integer Tensor with shape [batch] query_antecedent: a Tensor with shape [batch, length_q, channels] memory_antecedent: must be None. Attention normally allows this to be a Tensor with shape [batch, length_m, channels], but we currently only support memory for decoder-side self-attention. bias: bias Tensor (see attention_bias()) Returns: (data, new_query_antecedent, new_memory_antecedent, new_bias)
[ "Called", "prior", "to", "self", "-", "attention", "to", "incorporate", "memory", "items", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/transformer_memory.py#L339-L371
train
tensorflow/tensor2tensor
tensor2tensor/layers/transformer_memory.py
TransformerMemory.post_attention
def post_attention(self, token, x): """Called after self-attention. The memory can be updated here. Args: token: Data returned by pre_attention, which can be used to carry over state related to the current memory operation. x: a Tensor of data after self-attention and feed-forward Returns: a (possibly modified) version of the input x """ with tf.variable_scope(self.name + "/post_attention", reuse=tf.AUTO_REUSE): depth = common_layers.shape_list(x)[-1] actual_batch_size = common_layers.shape_list(x)[0] memory_output = tf.gather(token["retrieved_mem"], tf.range(actual_batch_size)) output = tf.add(tf.layers.dense(x, depth, use_bias=False), tf.layers.dense(memory_output, depth)) with tf.control_dependencies([output]): with tf.control_dependencies([ self.write(token["x"], token["access_logits"])]): return tf.identity(output)
python
def post_attention(self, token, x): """Called after self-attention. The memory can be updated here. Args: token: Data returned by pre_attention, which can be used to carry over state related to the current memory operation. x: a Tensor of data after self-attention and feed-forward Returns: a (possibly modified) version of the input x """ with tf.variable_scope(self.name + "/post_attention", reuse=tf.AUTO_REUSE): depth = common_layers.shape_list(x)[-1] actual_batch_size = common_layers.shape_list(x)[0] memory_output = tf.gather(token["retrieved_mem"], tf.range(actual_batch_size)) output = tf.add(tf.layers.dense(x, depth, use_bias=False), tf.layers.dense(memory_output, depth)) with tf.control_dependencies([output]): with tf.control_dependencies([ self.write(token["x"], token["access_logits"])]): return tf.identity(output)
[ "def", "post_attention", "(", "self", ",", "token", ",", "x", ")", ":", "with", "tf", ".", "variable_scope", "(", "self", ".", "name", "+", "\"/post_attention\"", ",", "reuse", "=", "tf", ".", "AUTO_REUSE", ")", ":", "depth", "=", "common_layers", ".", "shape_list", "(", "x", ")", "[", "-", "1", "]", "actual_batch_size", "=", "common_layers", ".", "shape_list", "(", "x", ")", "[", "0", "]", "memory_output", "=", "tf", ".", "gather", "(", "token", "[", "\"retrieved_mem\"", "]", ",", "tf", ".", "range", "(", "actual_batch_size", ")", ")", "output", "=", "tf", ".", "add", "(", "tf", ".", "layers", ".", "dense", "(", "x", ",", "depth", ",", "use_bias", "=", "False", ")", ",", "tf", ".", "layers", ".", "dense", "(", "memory_output", ",", "depth", ")", ")", "with", "tf", ".", "control_dependencies", "(", "[", "output", "]", ")", ":", "with", "tf", ".", "control_dependencies", "(", "[", "self", ".", "write", "(", "token", "[", "\"x\"", "]", ",", "token", "[", "\"access_logits\"", "]", ")", "]", ")", ":", "return", "tf", ".", "identity", "(", "output", ")" ]
Called after self-attention. The memory can be updated here. Args: token: Data returned by pre_attention, which can be used to carry over state related to the current memory operation. x: a Tensor of data after self-attention and feed-forward Returns: a (possibly modified) version of the input x
[ "Called", "after", "self", "-", "attention", ".", "The", "memory", "can", "be", "updated", "here", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/transformer_memory.py#L373-L393
train
tensorflow/tensor2tensor
tensor2tensor/rl/ppo_learner.py
_define_train
def _define_train( train_env, ppo_hparams, eval_env_fn=None, sampling_temp=1.0, **collect_kwargs ): """Define the training setup.""" memory, collect_summary, train_initialization = ( _define_collect( train_env, ppo_hparams, "ppo_train", eval_phase=False, sampling_temp=sampling_temp, **collect_kwargs)) ppo_summary = ppo.define_ppo_epoch( memory, ppo_hparams, train_env.action_space, train_env.batch_size) train_summary = tf.summary.merge([collect_summary, ppo_summary]) if ppo_hparams.eval_every_epochs: # TODO(koz4k): Do we need this at all? assert eval_env_fn is not None eval_env = eval_env_fn(in_graph=True) (_, eval_collect_summary, eval_initialization) = ( _define_collect( eval_env, ppo_hparams, "ppo_eval", eval_phase=True, sampling_temp=0.0, **collect_kwargs)) return (train_summary, eval_collect_summary, (train_initialization, eval_initialization)) else: return (train_summary, None, (train_initialization,))
python
def _define_train( train_env, ppo_hparams, eval_env_fn=None, sampling_temp=1.0, **collect_kwargs ): """Define the training setup.""" memory, collect_summary, train_initialization = ( _define_collect( train_env, ppo_hparams, "ppo_train", eval_phase=False, sampling_temp=sampling_temp, **collect_kwargs)) ppo_summary = ppo.define_ppo_epoch( memory, ppo_hparams, train_env.action_space, train_env.batch_size) train_summary = tf.summary.merge([collect_summary, ppo_summary]) if ppo_hparams.eval_every_epochs: # TODO(koz4k): Do we need this at all? assert eval_env_fn is not None eval_env = eval_env_fn(in_graph=True) (_, eval_collect_summary, eval_initialization) = ( _define_collect( eval_env, ppo_hparams, "ppo_eval", eval_phase=True, sampling_temp=0.0, **collect_kwargs)) return (train_summary, eval_collect_summary, (train_initialization, eval_initialization)) else: return (train_summary, None, (train_initialization,))
[ "def", "_define_train", "(", "train_env", ",", "ppo_hparams", ",", "eval_env_fn", "=", "None", ",", "sampling_temp", "=", "1.0", ",", "*", "*", "collect_kwargs", ")", ":", "memory", ",", "collect_summary", ",", "train_initialization", "=", "(", "_define_collect", "(", "train_env", ",", "ppo_hparams", ",", "\"ppo_train\"", ",", "eval_phase", "=", "False", ",", "sampling_temp", "=", "sampling_temp", ",", "*", "*", "collect_kwargs", ")", ")", "ppo_summary", "=", "ppo", ".", "define_ppo_epoch", "(", "memory", ",", "ppo_hparams", ",", "train_env", ".", "action_space", ",", "train_env", ".", "batch_size", ")", "train_summary", "=", "tf", ".", "summary", ".", "merge", "(", "[", "collect_summary", ",", "ppo_summary", "]", ")", "if", "ppo_hparams", ".", "eval_every_epochs", ":", "# TODO(koz4k): Do we need this at all?", "assert", "eval_env_fn", "is", "not", "None", "eval_env", "=", "eval_env_fn", "(", "in_graph", "=", "True", ")", "(", "_", ",", "eval_collect_summary", ",", "eval_initialization", ")", "=", "(", "_define_collect", "(", "eval_env", ",", "ppo_hparams", ",", "\"ppo_eval\"", ",", "eval_phase", "=", "True", ",", "sampling_temp", "=", "0.0", ",", "*", "*", "collect_kwargs", ")", ")", "return", "(", "train_summary", ",", "eval_collect_summary", ",", "(", "train_initialization", ",", "eval_initialization", ")", ")", "else", ":", "return", "(", "train_summary", ",", "None", ",", "(", "train_initialization", ",", ")", ")" ]
Define the training setup.
[ "Define", "the", "training", "setup", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/ppo_learner.py#L151-L186
train
tensorflow/tensor2tensor
tensor2tensor/rl/ppo_learner.py
_run_train
def _run_train(ppo_hparams, event_dir, model_dir, restarter, train_summary_op, eval_summary_op, initializers, report_fn=None, model_save_fn=None): """Train.""" summary_writer = tf.summary.FileWriter( event_dir, graph=tf.get_default_graph(), flush_secs=60) model_saver = tf.train.Saver( tf.global_variables(ppo_hparams.policy_network + "/.*") + tf.global_variables("training/" + ppo_hparams.policy_network + "/.*") + # tf.global_variables("clean_scope.*") + # Needed for sharing params. tf.global_variables("global_step") + tf.global_variables("losses_avg.*") + tf.global_variables("train_stats.*") ) global_step = tf.train.get_or_create_global_step() with tf.control_dependencies([tf.assign_add(global_step, 1)]): train_summary_op = tf.identity(train_summary_op) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for initializer in initializers: initializer(sess) trainer_lib.restore_checkpoint(model_dir, model_saver, sess) num_target_iterations = restarter.target_local_step num_completed_iterations = num_target_iterations - restarter.steps_to_go with restarter.training_loop(): for epoch_index in range(num_completed_iterations, num_target_iterations): summary = sess.run(train_summary_op) if summary_writer: summary_writer.add_summary(summary, epoch_index) if (ppo_hparams.eval_every_epochs and epoch_index % ppo_hparams.eval_every_epochs == 0): eval_summary = sess.run(eval_summary_op) if summary_writer: summary_writer.add_summary(eval_summary, epoch_index) if report_fn: summary_proto = tf.Summary() summary_proto.ParseFromString(eval_summary) for elem in summary_proto.value: if "mean_score" in elem.tag: report_fn(elem.simple_value, epoch_index) break if (model_saver and ppo_hparams.save_models_every_epochs and (epoch_index % ppo_hparams.save_models_every_epochs == 0 or (epoch_index + 1) == num_target_iterations)): ckpt_path = os.path.join( model_dir, "model.ckpt-{}".format(tf.train.global_step(sess, global_step)) ) model_saver.save(sess, ckpt_path) if model_save_fn: model_save_fn(model_dir)
python
def _run_train(ppo_hparams, event_dir, model_dir, restarter, train_summary_op, eval_summary_op, initializers, report_fn=None, model_save_fn=None): """Train.""" summary_writer = tf.summary.FileWriter( event_dir, graph=tf.get_default_graph(), flush_secs=60) model_saver = tf.train.Saver( tf.global_variables(ppo_hparams.policy_network + "/.*") + tf.global_variables("training/" + ppo_hparams.policy_network + "/.*") + # tf.global_variables("clean_scope.*") + # Needed for sharing params. tf.global_variables("global_step") + tf.global_variables("losses_avg.*") + tf.global_variables("train_stats.*") ) global_step = tf.train.get_or_create_global_step() with tf.control_dependencies([tf.assign_add(global_step, 1)]): train_summary_op = tf.identity(train_summary_op) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for initializer in initializers: initializer(sess) trainer_lib.restore_checkpoint(model_dir, model_saver, sess) num_target_iterations = restarter.target_local_step num_completed_iterations = num_target_iterations - restarter.steps_to_go with restarter.training_loop(): for epoch_index in range(num_completed_iterations, num_target_iterations): summary = sess.run(train_summary_op) if summary_writer: summary_writer.add_summary(summary, epoch_index) if (ppo_hparams.eval_every_epochs and epoch_index % ppo_hparams.eval_every_epochs == 0): eval_summary = sess.run(eval_summary_op) if summary_writer: summary_writer.add_summary(eval_summary, epoch_index) if report_fn: summary_proto = tf.Summary() summary_proto.ParseFromString(eval_summary) for elem in summary_proto.value: if "mean_score" in elem.tag: report_fn(elem.simple_value, epoch_index) break if (model_saver and ppo_hparams.save_models_every_epochs and (epoch_index % ppo_hparams.save_models_every_epochs == 0 or (epoch_index + 1) == num_target_iterations)): ckpt_path = os.path.join( model_dir, "model.ckpt-{}".format(tf.train.global_step(sess, global_step)) ) model_saver.save(sess, ckpt_path) if model_save_fn: model_save_fn(model_dir)
[ "def", "_run_train", "(", "ppo_hparams", ",", "event_dir", ",", "model_dir", ",", "restarter", ",", "train_summary_op", ",", "eval_summary_op", ",", "initializers", ",", "report_fn", "=", "None", ",", "model_save_fn", "=", "None", ")", ":", "summary_writer", "=", "tf", ".", "summary", ".", "FileWriter", "(", "event_dir", ",", "graph", "=", "tf", ".", "get_default_graph", "(", ")", ",", "flush_secs", "=", "60", ")", "model_saver", "=", "tf", ".", "train", ".", "Saver", "(", "tf", ".", "global_variables", "(", "ppo_hparams", ".", "policy_network", "+", "\"/.*\"", ")", "+", "tf", ".", "global_variables", "(", "\"training/\"", "+", "ppo_hparams", ".", "policy_network", "+", "\"/.*\"", ")", "+", "# tf.global_variables(\"clean_scope.*\") + # Needed for sharing params.", "tf", ".", "global_variables", "(", "\"global_step\"", ")", "+", "tf", ".", "global_variables", "(", "\"losses_avg.*\"", ")", "+", "tf", ".", "global_variables", "(", "\"train_stats.*\"", ")", ")", "global_step", "=", "tf", ".", "train", ".", "get_or_create_global_step", "(", ")", "with", "tf", ".", "control_dependencies", "(", "[", "tf", ".", "assign_add", "(", "global_step", ",", "1", ")", "]", ")", ":", "train_summary_op", "=", "tf", ".", "identity", "(", "train_summary_op", ")", "with", "tf", ".", "Session", "(", ")", "as", "sess", ":", "sess", ".", "run", "(", "tf", ".", "global_variables_initializer", "(", ")", ")", "for", "initializer", "in", "initializers", ":", "initializer", "(", "sess", ")", "trainer_lib", ".", "restore_checkpoint", "(", "model_dir", ",", "model_saver", ",", "sess", ")", "num_target_iterations", "=", "restarter", ".", "target_local_step", "num_completed_iterations", "=", "num_target_iterations", "-", "restarter", ".", "steps_to_go", "with", "restarter", ".", "training_loop", "(", ")", ":", "for", "epoch_index", "in", "range", "(", "num_completed_iterations", ",", "num_target_iterations", ")", ":", "summary", "=", "sess", ".", "run", "(", "train_summary_op", ")", "if", "summary_writer", ":", "summary_writer", ".", "add_summary", "(", "summary", ",", "epoch_index", ")", "if", "(", "ppo_hparams", ".", "eval_every_epochs", "and", "epoch_index", "%", "ppo_hparams", ".", "eval_every_epochs", "==", "0", ")", ":", "eval_summary", "=", "sess", ".", "run", "(", "eval_summary_op", ")", "if", "summary_writer", ":", "summary_writer", ".", "add_summary", "(", "eval_summary", ",", "epoch_index", ")", "if", "report_fn", ":", "summary_proto", "=", "tf", ".", "Summary", "(", ")", "summary_proto", ".", "ParseFromString", "(", "eval_summary", ")", "for", "elem", "in", "summary_proto", ".", "value", ":", "if", "\"mean_score\"", "in", "elem", ".", "tag", ":", "report_fn", "(", "elem", ".", "simple_value", ",", "epoch_index", ")", "break", "if", "(", "model_saver", "and", "ppo_hparams", ".", "save_models_every_epochs", "and", "(", "epoch_index", "%", "ppo_hparams", ".", "save_models_every_epochs", "==", "0", "or", "(", "epoch_index", "+", "1", ")", "==", "num_target_iterations", ")", ")", ":", "ckpt_path", "=", "os", ".", "path", ".", "join", "(", "model_dir", ",", "\"model.ckpt-{}\"", ".", "format", "(", "tf", ".", "train", ".", "global_step", "(", "sess", ",", "global_step", ")", ")", ")", "model_saver", ".", "save", "(", "sess", ",", "ckpt_path", ")", "if", "model_save_fn", ":", "model_save_fn", "(", "model_dir", ")" ]
Train.
[ "Train", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/ppo_learner.py#L189-L251
train
tensorflow/tensor2tensor
tensor2tensor/rl/ppo_learner.py
_rollout_metadata
def _rollout_metadata(batch_env): """Metadata for rollouts.""" batch_env_shape = batch_env.observ.get_shape().as_list() batch_size = [batch_env_shape[0]] shapes_types_names = [ # TODO(piotrmilos): possibly retrieve the observation type for batch_env (batch_size + batch_env_shape[1:], batch_env.observ_dtype, "observation"), (batch_size, tf.float32, "reward"), (batch_size, tf.bool, "done"), (batch_size + list(batch_env.action_shape), batch_env.action_dtype, "action"), (batch_size, tf.float32, "pdf"), (batch_size, tf.float32, "value_function"), ] return shapes_types_names
python
def _rollout_metadata(batch_env): """Metadata for rollouts.""" batch_env_shape = batch_env.observ.get_shape().as_list() batch_size = [batch_env_shape[0]] shapes_types_names = [ # TODO(piotrmilos): possibly retrieve the observation type for batch_env (batch_size + batch_env_shape[1:], batch_env.observ_dtype, "observation"), (batch_size, tf.float32, "reward"), (batch_size, tf.bool, "done"), (batch_size + list(batch_env.action_shape), batch_env.action_dtype, "action"), (batch_size, tf.float32, "pdf"), (batch_size, tf.float32, "value_function"), ] return shapes_types_names
[ "def", "_rollout_metadata", "(", "batch_env", ")", ":", "batch_env_shape", "=", "batch_env", ".", "observ", ".", "get_shape", "(", ")", ".", "as_list", "(", ")", "batch_size", "=", "[", "batch_env_shape", "[", "0", "]", "]", "shapes_types_names", "=", "[", "# TODO(piotrmilos): possibly retrieve the observation type for batch_env", "(", "batch_size", "+", "batch_env_shape", "[", "1", ":", "]", ",", "batch_env", ".", "observ_dtype", ",", "\"observation\"", ")", ",", "(", "batch_size", ",", "tf", ".", "float32", ",", "\"reward\"", ")", ",", "(", "batch_size", ",", "tf", ".", "bool", ",", "\"done\"", ")", ",", "(", "batch_size", "+", "list", "(", "batch_env", ".", "action_shape", ")", ",", "batch_env", ".", "action_dtype", ",", "\"action\"", ")", ",", "(", "batch_size", ",", "tf", ".", "float32", ",", "\"pdf\"", ")", ",", "(", "batch_size", ",", "tf", ".", "float32", ",", "\"value_function\"", ")", ",", "]", "return", "shapes_types_names" ]
Metadata for rollouts.
[ "Metadata", "for", "rollouts", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/ppo_learner.py#L254-L268
train
tensorflow/tensor2tensor
tensor2tensor/rl/ppo_learner.py
_define_collect
def _define_collect(batch_env, ppo_hparams, scope, frame_stack_size, eval_phase, sampling_temp, force_beginning_resets): """Collect trajectories. Args: batch_env: Batch environment. ppo_hparams: PPO hparams, defined in tensor2tensor.models.research.rl. scope: var scope. frame_stack_size: Number of last observations to feed into the policy. eval_phase: TODO(koz4k): Write docstring. sampling_temp: Sampling temperature for the policy. force_beginning_resets: Whether to reset at the beginning of each episode. Returns: Returns memory (observations, rewards, dones, actions, pdfs, values_functions) containing a rollout of environment from nested wrapped structure. """ epoch_length = ppo_hparams.epoch_length to_initialize = [] with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): num_agents = batch_env.batch_size to_initialize.append(batch_env) wrappers = [(StackWrapper, { "history": frame_stack_size }), (_MemoryWrapper, {})] rollout_metadata = None speculum = None for w in wrappers: tf.logging.info("Applying wrapper %s(%s) to env %s." % (str( w[0]), str(w[1]), str(batch_env))) batch_env = w[0](batch_env, **w[1]) to_initialize.append(batch_env) rollout_metadata = _rollout_metadata(batch_env) speculum = batch_env.speculum def initialization_lambda(sess): for batch_env in to_initialize: batch_env.initialize(sess) memory = [ tf.get_variable( # pylint: disable=g-complex-comprehension "collect_memory_%d_%s" % (epoch_length, name), shape=[epoch_length] + shape, dtype=dtype, initializer=tf.zeros_initializer(), trainable=False) for (shape, dtype, name) in rollout_metadata ] cumulative_rewards = tf.get_variable( "cumulative_rewards", len(batch_env), trainable=False) eval_phase_t = tf.convert_to_tensor(eval_phase) should_reset_var = tf.Variable(True, trainable=False) zeros_tensor = tf.zeros(len(batch_env)) force_beginning_resets = tf.convert_to_tensor(force_beginning_resets) def reset_ops_group(): return tf.group( batch_env.reset(tf.range(len(batch_env))), tf.assign(cumulative_rewards, zeros_tensor)) reset_op = tf.cond( tf.logical_or(should_reset_var.read_value(), force_beginning_resets), reset_ops_group, tf.no_op) with tf.control_dependencies([reset_op]): reset_once_op = tf.assign(should_reset_var, False) with tf.control_dependencies([reset_once_op]): def step(index, scores_sum, scores_num): """Single step.""" index %= epoch_length # Only needed in eval runs. # Note - the only way to ensure making a copy of tensor is to run simple # operation. We are waiting for tf.copy: # https://github.com/tensorflow/tensorflow/issues/11186 obs_copy = batch_env.observ + 0 def env_step(arg1, arg2, arg3): # pylint: disable=unused-argument """Step of the environment.""" (logits, value_function) = get_policy( obs_copy, ppo_hparams, batch_env.action_space ) action = common_layers.sample_with_temperature(logits, sampling_temp) action = tf.cast(action, tf.int32) action = tf.reshape(action, shape=(num_agents,)) reward, done = batch_env.simulate(action) pdf = tfp.distributions.Categorical(logits=logits).prob(action) pdf = tf.reshape(pdf, shape=(num_agents,)) value_function = tf.reshape(value_function, shape=(num_agents,)) done = tf.reshape(done, shape=(num_agents,)) with tf.control_dependencies([reward, done]): return tf.identity(pdf), tf.identity(value_function), \ tf.identity(done) # TODO(piotrmilos): while_body is executed at most once, # thus should be replaced with tf.cond pdf, value_function, top_level_done = tf.while_loop( lambda _1, _2, _3: tf.equal(speculum.size(), 0), env_step, [ tf.constant(0.0, shape=(num_agents,)), tf.constant(0.0, shape=(num_agents,)), tf.constant(False, shape=(num_agents,)) ], parallel_iterations=1, back_prop=False, ) with tf.control_dependencies([pdf, value_function]): obs, reward, done, action = speculum.dequeue() to_save = [obs, reward, done, action, pdf, value_function] save_ops = [ tf.scatter_update(memory_slot, index, value) for memory_slot, value in zip(memory, to_save) ] cumulate_rewards_op = cumulative_rewards.assign_add(reward) agent_indices_to_reset = tf.where(top_level_done)[:, 0] with tf.control_dependencies([cumulate_rewards_op]): # TODO(piotrmilos): possibly we need cumulative_rewards.read_value() scores_sum_delta = tf.reduce_sum( tf.gather(cumulative_rewards.read_value(), agent_indices_to_reset)) scores_num_delta = tf.count_nonzero(done, dtype=tf.int32) with tf.control_dependencies(save_ops + [scores_sum_delta, scores_num_delta]): reset_env_op = batch_env.reset(agent_indices_to_reset) reset_cumulative_rewards_op = tf.scatter_update( cumulative_rewards, agent_indices_to_reset, tf.gather(zeros_tensor, agent_indices_to_reset)) with tf.control_dependencies([reset_env_op, reset_cumulative_rewards_op]): return [ index + 1, scores_sum + scores_sum_delta, scores_num + scores_num_delta ] def stop_condition(i, _, resets): return tf.cond(eval_phase_t, lambda: resets < num_agents, lambda: i < epoch_length) init = [tf.constant(0), tf.constant(0.0), tf.constant(0)] index, scores_sum, scores_num = tf.while_loop( stop_condition, step, init, parallel_iterations=1, back_prop=False) # We handle force_beginning_resets differently. We assume that all envs are # reseted at the end of episod (though it happens at the beginning of the # next one scores_num = tf.cond(force_beginning_resets, lambda: scores_num + len(batch_env), lambda: scores_num) with tf.control_dependencies([scores_sum]): scores_sum = tf.cond( force_beginning_resets, lambda: scores_sum + tf.reduce_sum(cumulative_rewards.read_value()), lambda: scores_sum) mean_score = tf.cond( tf.greater(scores_num, 0), lambda: scores_sum / tf.cast(scores_num, tf.float32), lambda: 0.) printing = tf.Print(0, [mean_score, scores_sum, scores_num], "mean_score: ") with tf.control_dependencies([index, printing]): memory = [mem.read_value() for mem in memory] # When generating real data together with PPO training we must use single # agent. For PPO to work we reshape the history, as if it was generated # by real_ppo_effective_num_agents. if ppo_hparams.effective_num_agents is not None and not eval_phase: new_memory = [] effective_num_agents = ppo_hparams.effective_num_agents assert epoch_length % ppo_hparams.effective_num_agents == 0, ( "The rollout of ppo_hparams.epoch_length will be distributed amongst" "effective_num_agents of agents") new_epoch_length = int(epoch_length / effective_num_agents) for mem, info in zip(memory, rollout_metadata): shape, _, name = info new_shape = [effective_num_agents, new_epoch_length] + shape[1:] perm = list(range(len(shape) + 1)) perm[0] = 1 perm[1] = 0 mem = tf.transpose(mem, perm=perm) mem = tf.reshape(mem, shape=new_shape) mem = tf.transpose( mem, perm=perm, name="collect_memory_%d_%s" % (new_epoch_length, name)) new_memory.append(mem) memory = new_memory with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): mean_score_summary = tf.cond( tf.greater(scores_num, 0), lambda: tf.summary.scalar("mean_score_this_iter", mean_score), str) summaries = tf.summary.merge([ mean_score_summary, tf.summary.scalar("episodes_finished_this_iter", scores_num) ]) return memory, summaries, initialization_lambda
python
def _define_collect(batch_env, ppo_hparams, scope, frame_stack_size, eval_phase, sampling_temp, force_beginning_resets): """Collect trajectories. Args: batch_env: Batch environment. ppo_hparams: PPO hparams, defined in tensor2tensor.models.research.rl. scope: var scope. frame_stack_size: Number of last observations to feed into the policy. eval_phase: TODO(koz4k): Write docstring. sampling_temp: Sampling temperature for the policy. force_beginning_resets: Whether to reset at the beginning of each episode. Returns: Returns memory (observations, rewards, dones, actions, pdfs, values_functions) containing a rollout of environment from nested wrapped structure. """ epoch_length = ppo_hparams.epoch_length to_initialize = [] with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): num_agents = batch_env.batch_size to_initialize.append(batch_env) wrappers = [(StackWrapper, { "history": frame_stack_size }), (_MemoryWrapper, {})] rollout_metadata = None speculum = None for w in wrappers: tf.logging.info("Applying wrapper %s(%s) to env %s." % (str( w[0]), str(w[1]), str(batch_env))) batch_env = w[0](batch_env, **w[1]) to_initialize.append(batch_env) rollout_metadata = _rollout_metadata(batch_env) speculum = batch_env.speculum def initialization_lambda(sess): for batch_env in to_initialize: batch_env.initialize(sess) memory = [ tf.get_variable( # pylint: disable=g-complex-comprehension "collect_memory_%d_%s" % (epoch_length, name), shape=[epoch_length] + shape, dtype=dtype, initializer=tf.zeros_initializer(), trainable=False) for (shape, dtype, name) in rollout_metadata ] cumulative_rewards = tf.get_variable( "cumulative_rewards", len(batch_env), trainable=False) eval_phase_t = tf.convert_to_tensor(eval_phase) should_reset_var = tf.Variable(True, trainable=False) zeros_tensor = tf.zeros(len(batch_env)) force_beginning_resets = tf.convert_to_tensor(force_beginning_resets) def reset_ops_group(): return tf.group( batch_env.reset(tf.range(len(batch_env))), tf.assign(cumulative_rewards, zeros_tensor)) reset_op = tf.cond( tf.logical_or(should_reset_var.read_value(), force_beginning_resets), reset_ops_group, tf.no_op) with tf.control_dependencies([reset_op]): reset_once_op = tf.assign(should_reset_var, False) with tf.control_dependencies([reset_once_op]): def step(index, scores_sum, scores_num): """Single step.""" index %= epoch_length # Only needed in eval runs. # Note - the only way to ensure making a copy of tensor is to run simple # operation. We are waiting for tf.copy: # https://github.com/tensorflow/tensorflow/issues/11186 obs_copy = batch_env.observ + 0 def env_step(arg1, arg2, arg3): # pylint: disable=unused-argument """Step of the environment.""" (logits, value_function) = get_policy( obs_copy, ppo_hparams, batch_env.action_space ) action = common_layers.sample_with_temperature(logits, sampling_temp) action = tf.cast(action, tf.int32) action = tf.reshape(action, shape=(num_agents,)) reward, done = batch_env.simulate(action) pdf = tfp.distributions.Categorical(logits=logits).prob(action) pdf = tf.reshape(pdf, shape=(num_agents,)) value_function = tf.reshape(value_function, shape=(num_agents,)) done = tf.reshape(done, shape=(num_agents,)) with tf.control_dependencies([reward, done]): return tf.identity(pdf), tf.identity(value_function), \ tf.identity(done) # TODO(piotrmilos): while_body is executed at most once, # thus should be replaced with tf.cond pdf, value_function, top_level_done = tf.while_loop( lambda _1, _2, _3: tf.equal(speculum.size(), 0), env_step, [ tf.constant(0.0, shape=(num_agents,)), tf.constant(0.0, shape=(num_agents,)), tf.constant(False, shape=(num_agents,)) ], parallel_iterations=1, back_prop=False, ) with tf.control_dependencies([pdf, value_function]): obs, reward, done, action = speculum.dequeue() to_save = [obs, reward, done, action, pdf, value_function] save_ops = [ tf.scatter_update(memory_slot, index, value) for memory_slot, value in zip(memory, to_save) ] cumulate_rewards_op = cumulative_rewards.assign_add(reward) agent_indices_to_reset = tf.where(top_level_done)[:, 0] with tf.control_dependencies([cumulate_rewards_op]): # TODO(piotrmilos): possibly we need cumulative_rewards.read_value() scores_sum_delta = tf.reduce_sum( tf.gather(cumulative_rewards.read_value(), agent_indices_to_reset)) scores_num_delta = tf.count_nonzero(done, dtype=tf.int32) with tf.control_dependencies(save_ops + [scores_sum_delta, scores_num_delta]): reset_env_op = batch_env.reset(agent_indices_to_reset) reset_cumulative_rewards_op = tf.scatter_update( cumulative_rewards, agent_indices_to_reset, tf.gather(zeros_tensor, agent_indices_to_reset)) with tf.control_dependencies([reset_env_op, reset_cumulative_rewards_op]): return [ index + 1, scores_sum + scores_sum_delta, scores_num + scores_num_delta ] def stop_condition(i, _, resets): return tf.cond(eval_phase_t, lambda: resets < num_agents, lambda: i < epoch_length) init = [tf.constant(0), tf.constant(0.0), tf.constant(0)] index, scores_sum, scores_num = tf.while_loop( stop_condition, step, init, parallel_iterations=1, back_prop=False) # We handle force_beginning_resets differently. We assume that all envs are # reseted at the end of episod (though it happens at the beginning of the # next one scores_num = tf.cond(force_beginning_resets, lambda: scores_num + len(batch_env), lambda: scores_num) with tf.control_dependencies([scores_sum]): scores_sum = tf.cond( force_beginning_resets, lambda: scores_sum + tf.reduce_sum(cumulative_rewards.read_value()), lambda: scores_sum) mean_score = tf.cond( tf.greater(scores_num, 0), lambda: scores_sum / tf.cast(scores_num, tf.float32), lambda: 0.) printing = tf.Print(0, [mean_score, scores_sum, scores_num], "mean_score: ") with tf.control_dependencies([index, printing]): memory = [mem.read_value() for mem in memory] # When generating real data together with PPO training we must use single # agent. For PPO to work we reshape the history, as if it was generated # by real_ppo_effective_num_agents. if ppo_hparams.effective_num_agents is not None and not eval_phase: new_memory = [] effective_num_agents = ppo_hparams.effective_num_agents assert epoch_length % ppo_hparams.effective_num_agents == 0, ( "The rollout of ppo_hparams.epoch_length will be distributed amongst" "effective_num_agents of agents") new_epoch_length = int(epoch_length / effective_num_agents) for mem, info in zip(memory, rollout_metadata): shape, _, name = info new_shape = [effective_num_agents, new_epoch_length] + shape[1:] perm = list(range(len(shape) + 1)) perm[0] = 1 perm[1] = 0 mem = tf.transpose(mem, perm=perm) mem = tf.reshape(mem, shape=new_shape) mem = tf.transpose( mem, perm=perm, name="collect_memory_%d_%s" % (new_epoch_length, name)) new_memory.append(mem) memory = new_memory with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): mean_score_summary = tf.cond( tf.greater(scores_num, 0), lambda: tf.summary.scalar("mean_score_this_iter", mean_score), str) summaries = tf.summary.merge([ mean_score_summary, tf.summary.scalar("episodes_finished_this_iter", scores_num) ]) return memory, summaries, initialization_lambda
[ "def", "_define_collect", "(", "batch_env", ",", "ppo_hparams", ",", "scope", ",", "frame_stack_size", ",", "eval_phase", ",", "sampling_temp", ",", "force_beginning_resets", ")", ":", "epoch_length", "=", "ppo_hparams", ".", "epoch_length", "to_initialize", "=", "[", "]", "with", "tf", ".", "variable_scope", "(", "scope", ",", "reuse", "=", "tf", ".", "AUTO_REUSE", ")", ":", "num_agents", "=", "batch_env", ".", "batch_size", "to_initialize", ".", "append", "(", "batch_env", ")", "wrappers", "=", "[", "(", "StackWrapper", ",", "{", "\"history\"", ":", "frame_stack_size", "}", ")", ",", "(", "_MemoryWrapper", ",", "{", "}", ")", "]", "rollout_metadata", "=", "None", "speculum", "=", "None", "for", "w", "in", "wrappers", ":", "tf", ".", "logging", ".", "info", "(", "\"Applying wrapper %s(%s) to env %s.\"", "%", "(", "str", "(", "w", "[", "0", "]", ")", ",", "str", "(", "w", "[", "1", "]", ")", ",", "str", "(", "batch_env", ")", ")", ")", "batch_env", "=", "w", "[", "0", "]", "(", "batch_env", ",", "*", "*", "w", "[", "1", "]", ")", "to_initialize", ".", "append", "(", "batch_env", ")", "rollout_metadata", "=", "_rollout_metadata", "(", "batch_env", ")", "speculum", "=", "batch_env", ".", "speculum", "def", "initialization_lambda", "(", "sess", ")", ":", "for", "batch_env", "in", "to_initialize", ":", "batch_env", ".", "initialize", "(", "sess", ")", "memory", "=", "[", "tf", ".", "get_variable", "(", "# pylint: disable=g-complex-comprehension", "\"collect_memory_%d_%s\"", "%", "(", "epoch_length", ",", "name", ")", ",", "shape", "=", "[", "epoch_length", "]", "+", "shape", ",", "dtype", "=", "dtype", ",", "initializer", "=", "tf", ".", "zeros_initializer", "(", ")", ",", "trainable", "=", "False", ")", "for", "(", "shape", ",", "dtype", ",", "name", ")", "in", "rollout_metadata", "]", "cumulative_rewards", "=", "tf", ".", "get_variable", "(", "\"cumulative_rewards\"", ",", "len", "(", "batch_env", ")", ",", "trainable", "=", "False", ")", "eval_phase_t", "=", "tf", ".", "convert_to_tensor", "(", "eval_phase", ")", "should_reset_var", "=", "tf", ".", "Variable", "(", "True", ",", "trainable", "=", "False", ")", "zeros_tensor", "=", "tf", ".", "zeros", "(", "len", "(", "batch_env", ")", ")", "force_beginning_resets", "=", "tf", ".", "convert_to_tensor", "(", "force_beginning_resets", ")", "def", "reset_ops_group", "(", ")", ":", "return", "tf", ".", "group", "(", "batch_env", ".", "reset", "(", "tf", ".", "range", "(", "len", "(", "batch_env", ")", ")", ")", ",", "tf", ".", "assign", "(", "cumulative_rewards", ",", "zeros_tensor", ")", ")", "reset_op", "=", "tf", ".", "cond", "(", "tf", ".", "logical_or", "(", "should_reset_var", ".", "read_value", "(", ")", ",", "force_beginning_resets", ")", ",", "reset_ops_group", ",", "tf", ".", "no_op", ")", "with", "tf", ".", "control_dependencies", "(", "[", "reset_op", "]", ")", ":", "reset_once_op", "=", "tf", ".", "assign", "(", "should_reset_var", ",", "False", ")", "with", "tf", ".", "control_dependencies", "(", "[", "reset_once_op", "]", ")", ":", "def", "step", "(", "index", ",", "scores_sum", ",", "scores_num", ")", ":", "\"\"\"Single step.\"\"\"", "index", "%=", "epoch_length", "# Only needed in eval runs.", "# Note - the only way to ensure making a copy of tensor is to run simple", "# operation. We are waiting for tf.copy:", "# https://github.com/tensorflow/tensorflow/issues/11186", "obs_copy", "=", "batch_env", ".", "observ", "+", "0", "def", "env_step", "(", "arg1", ",", "arg2", ",", "arg3", ")", ":", "# pylint: disable=unused-argument", "\"\"\"Step of the environment.\"\"\"", "(", "logits", ",", "value_function", ")", "=", "get_policy", "(", "obs_copy", ",", "ppo_hparams", ",", "batch_env", ".", "action_space", ")", "action", "=", "common_layers", ".", "sample_with_temperature", "(", "logits", ",", "sampling_temp", ")", "action", "=", "tf", ".", "cast", "(", "action", ",", "tf", ".", "int32", ")", "action", "=", "tf", ".", "reshape", "(", "action", ",", "shape", "=", "(", "num_agents", ",", ")", ")", "reward", ",", "done", "=", "batch_env", ".", "simulate", "(", "action", ")", "pdf", "=", "tfp", ".", "distributions", ".", "Categorical", "(", "logits", "=", "logits", ")", ".", "prob", "(", "action", ")", "pdf", "=", "tf", ".", "reshape", "(", "pdf", ",", "shape", "=", "(", "num_agents", ",", ")", ")", "value_function", "=", "tf", ".", "reshape", "(", "value_function", ",", "shape", "=", "(", "num_agents", ",", ")", ")", "done", "=", "tf", ".", "reshape", "(", "done", ",", "shape", "=", "(", "num_agents", ",", ")", ")", "with", "tf", ".", "control_dependencies", "(", "[", "reward", ",", "done", "]", ")", ":", "return", "tf", ".", "identity", "(", "pdf", ")", ",", "tf", ".", "identity", "(", "value_function", ")", ",", "tf", ".", "identity", "(", "done", ")", "# TODO(piotrmilos): while_body is executed at most once,", "# thus should be replaced with tf.cond", "pdf", ",", "value_function", ",", "top_level_done", "=", "tf", ".", "while_loop", "(", "lambda", "_1", ",", "_2", ",", "_3", ":", "tf", ".", "equal", "(", "speculum", ".", "size", "(", ")", ",", "0", ")", ",", "env_step", ",", "[", "tf", ".", "constant", "(", "0.0", ",", "shape", "=", "(", "num_agents", ",", ")", ")", ",", "tf", ".", "constant", "(", "0.0", ",", "shape", "=", "(", "num_agents", ",", ")", ")", ",", "tf", ".", "constant", "(", "False", ",", "shape", "=", "(", "num_agents", ",", ")", ")", "]", ",", "parallel_iterations", "=", "1", ",", "back_prop", "=", "False", ",", ")", "with", "tf", ".", "control_dependencies", "(", "[", "pdf", ",", "value_function", "]", ")", ":", "obs", ",", "reward", ",", "done", ",", "action", "=", "speculum", ".", "dequeue", "(", ")", "to_save", "=", "[", "obs", ",", "reward", ",", "done", ",", "action", ",", "pdf", ",", "value_function", "]", "save_ops", "=", "[", "tf", ".", "scatter_update", "(", "memory_slot", ",", "index", ",", "value", ")", "for", "memory_slot", ",", "value", "in", "zip", "(", "memory", ",", "to_save", ")", "]", "cumulate_rewards_op", "=", "cumulative_rewards", ".", "assign_add", "(", "reward", ")", "agent_indices_to_reset", "=", "tf", ".", "where", "(", "top_level_done", ")", "[", ":", ",", "0", "]", "with", "tf", ".", "control_dependencies", "(", "[", "cumulate_rewards_op", "]", ")", ":", "# TODO(piotrmilos): possibly we need cumulative_rewards.read_value()", "scores_sum_delta", "=", "tf", ".", "reduce_sum", "(", "tf", ".", "gather", "(", "cumulative_rewards", ".", "read_value", "(", ")", ",", "agent_indices_to_reset", ")", ")", "scores_num_delta", "=", "tf", ".", "count_nonzero", "(", "done", ",", "dtype", "=", "tf", ".", "int32", ")", "with", "tf", ".", "control_dependencies", "(", "save_ops", "+", "[", "scores_sum_delta", ",", "scores_num_delta", "]", ")", ":", "reset_env_op", "=", "batch_env", ".", "reset", "(", "agent_indices_to_reset", ")", "reset_cumulative_rewards_op", "=", "tf", ".", "scatter_update", "(", "cumulative_rewards", ",", "agent_indices_to_reset", ",", "tf", ".", "gather", "(", "zeros_tensor", ",", "agent_indices_to_reset", ")", ")", "with", "tf", ".", "control_dependencies", "(", "[", "reset_env_op", ",", "reset_cumulative_rewards_op", "]", ")", ":", "return", "[", "index", "+", "1", ",", "scores_sum", "+", "scores_sum_delta", ",", "scores_num", "+", "scores_num_delta", "]", "def", "stop_condition", "(", "i", ",", "_", ",", "resets", ")", ":", "return", "tf", ".", "cond", "(", "eval_phase_t", ",", "lambda", ":", "resets", "<", "num_agents", ",", "lambda", ":", "i", "<", "epoch_length", ")", "init", "=", "[", "tf", ".", "constant", "(", "0", ")", ",", "tf", ".", "constant", "(", "0.0", ")", ",", "tf", ".", "constant", "(", "0", ")", "]", "index", ",", "scores_sum", ",", "scores_num", "=", "tf", ".", "while_loop", "(", "stop_condition", ",", "step", ",", "init", ",", "parallel_iterations", "=", "1", ",", "back_prop", "=", "False", ")", "# We handle force_beginning_resets differently. We assume that all envs are", "# reseted at the end of episod (though it happens at the beginning of the", "# next one", "scores_num", "=", "tf", ".", "cond", "(", "force_beginning_resets", ",", "lambda", ":", "scores_num", "+", "len", "(", "batch_env", ")", ",", "lambda", ":", "scores_num", ")", "with", "tf", ".", "control_dependencies", "(", "[", "scores_sum", "]", ")", ":", "scores_sum", "=", "tf", ".", "cond", "(", "force_beginning_resets", ",", "lambda", ":", "scores_sum", "+", "tf", ".", "reduce_sum", "(", "cumulative_rewards", ".", "read_value", "(", ")", ")", ",", "lambda", ":", "scores_sum", ")", "mean_score", "=", "tf", ".", "cond", "(", "tf", ".", "greater", "(", "scores_num", ",", "0", ")", ",", "lambda", ":", "scores_sum", "/", "tf", ".", "cast", "(", "scores_num", ",", "tf", ".", "float32", ")", ",", "lambda", ":", "0.", ")", "printing", "=", "tf", ".", "Print", "(", "0", ",", "[", "mean_score", ",", "scores_sum", ",", "scores_num", "]", ",", "\"mean_score: \"", ")", "with", "tf", ".", "control_dependencies", "(", "[", "index", ",", "printing", "]", ")", ":", "memory", "=", "[", "mem", ".", "read_value", "(", ")", "for", "mem", "in", "memory", "]", "# When generating real data together with PPO training we must use single", "# agent. For PPO to work we reshape the history, as if it was generated", "# by real_ppo_effective_num_agents.", "if", "ppo_hparams", ".", "effective_num_agents", "is", "not", "None", "and", "not", "eval_phase", ":", "new_memory", "=", "[", "]", "effective_num_agents", "=", "ppo_hparams", ".", "effective_num_agents", "assert", "epoch_length", "%", "ppo_hparams", ".", "effective_num_agents", "==", "0", ",", "(", "\"The rollout of ppo_hparams.epoch_length will be distributed amongst\"", "\"effective_num_agents of agents\"", ")", "new_epoch_length", "=", "int", "(", "epoch_length", "/", "effective_num_agents", ")", "for", "mem", ",", "info", "in", "zip", "(", "memory", ",", "rollout_metadata", ")", ":", "shape", ",", "_", ",", "name", "=", "info", "new_shape", "=", "[", "effective_num_agents", ",", "new_epoch_length", "]", "+", "shape", "[", "1", ":", "]", "perm", "=", "list", "(", "range", "(", "len", "(", "shape", ")", "+", "1", ")", ")", "perm", "[", "0", "]", "=", "1", "perm", "[", "1", "]", "=", "0", "mem", "=", "tf", ".", "transpose", "(", "mem", ",", "perm", "=", "perm", ")", "mem", "=", "tf", ".", "reshape", "(", "mem", ",", "shape", "=", "new_shape", ")", "mem", "=", "tf", ".", "transpose", "(", "mem", ",", "perm", "=", "perm", ",", "name", "=", "\"collect_memory_%d_%s\"", "%", "(", "new_epoch_length", ",", "name", ")", ")", "new_memory", ".", "append", "(", "mem", ")", "memory", "=", "new_memory", "with", "tf", ".", "variable_scope", "(", "scope", ",", "reuse", "=", "tf", ".", "AUTO_REUSE", ")", ":", "mean_score_summary", "=", "tf", ".", "cond", "(", "tf", ".", "greater", "(", "scores_num", ",", "0", ")", ",", "lambda", ":", "tf", ".", "summary", ".", "scalar", "(", "\"mean_score_this_iter\"", ",", "mean_score", ")", ",", "str", ")", "summaries", "=", "tf", ".", "summary", ".", "merge", "(", "[", "mean_score_summary", ",", "tf", ".", "summary", ".", "scalar", "(", "\"episodes_finished_this_iter\"", ",", "scores_num", ")", "]", ")", "return", "memory", ",", "summaries", ",", "initialization_lambda" ]
Collect trajectories. Args: batch_env: Batch environment. ppo_hparams: PPO hparams, defined in tensor2tensor.models.research.rl. scope: var scope. frame_stack_size: Number of last observations to feed into the policy. eval_phase: TODO(koz4k): Write docstring. sampling_temp: Sampling temperature for the policy. force_beginning_resets: Whether to reset at the beginning of each episode. Returns: Returns memory (observations, rewards, dones, actions, pdfs, values_functions) containing a rollout of environment from nested wrapped structure.
[ "Collect", "trajectories", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/ppo_learner.py#L310-L515
train
tensorflow/tensor2tensor
tensor2tensor/models/vanilla_gan.py
deconv2d
def deconv2d( input_, output_shape, k_h, k_w, d_h, d_w, stddev=0.02, name="deconv2d"): """Deconvolution layer.""" with tf.variable_scope(name): w = tf.get_variable( "w", [k_h, k_w, output_shape[-1], input_.get_shape()[-1]], initializer=tf.random_normal_initializer(stddev=stddev)) deconv = tf.nn.conv2d_transpose( input_, w, output_shape=output_shape, strides=[1, d_h, d_w, 1]) biases = tf.get_variable( "biases", [output_shape[-1]], initializer=tf.constant_initializer(0.0)) return tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
python
def deconv2d( input_, output_shape, k_h, k_w, d_h, d_w, stddev=0.02, name="deconv2d"): """Deconvolution layer.""" with tf.variable_scope(name): w = tf.get_variable( "w", [k_h, k_w, output_shape[-1], input_.get_shape()[-1]], initializer=tf.random_normal_initializer(stddev=stddev)) deconv = tf.nn.conv2d_transpose( input_, w, output_shape=output_shape, strides=[1, d_h, d_w, 1]) biases = tf.get_variable( "biases", [output_shape[-1]], initializer=tf.constant_initializer(0.0)) return tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
[ "def", "deconv2d", "(", "input_", ",", "output_shape", ",", "k_h", ",", "k_w", ",", "d_h", ",", "d_w", ",", "stddev", "=", "0.02", ",", "name", "=", "\"deconv2d\"", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ")", ":", "w", "=", "tf", ".", "get_variable", "(", "\"w\"", ",", "[", "k_h", ",", "k_w", ",", "output_shape", "[", "-", "1", "]", ",", "input_", ".", "get_shape", "(", ")", "[", "-", "1", "]", "]", ",", "initializer", "=", "tf", ".", "random_normal_initializer", "(", "stddev", "=", "stddev", ")", ")", "deconv", "=", "tf", ".", "nn", ".", "conv2d_transpose", "(", "input_", ",", "w", ",", "output_shape", "=", "output_shape", ",", "strides", "=", "[", "1", ",", "d_h", ",", "d_w", ",", "1", "]", ")", "biases", "=", "tf", ".", "get_variable", "(", "\"biases\"", ",", "[", "output_shape", "[", "-", "1", "]", "]", ",", "initializer", "=", "tf", ".", "constant_initializer", "(", "0.0", ")", ")", "return", "tf", ".", "reshape", "(", "tf", ".", "nn", ".", "bias_add", "(", "deconv", ",", "biases", ")", ",", "deconv", ".", "get_shape", "(", ")", ")" ]
Deconvolution layer.
[ "Deconvolution", "layer", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/vanilla_gan.py#L37-L48
train
tensorflow/tensor2tensor
tensor2tensor/models/vanilla_gan.py
sliced_gan
def sliced_gan(): """Basic parameters for a vanilla_gan.""" hparams = common_hparams.basic_params1() hparams.optimizer = "adam" hparams.learning_rate_constant = 0.0002 hparams.learning_rate_warmup_steps = 500 hparams.learning_rate_schedule = "constant * linear_warmup" hparams.label_smoothing = 0.0 hparams.batch_size = 128 hparams.hidden_size = 128 hparams.initializer = "uniform_unit_scaling" hparams.initializer_gain = 1.0 hparams.weight_decay = 1e-6 hparams.kernel_height = 4 hparams.kernel_width = 4 hparams.bottleneck_bits = 128 hparams.add_hparam("discriminator_batchnorm", True) hparams.add_hparam("num_sliced_vecs", 4096) return hparams
python
def sliced_gan(): """Basic parameters for a vanilla_gan.""" hparams = common_hparams.basic_params1() hparams.optimizer = "adam" hparams.learning_rate_constant = 0.0002 hparams.learning_rate_warmup_steps = 500 hparams.learning_rate_schedule = "constant * linear_warmup" hparams.label_smoothing = 0.0 hparams.batch_size = 128 hparams.hidden_size = 128 hparams.initializer = "uniform_unit_scaling" hparams.initializer_gain = 1.0 hparams.weight_decay = 1e-6 hparams.kernel_height = 4 hparams.kernel_width = 4 hparams.bottleneck_bits = 128 hparams.add_hparam("discriminator_batchnorm", True) hparams.add_hparam("num_sliced_vecs", 4096) return hparams
[ "def", "sliced_gan", "(", ")", ":", "hparams", "=", "common_hparams", ".", "basic_params1", "(", ")", "hparams", ".", "optimizer", "=", "\"adam\"", "hparams", ".", "learning_rate_constant", "=", "0.0002", "hparams", ".", "learning_rate_warmup_steps", "=", "500", "hparams", ".", "learning_rate_schedule", "=", "\"constant * linear_warmup\"", "hparams", ".", "label_smoothing", "=", "0.0", "hparams", ".", "batch_size", "=", "128", "hparams", ".", "hidden_size", "=", "128", "hparams", ".", "initializer", "=", "\"uniform_unit_scaling\"", "hparams", ".", "initializer_gain", "=", "1.0", "hparams", ".", "weight_decay", "=", "1e-6", "hparams", ".", "kernel_height", "=", "4", "hparams", ".", "kernel_width", "=", "4", "hparams", ".", "bottleneck_bits", "=", "128", "hparams", ".", "add_hparam", "(", "\"discriminator_batchnorm\"", ",", "True", ")", "hparams", ".", "add_hparam", "(", "\"num_sliced_vecs\"", ",", "4096", ")", "return", "hparams" ]
Basic parameters for a vanilla_gan.
[ "Basic", "parameters", "for", "a", "vanilla_gan", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/vanilla_gan.py#L199-L217
train
tensorflow/tensor2tensor
tensor2tensor/models/vanilla_gan.py
AbstractGAN.discriminator
def discriminator(self, x, is_training, reuse=False): """Discriminator architecture based on InfoGAN. Args: x: input images, shape [bs, h, w, channels] is_training: boolean, are we in train or eval model. reuse: boolean, should params be re-used. Returns: out_logit: the output logits (before sigmoid). """ hparams = self.hparams with tf.variable_scope( "discriminator", reuse=reuse, initializer=tf.random_normal_initializer(stddev=0.02)): batch_size, height, width = common_layers.shape_list(x)[:3] # Mapping x from [bs, h, w, c] to [bs, 1] net = tf.layers.conv2d(x, 64, (4, 4), strides=(2, 2), padding="SAME", name="d_conv1") # [bs, h/2, w/2, 64] net = lrelu(net) net = tf.layers.conv2d(net, 128, (4, 4), strides=(2, 2), padding="SAME", name="d_conv2") # [bs, h/4, w/4, 128] if hparams.discriminator_batchnorm: net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="d_bn2") net = lrelu(net) size = height * width net = tf.reshape(net, [batch_size, size * 8]) # [bs, h * w * 8] net = tf.layers.dense(net, 1024, name="d_fc3") # [bs, 1024] if hparams.discriminator_batchnorm: net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="d_bn3") net = lrelu(net) return net
python
def discriminator(self, x, is_training, reuse=False): """Discriminator architecture based on InfoGAN. Args: x: input images, shape [bs, h, w, channels] is_training: boolean, are we in train or eval model. reuse: boolean, should params be re-used. Returns: out_logit: the output logits (before sigmoid). """ hparams = self.hparams with tf.variable_scope( "discriminator", reuse=reuse, initializer=tf.random_normal_initializer(stddev=0.02)): batch_size, height, width = common_layers.shape_list(x)[:3] # Mapping x from [bs, h, w, c] to [bs, 1] net = tf.layers.conv2d(x, 64, (4, 4), strides=(2, 2), padding="SAME", name="d_conv1") # [bs, h/2, w/2, 64] net = lrelu(net) net = tf.layers.conv2d(net, 128, (4, 4), strides=(2, 2), padding="SAME", name="d_conv2") # [bs, h/4, w/4, 128] if hparams.discriminator_batchnorm: net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="d_bn2") net = lrelu(net) size = height * width net = tf.reshape(net, [batch_size, size * 8]) # [bs, h * w * 8] net = tf.layers.dense(net, 1024, name="d_fc3") # [bs, 1024] if hparams.discriminator_batchnorm: net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="d_bn3") net = lrelu(net) return net
[ "def", "discriminator", "(", "self", ",", "x", ",", "is_training", ",", "reuse", "=", "False", ")", ":", "hparams", "=", "self", ".", "hparams", "with", "tf", ".", "variable_scope", "(", "\"discriminator\"", ",", "reuse", "=", "reuse", ",", "initializer", "=", "tf", ".", "random_normal_initializer", "(", "stddev", "=", "0.02", ")", ")", ":", "batch_size", ",", "height", ",", "width", "=", "common_layers", ".", "shape_list", "(", "x", ")", "[", ":", "3", "]", "# Mapping x from [bs, h, w, c] to [bs, 1]", "net", "=", "tf", ".", "layers", ".", "conv2d", "(", "x", ",", "64", ",", "(", "4", ",", "4", ")", ",", "strides", "=", "(", "2", ",", "2", ")", ",", "padding", "=", "\"SAME\"", ",", "name", "=", "\"d_conv1\"", ")", "# [bs, h/2, w/2, 64]", "net", "=", "lrelu", "(", "net", ")", "net", "=", "tf", ".", "layers", ".", "conv2d", "(", "net", ",", "128", ",", "(", "4", ",", "4", ")", ",", "strides", "=", "(", "2", ",", "2", ")", ",", "padding", "=", "\"SAME\"", ",", "name", "=", "\"d_conv2\"", ")", "# [bs, h/4, w/4, 128]", "if", "hparams", ".", "discriminator_batchnorm", ":", "net", "=", "tf", ".", "layers", ".", "batch_normalization", "(", "net", ",", "training", "=", "is_training", ",", "momentum", "=", "0.999", ",", "name", "=", "\"d_bn2\"", ")", "net", "=", "lrelu", "(", "net", ")", "size", "=", "height", "*", "width", "net", "=", "tf", ".", "reshape", "(", "net", ",", "[", "batch_size", ",", "size", "*", "8", "]", ")", "# [bs, h * w * 8]", "net", "=", "tf", ".", "layers", ".", "dense", "(", "net", ",", "1024", ",", "name", "=", "\"d_fc3\"", ")", "# [bs, 1024]", "if", "hparams", ".", "discriminator_batchnorm", ":", "net", "=", "tf", ".", "layers", ".", "batch_normalization", "(", "net", ",", "training", "=", "is_training", ",", "momentum", "=", "0.999", ",", "name", "=", "\"d_bn3\"", ")", "net", "=", "lrelu", "(", "net", ")", "return", "net" ]
Discriminator architecture based on InfoGAN. Args: x: input images, shape [bs, h, w, channels] is_training: boolean, are we in train or eval model. reuse: boolean, should params be re-used. Returns: out_logit: the output logits (before sigmoid).
[ "Discriminator", "architecture", "based", "on", "InfoGAN", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/vanilla_gan.py#L58-L93
train
tensorflow/tensor2tensor
tensor2tensor/models/vanilla_gan.py
AbstractGAN.generator
def generator(self, z, is_training, out_shape): """Generator outputting image in [0, 1].""" hparams = self.hparams height, width, c_dim = out_shape batch_size = hparams.batch_size with tf.variable_scope( "generator", initializer=tf.random_normal_initializer(stddev=0.02)): net = tf.layers.dense(z, 1024, name="g_fc1") net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="g_bn1") net = lrelu(net) net = tf.layers.dense(net, 128 * (height // 4) * (width // 4), name="g_fc2") net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="g_bn2") net = lrelu(net) net = tf.reshape(net, [batch_size, height // 4, width // 4, 128]) net = deconv2d(net, [batch_size, height // 2, width // 2, 64], 4, 4, 2, 2, name="g_dc3") net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="g_bn3") net = lrelu(net) net = deconv2d(net, [batch_size, height, width, c_dim], 4, 4, 2, 2, name="g_dc4") out = tf.nn.sigmoid(net) return common_layers.convert_real_to_rgb(out)
python
def generator(self, z, is_training, out_shape): """Generator outputting image in [0, 1].""" hparams = self.hparams height, width, c_dim = out_shape batch_size = hparams.batch_size with tf.variable_scope( "generator", initializer=tf.random_normal_initializer(stddev=0.02)): net = tf.layers.dense(z, 1024, name="g_fc1") net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="g_bn1") net = lrelu(net) net = tf.layers.dense(net, 128 * (height // 4) * (width // 4), name="g_fc2") net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="g_bn2") net = lrelu(net) net = tf.reshape(net, [batch_size, height // 4, width // 4, 128]) net = deconv2d(net, [batch_size, height // 2, width // 2, 64], 4, 4, 2, 2, name="g_dc3") net = tf.layers.batch_normalization(net, training=is_training, momentum=0.999, name="g_bn3") net = lrelu(net) net = deconv2d(net, [batch_size, height, width, c_dim], 4, 4, 2, 2, name="g_dc4") out = tf.nn.sigmoid(net) return common_layers.convert_real_to_rgb(out)
[ "def", "generator", "(", "self", ",", "z", ",", "is_training", ",", "out_shape", ")", ":", "hparams", "=", "self", ".", "hparams", "height", ",", "width", ",", "c_dim", "=", "out_shape", "batch_size", "=", "hparams", ".", "batch_size", "with", "tf", ".", "variable_scope", "(", "\"generator\"", ",", "initializer", "=", "tf", ".", "random_normal_initializer", "(", "stddev", "=", "0.02", ")", ")", ":", "net", "=", "tf", ".", "layers", ".", "dense", "(", "z", ",", "1024", ",", "name", "=", "\"g_fc1\"", ")", "net", "=", "tf", ".", "layers", ".", "batch_normalization", "(", "net", ",", "training", "=", "is_training", ",", "momentum", "=", "0.999", ",", "name", "=", "\"g_bn1\"", ")", "net", "=", "lrelu", "(", "net", ")", "net", "=", "tf", ".", "layers", ".", "dense", "(", "net", ",", "128", "*", "(", "height", "//", "4", ")", "*", "(", "width", "//", "4", ")", ",", "name", "=", "\"g_fc2\"", ")", "net", "=", "tf", ".", "layers", ".", "batch_normalization", "(", "net", ",", "training", "=", "is_training", ",", "momentum", "=", "0.999", ",", "name", "=", "\"g_bn2\"", ")", "net", "=", "lrelu", "(", "net", ")", "net", "=", "tf", ".", "reshape", "(", "net", ",", "[", "batch_size", ",", "height", "//", "4", ",", "width", "//", "4", ",", "128", "]", ")", "net", "=", "deconv2d", "(", "net", ",", "[", "batch_size", ",", "height", "//", "2", ",", "width", "//", "2", ",", "64", "]", ",", "4", ",", "4", ",", "2", ",", "2", ",", "name", "=", "\"g_dc3\"", ")", "net", "=", "tf", ".", "layers", ".", "batch_normalization", "(", "net", ",", "training", "=", "is_training", ",", "momentum", "=", "0.999", ",", "name", "=", "\"g_bn3\"", ")", "net", "=", "lrelu", "(", "net", ")", "net", "=", "deconv2d", "(", "net", ",", "[", "batch_size", ",", "height", ",", "width", ",", "c_dim", "]", ",", "4", ",", "4", ",", "2", ",", "2", ",", "name", "=", "\"g_dc4\"", ")", "out", "=", "tf", ".", "nn", ".", "sigmoid", "(", "net", ")", "return", "common_layers", ".", "convert_real_to_rgb", "(", "out", ")" ]
Generator outputting image in [0, 1].
[ "Generator", "outputting", "image", "in", "[", "0", "1", "]", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/vanilla_gan.py#L95-L121
train
tensorflow/tensor2tensor
tensor2tensor/models/vanilla_gan.py
AbstractGAN.body
def body(self, features): """Body of the model. Args: features: a dictionary with the tensors. Returns: A pair (predictions, losses) where predictions is the generated image and losses is a dictionary of losses (that get added for the final loss). """ features["targets"] = features["inputs"] is_training = self.hparams.mode == tf.estimator.ModeKeys.TRAIN # Input images. inputs = tf.to_float(features["targets_raw"]) # Noise vector. z = tf.random_uniform([self.hparams.batch_size, self.hparams.bottleneck_bits], minval=-1, maxval=1, name="z") # Generator output: fake images. out_shape = common_layers.shape_list(inputs)[1:4] g = self.generator(z, is_training, out_shape) losses = self.losses(inputs, g) # pylint: disable=not-callable summary_g_image = tf.reshape( g[0, :], [1] + common_layers.shape_list(inputs)[1:]) tf.summary.image("generated", summary_g_image, max_outputs=1) if is_training: # Returns an dummy output and the losses dictionary. return tf.zeros_like(inputs), losses return tf.reshape(g, tf.shape(inputs)), losses
python
def body(self, features): """Body of the model. Args: features: a dictionary with the tensors. Returns: A pair (predictions, losses) where predictions is the generated image and losses is a dictionary of losses (that get added for the final loss). """ features["targets"] = features["inputs"] is_training = self.hparams.mode == tf.estimator.ModeKeys.TRAIN # Input images. inputs = tf.to_float(features["targets_raw"]) # Noise vector. z = tf.random_uniform([self.hparams.batch_size, self.hparams.bottleneck_bits], minval=-1, maxval=1, name="z") # Generator output: fake images. out_shape = common_layers.shape_list(inputs)[1:4] g = self.generator(z, is_training, out_shape) losses = self.losses(inputs, g) # pylint: disable=not-callable summary_g_image = tf.reshape( g[0, :], [1] + common_layers.shape_list(inputs)[1:]) tf.summary.image("generated", summary_g_image, max_outputs=1) if is_training: # Returns an dummy output and the losses dictionary. return tf.zeros_like(inputs), losses return tf.reshape(g, tf.shape(inputs)), losses
[ "def", "body", "(", "self", ",", "features", ")", ":", "features", "[", "\"targets\"", "]", "=", "features", "[", "\"inputs\"", "]", "is_training", "=", "self", ".", "hparams", ".", "mode", "==", "tf", ".", "estimator", ".", "ModeKeys", ".", "TRAIN", "# Input images.", "inputs", "=", "tf", ".", "to_float", "(", "features", "[", "\"targets_raw\"", "]", ")", "# Noise vector.", "z", "=", "tf", ".", "random_uniform", "(", "[", "self", ".", "hparams", ".", "batch_size", ",", "self", ".", "hparams", ".", "bottleneck_bits", "]", ",", "minval", "=", "-", "1", ",", "maxval", "=", "1", ",", "name", "=", "\"z\"", ")", "# Generator output: fake images.", "out_shape", "=", "common_layers", ".", "shape_list", "(", "inputs", ")", "[", "1", ":", "4", "]", "g", "=", "self", ".", "generator", "(", "z", ",", "is_training", ",", "out_shape", ")", "losses", "=", "self", ".", "losses", "(", "inputs", ",", "g", ")", "# pylint: disable=not-callable", "summary_g_image", "=", "tf", ".", "reshape", "(", "g", "[", "0", ",", ":", "]", ",", "[", "1", "]", "+", "common_layers", ".", "shape_list", "(", "inputs", ")", "[", "1", ":", "]", ")", "tf", ".", "summary", ".", "image", "(", "\"generated\"", ",", "summary_g_image", ",", "max_outputs", "=", "1", ")", "if", "is_training", ":", "# Returns an dummy output and the losses dictionary.", "return", "tf", ".", "zeros_like", "(", "inputs", ")", ",", "losses", "return", "tf", ".", "reshape", "(", "g", ",", "tf", ".", "shape", "(", "inputs", ")", ")", ",", "losses" ]
Body of the model. Args: features: a dictionary with the tensors. Returns: A pair (predictions, losses) where predictions is the generated image and losses is a dictionary of losses (that get added for the final loss).
[ "Body", "of", "the", "model", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/vanilla_gan.py#L127-L160
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
inputs
def inputs(num_devices, dataset_name, data_dir=None, input_name=None, num_chunks=0, append_targets=False): """Make Inputs for built-in datasets. Args: num_devices: how many devices to build the inputs for. dataset_name: a TFDS or T2T dataset name. If it's a T2T dataset name, prefix with "t2t_". data_dir: data directory. input_name: optional, name of the inputs from the dictionary. num_chunks: optional, into how many pieces should we chunk (large inputs). append_targets: optional, instead of inputs return a pair (inputs, targets) which is useful for autoregressive models. Returns: trax.inputs.Inputs """ assert data_dir, "Must provide a data directory" data_dir = os.path.expanduser(data_dir) (train_batches, train_eval_batches, eval_batches, input_name, input_shape) = _train_and_eval_batches( dataset_name, data_dir, input_name, num_devices) def numpy_stream(dataset): return dataset_to_stream( dataset, input_name, num_chunks=num_chunks, append_targets=append_targets) if num_chunks > 0: length = input_shape[0] input_shape = tuple( [tuple([length // num_chunks] + list(input_shape)[1:])] * num_chunks) return Inputs(train_stream=lambda: numpy_stream(train_batches), train_eval_stream=lambda: numpy_stream(train_eval_batches), eval_stream=lambda: numpy_stream(eval_batches), input_shape=input_shape)
python
def inputs(num_devices, dataset_name, data_dir=None, input_name=None, num_chunks=0, append_targets=False): """Make Inputs for built-in datasets. Args: num_devices: how many devices to build the inputs for. dataset_name: a TFDS or T2T dataset name. If it's a T2T dataset name, prefix with "t2t_". data_dir: data directory. input_name: optional, name of the inputs from the dictionary. num_chunks: optional, into how many pieces should we chunk (large inputs). append_targets: optional, instead of inputs return a pair (inputs, targets) which is useful for autoregressive models. Returns: trax.inputs.Inputs """ assert data_dir, "Must provide a data directory" data_dir = os.path.expanduser(data_dir) (train_batches, train_eval_batches, eval_batches, input_name, input_shape) = _train_and_eval_batches( dataset_name, data_dir, input_name, num_devices) def numpy_stream(dataset): return dataset_to_stream( dataset, input_name, num_chunks=num_chunks, append_targets=append_targets) if num_chunks > 0: length = input_shape[0] input_shape = tuple( [tuple([length // num_chunks] + list(input_shape)[1:])] * num_chunks) return Inputs(train_stream=lambda: numpy_stream(train_batches), train_eval_stream=lambda: numpy_stream(train_eval_batches), eval_stream=lambda: numpy_stream(eval_batches), input_shape=input_shape)
[ "def", "inputs", "(", "num_devices", ",", "dataset_name", ",", "data_dir", "=", "None", ",", "input_name", "=", "None", ",", "num_chunks", "=", "0", ",", "append_targets", "=", "False", ")", ":", "assert", "data_dir", ",", "\"Must provide a data directory\"", "data_dir", "=", "os", ".", "path", ".", "expanduser", "(", "data_dir", ")", "(", "train_batches", ",", "train_eval_batches", ",", "eval_batches", ",", "input_name", ",", "input_shape", ")", "=", "_train_and_eval_batches", "(", "dataset_name", ",", "data_dir", ",", "input_name", ",", "num_devices", ")", "def", "numpy_stream", "(", "dataset", ")", ":", "return", "dataset_to_stream", "(", "dataset", ",", "input_name", ",", "num_chunks", "=", "num_chunks", ",", "append_targets", "=", "append_targets", ")", "if", "num_chunks", ">", "0", ":", "length", "=", "input_shape", "[", "0", "]", "input_shape", "=", "tuple", "(", "[", "tuple", "(", "[", "length", "//", "num_chunks", "]", "+", "list", "(", "input_shape", ")", "[", "1", ":", "]", ")", "]", "*", "num_chunks", ")", "return", "Inputs", "(", "train_stream", "=", "lambda", ":", "numpy_stream", "(", "train_batches", ")", ",", "train_eval_stream", "=", "lambda", ":", "numpy_stream", "(", "train_eval_batches", ")", ",", "eval_stream", "=", "lambda", ":", "numpy_stream", "(", "eval_batches", ")", ",", "input_shape", "=", "input_shape", ")" ]
Make Inputs for built-in datasets. Args: num_devices: how many devices to build the inputs for. dataset_name: a TFDS or T2T dataset name. If it's a T2T dataset name, prefix with "t2t_". data_dir: data directory. input_name: optional, name of the inputs from the dictionary. num_chunks: optional, into how many pieces should we chunk (large inputs). append_targets: optional, instead of inputs return a pair (inputs, targets) which is useful for autoregressive models. Returns: trax.inputs.Inputs
[ "Make", "Inputs", "for", "built", "-", "in", "datasets", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L58-L95
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
random_inputs
def random_inputs( num_devices, input_shape=gin.REQUIRED, input_dtype=np.int32, input_range=(0, 255), output_shape=gin.REQUIRED, output_dtype=np.int32, output_range=(0, 9)): """Make random Inputs for debugging. Args: num_devices: how many devices to build the inputs for. input_shape: the shape of inputs (including batch dimension). input_dtype: the type of the inputs (int32 by default). input_range: the range of inputs (defaults to (0, 255)). output_shape: the shape of outputs (including batch dimension). output_dtype: the type of the outputs (int32 by default). output_range: the range of outputs (defaults to (0, 9)). Returns: trax.inputs.Inputs """ if input_shape[0] % num_devices != 0: tf.logging.fatal( "num_devices[%d] should divide the first dimension of input_shape[%s]", num_devices, input_shape) if output_shape[0] % num_devices != 0: tf.logging.fatal( "num_devices[%d] should divide the first dimension of output_shape[%s]", num_devices, output_shape) def random_minibatches(): """Generate a stream of random mini-batches.""" if input_dtype in [np.float16, np.float32, np.float64]: rand = np.random.uniform else: rand = np.random.random_integers while True: inp = rand(input_range[0], input_range[1], input_shape) inp = inp.astype(input_dtype) out = rand(output_range[0], output_range[1], output_shape) out = out.astype(output_dtype) yield inp, out input_shape_without_batch = list(input_shape)[1:] return Inputs(train_stream=random_minibatches, train_eval_stream=random_minibatches, eval_stream=random_minibatches, input_shape=input_shape_without_batch)
python
def random_inputs( num_devices, input_shape=gin.REQUIRED, input_dtype=np.int32, input_range=(0, 255), output_shape=gin.REQUIRED, output_dtype=np.int32, output_range=(0, 9)): """Make random Inputs for debugging. Args: num_devices: how many devices to build the inputs for. input_shape: the shape of inputs (including batch dimension). input_dtype: the type of the inputs (int32 by default). input_range: the range of inputs (defaults to (0, 255)). output_shape: the shape of outputs (including batch dimension). output_dtype: the type of the outputs (int32 by default). output_range: the range of outputs (defaults to (0, 9)). Returns: trax.inputs.Inputs """ if input_shape[0] % num_devices != 0: tf.logging.fatal( "num_devices[%d] should divide the first dimension of input_shape[%s]", num_devices, input_shape) if output_shape[0] % num_devices != 0: tf.logging.fatal( "num_devices[%d] should divide the first dimension of output_shape[%s]", num_devices, output_shape) def random_minibatches(): """Generate a stream of random mini-batches.""" if input_dtype in [np.float16, np.float32, np.float64]: rand = np.random.uniform else: rand = np.random.random_integers while True: inp = rand(input_range[0], input_range[1], input_shape) inp = inp.astype(input_dtype) out = rand(output_range[0], output_range[1], output_shape) out = out.astype(output_dtype) yield inp, out input_shape_without_batch = list(input_shape)[1:] return Inputs(train_stream=random_minibatches, train_eval_stream=random_minibatches, eval_stream=random_minibatches, input_shape=input_shape_without_batch)
[ "def", "random_inputs", "(", "num_devices", ",", "input_shape", "=", "gin", ".", "REQUIRED", ",", "input_dtype", "=", "np", ".", "int32", ",", "input_range", "=", "(", "0", ",", "255", ")", ",", "output_shape", "=", "gin", ".", "REQUIRED", ",", "output_dtype", "=", "np", ".", "int32", ",", "output_range", "=", "(", "0", ",", "9", ")", ")", ":", "if", "input_shape", "[", "0", "]", "%", "num_devices", "!=", "0", ":", "tf", ".", "logging", ".", "fatal", "(", "\"num_devices[%d] should divide the first dimension of input_shape[%s]\"", ",", "num_devices", ",", "input_shape", ")", "if", "output_shape", "[", "0", "]", "%", "num_devices", "!=", "0", ":", "tf", ".", "logging", ".", "fatal", "(", "\"num_devices[%d] should divide the first dimension of output_shape[%s]\"", ",", "num_devices", ",", "output_shape", ")", "def", "random_minibatches", "(", ")", ":", "\"\"\"Generate a stream of random mini-batches.\"\"\"", "if", "input_dtype", "in", "[", "np", ".", "float16", ",", "np", ".", "float32", ",", "np", ".", "float64", "]", ":", "rand", "=", "np", ".", "random", ".", "uniform", "else", ":", "rand", "=", "np", ".", "random", ".", "random_integers", "while", "True", ":", "inp", "=", "rand", "(", "input_range", "[", "0", "]", ",", "input_range", "[", "1", "]", ",", "input_shape", ")", "inp", "=", "inp", ".", "astype", "(", "input_dtype", ")", "out", "=", "rand", "(", "output_range", "[", "0", "]", ",", "output_range", "[", "1", "]", ",", "output_shape", ")", "out", "=", "out", ".", "astype", "(", "output_dtype", ")", "yield", "inp", ",", "out", "input_shape_without_batch", "=", "list", "(", "input_shape", ")", "[", "1", ":", "]", "return", "Inputs", "(", "train_stream", "=", "random_minibatches", ",", "train_eval_stream", "=", "random_minibatches", ",", "eval_stream", "=", "random_minibatches", ",", "input_shape", "=", "input_shape_without_batch", ")" ]
Make random Inputs for debugging. Args: num_devices: how many devices to build the inputs for. input_shape: the shape of inputs (including batch dimension). input_dtype: the type of the inputs (int32 by default). input_range: the range of inputs (defaults to (0, 255)). output_shape: the shape of outputs (including batch dimension). output_dtype: the type of the outputs (int32 by default). output_range: the range of outputs (defaults to (0, 9)). Returns: trax.inputs.Inputs
[ "Make", "random", "Inputs", "for", "debugging", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L99-L143
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
dataset_to_stream
def dataset_to_stream(dataset, input_name, num_chunks=0, append_targets=False): """Takes a tf.Dataset and creates a numpy stream of ready batches.""" for example in tfds.as_numpy(dataset): inp, out = example[0][input_name], example[1] if len(out.shape) > 1 and out.shape[-1] == 1: out = np.squeeze(out, axis=-1) if num_chunks > 0: inp = np.split(inp, num_chunks, axis=1) out = np.split(out, num_chunks, axis=1) if append_targets: inp = (inp, out) yield inp, out
python
def dataset_to_stream(dataset, input_name, num_chunks=0, append_targets=False): """Takes a tf.Dataset and creates a numpy stream of ready batches.""" for example in tfds.as_numpy(dataset): inp, out = example[0][input_name], example[1] if len(out.shape) > 1 and out.shape[-1] == 1: out = np.squeeze(out, axis=-1) if num_chunks > 0: inp = np.split(inp, num_chunks, axis=1) out = np.split(out, num_chunks, axis=1) if append_targets: inp = (inp, out) yield inp, out
[ "def", "dataset_to_stream", "(", "dataset", ",", "input_name", ",", "num_chunks", "=", "0", ",", "append_targets", "=", "False", ")", ":", "for", "example", "in", "tfds", ".", "as_numpy", "(", "dataset", ")", ":", "inp", ",", "out", "=", "example", "[", "0", "]", "[", "input_name", "]", ",", "example", "[", "1", "]", "if", "len", "(", "out", ".", "shape", ")", ">", "1", "and", "out", ".", "shape", "[", "-", "1", "]", "==", "1", ":", "out", "=", "np", ".", "squeeze", "(", "out", ",", "axis", "=", "-", "1", ")", "if", "num_chunks", ">", "0", ":", "inp", "=", "np", ".", "split", "(", "inp", ",", "num_chunks", ",", "axis", "=", "1", ")", "out", "=", "np", ".", "split", "(", "out", ",", "num_chunks", ",", "axis", "=", "1", ")", "if", "append_targets", ":", "inp", "=", "(", "inp", ",", "out", ")", "yield", "inp", ",", "out" ]
Takes a tf.Dataset and creates a numpy stream of ready batches.
[ "Takes", "a", "tf", ".", "Dataset", "and", "creates", "a", "numpy", "stream", "of", "ready", "batches", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L146-L157
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
_train_and_eval_dataset_v1
def _train_and_eval_dataset_v1(problem_name, data_dir): """Return train and evaluation datasets, feature info and supervised keys.""" assert not tf.executing_eagerly(), "tf.eager mode must be turned off." problem = t2t_problems.problem(problem_name) train_dataset = problem.dataset(tf.estimator.ModeKeys.TRAIN, data_dir) train_dataset = train_dataset.map(_select_features) eval_dataset = problem.dataset(tf.estimator.ModeKeys.EVAL, data_dir) eval_dataset = eval_dataset.map(_select_features) hparams = problem.get_hparams() # We take a few training examples to guess the shapes. input_shapes, target_shapes = [], [] example_tensor = train_dataset.make_one_shot_iterator().get_next() sess = tf.Session() example1 = sess.run(example_tensor) example2 = sess.run(example_tensor) example3 = sess.run(example_tensor) # We use "inputs" as input except for purely auto-regressive tasks like # language models where "targets" are used as input_key. input_key = "inputs" if "inputs" in example1 else "targets" supervised_keys = ([input_key], ["targets"]) for example in [example1, example2, example3]: input_shapes.append(list(example[input_key].shape)) target_shapes.append(list(example["targets"].shape)) input_vocab_size = hparams.vocab_size[input_key] target_vocab_size = hparams.vocab_size["targets"] input_info = _make_info(input_shapes, input_vocab_size) target_info = _make_info(target_shapes, target_vocab_size) info = {input_key: input_info, "targets": target_info} return train_dataset, eval_dataset, info, supervised_keys
python
def _train_and_eval_dataset_v1(problem_name, data_dir): """Return train and evaluation datasets, feature info and supervised keys.""" assert not tf.executing_eagerly(), "tf.eager mode must be turned off." problem = t2t_problems.problem(problem_name) train_dataset = problem.dataset(tf.estimator.ModeKeys.TRAIN, data_dir) train_dataset = train_dataset.map(_select_features) eval_dataset = problem.dataset(tf.estimator.ModeKeys.EVAL, data_dir) eval_dataset = eval_dataset.map(_select_features) hparams = problem.get_hparams() # We take a few training examples to guess the shapes. input_shapes, target_shapes = [], [] example_tensor = train_dataset.make_one_shot_iterator().get_next() sess = tf.Session() example1 = sess.run(example_tensor) example2 = sess.run(example_tensor) example3 = sess.run(example_tensor) # We use "inputs" as input except for purely auto-regressive tasks like # language models where "targets" are used as input_key. input_key = "inputs" if "inputs" in example1 else "targets" supervised_keys = ([input_key], ["targets"]) for example in [example1, example2, example3]: input_shapes.append(list(example[input_key].shape)) target_shapes.append(list(example["targets"].shape)) input_vocab_size = hparams.vocab_size[input_key] target_vocab_size = hparams.vocab_size["targets"] input_info = _make_info(input_shapes, input_vocab_size) target_info = _make_info(target_shapes, target_vocab_size) info = {input_key: input_info, "targets": target_info} return train_dataset, eval_dataset, info, supervised_keys
[ "def", "_train_and_eval_dataset_v1", "(", "problem_name", ",", "data_dir", ")", ":", "assert", "not", "tf", ".", "executing_eagerly", "(", ")", ",", "\"tf.eager mode must be turned off.\"", "problem", "=", "t2t_problems", ".", "problem", "(", "problem_name", ")", "train_dataset", "=", "problem", ".", "dataset", "(", "tf", ".", "estimator", ".", "ModeKeys", ".", "TRAIN", ",", "data_dir", ")", "train_dataset", "=", "train_dataset", ".", "map", "(", "_select_features", ")", "eval_dataset", "=", "problem", ".", "dataset", "(", "tf", ".", "estimator", ".", "ModeKeys", ".", "EVAL", ",", "data_dir", ")", "eval_dataset", "=", "eval_dataset", ".", "map", "(", "_select_features", ")", "hparams", "=", "problem", ".", "get_hparams", "(", ")", "# We take a few training examples to guess the shapes.", "input_shapes", ",", "target_shapes", "=", "[", "]", ",", "[", "]", "example_tensor", "=", "train_dataset", ".", "make_one_shot_iterator", "(", ")", ".", "get_next", "(", ")", "sess", "=", "tf", ".", "Session", "(", ")", "example1", "=", "sess", ".", "run", "(", "example_tensor", ")", "example2", "=", "sess", ".", "run", "(", "example_tensor", ")", "example3", "=", "sess", ".", "run", "(", "example_tensor", ")", "# We use \"inputs\" as input except for purely auto-regressive tasks like", "# language models where \"targets\" are used as input_key.", "input_key", "=", "\"inputs\"", "if", "\"inputs\"", "in", "example1", "else", "\"targets\"", "supervised_keys", "=", "(", "[", "input_key", "]", ",", "[", "\"targets\"", "]", ")", "for", "example", "in", "[", "example1", ",", "example2", ",", "example3", "]", ":", "input_shapes", ".", "append", "(", "list", "(", "example", "[", "input_key", "]", ".", "shape", ")", ")", "target_shapes", ".", "append", "(", "list", "(", "example", "[", "\"targets\"", "]", ".", "shape", ")", ")", "input_vocab_size", "=", "hparams", ".", "vocab_size", "[", "input_key", "]", "target_vocab_size", "=", "hparams", ".", "vocab_size", "[", "\"targets\"", "]", "input_info", "=", "_make_info", "(", "input_shapes", ",", "input_vocab_size", ")", "target_info", "=", "_make_info", "(", "target_shapes", ",", "target_vocab_size", ")", "info", "=", "{", "input_key", ":", "input_info", ",", "\"targets\"", ":", "target_info", "}", "return", "train_dataset", ",", "eval_dataset", ",", "info", ",", "supervised_keys" ]
Return train and evaluation datasets, feature info and supervised keys.
[ "Return", "train", "and", "evaluation", "datasets", "feature", "info", "and", "supervised", "keys", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L229-L257
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
batch_fun
def batch_fun(dataset, training, shapes, target_names, num_devices, batch_size_per_device=32, batch_size=None, eval_batch_size=32, bucket_length=32, buckets=None, batch_shuffle_size=128, max_eval_length=None): """Batching function.""" del target_names # Batch size is batch_size_per_device * num_devices unless given directly. batch_size = batch_size or batch_size_per_device * num_devices # If bucketing is not specified, check if target shapes are variable. cur_batch_size = batch_size if training else eval_batch_size # Make cur_batch_size divisible by num_devices. cur_batch_size = max(cur_batch_size // num_devices, 1) * num_devices # Create heuristic buckets is none are specified. if buckets is None: variable_target_shapes = False target_shape = shapes[1] for dim in target_shape: if dim is None: variable_target_shapes = True tf.logging.info("Heuristically setting bucketing to %s based on shapes " "of target tensors." % variable_target_shapes) if variable_target_shapes: bucket_boundaries = [bucket_length // 4, bucket_length // 2, bucket_length, bucket_length * 2, bucket_length * 4, bucket_length * 8, bucket_length * 16] # We will pad to boundaries which pads to bucket_boundary - 1: add 1 here. bucket_boundaries = [b + 1 for b in bucket_boundaries] if not training: max_eval_length = max_eval_length or bucket_length * 32 bucket_boundaries[-1] = max_eval_length bucket_batch_sizes = [cur_batch_size * 4, cur_batch_size * 2, cur_batch_size, cur_batch_size // 2, cur_batch_size // 4, cur_batch_size // 8, cur_batch_size // 16, 1] if not training: bucket_batch_sizes[-2] = cur_batch_size // max_eval_length # Make batch sizes divisible by num_devices. bucket_batch_sizes = [max(b // num_devices, 1) * num_devices for b in bucket_batch_sizes] buckets = (bucket_boundaries, bucket_batch_sizes) if buckets: tf.logging.info("Bucketing with buckets %s." % str(buckets)) def example_length(_, target): return tf.shape(target)[0] boundaries, batch_sizes = buckets dataset = dataset.apply(tf.data.experimental.bucket_by_sequence_length( example_length, boundaries, batch_sizes, pad_to_bucket_boundary=True)) else: dataset = dataset.padded_batch(cur_batch_size, shapes) if training: return dataset.shuffle(batch_shuffle_size) return dataset
python
def batch_fun(dataset, training, shapes, target_names, num_devices, batch_size_per_device=32, batch_size=None, eval_batch_size=32, bucket_length=32, buckets=None, batch_shuffle_size=128, max_eval_length=None): """Batching function.""" del target_names # Batch size is batch_size_per_device * num_devices unless given directly. batch_size = batch_size or batch_size_per_device * num_devices # If bucketing is not specified, check if target shapes are variable. cur_batch_size = batch_size if training else eval_batch_size # Make cur_batch_size divisible by num_devices. cur_batch_size = max(cur_batch_size // num_devices, 1) * num_devices # Create heuristic buckets is none are specified. if buckets is None: variable_target_shapes = False target_shape = shapes[1] for dim in target_shape: if dim is None: variable_target_shapes = True tf.logging.info("Heuristically setting bucketing to %s based on shapes " "of target tensors." % variable_target_shapes) if variable_target_shapes: bucket_boundaries = [bucket_length // 4, bucket_length // 2, bucket_length, bucket_length * 2, bucket_length * 4, bucket_length * 8, bucket_length * 16] # We will pad to boundaries which pads to bucket_boundary - 1: add 1 here. bucket_boundaries = [b + 1 for b in bucket_boundaries] if not training: max_eval_length = max_eval_length or bucket_length * 32 bucket_boundaries[-1] = max_eval_length bucket_batch_sizes = [cur_batch_size * 4, cur_batch_size * 2, cur_batch_size, cur_batch_size // 2, cur_batch_size // 4, cur_batch_size // 8, cur_batch_size // 16, 1] if not training: bucket_batch_sizes[-2] = cur_batch_size // max_eval_length # Make batch sizes divisible by num_devices. bucket_batch_sizes = [max(b // num_devices, 1) * num_devices for b in bucket_batch_sizes] buckets = (bucket_boundaries, bucket_batch_sizes) if buckets: tf.logging.info("Bucketing with buckets %s." % str(buckets)) def example_length(_, target): return tf.shape(target)[0] boundaries, batch_sizes = buckets dataset = dataset.apply(tf.data.experimental.bucket_by_sequence_length( example_length, boundaries, batch_sizes, pad_to_bucket_boundary=True)) else: dataset = dataset.padded_batch(cur_batch_size, shapes) if training: return dataset.shuffle(batch_shuffle_size) return dataset
[ "def", "batch_fun", "(", "dataset", ",", "training", ",", "shapes", ",", "target_names", ",", "num_devices", ",", "batch_size_per_device", "=", "32", ",", "batch_size", "=", "None", ",", "eval_batch_size", "=", "32", ",", "bucket_length", "=", "32", ",", "buckets", "=", "None", ",", "batch_shuffle_size", "=", "128", ",", "max_eval_length", "=", "None", ")", ":", "del", "target_names", "# Batch size is batch_size_per_device * num_devices unless given directly.", "batch_size", "=", "batch_size", "or", "batch_size_per_device", "*", "num_devices", "# If bucketing is not specified, check if target shapes are variable.", "cur_batch_size", "=", "batch_size", "if", "training", "else", "eval_batch_size", "# Make cur_batch_size divisible by num_devices.", "cur_batch_size", "=", "max", "(", "cur_batch_size", "//", "num_devices", ",", "1", ")", "*", "num_devices", "# Create heuristic buckets is none are specified.", "if", "buckets", "is", "None", ":", "variable_target_shapes", "=", "False", "target_shape", "=", "shapes", "[", "1", "]", "for", "dim", "in", "target_shape", ":", "if", "dim", "is", "None", ":", "variable_target_shapes", "=", "True", "tf", ".", "logging", ".", "info", "(", "\"Heuristically setting bucketing to %s based on shapes \"", "\"of target tensors.\"", "%", "variable_target_shapes", ")", "if", "variable_target_shapes", ":", "bucket_boundaries", "=", "[", "bucket_length", "//", "4", ",", "bucket_length", "//", "2", ",", "bucket_length", ",", "bucket_length", "*", "2", ",", "bucket_length", "*", "4", ",", "bucket_length", "*", "8", ",", "bucket_length", "*", "16", "]", "# We will pad to boundaries which pads to bucket_boundary - 1: add 1 here.", "bucket_boundaries", "=", "[", "b", "+", "1", "for", "b", "in", "bucket_boundaries", "]", "if", "not", "training", ":", "max_eval_length", "=", "max_eval_length", "or", "bucket_length", "*", "32", "bucket_boundaries", "[", "-", "1", "]", "=", "max_eval_length", "bucket_batch_sizes", "=", "[", "cur_batch_size", "*", "4", ",", "cur_batch_size", "*", "2", ",", "cur_batch_size", ",", "cur_batch_size", "//", "2", ",", "cur_batch_size", "//", "4", ",", "cur_batch_size", "//", "8", ",", "cur_batch_size", "//", "16", ",", "1", "]", "if", "not", "training", ":", "bucket_batch_sizes", "[", "-", "2", "]", "=", "cur_batch_size", "//", "max_eval_length", "# Make batch sizes divisible by num_devices.", "bucket_batch_sizes", "=", "[", "max", "(", "b", "//", "num_devices", ",", "1", ")", "*", "num_devices", "for", "b", "in", "bucket_batch_sizes", "]", "buckets", "=", "(", "bucket_boundaries", ",", "bucket_batch_sizes", ")", "if", "buckets", ":", "tf", ".", "logging", ".", "info", "(", "\"Bucketing with buckets %s.\"", "%", "str", "(", "buckets", ")", ")", "def", "example_length", "(", "_", ",", "target", ")", ":", "return", "tf", ".", "shape", "(", "target", ")", "[", "0", "]", "boundaries", ",", "batch_sizes", "=", "buckets", "dataset", "=", "dataset", ".", "apply", "(", "tf", ".", "data", ".", "experimental", ".", "bucket_by_sequence_length", "(", "example_length", ",", "boundaries", ",", "batch_sizes", ",", "pad_to_bucket_boundary", "=", "True", ")", ")", "else", ":", "dataset", "=", "dataset", ".", "padded_batch", "(", "cur_batch_size", ",", "shapes", ")", "if", "training", ":", "return", "dataset", ".", "shuffle", "(", "batch_shuffle_size", ")", "return", "dataset" ]
Batching function.
[ "Batching", "function", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L262-L316
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
lm1b_preprocess
def lm1b_preprocess(dataset, training, max_target_length=-1, max_eval_target_length=-1): """Preprocessing for LM1B: filter out targets exceeding maximum length.""" def target_right_length(_, target): return tf.less(tf.shape(target)[0], max_target_length + 1) def eval_target_right_length(_, target): return tf.less(tf.shape(target)[0], max_eval_target_length + 1) if max_target_length > 0 and training: dataset = dataset.filter(target_right_length) if max_eval_target_length > 0 and not training: dataset = dataset.filter(eval_target_right_length) return dataset
python
def lm1b_preprocess(dataset, training, max_target_length=-1, max_eval_target_length=-1): """Preprocessing for LM1B: filter out targets exceeding maximum length.""" def target_right_length(_, target): return tf.less(tf.shape(target)[0], max_target_length + 1) def eval_target_right_length(_, target): return tf.less(tf.shape(target)[0], max_eval_target_length + 1) if max_target_length > 0 and training: dataset = dataset.filter(target_right_length) if max_eval_target_length > 0 and not training: dataset = dataset.filter(eval_target_right_length) return dataset
[ "def", "lm1b_preprocess", "(", "dataset", ",", "training", ",", "max_target_length", "=", "-", "1", ",", "max_eval_target_length", "=", "-", "1", ")", ":", "def", "target_right_length", "(", "_", ",", "target", ")", ":", "return", "tf", ".", "less", "(", "tf", ".", "shape", "(", "target", ")", "[", "0", "]", ",", "max_target_length", "+", "1", ")", "def", "eval_target_right_length", "(", "_", ",", "target", ")", ":", "return", "tf", ".", "less", "(", "tf", ".", "shape", "(", "target", ")", "[", "0", "]", ",", "max_eval_target_length", "+", "1", ")", "if", "max_target_length", ">", "0", "and", "training", ":", "dataset", "=", "dataset", ".", "filter", "(", "target_right_length", ")", "if", "max_eval_target_length", ">", "0", "and", "not", "training", ":", "dataset", "=", "dataset", ".", "filter", "(", "eval_target_right_length", ")", "return", "dataset" ]
Preprocessing for LM1B: filter out targets exceeding maximum length.
[ "Preprocessing", "for", "LM1B", ":", "filter", "out", "targets", "exceeding", "maximum", "length", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L337-L353
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
shuffle_and_batch_data
def shuffle_and_batch_data(dataset, target_names, features_info, training, num_devices, shuffle_buffer_size=1024, preprocess_fun=no_preprocess): """Shuffle and batch the given dataset.""" def append_targets(example): """Append targets to the example dictionary. Needed for Keras.""" if len(target_names) == 1: return (example, example[target_names[0]]) targets = {} for name in target_names: targets[name] = example[name] return (example, targets) dataset = dataset.map(append_targets) if training: dataset = dataset.repeat() # Skip a random fraction at the beginning of the stream. The skip is # essential for synchronous highly-parallel training to avoid multiple # replicas reading the same data in lock-step. dataset = dataset.skip(random.randint(0, _MAX_SKIP_EXAMPLES)) dataset = preprocess_fun(dataset, training) shapes = {k: features_info[k].shape for k in features_info} shapes = (shapes, shapes[target_names[0]]) dataset = dataset.shuffle(shuffle_buffer_size) dataset = batch_fun(dataset, training, shapes, target_names, num_devices) return dataset.prefetch(2)
python
def shuffle_and_batch_data(dataset, target_names, features_info, training, num_devices, shuffle_buffer_size=1024, preprocess_fun=no_preprocess): """Shuffle and batch the given dataset.""" def append_targets(example): """Append targets to the example dictionary. Needed for Keras.""" if len(target_names) == 1: return (example, example[target_names[0]]) targets = {} for name in target_names: targets[name] = example[name] return (example, targets) dataset = dataset.map(append_targets) if training: dataset = dataset.repeat() # Skip a random fraction at the beginning of the stream. The skip is # essential for synchronous highly-parallel training to avoid multiple # replicas reading the same data in lock-step. dataset = dataset.skip(random.randint(0, _MAX_SKIP_EXAMPLES)) dataset = preprocess_fun(dataset, training) shapes = {k: features_info[k].shape for k in features_info} shapes = (shapes, shapes[target_names[0]]) dataset = dataset.shuffle(shuffle_buffer_size) dataset = batch_fun(dataset, training, shapes, target_names, num_devices) return dataset.prefetch(2)
[ "def", "shuffle_and_batch_data", "(", "dataset", ",", "target_names", ",", "features_info", ",", "training", ",", "num_devices", ",", "shuffle_buffer_size", "=", "1024", ",", "preprocess_fun", "=", "no_preprocess", ")", ":", "def", "append_targets", "(", "example", ")", ":", "\"\"\"Append targets to the example dictionary. Needed for Keras.\"\"\"", "if", "len", "(", "target_names", ")", "==", "1", ":", "return", "(", "example", ",", "example", "[", "target_names", "[", "0", "]", "]", ")", "targets", "=", "{", "}", "for", "name", "in", "target_names", ":", "targets", "[", "name", "]", "=", "example", "[", "name", "]", "return", "(", "example", ",", "targets", ")", "dataset", "=", "dataset", ".", "map", "(", "append_targets", ")", "if", "training", ":", "dataset", "=", "dataset", ".", "repeat", "(", ")", "# Skip a random fraction at the beginning of the stream. The skip is", "# essential for synchronous highly-parallel training to avoid multiple", "# replicas reading the same data in lock-step.", "dataset", "=", "dataset", ".", "skip", "(", "random", ".", "randint", "(", "0", ",", "_MAX_SKIP_EXAMPLES", ")", ")", "dataset", "=", "preprocess_fun", "(", "dataset", ",", "training", ")", "shapes", "=", "{", "k", ":", "features_info", "[", "k", "]", ".", "shape", "for", "k", "in", "features_info", "}", "shapes", "=", "(", "shapes", ",", "shapes", "[", "target_names", "[", "0", "]", "]", ")", "dataset", "=", "dataset", ".", "shuffle", "(", "shuffle_buffer_size", ")", "dataset", "=", "batch_fun", "(", "dataset", ",", "training", ",", "shapes", ",", "target_names", ",", "num_devices", ")", "return", "dataset", ".", "prefetch", "(", "2", ")" ]
Shuffle and batch the given dataset.
[ "Shuffle", "and", "batch", "the", "given", "dataset", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L357-L385
train
tensorflow/tensor2tensor
tensor2tensor/trax/inputs.py
_train_and_eval_batches
def _train_and_eval_batches(dataset, data_dir, input_name, num_devices): """Return train and eval batches with input name and shape.""" (train_data, eval_data, features_info, keys) = train_and_eval_dataset( dataset, data_dir) input_names, target_names = keys[0], keys[1] train_batches = shuffle_and_batch_data( train_data, target_names, features_info, training=True, num_devices=num_devices) train_eval_batches = shuffle_and_batch_data( # Data for eval-on-train. train_data, target_names, features_info, training=False, num_devices=num_devices) eval_batches = shuffle_and_batch_data( eval_data, target_names, features_info, training=False, num_devices=num_devices) input_name = input_name or input_names[0] input_shape = features_info[input_name].shape return (train_batches, train_eval_batches, eval_batches, input_name, list(input_shape))
python
def _train_and_eval_batches(dataset, data_dir, input_name, num_devices): """Return train and eval batches with input name and shape.""" (train_data, eval_data, features_info, keys) = train_and_eval_dataset( dataset, data_dir) input_names, target_names = keys[0], keys[1] train_batches = shuffle_and_batch_data( train_data, target_names, features_info, training=True, num_devices=num_devices) train_eval_batches = shuffle_and_batch_data( # Data for eval-on-train. train_data, target_names, features_info, training=False, num_devices=num_devices) eval_batches = shuffle_and_batch_data( eval_data, target_names, features_info, training=False, num_devices=num_devices) input_name = input_name or input_names[0] input_shape = features_info[input_name].shape return (train_batches, train_eval_batches, eval_batches, input_name, list(input_shape))
[ "def", "_train_and_eval_batches", "(", "dataset", ",", "data_dir", ",", "input_name", ",", "num_devices", ")", ":", "(", "train_data", ",", "eval_data", ",", "features_info", ",", "keys", ")", "=", "train_and_eval_dataset", "(", "dataset", ",", "data_dir", ")", "input_names", ",", "target_names", "=", "keys", "[", "0", "]", ",", "keys", "[", "1", "]", "train_batches", "=", "shuffle_and_batch_data", "(", "train_data", ",", "target_names", ",", "features_info", ",", "training", "=", "True", ",", "num_devices", "=", "num_devices", ")", "train_eval_batches", "=", "shuffle_and_batch_data", "(", "# Data for eval-on-train.", "train_data", ",", "target_names", ",", "features_info", ",", "training", "=", "False", ",", "num_devices", "=", "num_devices", ")", "eval_batches", "=", "shuffle_and_batch_data", "(", "eval_data", ",", "target_names", ",", "features_info", ",", "training", "=", "False", ",", "num_devices", "=", "num_devices", ")", "input_name", "=", "input_name", "or", "input_names", "[", "0", "]", "input_shape", "=", "features_info", "[", "input_name", "]", ".", "shape", "return", "(", "train_batches", ",", "train_eval_batches", ",", "eval_batches", ",", "input_name", ",", "list", "(", "input_shape", ")", ")" ]
Return train and eval batches with input name and shape.
[ "Return", "train", "and", "eval", "batches", "with", "input", "name", "and", "shape", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/inputs.py#L388-L405
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
get_multi_dataset
def get_multi_dataset(datasets, pmf=None): """Returns a Dataset that samples records from one or more Datasets. Args: datasets: A list of one or more Dataset objects to sample from. pmf: A tensor of shape [len(datasets)], the probabilities to sample each dataset with. This tensor is often constructed with the global_step. If this is None, we sample from the datasets uniformly at random. Returns: A Dataset object containing records from multiple datasets. Note that because this dataset iterates through other datasets it is stateful, thus you will need to call make_initializable_iterator instead of make_one_shot_iterator. """ pmf = tf.fill([len(datasets)], 1.0 / len(datasets)) if pmf is None else pmf samplers = [d.repeat().make_one_shot_iterator().get_next for d in datasets] sample = lambda _: categorical_case(pmf, samplers) return tf.data.Dataset.from_tensors([]).repeat().map(sample)
python
def get_multi_dataset(datasets, pmf=None): """Returns a Dataset that samples records from one or more Datasets. Args: datasets: A list of one or more Dataset objects to sample from. pmf: A tensor of shape [len(datasets)], the probabilities to sample each dataset with. This tensor is often constructed with the global_step. If this is None, we sample from the datasets uniformly at random. Returns: A Dataset object containing records from multiple datasets. Note that because this dataset iterates through other datasets it is stateful, thus you will need to call make_initializable_iterator instead of make_one_shot_iterator. """ pmf = tf.fill([len(datasets)], 1.0 / len(datasets)) if pmf is None else pmf samplers = [d.repeat().make_one_shot_iterator().get_next for d in datasets] sample = lambda _: categorical_case(pmf, samplers) return tf.data.Dataset.from_tensors([]).repeat().map(sample)
[ "def", "get_multi_dataset", "(", "datasets", ",", "pmf", "=", "None", ")", ":", "pmf", "=", "tf", ".", "fill", "(", "[", "len", "(", "datasets", ")", "]", ",", "1.0", "/", "len", "(", "datasets", ")", ")", "if", "pmf", "is", "None", "else", "pmf", "samplers", "=", "[", "d", ".", "repeat", "(", ")", ".", "make_one_shot_iterator", "(", ")", ".", "get_next", "for", "d", "in", "datasets", "]", "sample", "=", "lambda", "_", ":", "categorical_case", "(", "pmf", ",", "samplers", ")", "return", "tf", ".", "data", ".", "Dataset", ".", "from_tensors", "(", "[", "]", ")", ".", "repeat", "(", ")", ".", "map", "(", "sample", ")" ]
Returns a Dataset that samples records from one or more Datasets. Args: datasets: A list of one or more Dataset objects to sample from. pmf: A tensor of shape [len(datasets)], the probabilities to sample each dataset with. This tensor is often constructed with the global_step. If this is None, we sample from the datasets uniformly at random. Returns: A Dataset object containing records from multiple datasets. Note that because this dataset iterates through other datasets it is stateful, thus you will need to call make_initializable_iterator instead of make_one_shot_iterator.
[ "Returns", "a", "Dataset", "that", "samples", "records", "from", "one", "or", "more", "Datasets", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L205-L223
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
get_schedule_distribution
def get_schedule_distribution(schedule, global_step=None): """Computes the pmf of a schedule given the global_step. Args: schedule: A schedule tuple, see encode_schedule for details. global_step: A scalar tensor, the step to query the schedule. Returns: A 1-D tensor of probs, the sampling distribution of the global_step. """ interpolation, steps, pmfs = schedule if len(pmfs) == 1: # py_func doesn't seem to work on TPU - at least get the constant case to # run. # TODO(noam): get the general case working. return pmfs[0] if global_step is None: global_step = tf.train.get_or_create_global_step() if interpolation == 'step': interpolation_fn = step_interpolation elif interpolation == 'linear': interpolation_fn = linear_interpolation else: raise ValueError('Invalid interpolation strategy: %s' % interpolation) return tf.reshape( tf.py_func( func=lambda x: interpolation_fn(x, np.array(steps), np.array(pmfs)), inp=[global_step], Tout=tf.float32), [len(pmfs[0])])
python
def get_schedule_distribution(schedule, global_step=None): """Computes the pmf of a schedule given the global_step. Args: schedule: A schedule tuple, see encode_schedule for details. global_step: A scalar tensor, the step to query the schedule. Returns: A 1-D tensor of probs, the sampling distribution of the global_step. """ interpolation, steps, pmfs = schedule if len(pmfs) == 1: # py_func doesn't seem to work on TPU - at least get the constant case to # run. # TODO(noam): get the general case working. return pmfs[0] if global_step is None: global_step = tf.train.get_or_create_global_step() if interpolation == 'step': interpolation_fn = step_interpolation elif interpolation == 'linear': interpolation_fn = linear_interpolation else: raise ValueError('Invalid interpolation strategy: %s' % interpolation) return tf.reshape( tf.py_func( func=lambda x: interpolation_fn(x, np.array(steps), np.array(pmfs)), inp=[global_step], Tout=tf.float32), [len(pmfs[0])])
[ "def", "get_schedule_distribution", "(", "schedule", ",", "global_step", "=", "None", ")", ":", "interpolation", ",", "steps", ",", "pmfs", "=", "schedule", "if", "len", "(", "pmfs", ")", "==", "1", ":", "# py_func doesn't seem to work on TPU - at least get the constant case to", "# run.", "# TODO(noam): get the general case working.", "return", "pmfs", "[", "0", "]", "if", "global_step", "is", "None", ":", "global_step", "=", "tf", ".", "train", ".", "get_or_create_global_step", "(", ")", "if", "interpolation", "==", "'step'", ":", "interpolation_fn", "=", "step_interpolation", "elif", "interpolation", "==", "'linear'", ":", "interpolation_fn", "=", "linear_interpolation", "else", ":", "raise", "ValueError", "(", "'Invalid interpolation strategy: %s'", "%", "interpolation", ")", "return", "tf", ".", "reshape", "(", "tf", ".", "py_func", "(", "func", "=", "lambda", "x", ":", "interpolation_fn", "(", "x", ",", "np", ".", "array", "(", "steps", ")", ",", "np", ".", "array", "(", "pmfs", ")", ")", ",", "inp", "=", "[", "global_step", "]", ",", "Tout", "=", "tf", ".", "float32", ")", ",", "[", "len", "(", "pmfs", "[", "0", "]", ")", "]", ")" ]
Computes the pmf of a schedule given the global_step. Args: schedule: A schedule tuple, see encode_schedule for details. global_step: A scalar tensor, the step to query the schedule. Returns: A 1-D tensor of probs, the sampling distribution of the global_step.
[ "Computes", "the", "pmf", "of", "a", "schedule", "given", "the", "global_step", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L226-L253
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
categorical_case
def categorical_case(pmf, fns, rand=None): """Returns the outputs of fns[i] with probability pmf[i]. Args: pmf: A 1-D tensor of probabilities, the probability mass function. fns: A list of callables that return tensors, same length as pmf. rand: An optional scalar between 0.0 and 1.0, the output of an RNG. Returns: A tensor, the output of fns[i] with probability pmf[i]. """ rand = tf.random_uniform([]) if rand is None else rand cmf = tf.pad(tf.cumsum(pmf), [(1, 0)]) cmf = [cmf[i] for i in range(len(fns) + 1)] preds = [(rand >= a) & (rand < b) for a, b in zip(cmf[:-1], cmf[1:])] return tf.case(list(zip(preds, fns)), exclusive=True)
python
def categorical_case(pmf, fns, rand=None): """Returns the outputs of fns[i] with probability pmf[i]. Args: pmf: A 1-D tensor of probabilities, the probability mass function. fns: A list of callables that return tensors, same length as pmf. rand: An optional scalar between 0.0 and 1.0, the output of an RNG. Returns: A tensor, the output of fns[i] with probability pmf[i]. """ rand = tf.random_uniform([]) if rand is None else rand cmf = tf.pad(tf.cumsum(pmf), [(1, 0)]) cmf = [cmf[i] for i in range(len(fns) + 1)] preds = [(rand >= a) & (rand < b) for a, b in zip(cmf[:-1], cmf[1:])] return tf.case(list(zip(preds, fns)), exclusive=True)
[ "def", "categorical_case", "(", "pmf", ",", "fns", ",", "rand", "=", "None", ")", ":", "rand", "=", "tf", ".", "random_uniform", "(", "[", "]", ")", "if", "rand", "is", "None", "else", "rand", "cmf", "=", "tf", ".", "pad", "(", "tf", ".", "cumsum", "(", "pmf", ")", ",", "[", "(", "1", ",", "0", ")", "]", ")", "cmf", "=", "[", "cmf", "[", "i", "]", "for", "i", "in", "range", "(", "len", "(", "fns", ")", "+", "1", ")", "]", "preds", "=", "[", "(", "rand", ">=", "a", ")", "&", "(", "rand", "<", "b", ")", "for", "a", ",", "b", "in", "zip", "(", "cmf", "[", ":", "-", "1", "]", ",", "cmf", "[", "1", ":", "]", ")", "]", "return", "tf", ".", "case", "(", "list", "(", "zip", "(", "preds", ",", "fns", ")", ")", ",", "exclusive", "=", "True", ")" ]
Returns the outputs of fns[i] with probability pmf[i]. Args: pmf: A 1-D tensor of probabilities, the probability mass function. fns: A list of callables that return tensors, same length as pmf. rand: An optional scalar between 0.0 and 1.0, the output of an RNG. Returns: A tensor, the output of fns[i] with probability pmf[i].
[ "Returns", "the", "outputs", "of", "fns", "[", "i", "]", "with", "probability", "pmf", "[", "i", "]", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L256-L271
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
linear_interpolation
def linear_interpolation(x, xp, fp, **kwargs): """Multi-dimensional linear interpolation. Returns the multi-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x. Note that *N and *M indicate zero or more dimensions. Args: x: An array of shape [*N], the x-coordinates of the interpolated values. xp: An np.array of shape [D], the x-coordinates of the data points, must be increasing. fp: An np.array of shape [D, *M], the y-coordinates of the data points. **kwargs: Keywords for np.interp. Returns: An array of shape [*N, *M], the interpolated values. """ yp = fp.reshape([fp.shape[0], -1]).transpose() y = np.stack([np.interp(x, xp, zp, **kwargs) for zp in yp]).transpose() return y.reshape(x.shape[:1] + fp.shape[1:]).astype(np.float32)
python
def linear_interpolation(x, xp, fp, **kwargs): """Multi-dimensional linear interpolation. Returns the multi-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x. Note that *N and *M indicate zero or more dimensions. Args: x: An array of shape [*N], the x-coordinates of the interpolated values. xp: An np.array of shape [D], the x-coordinates of the data points, must be increasing. fp: An np.array of shape [D, *M], the y-coordinates of the data points. **kwargs: Keywords for np.interp. Returns: An array of shape [*N, *M], the interpolated values. """ yp = fp.reshape([fp.shape[0], -1]).transpose() y = np.stack([np.interp(x, xp, zp, **kwargs) for zp in yp]).transpose() return y.reshape(x.shape[:1] + fp.shape[1:]).astype(np.float32)
[ "def", "linear_interpolation", "(", "x", ",", "xp", ",", "fp", ",", "*", "*", "kwargs", ")", ":", "yp", "=", "fp", ".", "reshape", "(", "[", "fp", ".", "shape", "[", "0", "]", ",", "-", "1", "]", ")", ".", "transpose", "(", ")", "y", "=", "np", ".", "stack", "(", "[", "np", ".", "interp", "(", "x", ",", "xp", ",", "zp", ",", "*", "*", "kwargs", ")", "for", "zp", "in", "yp", "]", ")", ".", "transpose", "(", ")", "return", "y", ".", "reshape", "(", "x", ".", "shape", "[", ":", "1", "]", "+", "fp", ".", "shape", "[", "1", ":", "]", ")", ".", "astype", "(", "np", ".", "float32", ")" ]
Multi-dimensional linear interpolation. Returns the multi-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x. Note that *N and *M indicate zero or more dimensions. Args: x: An array of shape [*N], the x-coordinates of the interpolated values. xp: An np.array of shape [D], the x-coordinates of the data points, must be increasing. fp: An np.array of shape [D, *M], the y-coordinates of the data points. **kwargs: Keywords for np.interp. Returns: An array of shape [*N, *M], the interpolated values.
[ "Multi", "-", "dimensional", "linear", "interpolation", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L274-L294
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
step_interpolation
def step_interpolation(x, xp, fp, **kwargs): """Multi-dimensional step interpolation. Returns the multi-dimensional step interpolant to a function with given discrete data points (xp, fp), evaluated at x. Note that *N and *M indicate zero or more dimensions. Args: x: An array of shape [*N], the x-coordinates of the interpolated values. xp: An np.array of shape [D], the x-coordinates of the data points, must be increasing. fp: An np.array of shape [D, *M], the y-coordinates of the data points. **kwargs: Unused. Returns: An array of shape [*N, *M], the interpolated values. """ del kwargs # Unused. xp = np.expand_dims(xp, -1) lower, upper = xp[:-1], xp[1:] conditions = (x >= lower) & (x < upper) # Underflow and overflow conditions and values. Values default to fp[0] and # fp[-1] respectively. conditions = np.concatenate([[x < xp[0]], conditions, [x >= xp[-1]]]) values = np.concatenate([[fp[0]], fp]) assert np.all(np.sum(conditions, 0) == 1), 'xp must be increasing.' indices = np.argmax(conditions, 0) return values[indices].astype(np.float32)
python
def step_interpolation(x, xp, fp, **kwargs): """Multi-dimensional step interpolation. Returns the multi-dimensional step interpolant to a function with given discrete data points (xp, fp), evaluated at x. Note that *N and *M indicate zero or more dimensions. Args: x: An array of shape [*N], the x-coordinates of the interpolated values. xp: An np.array of shape [D], the x-coordinates of the data points, must be increasing. fp: An np.array of shape [D, *M], the y-coordinates of the data points. **kwargs: Unused. Returns: An array of shape [*N, *M], the interpolated values. """ del kwargs # Unused. xp = np.expand_dims(xp, -1) lower, upper = xp[:-1], xp[1:] conditions = (x >= lower) & (x < upper) # Underflow and overflow conditions and values. Values default to fp[0] and # fp[-1] respectively. conditions = np.concatenate([[x < xp[0]], conditions, [x >= xp[-1]]]) values = np.concatenate([[fp[0]], fp]) assert np.all(np.sum(conditions, 0) == 1), 'xp must be increasing.' indices = np.argmax(conditions, 0) return values[indices].astype(np.float32)
[ "def", "step_interpolation", "(", "x", ",", "xp", ",", "fp", ",", "*", "*", "kwargs", ")", ":", "del", "kwargs", "# Unused.", "xp", "=", "np", ".", "expand_dims", "(", "xp", ",", "-", "1", ")", "lower", ",", "upper", "=", "xp", "[", ":", "-", "1", "]", ",", "xp", "[", "1", ":", "]", "conditions", "=", "(", "x", ">=", "lower", ")", "&", "(", "x", "<", "upper", ")", "# Underflow and overflow conditions and values. Values default to fp[0] and", "# fp[-1] respectively.", "conditions", "=", "np", ".", "concatenate", "(", "[", "[", "x", "<", "xp", "[", "0", "]", "]", ",", "conditions", ",", "[", "x", ">=", "xp", "[", "-", "1", "]", "]", "]", ")", "values", "=", "np", ".", "concatenate", "(", "[", "[", "fp", "[", "0", "]", "]", ",", "fp", "]", ")", "assert", "np", ".", "all", "(", "np", ".", "sum", "(", "conditions", ",", "0", ")", "==", "1", ")", ",", "'xp must be increasing.'", "indices", "=", "np", ".", "argmax", "(", "conditions", ",", "0", ")", "return", "values", "[", "indices", "]", ".", "astype", "(", "np", ".", "float32", ")" ]
Multi-dimensional step interpolation. Returns the multi-dimensional step interpolant to a function with given discrete data points (xp, fp), evaluated at x. Note that *N and *M indicate zero or more dimensions. Args: x: An array of shape [*N], the x-coordinates of the interpolated values. xp: An np.array of shape [D], the x-coordinates of the data points, must be increasing. fp: An np.array of shape [D, *M], the y-coordinates of the data points. **kwargs: Unused. Returns: An array of shape [*N, *M], the interpolated values.
[ "Multi", "-", "dimensional", "step", "interpolation", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L297-L325
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
epoch_rates_to_pmf
def epoch_rates_to_pmf(problems, epoch_rates=None): """Create a probability-mass-function based on relative epoch rates. if epoch_rates=None, then we use uniform epoch rates [1.0] * len(problems) i.e. it takes each problem the same time to go through one epoch. If epoch_rates is given, then these are the relative numbers of epochs of each problem to go through in a given amount of time. Each must have problem.num_training_examples implemented. Args: problems: a list of Problem instances. epoch_rates: an optional list of float Returns: a list of floating point values. """ if epoch_rates is None: epoch_rates = [1.0] * len(problems) example_rates = [epoch_rate * p.num_training_examples for p, epoch_rate in zip(problems, epoch_rates)] return example_rates_to_pmf(example_rates)
python
def epoch_rates_to_pmf(problems, epoch_rates=None): """Create a probability-mass-function based on relative epoch rates. if epoch_rates=None, then we use uniform epoch rates [1.0] * len(problems) i.e. it takes each problem the same time to go through one epoch. If epoch_rates is given, then these are the relative numbers of epochs of each problem to go through in a given amount of time. Each must have problem.num_training_examples implemented. Args: problems: a list of Problem instances. epoch_rates: an optional list of float Returns: a list of floating point values. """ if epoch_rates is None: epoch_rates = [1.0] * len(problems) example_rates = [epoch_rate * p.num_training_examples for p, epoch_rate in zip(problems, epoch_rates)] return example_rates_to_pmf(example_rates)
[ "def", "epoch_rates_to_pmf", "(", "problems", ",", "epoch_rates", "=", "None", ")", ":", "if", "epoch_rates", "is", "None", ":", "epoch_rates", "=", "[", "1.0", "]", "*", "len", "(", "problems", ")", "example_rates", "=", "[", "epoch_rate", "*", "p", ".", "num_training_examples", "for", "p", ",", "epoch_rate", "in", "zip", "(", "problems", ",", "epoch_rates", ")", "]", "return", "example_rates_to_pmf", "(", "example_rates", ")" ]
Create a probability-mass-function based on relative epoch rates. if epoch_rates=None, then we use uniform epoch rates [1.0] * len(problems) i.e. it takes each problem the same time to go through one epoch. If epoch_rates is given, then these are the relative numbers of epochs of each problem to go through in a given amount of time. Each must have problem.num_training_examples implemented. Args: problems: a list of Problem instances. epoch_rates: an optional list of float Returns: a list of floating point values.
[ "Create", "a", "probability", "-", "mass", "-", "function", "based", "on", "relative", "epoch", "rates", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L353-L375
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
encode_schedule
def encode_schedule(schedule): """Encodes a schedule tuple into a string. Args: schedule: A tuple containing (interpolation, steps, pmfs), where interpolation is a string specifying the interpolation strategy, steps is an int array_like of shape [N] specifying the global steps, and pmfs is an array_like of shape [N, M] where pmf[i] is the sampling distribution at global step steps[i]. N is the number of schedule requirements to interpolate and M is the size of the probability space. Returns: The string encoding of the schedule tuple. """ interpolation, steps, pmfs = schedule return interpolation + ' ' + ' '.join( '@' + str(s) + ' ' + ' '.join(map(str, p)) for s, p in zip(steps, pmfs))
python
def encode_schedule(schedule): """Encodes a schedule tuple into a string. Args: schedule: A tuple containing (interpolation, steps, pmfs), where interpolation is a string specifying the interpolation strategy, steps is an int array_like of shape [N] specifying the global steps, and pmfs is an array_like of shape [N, M] where pmf[i] is the sampling distribution at global step steps[i]. N is the number of schedule requirements to interpolate and M is the size of the probability space. Returns: The string encoding of the schedule tuple. """ interpolation, steps, pmfs = schedule return interpolation + ' ' + ' '.join( '@' + str(s) + ' ' + ' '.join(map(str, p)) for s, p in zip(steps, pmfs))
[ "def", "encode_schedule", "(", "schedule", ")", ":", "interpolation", ",", "steps", ",", "pmfs", "=", "schedule", "return", "interpolation", "+", "' '", "+", "' '", ".", "join", "(", "'@'", "+", "str", "(", "s", ")", "+", "' '", "+", "' '", ".", "join", "(", "map", "(", "str", ",", "p", ")", ")", "for", "s", ",", "p", "in", "zip", "(", "steps", ",", "pmfs", ")", ")" ]
Encodes a schedule tuple into a string. Args: schedule: A tuple containing (interpolation, steps, pmfs), where interpolation is a string specifying the interpolation strategy, steps is an int array_like of shape [N] specifying the global steps, and pmfs is an array_like of shape [N, M] where pmf[i] is the sampling distribution at global step steps[i]. N is the number of schedule requirements to interpolate and M is the size of the probability space. Returns: The string encoding of the schedule tuple.
[ "Encodes", "a", "schedule", "tuple", "into", "a", "string", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L378-L394
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
decode_schedule
def decode_schedule(string): """Decodes a string into a schedule tuple. Args: string: The string encoding of a schedule tuple. Returns: A schedule tuple, see encode_schedule for details. """ splits = string.split() steps = [int(x[1:]) for x in splits[1:] if x[0] == '@'] pmfs = np.reshape( [float(x) for x in splits[1:] if x[0] != '@'], [len(steps), -1]) return splits[0], tuplize(steps), tuplize(pmfs)
python
def decode_schedule(string): """Decodes a string into a schedule tuple. Args: string: The string encoding of a schedule tuple. Returns: A schedule tuple, see encode_schedule for details. """ splits = string.split() steps = [int(x[1:]) for x in splits[1:] if x[0] == '@'] pmfs = np.reshape( [float(x) for x in splits[1:] if x[0] != '@'], [len(steps), -1]) return splits[0], tuplize(steps), tuplize(pmfs)
[ "def", "decode_schedule", "(", "string", ")", ":", "splits", "=", "string", ".", "split", "(", ")", "steps", "=", "[", "int", "(", "x", "[", "1", ":", "]", ")", "for", "x", "in", "splits", "[", "1", ":", "]", "if", "x", "[", "0", "]", "==", "'@'", "]", "pmfs", "=", "np", ".", "reshape", "(", "[", "float", "(", "x", ")", "for", "x", "in", "splits", "[", "1", ":", "]", "if", "x", "[", "0", "]", "!=", "'@'", "]", ",", "[", "len", "(", "steps", ")", ",", "-", "1", "]", ")", "return", "splits", "[", "0", "]", ",", "tuplize", "(", "steps", ")", ",", "tuplize", "(", "pmfs", ")" ]
Decodes a string into a schedule tuple. Args: string: The string encoding of a schedule tuple. Returns: A schedule tuple, see encode_schedule for details.
[ "Decodes", "a", "string", "into", "a", "schedule", "tuple", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L397-L410
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
tuplize
def tuplize(nested): """Recursively converts iterables into tuples. Args: nested: A nested structure of items and iterables. Returns: A nested structure of items and tuples. """ if isinstance(nested, str): return nested try: return tuple(map(tuplize, nested)) except TypeError: return nested
python
def tuplize(nested): """Recursively converts iterables into tuples. Args: nested: A nested structure of items and iterables. Returns: A nested structure of items and tuples. """ if isinstance(nested, str): return nested try: return tuple(map(tuplize, nested)) except TypeError: return nested
[ "def", "tuplize", "(", "nested", ")", ":", "if", "isinstance", "(", "nested", ",", "str", ")", ":", "return", "nested", "try", ":", "return", "tuple", "(", "map", "(", "tuplize", ",", "nested", ")", ")", "except", "TypeError", ":", "return", "nested" ]
Recursively converts iterables into tuples. Args: nested: A nested structure of items and iterables. Returns: A nested structure of items and tuples.
[ "Recursively", "converts", "iterables", "into", "tuples", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L413-L427
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
MultiProblemV2.filepattern
def filepattern(self, *args, **kwargs): """Returns a list of filepatterns, one for each problem.""" return [p.filepattern(*args, **kwargs) for p in self.problems]
python
def filepattern(self, *args, **kwargs): """Returns a list of filepatterns, one for each problem.""" return [p.filepattern(*args, **kwargs) for p in self.problems]
[ "def", "filepattern", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "return", "[", "p", ".", "filepattern", "(", "*", "args", ",", "*", "*", "kwargs", ")", "for", "p", "in", "self", ".", "problems", "]" ]
Returns a list of filepatterns, one for each problem.
[ "Returns", "a", "list", "of", "filepatterns", "one", "for", "each", "problem", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L82-L84
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
MultiProblemV2.generate_data
def generate_data(self, *args, **kwargs): """Generates data for each problem.""" for p in self.problems: p.generate_data(*args, **kwargs)
python
def generate_data(self, *args, **kwargs): """Generates data for each problem.""" for p in self.problems: p.generate_data(*args, **kwargs)
[ "def", "generate_data", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "for", "p", "in", "self", ".", "problems", ":", "p", ".", "generate_data", "(", "*", "args", ",", "*", "*", "kwargs", ")" ]
Generates data for each problem.
[ "Generates", "data", "for", "each", "problem", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L86-L89
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
MultiProblemV2.dataset
def dataset(self, mode, hparams=None, global_step=None, **kwargs): """Returns a dataset containing examples from multiple problems. Args: mode: A member of problem.DatasetSplit. hparams: A tf.HParams object, the model hparams. global_step: A scalar tensor used to compute the sampling distribution. If global_step is None, we call tf.train.get_or_create_global_step by default. **kwargs: Keywords for problem.Problem.Dataset. Returns: A dataset containing examples from multiple problems. """ datasets = [p.dataset(mode, **kwargs) for p in self.problems] datasets = [ d.map(lambda x, i=j: self.normalize_example( # pylint: disable=g-long-lambda dict(x, problem_id=tf.constant([i])), hparams)) for j, d in enumerate(datasets) # Tag examples with a problem_id. ] if mode is problem.DatasetSplit.TRAIN: if global_step is None: global_step = tf.train.get_or_create_global_step() pmf = get_schedule_distribution(self.schedule, global_step) return get_multi_dataset(datasets, pmf) elif self.only_eval_first_problem: return datasets[0] else: datasets = [d.repeat() for d in datasets] return tf.data.Dataset.zip(tuple(datasets)).flat_map( lambda *x: functools.reduce( # pylint: disable=g-long-lambda tf.data.Dataset.concatenate, map(tf.data.Dataset.from_tensors, x)))
python
def dataset(self, mode, hparams=None, global_step=None, **kwargs): """Returns a dataset containing examples from multiple problems. Args: mode: A member of problem.DatasetSplit. hparams: A tf.HParams object, the model hparams. global_step: A scalar tensor used to compute the sampling distribution. If global_step is None, we call tf.train.get_or_create_global_step by default. **kwargs: Keywords for problem.Problem.Dataset. Returns: A dataset containing examples from multiple problems. """ datasets = [p.dataset(mode, **kwargs) for p in self.problems] datasets = [ d.map(lambda x, i=j: self.normalize_example( # pylint: disable=g-long-lambda dict(x, problem_id=tf.constant([i])), hparams)) for j, d in enumerate(datasets) # Tag examples with a problem_id. ] if mode is problem.DatasetSplit.TRAIN: if global_step is None: global_step = tf.train.get_or_create_global_step() pmf = get_schedule_distribution(self.schedule, global_step) return get_multi_dataset(datasets, pmf) elif self.only_eval_first_problem: return datasets[0] else: datasets = [d.repeat() for d in datasets] return tf.data.Dataset.zip(tuple(datasets)).flat_map( lambda *x: functools.reduce( # pylint: disable=g-long-lambda tf.data.Dataset.concatenate, map(tf.data.Dataset.from_tensors, x)))
[ "def", "dataset", "(", "self", ",", "mode", ",", "hparams", "=", "None", ",", "global_step", "=", "None", ",", "*", "*", "kwargs", ")", ":", "datasets", "=", "[", "p", ".", "dataset", "(", "mode", ",", "*", "*", "kwargs", ")", "for", "p", "in", "self", ".", "problems", "]", "datasets", "=", "[", "d", ".", "map", "(", "lambda", "x", ",", "i", "=", "j", ":", "self", ".", "normalize_example", "(", "# pylint: disable=g-long-lambda", "dict", "(", "x", ",", "problem_id", "=", "tf", ".", "constant", "(", "[", "i", "]", ")", ")", ",", "hparams", ")", ")", "for", "j", ",", "d", "in", "enumerate", "(", "datasets", ")", "# Tag examples with a problem_id.", "]", "if", "mode", "is", "problem", ".", "DatasetSplit", ".", "TRAIN", ":", "if", "global_step", "is", "None", ":", "global_step", "=", "tf", ".", "train", ".", "get_or_create_global_step", "(", ")", "pmf", "=", "get_schedule_distribution", "(", "self", ".", "schedule", ",", "global_step", ")", "return", "get_multi_dataset", "(", "datasets", ",", "pmf", ")", "elif", "self", ".", "only_eval_first_problem", ":", "return", "datasets", "[", "0", "]", "else", ":", "datasets", "=", "[", "d", ".", "repeat", "(", ")", "for", "d", "in", "datasets", "]", "return", "tf", ".", "data", ".", "Dataset", ".", "zip", "(", "tuple", "(", "datasets", ")", ")", ".", "flat_map", "(", "lambda", "*", "x", ":", "functools", ".", "reduce", "(", "# pylint: disable=g-long-lambda", "tf", ".", "data", ".", "Dataset", ".", "concatenate", ",", "map", "(", "tf", ".", "data", ".", "Dataset", ".", "from_tensors", ",", "x", ")", ")", ")" ]
Returns a dataset containing examples from multiple problems. Args: mode: A member of problem.DatasetSplit. hparams: A tf.HParams object, the model hparams. global_step: A scalar tensor used to compute the sampling distribution. If global_step is None, we call tf.train.get_or_create_global_step by default. **kwargs: Keywords for problem.Problem.Dataset. Returns: A dataset containing examples from multiple problems.
[ "Returns", "a", "dataset", "containing", "examples", "from", "multiple", "problems", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L101-L133
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
MultiText2TextProblem.normalize_example
def normalize_example(self, example, hparams): """Assumes that example contains both inputs and targets.""" length = self.max_length(hparams) def _to_constant_shape(tensor): tensor = tensor[:length] tensor = tf.pad(tensor, [(0, length - tf.shape(tensor)[0])]) return tf.reshape(tensor, [length]) if self.has_inputs: example['inputs'] = _to_constant_shape(example['inputs']) example['targets'] = _to_constant_shape(example['targets']) elif 'inputs' in example: if self.packed_length: raise ValueError('cannot concatenate packed examples on the fly.') inputs = example.pop('inputs')[:-1] # Remove EOS token. targets = tf.concat([inputs, example['targets']], 0) example['targets'] = _to_constant_shape(targets) else: example['targets'] = _to_constant_shape(example['targets']) if self.packed_length: if self.has_inputs: if 'inputs_segmentation' in example: example['inputs_segmentation'] = _to_constant_shape( example['inputs_segmentation']) example['inputs_position'] = _to_constant_shape( example['inputs_position']) else: example['inputs_segmentation'] = tf.to_int64( tf.not_equal(example['inputs'], 0)) example['inputs_position'] = ( example['inputs_segmentation'] * tf.range(length, dtype=tf.int64)) if 'targets_segmentation' in example: example['targets_segmentation'] = _to_constant_shape( example['targets_segmentation']) example['targets_position'] = _to_constant_shape( example['targets_position']) else: example['targets_segmentation'] = tf.to_int64( tf.not_equal(example['targets'], 0)) example['targets_position'] = ( example['targets_segmentation'] * tf.range(length, dtype=tf.int64)) return example
python
def normalize_example(self, example, hparams): """Assumes that example contains both inputs and targets.""" length = self.max_length(hparams) def _to_constant_shape(tensor): tensor = tensor[:length] tensor = tf.pad(tensor, [(0, length - tf.shape(tensor)[0])]) return tf.reshape(tensor, [length]) if self.has_inputs: example['inputs'] = _to_constant_shape(example['inputs']) example['targets'] = _to_constant_shape(example['targets']) elif 'inputs' in example: if self.packed_length: raise ValueError('cannot concatenate packed examples on the fly.') inputs = example.pop('inputs')[:-1] # Remove EOS token. targets = tf.concat([inputs, example['targets']], 0) example['targets'] = _to_constant_shape(targets) else: example['targets'] = _to_constant_shape(example['targets']) if self.packed_length: if self.has_inputs: if 'inputs_segmentation' in example: example['inputs_segmentation'] = _to_constant_shape( example['inputs_segmentation']) example['inputs_position'] = _to_constant_shape( example['inputs_position']) else: example['inputs_segmentation'] = tf.to_int64( tf.not_equal(example['inputs'], 0)) example['inputs_position'] = ( example['inputs_segmentation'] * tf.range(length, dtype=tf.int64)) if 'targets_segmentation' in example: example['targets_segmentation'] = _to_constant_shape( example['targets_segmentation']) example['targets_position'] = _to_constant_shape( example['targets_position']) else: example['targets_segmentation'] = tf.to_int64( tf.not_equal(example['targets'], 0)) example['targets_position'] = ( example['targets_segmentation'] * tf.range(length, dtype=tf.int64)) return example
[ "def", "normalize_example", "(", "self", ",", "example", ",", "hparams", ")", ":", "length", "=", "self", ".", "max_length", "(", "hparams", ")", "def", "_to_constant_shape", "(", "tensor", ")", ":", "tensor", "=", "tensor", "[", ":", "length", "]", "tensor", "=", "tf", ".", "pad", "(", "tensor", ",", "[", "(", "0", ",", "length", "-", "tf", ".", "shape", "(", "tensor", ")", "[", "0", "]", ")", "]", ")", "return", "tf", ".", "reshape", "(", "tensor", ",", "[", "length", "]", ")", "if", "self", ".", "has_inputs", ":", "example", "[", "'inputs'", "]", "=", "_to_constant_shape", "(", "example", "[", "'inputs'", "]", ")", "example", "[", "'targets'", "]", "=", "_to_constant_shape", "(", "example", "[", "'targets'", "]", ")", "elif", "'inputs'", "in", "example", ":", "if", "self", ".", "packed_length", ":", "raise", "ValueError", "(", "'cannot concatenate packed examples on the fly.'", ")", "inputs", "=", "example", ".", "pop", "(", "'inputs'", ")", "[", ":", "-", "1", "]", "# Remove EOS token.", "targets", "=", "tf", ".", "concat", "(", "[", "inputs", ",", "example", "[", "'targets'", "]", "]", ",", "0", ")", "example", "[", "'targets'", "]", "=", "_to_constant_shape", "(", "targets", ")", "else", ":", "example", "[", "'targets'", "]", "=", "_to_constant_shape", "(", "example", "[", "'targets'", "]", ")", "if", "self", ".", "packed_length", ":", "if", "self", ".", "has_inputs", ":", "if", "'inputs_segmentation'", "in", "example", ":", "example", "[", "'inputs_segmentation'", "]", "=", "_to_constant_shape", "(", "example", "[", "'inputs_segmentation'", "]", ")", "example", "[", "'inputs_position'", "]", "=", "_to_constant_shape", "(", "example", "[", "'inputs_position'", "]", ")", "else", ":", "example", "[", "'inputs_segmentation'", "]", "=", "tf", ".", "to_int64", "(", "tf", ".", "not_equal", "(", "example", "[", "'inputs'", "]", ",", "0", ")", ")", "example", "[", "'inputs_position'", "]", "=", "(", "example", "[", "'inputs_segmentation'", "]", "*", "tf", ".", "range", "(", "length", ",", "dtype", "=", "tf", ".", "int64", ")", ")", "if", "'targets_segmentation'", "in", "example", ":", "example", "[", "'targets_segmentation'", "]", "=", "_to_constant_shape", "(", "example", "[", "'targets_segmentation'", "]", ")", "example", "[", "'targets_position'", "]", "=", "_to_constant_shape", "(", "example", "[", "'targets_position'", "]", ")", "else", ":", "example", "[", "'targets_segmentation'", "]", "=", "tf", ".", "to_int64", "(", "tf", ".", "not_equal", "(", "example", "[", "'targets'", "]", ",", "0", ")", ")", "example", "[", "'targets_position'", "]", "=", "(", "example", "[", "'targets_segmentation'", "]", "*", "tf", ".", "range", "(", "length", ",", "dtype", "=", "tf", ".", "int64", ")", ")", "return", "example" ]
Assumes that example contains both inputs and targets.
[ "Assumes", "that", "example", "contains", "both", "inputs", "and", "targets", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L139-L181
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/multi_problem_v2.py
MultiText2TextProblem.generate_data_with_shared_vocab
def generate_data_with_shared_vocab(self, data_dir, tmp_dir, task_id=-1): """Generates TF-Records for problems using a global vocabulary file.""" global_vocab_filename = os.path.join(data_dir, self.vocab_filename) if not tf.gfile.Exists(global_vocab_filename): raise ValueError( 'Global vocabulary file: %s does not exist, ' 'please create one using build_vocab.py' % global_vocab_filename) # Before generating data, we copy the global vocabulary file to the children # locations. Although this is not the most disk efficient strategy, it # imposes the fewest changes to the text-to-text API. for p in self.problems: local_vocab_filename = os.path.join(data_dir, p.vocab_filename) if not tf.gfile.Exists(local_vocab_filename): tf.gfile.Copy(global_vocab_filename, local_vocab_filename) p.generate_data(data_dir, tmp_dir, task_id)
python
def generate_data_with_shared_vocab(self, data_dir, tmp_dir, task_id=-1): """Generates TF-Records for problems using a global vocabulary file.""" global_vocab_filename = os.path.join(data_dir, self.vocab_filename) if not tf.gfile.Exists(global_vocab_filename): raise ValueError( 'Global vocabulary file: %s does not exist, ' 'please create one using build_vocab.py' % global_vocab_filename) # Before generating data, we copy the global vocabulary file to the children # locations. Although this is not the most disk efficient strategy, it # imposes the fewest changes to the text-to-text API. for p in self.problems: local_vocab_filename = os.path.join(data_dir, p.vocab_filename) if not tf.gfile.Exists(local_vocab_filename): tf.gfile.Copy(global_vocab_filename, local_vocab_filename) p.generate_data(data_dir, tmp_dir, task_id)
[ "def", "generate_data_with_shared_vocab", "(", "self", ",", "data_dir", ",", "tmp_dir", ",", "task_id", "=", "-", "1", ")", ":", "global_vocab_filename", "=", "os", ".", "path", ".", "join", "(", "data_dir", ",", "self", ".", "vocab_filename", ")", "if", "not", "tf", ".", "gfile", ".", "Exists", "(", "global_vocab_filename", ")", ":", "raise", "ValueError", "(", "'Global vocabulary file: %s does not exist, '", "'please create one using build_vocab.py'", "%", "global_vocab_filename", ")", "# Before generating data, we copy the global vocabulary file to the children", "# locations. Although this is not the most disk efficient strategy, it", "# imposes the fewest changes to the text-to-text API.", "for", "p", "in", "self", ".", "problems", ":", "local_vocab_filename", "=", "os", ".", "path", ".", "join", "(", "data_dir", ",", "p", ".", "vocab_filename", ")", "if", "not", "tf", ".", "gfile", ".", "Exists", "(", "local_vocab_filename", ")", ":", "tf", ".", "gfile", ".", "Copy", "(", "global_vocab_filename", ",", "local_vocab_filename", ")", "p", ".", "generate_data", "(", "data_dir", ",", "tmp_dir", ",", "task_id", ")" ]
Generates TF-Records for problems using a global vocabulary file.
[ "Generates", "TF", "-", "Records", "for", "problems", "using", "a", "global", "vocabulary", "file", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/multi_problem_v2.py#L183-L197
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
lengths_to_area_mask
def lengths_to_area_mask(feature_length, length, max_area_size): """Generates a non-padding mask for areas based on lengths. Args: feature_length: a tensor of [batch_size] length: the length of the batch max_area_size: the maximum area size considered Returns: mask: a tensor in shape of [batch_size, num_areas] """ paddings = tf.cast(tf.expand_dims( tf.logical_not( tf.sequence_mask(feature_length, maxlen=length)), 2), tf.float32) _, _, area_sum, _, _ = compute_area_features(paddings, max_area_width=max_area_size) mask = tf.squeeze(tf.logical_not(tf.cast(area_sum, tf.bool)), [2]) return mask
python
def lengths_to_area_mask(feature_length, length, max_area_size): """Generates a non-padding mask for areas based on lengths. Args: feature_length: a tensor of [batch_size] length: the length of the batch max_area_size: the maximum area size considered Returns: mask: a tensor in shape of [batch_size, num_areas] """ paddings = tf.cast(tf.expand_dims( tf.logical_not( tf.sequence_mask(feature_length, maxlen=length)), 2), tf.float32) _, _, area_sum, _, _ = compute_area_features(paddings, max_area_width=max_area_size) mask = tf.squeeze(tf.logical_not(tf.cast(area_sum, tf.bool)), [2]) return mask
[ "def", "lengths_to_area_mask", "(", "feature_length", ",", "length", ",", "max_area_size", ")", ":", "paddings", "=", "tf", ".", "cast", "(", "tf", ".", "expand_dims", "(", "tf", ".", "logical_not", "(", "tf", ".", "sequence_mask", "(", "feature_length", ",", "maxlen", "=", "length", ")", ")", ",", "2", ")", ",", "tf", ".", "float32", ")", "_", ",", "_", ",", "area_sum", ",", "_", ",", "_", "=", "compute_area_features", "(", "paddings", ",", "max_area_width", "=", "max_area_size", ")", "mask", "=", "tf", ".", "squeeze", "(", "tf", ".", "logical_not", "(", "tf", ".", "cast", "(", "area_sum", ",", "tf", ".", "bool", ")", ")", ",", "[", "2", "]", ")", "return", "mask" ]
Generates a non-padding mask for areas based on lengths. Args: feature_length: a tensor of [batch_size] length: the length of the batch max_area_size: the maximum area size considered Returns: mask: a tensor in shape of [batch_size, num_areas]
[ "Generates", "a", "non", "-", "padding", "mask", "for", "areas", "based", "on", "lengths", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L27-L44
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
_pool_one_shape
def _pool_one_shape(features_2d, area_width, area_height, batch_size, width, height, depth, fn=tf.reduce_max, name=None): """Pools for an area in features_2d. Args: features_2d: a Tensor in a shape of [batch_size, height, width, depth]. area_width: the max width allowed for an area. area_height: the max height allowed for an area. batch_size: the batch size. width: the width of the memory. height: the height of the memory. depth: the depth of the features. fn: the TF function for the pooling. name: the op name. Returns: pool_tensor: A Tensor of shape [batch_size, num_areas, depth] """ with tf.name_scope(name, default_name="pool_one_shape"): images = [] for y_shift in range(area_height): image_height = tf.maximum(height - area_height + 1 + y_shift, 0) for x_shift in range(area_width): image_width = tf.maximum(width - area_width + 1 + x_shift, 0) area = features_2d[:, y_shift:image_height, x_shift:image_width, :] flatten_area = tf.reshape(area, [batch_size, -1, depth, 1]) images.append(flatten_area) image_tensor = tf.concat(images, axis=3) max_tensor = fn(image_tensor, axis=3) return max_tensor
python
def _pool_one_shape(features_2d, area_width, area_height, batch_size, width, height, depth, fn=tf.reduce_max, name=None): """Pools for an area in features_2d. Args: features_2d: a Tensor in a shape of [batch_size, height, width, depth]. area_width: the max width allowed for an area. area_height: the max height allowed for an area. batch_size: the batch size. width: the width of the memory. height: the height of the memory. depth: the depth of the features. fn: the TF function for the pooling. name: the op name. Returns: pool_tensor: A Tensor of shape [batch_size, num_areas, depth] """ with tf.name_scope(name, default_name="pool_one_shape"): images = [] for y_shift in range(area_height): image_height = tf.maximum(height - area_height + 1 + y_shift, 0) for x_shift in range(area_width): image_width = tf.maximum(width - area_width + 1 + x_shift, 0) area = features_2d[:, y_shift:image_height, x_shift:image_width, :] flatten_area = tf.reshape(area, [batch_size, -1, depth, 1]) images.append(flatten_area) image_tensor = tf.concat(images, axis=3) max_tensor = fn(image_tensor, axis=3) return max_tensor
[ "def", "_pool_one_shape", "(", "features_2d", ",", "area_width", ",", "area_height", ",", "batch_size", ",", "width", ",", "height", ",", "depth", ",", "fn", "=", "tf", ".", "reduce_max", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "name_scope", "(", "name", ",", "default_name", "=", "\"pool_one_shape\"", ")", ":", "images", "=", "[", "]", "for", "y_shift", "in", "range", "(", "area_height", ")", ":", "image_height", "=", "tf", ".", "maximum", "(", "height", "-", "area_height", "+", "1", "+", "y_shift", ",", "0", ")", "for", "x_shift", "in", "range", "(", "area_width", ")", ":", "image_width", "=", "tf", ".", "maximum", "(", "width", "-", "area_width", "+", "1", "+", "x_shift", ",", "0", ")", "area", "=", "features_2d", "[", ":", ",", "y_shift", ":", "image_height", ",", "x_shift", ":", "image_width", ",", ":", "]", "flatten_area", "=", "tf", ".", "reshape", "(", "area", ",", "[", "batch_size", ",", "-", "1", ",", "depth", ",", "1", "]", ")", "images", ".", "append", "(", "flatten_area", ")", "image_tensor", "=", "tf", ".", "concat", "(", "images", ",", "axis", "=", "3", ")", "max_tensor", "=", "fn", "(", "image_tensor", ",", "axis", "=", "3", ")", "return", "max_tensor" ]
Pools for an area in features_2d. Args: features_2d: a Tensor in a shape of [batch_size, height, width, depth]. area_width: the max width allowed for an area. area_height: the max height allowed for an area. batch_size: the batch size. width: the width of the memory. height: the height of the memory. depth: the depth of the features. fn: the TF function for the pooling. name: the op name. Returns: pool_tensor: A Tensor of shape [batch_size, num_areas, depth]
[ "Pools", "for", "an", "area", "in", "features_2d", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L47-L75
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
basic_pool
def basic_pool(features, max_area_width, max_area_height=1, height=1, fn=tf.reduce_max, name=None): """Pools for each area based on a given pooling function (fn). Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. fn: the TF function for the pooling. name: the namescope. Returns: pool_results: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1] """ with tf.name_scope(name, default_name="basic_pool"): feature_shape = common_layers.shape_list(features) batch_size = feature_shape[0] length = feature_shape[-2] depth = feature_shape[-1] width = length // height features_2d = tf.reshape(features, [batch_size, height, width, depth]) height_list = [] width_list = [] pool_list = [] size_tensor = tf.ones_like(features_2d[:, :, :, 0], dtype=tf.int32) for area_height in range(max_area_height): for area_width in range(max_area_width): pool_tensor = _pool_one_shape(features_2d, area_width=area_width + 1, area_height=area_height + 1, batch_size=batch_size, width=width, height=height, depth=depth, fn=fn) pool_list.append( tf.reshape(pool_tensor, [batch_size, -1, depth])) height_list.append( tf.reshape( size_tensor[:, area_height:, area_width:] *\ (area_height + 1), [batch_size, -1])) width_list.append( tf.reshape( size_tensor[:, area_height:, area_width:] *\ (area_width + 1), [batch_size, -1])) pool_results = tf.concat(pool_list, axis=1) area_heights = tf.expand_dims(tf.concat(height_list, axis=1), 2) area_widths = tf.expand_dims(tf.concat(width_list, axis=1), 2) return pool_results, area_heights, area_widths
python
def basic_pool(features, max_area_width, max_area_height=1, height=1, fn=tf.reduce_max, name=None): """Pools for each area based on a given pooling function (fn). Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. fn: the TF function for the pooling. name: the namescope. Returns: pool_results: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1] """ with tf.name_scope(name, default_name="basic_pool"): feature_shape = common_layers.shape_list(features) batch_size = feature_shape[0] length = feature_shape[-2] depth = feature_shape[-1] width = length // height features_2d = tf.reshape(features, [batch_size, height, width, depth]) height_list = [] width_list = [] pool_list = [] size_tensor = tf.ones_like(features_2d[:, :, :, 0], dtype=tf.int32) for area_height in range(max_area_height): for area_width in range(max_area_width): pool_tensor = _pool_one_shape(features_2d, area_width=area_width + 1, area_height=area_height + 1, batch_size=batch_size, width=width, height=height, depth=depth, fn=fn) pool_list.append( tf.reshape(pool_tensor, [batch_size, -1, depth])) height_list.append( tf.reshape( size_tensor[:, area_height:, area_width:] *\ (area_height + 1), [batch_size, -1])) width_list.append( tf.reshape( size_tensor[:, area_height:, area_width:] *\ (area_width + 1), [batch_size, -1])) pool_results = tf.concat(pool_list, axis=1) area_heights = tf.expand_dims(tf.concat(height_list, axis=1), 2) area_widths = tf.expand_dims(tf.concat(width_list, axis=1), 2) return pool_results, area_heights, area_widths
[ "def", "basic_pool", "(", "features", ",", "max_area_width", ",", "max_area_height", "=", "1", ",", "height", "=", "1", ",", "fn", "=", "tf", ".", "reduce_max", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "name_scope", "(", "name", ",", "default_name", "=", "\"basic_pool\"", ")", ":", "feature_shape", "=", "common_layers", ".", "shape_list", "(", "features", ")", "batch_size", "=", "feature_shape", "[", "0", "]", "length", "=", "feature_shape", "[", "-", "2", "]", "depth", "=", "feature_shape", "[", "-", "1", "]", "width", "=", "length", "//", "height", "features_2d", "=", "tf", ".", "reshape", "(", "features", ",", "[", "batch_size", ",", "height", ",", "width", ",", "depth", "]", ")", "height_list", "=", "[", "]", "width_list", "=", "[", "]", "pool_list", "=", "[", "]", "size_tensor", "=", "tf", ".", "ones_like", "(", "features_2d", "[", ":", ",", ":", ",", ":", ",", "0", "]", ",", "dtype", "=", "tf", ".", "int32", ")", "for", "area_height", "in", "range", "(", "max_area_height", ")", ":", "for", "area_width", "in", "range", "(", "max_area_width", ")", ":", "pool_tensor", "=", "_pool_one_shape", "(", "features_2d", ",", "area_width", "=", "area_width", "+", "1", ",", "area_height", "=", "area_height", "+", "1", ",", "batch_size", "=", "batch_size", ",", "width", "=", "width", ",", "height", "=", "height", ",", "depth", "=", "depth", ",", "fn", "=", "fn", ")", "pool_list", ".", "append", "(", "tf", ".", "reshape", "(", "pool_tensor", ",", "[", "batch_size", ",", "-", "1", ",", "depth", "]", ")", ")", "height_list", ".", "append", "(", "tf", ".", "reshape", "(", "size_tensor", "[", ":", ",", "area_height", ":", ",", "area_width", ":", "]", "*", "(", "area_height", "+", "1", ")", ",", "[", "batch_size", ",", "-", "1", "]", ")", ")", "width_list", ".", "append", "(", "tf", ".", "reshape", "(", "size_tensor", "[", ":", ",", "area_height", ":", ",", "area_width", ":", "]", "*", "(", "area_width", "+", "1", ")", ",", "[", "batch_size", ",", "-", "1", "]", ")", ")", "pool_results", "=", "tf", ".", "concat", "(", "pool_list", ",", "axis", "=", "1", ")", "area_heights", "=", "tf", ".", "expand_dims", "(", "tf", ".", "concat", "(", "height_list", ",", "axis", "=", "1", ")", ",", "2", ")", "area_widths", "=", "tf", ".", "expand_dims", "(", "tf", ".", "concat", "(", "width_list", ",", "axis", "=", "1", ")", ",", "2", ")", "return", "pool_results", ",", "area_heights", ",", "area_widths" ]
Pools for each area based on a given pooling function (fn). Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. fn: the TF function for the pooling. name: the namescope. Returns: pool_results: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1]
[ "Pools", "for", "each", "area", "based", "on", "a", "given", "pooling", "function", "(", "fn", ")", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L78-L128
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
_compute_sum_image
def _compute_sum_image(features, max_area_width, max_area_height=1, height=1, name=None): """Computes area sums for features. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. name: the namescope. Returns: sum_image: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1] """ with tf.name_scope(name, default_name="compute_sum_image"): feature_shape = common_layers.shape_list(features) batch_size = feature_shape[0] length = feature_shape[-2] depth = feature_shape[-1] width = length // height features_2d = tf.reshape(features, [batch_size, height, width, depth]) width_cum = tf.cumsum(features_2d, axis=-2, name="compute_integral_h") integral_image = tf.cumsum(width_cum, axis=-3, name="compute_integral_v") padded_image = tf.pad( integral_image, [[0, 0], [1, 0], [1, 0], [0, 0]], constant_values=0) height_list = [] width_list = [] dst_images = [] src_images_diag = [] src_images_h = [] src_images_v = [] size_tensor = tf.ones_like(padded_image[:, :, :, 0], dtype=tf.int32) for area_height in range(max_area_height): for area_width in range(max_area_width): dst_images.append( tf.reshape( padded_image[:, area_height + 1:, area_width + 1:, :], [batch_size, -1, depth])) src_images_diag.append( tf.reshape( padded_image[:, :-area_height - 1, :-area_width - 1, :], [batch_size, -1, depth])) src_images_h.append( tf.reshape( padded_image[:, area_height + 1:, :-area_width - 1, :], [batch_size, -1, depth])) src_images_v.append( tf.reshape( padded_image[:, :-area_height - 1, area_width + 1:, :], [batch_size, -1, depth])) height_list.append( tf.reshape( size_tensor[:, area_height + 1:, area_width + 1:] *\ (area_height + 1), [batch_size, -1])) width_list.append( tf.reshape( size_tensor[:, area_height + 1:, area_width + 1:] *\ (area_width + 1), [batch_size, -1])) sum_image = tf.subtract( tf.concat(dst_images, axis=1) + tf.concat(src_images_diag, axis=1), tf.concat(src_images_v, axis=1) + tf.concat(src_images_h, axis=1)) area_heights = tf.expand_dims(tf.concat(height_list, axis=1), 2) area_widths = tf.expand_dims(tf.concat(width_list, axis=1), 2) return sum_image, area_heights, area_widths
python
def _compute_sum_image(features, max_area_width, max_area_height=1, height=1, name=None): """Computes area sums for features. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. name: the namescope. Returns: sum_image: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1] """ with tf.name_scope(name, default_name="compute_sum_image"): feature_shape = common_layers.shape_list(features) batch_size = feature_shape[0] length = feature_shape[-2] depth = feature_shape[-1] width = length // height features_2d = tf.reshape(features, [batch_size, height, width, depth]) width_cum = tf.cumsum(features_2d, axis=-2, name="compute_integral_h") integral_image = tf.cumsum(width_cum, axis=-3, name="compute_integral_v") padded_image = tf.pad( integral_image, [[0, 0], [1, 0], [1, 0], [0, 0]], constant_values=0) height_list = [] width_list = [] dst_images = [] src_images_diag = [] src_images_h = [] src_images_v = [] size_tensor = tf.ones_like(padded_image[:, :, :, 0], dtype=tf.int32) for area_height in range(max_area_height): for area_width in range(max_area_width): dst_images.append( tf.reshape( padded_image[:, area_height + 1:, area_width + 1:, :], [batch_size, -1, depth])) src_images_diag.append( tf.reshape( padded_image[:, :-area_height - 1, :-area_width - 1, :], [batch_size, -1, depth])) src_images_h.append( tf.reshape( padded_image[:, area_height + 1:, :-area_width - 1, :], [batch_size, -1, depth])) src_images_v.append( tf.reshape( padded_image[:, :-area_height - 1, area_width + 1:, :], [batch_size, -1, depth])) height_list.append( tf.reshape( size_tensor[:, area_height + 1:, area_width + 1:] *\ (area_height + 1), [batch_size, -1])) width_list.append( tf.reshape( size_tensor[:, area_height + 1:, area_width + 1:] *\ (area_width + 1), [batch_size, -1])) sum_image = tf.subtract( tf.concat(dst_images, axis=1) + tf.concat(src_images_diag, axis=1), tf.concat(src_images_v, axis=1) + tf.concat(src_images_h, axis=1)) area_heights = tf.expand_dims(tf.concat(height_list, axis=1), 2) area_widths = tf.expand_dims(tf.concat(width_list, axis=1), 2) return sum_image, area_heights, area_widths
[ "def", "_compute_sum_image", "(", "features", ",", "max_area_width", ",", "max_area_height", "=", "1", ",", "height", "=", "1", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "name_scope", "(", "name", ",", "default_name", "=", "\"compute_sum_image\"", ")", ":", "feature_shape", "=", "common_layers", ".", "shape_list", "(", "features", ")", "batch_size", "=", "feature_shape", "[", "0", "]", "length", "=", "feature_shape", "[", "-", "2", "]", "depth", "=", "feature_shape", "[", "-", "1", "]", "width", "=", "length", "//", "height", "features_2d", "=", "tf", ".", "reshape", "(", "features", ",", "[", "batch_size", ",", "height", ",", "width", ",", "depth", "]", ")", "width_cum", "=", "tf", ".", "cumsum", "(", "features_2d", ",", "axis", "=", "-", "2", ",", "name", "=", "\"compute_integral_h\"", ")", "integral_image", "=", "tf", ".", "cumsum", "(", "width_cum", ",", "axis", "=", "-", "3", ",", "name", "=", "\"compute_integral_v\"", ")", "padded_image", "=", "tf", ".", "pad", "(", "integral_image", ",", "[", "[", "0", ",", "0", "]", ",", "[", "1", ",", "0", "]", ",", "[", "1", ",", "0", "]", ",", "[", "0", ",", "0", "]", "]", ",", "constant_values", "=", "0", ")", "height_list", "=", "[", "]", "width_list", "=", "[", "]", "dst_images", "=", "[", "]", "src_images_diag", "=", "[", "]", "src_images_h", "=", "[", "]", "src_images_v", "=", "[", "]", "size_tensor", "=", "tf", ".", "ones_like", "(", "padded_image", "[", ":", ",", ":", ",", ":", ",", "0", "]", ",", "dtype", "=", "tf", ".", "int32", ")", "for", "area_height", "in", "range", "(", "max_area_height", ")", ":", "for", "area_width", "in", "range", "(", "max_area_width", ")", ":", "dst_images", ".", "append", "(", "tf", ".", "reshape", "(", "padded_image", "[", ":", ",", "area_height", "+", "1", ":", ",", "area_width", "+", "1", ":", ",", ":", "]", ",", "[", "batch_size", ",", "-", "1", ",", "depth", "]", ")", ")", "src_images_diag", ".", "append", "(", "tf", ".", "reshape", "(", "padded_image", "[", ":", ",", ":", "-", "area_height", "-", "1", ",", ":", "-", "area_width", "-", "1", ",", ":", "]", ",", "[", "batch_size", ",", "-", "1", ",", "depth", "]", ")", ")", "src_images_h", ".", "append", "(", "tf", ".", "reshape", "(", "padded_image", "[", ":", ",", "area_height", "+", "1", ":", ",", ":", "-", "area_width", "-", "1", ",", ":", "]", ",", "[", "batch_size", ",", "-", "1", ",", "depth", "]", ")", ")", "src_images_v", ".", "append", "(", "tf", ".", "reshape", "(", "padded_image", "[", ":", ",", ":", "-", "area_height", "-", "1", ",", "area_width", "+", "1", ":", ",", ":", "]", ",", "[", "batch_size", ",", "-", "1", ",", "depth", "]", ")", ")", "height_list", ".", "append", "(", "tf", ".", "reshape", "(", "size_tensor", "[", ":", ",", "area_height", "+", "1", ":", ",", "area_width", "+", "1", ":", "]", "*", "(", "area_height", "+", "1", ")", ",", "[", "batch_size", ",", "-", "1", "]", ")", ")", "width_list", ".", "append", "(", "tf", ".", "reshape", "(", "size_tensor", "[", ":", ",", "area_height", "+", "1", ":", ",", "area_width", "+", "1", ":", "]", "*", "(", "area_width", "+", "1", ")", ",", "[", "batch_size", ",", "-", "1", "]", ")", ")", "sum_image", "=", "tf", ".", "subtract", "(", "tf", ".", "concat", "(", "dst_images", ",", "axis", "=", "1", ")", "+", "tf", ".", "concat", "(", "src_images_diag", ",", "axis", "=", "1", ")", ",", "tf", ".", "concat", "(", "src_images_v", ",", "axis", "=", "1", ")", "+", "tf", ".", "concat", "(", "src_images_h", ",", "axis", "=", "1", ")", ")", "area_heights", "=", "tf", ".", "expand_dims", "(", "tf", ".", "concat", "(", "height_list", ",", "axis", "=", "1", ")", ",", "2", ")", "area_widths", "=", "tf", ".", "expand_dims", "(", "tf", ".", "concat", "(", "width_list", ",", "axis", "=", "1", ")", ",", "2", ")", "return", "sum_image", ",", "area_heights", ",", "area_widths" ]
Computes area sums for features. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. name: the namescope. Returns: sum_image: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1]
[ "Computes", "area", "sums", "for", "features", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L131-L196
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
compute_area_features
def compute_area_features(features, max_area_width, max_area_height=1, height=1, epsilon=1e-6): """Computes features for each area. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. epsilon: the epsilon added to the variance for computing standard deviation. Returns: area_mean: A Tensor of shape [batch_size, num_areas, depth] area_std: A Tensor of shape [batch_size, num_areas, depth] area_sum: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1] """ with tf.name_scope("compute_area_features"): tf.logging.info("area_attention compute_area_features: %d x %d", max_area_height, max_area_width) area_sum, area_heights, area_widths = _compute_sum_image( features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) area_squared_sum, _, _ = _compute_sum_image( tf.pow(features, 2), max_area_width=max_area_width, max_area_height=max_area_height, height=height) sizes = tf.multiply(area_heights, area_widths) float_area_sizes = tf.to_float(sizes) area_mean = tf.div(area_sum, float_area_sizes) s2_n = tf.div(area_squared_sum, float_area_sizes) area_variance = tf.subtract(s2_n, tf.pow(area_mean, 2)) area_std = tf.sqrt(tf.abs(area_variance) + epsilon) return area_mean, area_std, area_sum, area_heights, area_widths
python
def compute_area_features(features, max_area_width, max_area_height=1, height=1, epsilon=1e-6): """Computes features for each area. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. epsilon: the epsilon added to the variance for computing standard deviation. Returns: area_mean: A Tensor of shape [batch_size, num_areas, depth] area_std: A Tensor of shape [batch_size, num_areas, depth] area_sum: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1] """ with tf.name_scope("compute_area_features"): tf.logging.info("area_attention compute_area_features: %d x %d", max_area_height, max_area_width) area_sum, area_heights, area_widths = _compute_sum_image( features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) area_squared_sum, _, _ = _compute_sum_image( tf.pow(features, 2), max_area_width=max_area_width, max_area_height=max_area_height, height=height) sizes = tf.multiply(area_heights, area_widths) float_area_sizes = tf.to_float(sizes) area_mean = tf.div(area_sum, float_area_sizes) s2_n = tf.div(area_squared_sum, float_area_sizes) area_variance = tf.subtract(s2_n, tf.pow(area_mean, 2)) area_std = tf.sqrt(tf.abs(area_variance) + epsilon) return area_mean, area_std, area_sum, area_heights, area_widths
[ "def", "compute_area_features", "(", "features", ",", "max_area_width", ",", "max_area_height", "=", "1", ",", "height", "=", "1", ",", "epsilon", "=", "1e-6", ")", ":", "with", "tf", ".", "name_scope", "(", "\"compute_area_features\"", ")", ":", "tf", ".", "logging", ".", "info", "(", "\"area_attention compute_area_features: %d x %d\"", ",", "max_area_height", ",", "max_area_width", ")", "area_sum", ",", "area_heights", ",", "area_widths", "=", "_compute_sum_image", "(", "features", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "height", ")", "area_squared_sum", ",", "_", ",", "_", "=", "_compute_sum_image", "(", "tf", ".", "pow", "(", "features", ",", "2", ")", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "height", ")", "sizes", "=", "tf", ".", "multiply", "(", "area_heights", ",", "area_widths", ")", "float_area_sizes", "=", "tf", ".", "to_float", "(", "sizes", ")", "area_mean", "=", "tf", ".", "div", "(", "area_sum", ",", "float_area_sizes", ")", "s2_n", "=", "tf", ".", "div", "(", "area_squared_sum", ",", "float_area_sizes", ")", "area_variance", "=", "tf", ".", "subtract", "(", "s2_n", ",", "tf", ".", "pow", "(", "area_mean", ",", "2", ")", ")", "area_std", "=", "tf", ".", "sqrt", "(", "tf", ".", "abs", "(", "area_variance", ")", "+", "epsilon", ")", "return", "area_mean", ",", "area_std", ",", "area_sum", ",", "area_heights", ",", "area_widths" ]
Computes features for each area. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. epsilon: the epsilon added to the variance for computing standard deviation. Returns: area_mean: A Tensor of shape [batch_size, num_areas, depth] area_std: A Tensor of shape [batch_size, num_areas, depth] area_sum: A Tensor of shape [batch_size, num_areas, depth] area_heights: A Tensor of shape [batch_size, num_areas, 1] area_widths: A Tensor of shape [batch_size, num_areas, 1]
[ "Computes", "features", "for", "each", "area", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L199-L231
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
compute_area_key
def compute_area_key(features, max_area_width, max_area_height=1, height=1, mode="mean", training=True, name=None): """Computes the key for each area. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. mode: whether to combine different area features or only use the vector mean of each area, which can be "mean", "concat", "sum", "sample_concat", and "sample_sum". training: indicating if it is in the training mode. name: the name for setting the variable scope. Returns: area_key: a Tensor in the shape of [batch_size, num_areas, depth] """ tf.logging.info("area_attention mode=%s", mode) area_mean, area_std, _, area_heights, area_widths =\ compute_area_features(features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) if mode == "mean": return area_mean elif mode == "max": area_max, _, _ = basic_pool(features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) return area_max elif mode == "sample": if training: area_mean += (area_std * tf.random_normal(tf.shape(area_std))) return area_mean with tf.variable_scope( name, default_name="combine_area_features", values=[area_mean, area_std, area_heights, area_widths]): depth = common_layers.shape_list(area_mean)[-1] height_embed = tf.nn.embedding_lookup( params=tf.get_variable("area_height_emb", [max_area_height, depth // 2]), ids=area_heights[:, :, 0] - 1) width_embed = tf.nn.embedding_lookup( params=tf.get_variable("area_width_emb", [max_area_width, depth // 2]), ids=area_widths[:, :, 0] - 1) size_embed = tf.concat([height_embed, width_embed], -1) if mode == "concat": feature_concat = tf.concat([area_mean, area_std, size_embed], -1) elif mode == "max_concat": area_max, _, _ = basic_pool(features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) feature_concat = tf.concat([area_max, size_embed], -1) elif mode == "sum": feature_concat = size_embed + area_mean + area_std elif mode == "sample_concat": if training: area_mean += (area_std * tf.random_normal(tf.shape(area_std))) feature_concat = tf.concat([area_mean, size_embed], -1) elif mode == "sample_sum": if training: area_mean += (area_std * tf.random_normal(tf.shape(area_std))) feature_concat = area_mean + size_embed else: raise ValueError("Unsupported area key mode=%s" % mode) feature_hidden = tf.layers.dense(inputs=feature_concat, units=depth, activation=tf.nn.relu) area_key = tf.layers.dense(feature_hidden, units=depth) return area_key
python
def compute_area_key(features, max_area_width, max_area_height=1, height=1, mode="mean", training=True, name=None): """Computes the key for each area. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. mode: whether to combine different area features or only use the vector mean of each area, which can be "mean", "concat", "sum", "sample_concat", and "sample_sum". training: indicating if it is in the training mode. name: the name for setting the variable scope. Returns: area_key: a Tensor in the shape of [batch_size, num_areas, depth] """ tf.logging.info("area_attention mode=%s", mode) area_mean, area_std, _, area_heights, area_widths =\ compute_area_features(features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) if mode == "mean": return area_mean elif mode == "max": area_max, _, _ = basic_pool(features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) return area_max elif mode == "sample": if training: area_mean += (area_std * tf.random_normal(tf.shape(area_std))) return area_mean with tf.variable_scope( name, default_name="combine_area_features", values=[area_mean, area_std, area_heights, area_widths]): depth = common_layers.shape_list(area_mean)[-1] height_embed = tf.nn.embedding_lookup( params=tf.get_variable("area_height_emb", [max_area_height, depth // 2]), ids=area_heights[:, :, 0] - 1) width_embed = tf.nn.embedding_lookup( params=tf.get_variable("area_width_emb", [max_area_width, depth // 2]), ids=area_widths[:, :, 0] - 1) size_embed = tf.concat([height_embed, width_embed], -1) if mode == "concat": feature_concat = tf.concat([area_mean, area_std, size_embed], -1) elif mode == "max_concat": area_max, _, _ = basic_pool(features, max_area_width=max_area_width, max_area_height=max_area_height, height=height) feature_concat = tf.concat([area_max, size_embed], -1) elif mode == "sum": feature_concat = size_embed + area_mean + area_std elif mode == "sample_concat": if training: area_mean += (area_std * tf.random_normal(tf.shape(area_std))) feature_concat = tf.concat([area_mean, size_embed], -1) elif mode == "sample_sum": if training: area_mean += (area_std * tf.random_normal(tf.shape(area_std))) feature_concat = area_mean + size_embed else: raise ValueError("Unsupported area key mode=%s" % mode) feature_hidden = tf.layers.dense(inputs=feature_concat, units=depth, activation=tf.nn.relu) area_key = tf.layers.dense(feature_hidden, units=depth) return area_key
[ "def", "compute_area_key", "(", "features", ",", "max_area_width", ",", "max_area_height", "=", "1", ",", "height", "=", "1", ",", "mode", "=", "\"mean\"", ",", "training", "=", "True", ",", "name", "=", "None", ")", ":", "tf", ".", "logging", ".", "info", "(", "\"area_attention mode=%s\"", ",", "mode", ")", "area_mean", ",", "area_std", ",", "_", ",", "area_heights", ",", "area_widths", "=", "compute_area_features", "(", "features", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "height", ")", "if", "mode", "==", "\"mean\"", ":", "return", "area_mean", "elif", "mode", "==", "\"max\"", ":", "area_max", ",", "_", ",", "_", "=", "basic_pool", "(", "features", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "height", ")", "return", "area_max", "elif", "mode", "==", "\"sample\"", ":", "if", "training", ":", "area_mean", "+=", "(", "area_std", "*", "tf", ".", "random_normal", "(", "tf", ".", "shape", "(", "area_std", ")", ")", ")", "return", "area_mean", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"combine_area_features\"", ",", "values", "=", "[", "area_mean", ",", "area_std", ",", "area_heights", ",", "area_widths", "]", ")", ":", "depth", "=", "common_layers", ".", "shape_list", "(", "area_mean", ")", "[", "-", "1", "]", "height_embed", "=", "tf", ".", "nn", ".", "embedding_lookup", "(", "params", "=", "tf", ".", "get_variable", "(", "\"area_height_emb\"", ",", "[", "max_area_height", ",", "depth", "//", "2", "]", ")", ",", "ids", "=", "area_heights", "[", ":", ",", ":", ",", "0", "]", "-", "1", ")", "width_embed", "=", "tf", ".", "nn", ".", "embedding_lookup", "(", "params", "=", "tf", ".", "get_variable", "(", "\"area_width_emb\"", ",", "[", "max_area_width", ",", "depth", "//", "2", "]", ")", ",", "ids", "=", "area_widths", "[", ":", ",", ":", ",", "0", "]", "-", "1", ")", "size_embed", "=", "tf", ".", "concat", "(", "[", "height_embed", ",", "width_embed", "]", ",", "-", "1", ")", "if", "mode", "==", "\"concat\"", ":", "feature_concat", "=", "tf", ".", "concat", "(", "[", "area_mean", ",", "area_std", ",", "size_embed", "]", ",", "-", "1", ")", "elif", "mode", "==", "\"max_concat\"", ":", "area_max", ",", "_", ",", "_", "=", "basic_pool", "(", "features", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "height", ")", "feature_concat", "=", "tf", ".", "concat", "(", "[", "area_max", ",", "size_embed", "]", ",", "-", "1", ")", "elif", "mode", "==", "\"sum\"", ":", "feature_concat", "=", "size_embed", "+", "area_mean", "+", "area_std", "elif", "mode", "==", "\"sample_concat\"", ":", "if", "training", ":", "area_mean", "+=", "(", "area_std", "*", "tf", ".", "random_normal", "(", "tf", ".", "shape", "(", "area_std", ")", ")", ")", "feature_concat", "=", "tf", ".", "concat", "(", "[", "area_mean", ",", "size_embed", "]", ",", "-", "1", ")", "elif", "mode", "==", "\"sample_sum\"", ":", "if", "training", ":", "area_mean", "+=", "(", "area_std", "*", "tf", ".", "random_normal", "(", "tf", ".", "shape", "(", "area_std", ")", ")", ")", "feature_concat", "=", "area_mean", "+", "size_embed", "else", ":", "raise", "ValueError", "(", "\"Unsupported area key mode=%s\"", "%", "mode", ")", "feature_hidden", "=", "tf", ".", "layers", ".", "dense", "(", "inputs", "=", "feature_concat", ",", "units", "=", "depth", ",", "activation", "=", "tf", ".", "nn", ".", "relu", ")", "area_key", "=", "tf", ".", "layers", ".", "dense", "(", "feature_hidden", ",", "units", "=", "depth", ")", "return", "area_key" ]
Computes the key for each area. Args: features: a Tensor in a shape of [batch_size, height * width, depth]. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. height: the height of the image. mode: whether to combine different area features or only use the vector mean of each area, which can be "mean", "concat", "sum", "sample_concat", and "sample_sum". training: indicating if it is in the training mode. name: the name for setting the variable scope. Returns: area_key: a Tensor in the shape of [batch_size, num_areas, depth]
[ "Computes", "the", "key", "for", "each", "area", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L234-L302
train
tensorflow/tensor2tensor
tensor2tensor/layers/area_attention.py
dot_product_area_attention
def dot_product_area_attention(q, k, v, bias, dropout_rate=0.0, image_shapes=None, name=None, attention_image_summary=None, save_weights_to=None, dropout_broadcast_dims=None, max_area_width=1, max_area_height=1, memory_height=1, area_key_mode="mean", area_value_mode="sum", top_k_areas=0, area_temperature=1.0, training=True): """Dot-product area attention. Args: q: Tensor with shape [..., length_q, depth_k]. k: Tensor with shape [..., length_kv, depth_k]. Leading dimensions must match with q. v: Tensor with shape [..., length_kv, depth_v] Leading dimensions must match with q. bias: bias Tensor (see attention_bias()) dropout_rate: a float. image_shapes: optional tuple of integer scalars. see comments for attention_image_summary() name: an optional string attention_image_summary: the callback for making image summary of attention. save_weights_to: an optional dictionary to capture attention weights for visualization; the weights tensor will be appended there under a string key created from the variable scope (including name). dropout_broadcast_dims: an optional list of integers less than rank of q. Specifies in which dimensions to broadcast the dropout decisions. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. memory_height: the height of the memory. area_key_mode: the mode for computing area keys, which can be "mean", "concat", "sum", "sample_concat", and "sample_sum". area_value_mode: the mode for computing area values, which can be either "mean", or "sum". top_k_areas: Use the top key areas for attention. area_temperature: the temperature for attention softmax. training: indicating if it is in the training mode. Returns: Tensor with shape [..., length_q, depth_v]. """ tf.logging.info("dot_product_area_attention: " "area_h=%d, area_w=%d, mem_h=%d, " "area_key_mode=%s, area_value_mode=%s, " "area_temperature=%f", max_area_height, max_area_width, memory_height, area_key_mode, area_value_mode, area_temperature) with tf.variable_scope( name, default_name="dot_product_area_attention", values=[q, k, v]) as scope: mem_shape = common_layers.shape_list(k) batch_size = mem_shape[0] head_size = mem_shape[1] length = mem_shape[2] depth = mem_shape[3] k_area = compute_area_key( tf.reshape(k, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height, mode=area_key_mode, training=training) if area_value_mode == "mean": v_area, _, _, _, _ = compute_area_features( tf.reshape(v, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height) elif area_value_mode == "max": v_area, _, _ = basic_pool(tf.reshape(v, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height, fn=tf.reduce_max) elif area_value_mode == "sum": _, _, v_area, _, _ = compute_area_features( tf.reshape(v, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height) else: raise ValueError("Unsupported area value mode=%s" % area_value_mode) k = tf.reshape(k_area, [batch_size, head_size, -1, depth]) v = tf.reshape(v_area, [batch_size, head_size, -1, depth]) logits = tf.matmul(q, k, transpose_b=True) # [..., length_q, length_kv] if bias is not None: bias = common_layers.cast_like(bias, logits) with tf.name_scope("compute_area_att_bias", values=[bias]): bias_shape = common_layers.shape_list(bias) mem_length = bias_shape[-1] bias_values = tf.reshape( tf.to_float(tf.less(bias, -1)), [-1, mem_length, 1]) _, _, padding_sum, _, _ = compute_area_features( bias_values, max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height) bias = tf.where( tf.cast(tf.to_int32(padding_sum), tf.bool), tf.fill(tf.shape(padding_sum), -np.inf), tf.zeros_like(padding_sum, dtype=tf.float32)) bias = tf.reshape(bias, [bias_shape[0], bias_shape[1], bias_shape[2], -1]) logits += bias logits = logits / area_temperature weights = tf.nn.softmax(logits, name="attention_weights") if top_k_areas > 0: tf.logging.info("area_attention top_k_areas=%d", top_k_areas) top_k = tf.minimum(common_layers.shape_list(weights)[-1], top_k_areas) top_weights, _ = tf.nn.top_k(weights, k=top_k) min_values = tf.reduce_min(top_weights, -1, keepdims=True) weights = tf.where(tf.greater_equal(weights, min_values), weights, tf.zeros_like(weights)) weights = tf.div(weights, tf.reduce_sum(weights, -1, keepdims=True)) if save_weights_to is not None: save_weights_to[scope.name] = weights save_weights_to[scope.name + "/logits"] = logits # Drop out attention links for each head. weights = common_layers.dropout_with_broadcast_dims( weights, 1.0 - dropout_rate, broadcast_dims=dropout_broadcast_dims) if common_layers.should_generate_summaries() and attention_image_summary: attention_image_summary(weights, image_shapes) return tf.matmul(weights, v)
python
def dot_product_area_attention(q, k, v, bias, dropout_rate=0.0, image_shapes=None, name=None, attention_image_summary=None, save_weights_to=None, dropout_broadcast_dims=None, max_area_width=1, max_area_height=1, memory_height=1, area_key_mode="mean", area_value_mode="sum", top_k_areas=0, area_temperature=1.0, training=True): """Dot-product area attention. Args: q: Tensor with shape [..., length_q, depth_k]. k: Tensor with shape [..., length_kv, depth_k]. Leading dimensions must match with q. v: Tensor with shape [..., length_kv, depth_v] Leading dimensions must match with q. bias: bias Tensor (see attention_bias()) dropout_rate: a float. image_shapes: optional tuple of integer scalars. see comments for attention_image_summary() name: an optional string attention_image_summary: the callback for making image summary of attention. save_weights_to: an optional dictionary to capture attention weights for visualization; the weights tensor will be appended there under a string key created from the variable scope (including name). dropout_broadcast_dims: an optional list of integers less than rank of q. Specifies in which dimensions to broadcast the dropout decisions. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. memory_height: the height of the memory. area_key_mode: the mode for computing area keys, which can be "mean", "concat", "sum", "sample_concat", and "sample_sum". area_value_mode: the mode for computing area values, which can be either "mean", or "sum". top_k_areas: Use the top key areas for attention. area_temperature: the temperature for attention softmax. training: indicating if it is in the training mode. Returns: Tensor with shape [..., length_q, depth_v]. """ tf.logging.info("dot_product_area_attention: " "area_h=%d, area_w=%d, mem_h=%d, " "area_key_mode=%s, area_value_mode=%s, " "area_temperature=%f", max_area_height, max_area_width, memory_height, area_key_mode, area_value_mode, area_temperature) with tf.variable_scope( name, default_name="dot_product_area_attention", values=[q, k, v]) as scope: mem_shape = common_layers.shape_list(k) batch_size = mem_shape[0] head_size = mem_shape[1] length = mem_shape[2] depth = mem_shape[3] k_area = compute_area_key( tf.reshape(k, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height, mode=area_key_mode, training=training) if area_value_mode == "mean": v_area, _, _, _, _ = compute_area_features( tf.reshape(v, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height) elif area_value_mode == "max": v_area, _, _ = basic_pool(tf.reshape(v, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height, fn=tf.reduce_max) elif area_value_mode == "sum": _, _, v_area, _, _ = compute_area_features( tf.reshape(v, [-1, length, depth]), max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height) else: raise ValueError("Unsupported area value mode=%s" % area_value_mode) k = tf.reshape(k_area, [batch_size, head_size, -1, depth]) v = tf.reshape(v_area, [batch_size, head_size, -1, depth]) logits = tf.matmul(q, k, transpose_b=True) # [..., length_q, length_kv] if bias is not None: bias = common_layers.cast_like(bias, logits) with tf.name_scope("compute_area_att_bias", values=[bias]): bias_shape = common_layers.shape_list(bias) mem_length = bias_shape[-1] bias_values = tf.reshape( tf.to_float(tf.less(bias, -1)), [-1, mem_length, 1]) _, _, padding_sum, _, _ = compute_area_features( bias_values, max_area_width=max_area_width, max_area_height=max_area_height, height=memory_height) bias = tf.where( tf.cast(tf.to_int32(padding_sum), tf.bool), tf.fill(tf.shape(padding_sum), -np.inf), tf.zeros_like(padding_sum, dtype=tf.float32)) bias = tf.reshape(bias, [bias_shape[0], bias_shape[1], bias_shape[2], -1]) logits += bias logits = logits / area_temperature weights = tf.nn.softmax(logits, name="attention_weights") if top_k_areas > 0: tf.logging.info("area_attention top_k_areas=%d", top_k_areas) top_k = tf.minimum(common_layers.shape_list(weights)[-1], top_k_areas) top_weights, _ = tf.nn.top_k(weights, k=top_k) min_values = tf.reduce_min(top_weights, -1, keepdims=True) weights = tf.where(tf.greater_equal(weights, min_values), weights, tf.zeros_like(weights)) weights = tf.div(weights, tf.reduce_sum(weights, -1, keepdims=True)) if save_weights_to is not None: save_weights_to[scope.name] = weights save_weights_to[scope.name + "/logits"] = logits # Drop out attention links for each head. weights = common_layers.dropout_with_broadcast_dims( weights, 1.0 - dropout_rate, broadcast_dims=dropout_broadcast_dims) if common_layers.should_generate_summaries() and attention_image_summary: attention_image_summary(weights, image_shapes) return tf.matmul(weights, v)
[ "def", "dot_product_area_attention", "(", "q", ",", "k", ",", "v", ",", "bias", ",", "dropout_rate", "=", "0.0", ",", "image_shapes", "=", "None", ",", "name", "=", "None", ",", "attention_image_summary", "=", "None", ",", "save_weights_to", "=", "None", ",", "dropout_broadcast_dims", "=", "None", ",", "max_area_width", "=", "1", ",", "max_area_height", "=", "1", ",", "memory_height", "=", "1", ",", "area_key_mode", "=", "\"mean\"", ",", "area_value_mode", "=", "\"sum\"", ",", "top_k_areas", "=", "0", ",", "area_temperature", "=", "1.0", ",", "training", "=", "True", ")", ":", "tf", ".", "logging", ".", "info", "(", "\"dot_product_area_attention: \"", "\"area_h=%d, area_w=%d, mem_h=%d, \"", "\"area_key_mode=%s, area_value_mode=%s, \"", "\"area_temperature=%f\"", ",", "max_area_height", ",", "max_area_width", ",", "memory_height", ",", "area_key_mode", ",", "area_value_mode", ",", "area_temperature", ")", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"dot_product_area_attention\"", ",", "values", "=", "[", "q", ",", "k", ",", "v", "]", ")", "as", "scope", ":", "mem_shape", "=", "common_layers", ".", "shape_list", "(", "k", ")", "batch_size", "=", "mem_shape", "[", "0", "]", "head_size", "=", "mem_shape", "[", "1", "]", "length", "=", "mem_shape", "[", "2", "]", "depth", "=", "mem_shape", "[", "3", "]", "k_area", "=", "compute_area_key", "(", "tf", ".", "reshape", "(", "k", ",", "[", "-", "1", ",", "length", ",", "depth", "]", ")", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "memory_height", ",", "mode", "=", "area_key_mode", ",", "training", "=", "training", ")", "if", "area_value_mode", "==", "\"mean\"", ":", "v_area", ",", "_", ",", "_", ",", "_", ",", "_", "=", "compute_area_features", "(", "tf", ".", "reshape", "(", "v", ",", "[", "-", "1", ",", "length", ",", "depth", "]", ")", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "memory_height", ")", "elif", "area_value_mode", "==", "\"max\"", ":", "v_area", ",", "_", ",", "_", "=", "basic_pool", "(", "tf", ".", "reshape", "(", "v", ",", "[", "-", "1", ",", "length", ",", "depth", "]", ")", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "memory_height", ",", "fn", "=", "tf", ".", "reduce_max", ")", "elif", "area_value_mode", "==", "\"sum\"", ":", "_", ",", "_", ",", "v_area", ",", "_", ",", "_", "=", "compute_area_features", "(", "tf", ".", "reshape", "(", "v", ",", "[", "-", "1", ",", "length", ",", "depth", "]", ")", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "memory_height", ")", "else", ":", "raise", "ValueError", "(", "\"Unsupported area value mode=%s\"", "%", "area_value_mode", ")", "k", "=", "tf", ".", "reshape", "(", "k_area", ",", "[", "batch_size", ",", "head_size", ",", "-", "1", ",", "depth", "]", ")", "v", "=", "tf", ".", "reshape", "(", "v_area", ",", "[", "batch_size", ",", "head_size", ",", "-", "1", ",", "depth", "]", ")", "logits", "=", "tf", ".", "matmul", "(", "q", ",", "k", ",", "transpose_b", "=", "True", ")", "# [..., length_q, length_kv]", "if", "bias", "is", "not", "None", ":", "bias", "=", "common_layers", ".", "cast_like", "(", "bias", ",", "logits", ")", "with", "tf", ".", "name_scope", "(", "\"compute_area_att_bias\"", ",", "values", "=", "[", "bias", "]", ")", ":", "bias_shape", "=", "common_layers", ".", "shape_list", "(", "bias", ")", "mem_length", "=", "bias_shape", "[", "-", "1", "]", "bias_values", "=", "tf", ".", "reshape", "(", "tf", ".", "to_float", "(", "tf", ".", "less", "(", "bias", ",", "-", "1", ")", ")", ",", "[", "-", "1", ",", "mem_length", ",", "1", "]", ")", "_", ",", "_", ",", "padding_sum", ",", "_", ",", "_", "=", "compute_area_features", "(", "bias_values", ",", "max_area_width", "=", "max_area_width", ",", "max_area_height", "=", "max_area_height", ",", "height", "=", "memory_height", ")", "bias", "=", "tf", ".", "where", "(", "tf", ".", "cast", "(", "tf", ".", "to_int32", "(", "padding_sum", ")", ",", "tf", ".", "bool", ")", ",", "tf", ".", "fill", "(", "tf", ".", "shape", "(", "padding_sum", ")", ",", "-", "np", ".", "inf", ")", ",", "tf", ".", "zeros_like", "(", "padding_sum", ",", "dtype", "=", "tf", ".", "float32", ")", ")", "bias", "=", "tf", ".", "reshape", "(", "bias", ",", "[", "bias_shape", "[", "0", "]", ",", "bias_shape", "[", "1", "]", ",", "bias_shape", "[", "2", "]", ",", "-", "1", "]", ")", "logits", "+=", "bias", "logits", "=", "logits", "/", "area_temperature", "weights", "=", "tf", ".", "nn", ".", "softmax", "(", "logits", ",", "name", "=", "\"attention_weights\"", ")", "if", "top_k_areas", ">", "0", ":", "tf", ".", "logging", ".", "info", "(", "\"area_attention top_k_areas=%d\"", ",", "top_k_areas", ")", "top_k", "=", "tf", ".", "minimum", "(", "common_layers", ".", "shape_list", "(", "weights", ")", "[", "-", "1", "]", ",", "top_k_areas", ")", "top_weights", ",", "_", "=", "tf", ".", "nn", ".", "top_k", "(", "weights", ",", "k", "=", "top_k", ")", "min_values", "=", "tf", ".", "reduce_min", "(", "top_weights", ",", "-", "1", ",", "keepdims", "=", "True", ")", "weights", "=", "tf", ".", "where", "(", "tf", ".", "greater_equal", "(", "weights", ",", "min_values", ")", ",", "weights", ",", "tf", ".", "zeros_like", "(", "weights", ")", ")", "weights", "=", "tf", ".", "div", "(", "weights", ",", "tf", ".", "reduce_sum", "(", "weights", ",", "-", "1", ",", "keepdims", "=", "True", ")", ")", "if", "save_weights_to", "is", "not", "None", ":", "save_weights_to", "[", "scope", ".", "name", "]", "=", "weights", "save_weights_to", "[", "scope", ".", "name", "+", "\"/logits\"", "]", "=", "logits", "# Drop out attention links for each head.", "weights", "=", "common_layers", ".", "dropout_with_broadcast_dims", "(", "weights", ",", "1.0", "-", "dropout_rate", ",", "broadcast_dims", "=", "dropout_broadcast_dims", ")", "if", "common_layers", ".", "should_generate_summaries", "(", ")", "and", "attention_image_summary", ":", "attention_image_summary", "(", "weights", ",", "image_shapes", ")", "return", "tf", ".", "matmul", "(", "weights", ",", "v", ")" ]
Dot-product area attention. Args: q: Tensor with shape [..., length_q, depth_k]. k: Tensor with shape [..., length_kv, depth_k]. Leading dimensions must match with q. v: Tensor with shape [..., length_kv, depth_v] Leading dimensions must match with q. bias: bias Tensor (see attention_bias()) dropout_rate: a float. image_shapes: optional tuple of integer scalars. see comments for attention_image_summary() name: an optional string attention_image_summary: the callback for making image summary of attention. save_weights_to: an optional dictionary to capture attention weights for visualization; the weights tensor will be appended there under a string key created from the variable scope (including name). dropout_broadcast_dims: an optional list of integers less than rank of q. Specifies in which dimensions to broadcast the dropout decisions. max_area_width: the max width allowed for an area. max_area_height: the max height allowed for an area. memory_height: the height of the memory. area_key_mode: the mode for computing area keys, which can be "mean", "concat", "sum", "sample_concat", and "sample_sum". area_value_mode: the mode for computing area values, which can be either "mean", or "sum". top_k_areas: Use the top key areas for attention. area_temperature: the temperature for attention softmax. training: indicating if it is in the training mode. Returns: Tensor with shape [..., length_q, depth_v].
[ "Dot", "-", "product", "area", "attention", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/area_attention.py#L305-L433
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
setup_directories
def setup_directories(base_dir, subdirs): """Setup directories.""" base_dir = os.path.expanduser(base_dir) tf.gfile.MakeDirs(base_dir) all_dirs = {} for subdir in subdirs: if isinstance(subdir, six.string_types): subdir_tuple = (subdir,) else: subdir_tuple = subdir dir_name = os.path.join(base_dir, *subdir_tuple) tf.gfile.MakeDirs(dir_name) all_dirs[subdir] = dir_name return all_dirs
python
def setup_directories(base_dir, subdirs): """Setup directories.""" base_dir = os.path.expanduser(base_dir) tf.gfile.MakeDirs(base_dir) all_dirs = {} for subdir in subdirs: if isinstance(subdir, six.string_types): subdir_tuple = (subdir,) else: subdir_tuple = subdir dir_name = os.path.join(base_dir, *subdir_tuple) tf.gfile.MakeDirs(dir_name) all_dirs[subdir] = dir_name return all_dirs
[ "def", "setup_directories", "(", "base_dir", ",", "subdirs", ")", ":", "base_dir", "=", "os", ".", "path", ".", "expanduser", "(", "base_dir", ")", "tf", ".", "gfile", ".", "MakeDirs", "(", "base_dir", ")", "all_dirs", "=", "{", "}", "for", "subdir", "in", "subdirs", ":", "if", "isinstance", "(", "subdir", ",", "six", ".", "string_types", ")", ":", "subdir_tuple", "=", "(", "subdir", ",", ")", "else", ":", "subdir_tuple", "=", "subdir", "dir_name", "=", "os", ".", "path", ".", "join", "(", "base_dir", ",", "*", "subdir_tuple", ")", "tf", ".", "gfile", ".", "MakeDirs", "(", "dir_name", ")", "all_dirs", "[", "subdir", "]", "=", "dir_name", "return", "all_dirs" ]
Setup directories.
[ "Setup", "directories", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L68-L82
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
make_relative_timing_fn
def make_relative_timing_fn(): """Make a function that logs the duration since it was made.""" start_time = time.time() def format_relative_time(): time_delta = time.time() - start_time return str(datetime.timedelta(seconds=time_delta)) def log_relative_time(): tf.logging.info("Timing: %s", format_relative_time()) return log_relative_time
python
def make_relative_timing_fn(): """Make a function that logs the duration since it was made.""" start_time = time.time() def format_relative_time(): time_delta = time.time() - start_time return str(datetime.timedelta(seconds=time_delta)) def log_relative_time(): tf.logging.info("Timing: %s", format_relative_time()) return log_relative_time
[ "def", "make_relative_timing_fn", "(", ")", ":", "start_time", "=", "time", ".", "time", "(", ")", "def", "format_relative_time", "(", ")", ":", "time_delta", "=", "time", ".", "time", "(", ")", "-", "start_time", "return", "str", "(", "datetime", ".", "timedelta", "(", "seconds", "=", "time_delta", ")", ")", "def", "log_relative_time", "(", ")", ":", "tf", ".", "logging", ".", "info", "(", "\"Timing: %s\"", ",", "format_relative_time", "(", ")", ")", "return", "log_relative_time" ]
Make a function that logs the duration since it was made.
[ "Make", "a", "function", "that", "logs", "the", "duration", "since", "it", "was", "made", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L85-L96
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
train_supervised
def train_supervised(problem, model_name, hparams, data_dir, output_dir, train_steps, eval_steps, local_eval_frequency=None, schedule="continuous_train_and_eval"): """Train supervised.""" if local_eval_frequency is None: local_eval_frequency = FLAGS.local_eval_frequency exp_fn = trainer_lib.create_experiment_fn( model_name, problem, data_dir, train_steps, eval_steps, min_eval_frequency=local_eval_frequency ) run_config = trainer_lib.create_run_config(model_name, model_dir=output_dir) exp = exp_fn(run_config, hparams) getattr(exp, schedule)()
python
def train_supervised(problem, model_name, hparams, data_dir, output_dir, train_steps, eval_steps, local_eval_frequency=None, schedule="continuous_train_and_eval"): """Train supervised.""" if local_eval_frequency is None: local_eval_frequency = FLAGS.local_eval_frequency exp_fn = trainer_lib.create_experiment_fn( model_name, problem, data_dir, train_steps, eval_steps, min_eval_frequency=local_eval_frequency ) run_config = trainer_lib.create_run_config(model_name, model_dir=output_dir) exp = exp_fn(run_config, hparams) getattr(exp, schedule)()
[ "def", "train_supervised", "(", "problem", ",", "model_name", ",", "hparams", ",", "data_dir", ",", "output_dir", ",", "train_steps", ",", "eval_steps", ",", "local_eval_frequency", "=", "None", ",", "schedule", "=", "\"continuous_train_and_eval\"", ")", ":", "if", "local_eval_frequency", "is", "None", ":", "local_eval_frequency", "=", "FLAGS", ".", "local_eval_frequency", "exp_fn", "=", "trainer_lib", ".", "create_experiment_fn", "(", "model_name", ",", "problem", ",", "data_dir", ",", "train_steps", ",", "eval_steps", ",", "min_eval_frequency", "=", "local_eval_frequency", ")", "run_config", "=", "trainer_lib", ".", "create_run_config", "(", "model_name", ",", "model_dir", "=", "output_dir", ")", "exp", "=", "exp_fn", "(", "run_config", ",", "hparams", ")", "getattr", "(", "exp", ",", "schedule", ")", "(", ")" ]
Train supervised.
[ "Train", "supervised", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L125-L138
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
train_agent
def train_agent(real_env, learner, world_model_dir, hparams, epoch): """Train the PPO agent in the simulated environment.""" initial_frame_chooser = rl_utils.make_initial_frame_chooser( real_env, hparams.frame_stack_size, hparams.simulation_random_starts, hparams.simulation_flip_first_random_for_beginning ) env_fn = rl.make_simulated_env_fn_from_hparams( real_env, hparams, batch_size=hparams.simulated_batch_size, initial_frame_chooser=initial_frame_chooser, model_dir=world_model_dir, sim_video_dir=os.path.join( learner.agent_model_dir, "sim_videos_{}".format(epoch) ) ) base_algo_str = hparams.base_algo train_hparams = trainer_lib.create_hparams(hparams.base_algo_params) if hparams.wm_policy_param_sharing: train_hparams.optimizer_zero_grads = True rl_utils.update_hparams_from_hparams( train_hparams, hparams, base_algo_str + "_" ) final_epoch = hparams.epochs - 1 is_special_epoch = (epoch + 3) == final_epoch or (epoch + 7) == final_epoch is_final_epoch = epoch == final_epoch env_step_multiplier = 3 if is_final_epoch else 2 if is_special_epoch else 1 learner.train( env_fn, train_hparams, simulated=True, save_continuously=True, epoch=epoch, env_step_multiplier=env_step_multiplier )
python
def train_agent(real_env, learner, world_model_dir, hparams, epoch): """Train the PPO agent in the simulated environment.""" initial_frame_chooser = rl_utils.make_initial_frame_chooser( real_env, hparams.frame_stack_size, hparams.simulation_random_starts, hparams.simulation_flip_first_random_for_beginning ) env_fn = rl.make_simulated_env_fn_from_hparams( real_env, hparams, batch_size=hparams.simulated_batch_size, initial_frame_chooser=initial_frame_chooser, model_dir=world_model_dir, sim_video_dir=os.path.join( learner.agent_model_dir, "sim_videos_{}".format(epoch) ) ) base_algo_str = hparams.base_algo train_hparams = trainer_lib.create_hparams(hparams.base_algo_params) if hparams.wm_policy_param_sharing: train_hparams.optimizer_zero_grads = True rl_utils.update_hparams_from_hparams( train_hparams, hparams, base_algo_str + "_" ) final_epoch = hparams.epochs - 1 is_special_epoch = (epoch + 3) == final_epoch or (epoch + 7) == final_epoch is_final_epoch = epoch == final_epoch env_step_multiplier = 3 if is_final_epoch else 2 if is_special_epoch else 1 learner.train( env_fn, train_hparams, simulated=True, save_continuously=True, epoch=epoch, env_step_multiplier=env_step_multiplier )
[ "def", "train_agent", "(", "real_env", ",", "learner", ",", "world_model_dir", ",", "hparams", ",", "epoch", ")", ":", "initial_frame_chooser", "=", "rl_utils", ".", "make_initial_frame_chooser", "(", "real_env", ",", "hparams", ".", "frame_stack_size", ",", "hparams", ".", "simulation_random_starts", ",", "hparams", ".", "simulation_flip_first_random_for_beginning", ")", "env_fn", "=", "rl", ".", "make_simulated_env_fn_from_hparams", "(", "real_env", ",", "hparams", ",", "batch_size", "=", "hparams", ".", "simulated_batch_size", ",", "initial_frame_chooser", "=", "initial_frame_chooser", ",", "model_dir", "=", "world_model_dir", ",", "sim_video_dir", "=", "os", ".", "path", ".", "join", "(", "learner", ".", "agent_model_dir", ",", "\"sim_videos_{}\"", ".", "format", "(", "epoch", ")", ")", ")", "base_algo_str", "=", "hparams", ".", "base_algo", "train_hparams", "=", "trainer_lib", ".", "create_hparams", "(", "hparams", ".", "base_algo_params", ")", "if", "hparams", ".", "wm_policy_param_sharing", ":", "train_hparams", ".", "optimizer_zero_grads", "=", "True", "rl_utils", ".", "update_hparams_from_hparams", "(", "train_hparams", ",", "hparams", ",", "base_algo_str", "+", "\"_\"", ")", "final_epoch", "=", "hparams", ".", "epochs", "-", "1", "is_special_epoch", "=", "(", "epoch", "+", "3", ")", "==", "final_epoch", "or", "(", "epoch", "+", "7", ")", "==", "final_epoch", "is_final_epoch", "=", "epoch", "==", "final_epoch", "env_step_multiplier", "=", "3", "if", "is_final_epoch", "else", "2", "if", "is_special_epoch", "else", "1", "learner", ".", "train", "(", "env_fn", ",", "train_hparams", ",", "simulated", "=", "True", ",", "save_continuously", "=", "True", ",", "epoch", "=", "epoch", ",", "env_step_multiplier", "=", "env_step_multiplier", ")" ]
Train the PPO agent in the simulated environment.
[ "Train", "the", "PPO", "agent", "in", "the", "simulated", "environment", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L141-L170
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
train_agent_real_env
def train_agent_real_env(env, learner, hparams, epoch): """Train the PPO agent in the real environment.""" base_algo_str = hparams.base_algo train_hparams = trainer_lib.create_hparams(hparams.base_algo_params) rl_utils.update_hparams_from_hparams( train_hparams, hparams, "real_" + base_algo_str + "_" ) if hparams.wm_policy_param_sharing: train_hparams.optimizer_zero_grads = True env_fn = rl.make_real_env_fn(env) num_env_steps = real_env_step_increment(hparams) learner.train( env_fn, train_hparams, simulated=False, save_continuously=False, epoch=epoch, sampling_temp=hparams.real_sampling_temp, num_env_steps=num_env_steps, ) # Save unfinished rollouts to history. env.reset()
python
def train_agent_real_env(env, learner, hparams, epoch): """Train the PPO agent in the real environment.""" base_algo_str = hparams.base_algo train_hparams = trainer_lib.create_hparams(hparams.base_algo_params) rl_utils.update_hparams_from_hparams( train_hparams, hparams, "real_" + base_algo_str + "_" ) if hparams.wm_policy_param_sharing: train_hparams.optimizer_zero_grads = True env_fn = rl.make_real_env_fn(env) num_env_steps = real_env_step_increment(hparams) learner.train( env_fn, train_hparams, simulated=False, save_continuously=False, epoch=epoch, sampling_temp=hparams.real_sampling_temp, num_env_steps=num_env_steps, ) # Save unfinished rollouts to history. env.reset()
[ "def", "train_agent_real_env", "(", "env", ",", "learner", ",", "hparams", ",", "epoch", ")", ":", "base_algo_str", "=", "hparams", ".", "base_algo", "train_hparams", "=", "trainer_lib", ".", "create_hparams", "(", "hparams", ".", "base_algo_params", ")", "rl_utils", ".", "update_hparams_from_hparams", "(", "train_hparams", ",", "hparams", ",", "\"real_\"", "+", "base_algo_str", "+", "\"_\"", ")", "if", "hparams", ".", "wm_policy_param_sharing", ":", "train_hparams", ".", "optimizer_zero_grads", "=", "True", "env_fn", "=", "rl", ".", "make_real_env_fn", "(", "env", ")", "num_env_steps", "=", "real_env_step_increment", "(", "hparams", ")", "learner", ".", "train", "(", "env_fn", ",", "train_hparams", ",", "simulated", "=", "False", ",", "save_continuously", "=", "False", ",", "epoch", "=", "epoch", ",", "sampling_temp", "=", "hparams", ".", "real_sampling_temp", ",", "num_env_steps", "=", "num_env_steps", ",", ")", "# Save unfinished rollouts to history.", "env", ".", "reset", "(", ")" ]
Train the PPO agent in the real environment.
[ "Train", "the", "PPO", "agent", "in", "the", "real", "environment", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L173-L196
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
train_world_model
def train_world_model( env, data_dir, output_dir, hparams, world_model_steps_num, epoch ): """Train the world model on problem_name.""" world_model_steps_num += world_model_step_increment( hparams, is_initial_epoch=(epoch == 0) ) model_hparams = trainer_lib.create_hparams(hparams.generative_model_params) model_hparams.learning_rate = model_hparams.learning_rate_constant if epoch > 0: model_hparams.learning_rate *= hparams.learning_rate_bump if hparams.wm_policy_param_sharing: model_hparams.optimizer_zero_grads = True restarter = Restarter("world_model", output_dir, world_model_steps_num) if restarter.should_skip: return world_model_steps_num with restarter.training_loop(): train_supervised( problem=env, model_name=hparams.generative_model, hparams=model_hparams, data_dir=data_dir, output_dir=output_dir, train_steps=restarter.target_global_step, eval_steps=100, local_eval_frequency=2000 ) return world_model_steps_num
python
def train_world_model( env, data_dir, output_dir, hparams, world_model_steps_num, epoch ): """Train the world model on problem_name.""" world_model_steps_num += world_model_step_increment( hparams, is_initial_epoch=(epoch == 0) ) model_hparams = trainer_lib.create_hparams(hparams.generative_model_params) model_hparams.learning_rate = model_hparams.learning_rate_constant if epoch > 0: model_hparams.learning_rate *= hparams.learning_rate_bump if hparams.wm_policy_param_sharing: model_hparams.optimizer_zero_grads = True restarter = Restarter("world_model", output_dir, world_model_steps_num) if restarter.should_skip: return world_model_steps_num with restarter.training_loop(): train_supervised( problem=env, model_name=hparams.generative_model, hparams=model_hparams, data_dir=data_dir, output_dir=output_dir, train_steps=restarter.target_global_step, eval_steps=100, local_eval_frequency=2000 ) return world_model_steps_num
[ "def", "train_world_model", "(", "env", ",", "data_dir", ",", "output_dir", ",", "hparams", ",", "world_model_steps_num", ",", "epoch", ")", ":", "world_model_steps_num", "+=", "world_model_step_increment", "(", "hparams", ",", "is_initial_epoch", "=", "(", "epoch", "==", "0", ")", ")", "model_hparams", "=", "trainer_lib", ".", "create_hparams", "(", "hparams", ".", "generative_model_params", ")", "model_hparams", ".", "learning_rate", "=", "model_hparams", ".", "learning_rate_constant", "if", "epoch", ">", "0", ":", "model_hparams", ".", "learning_rate", "*=", "hparams", ".", "learning_rate_bump", "if", "hparams", ".", "wm_policy_param_sharing", ":", "model_hparams", ".", "optimizer_zero_grads", "=", "True", "restarter", "=", "Restarter", "(", "\"world_model\"", ",", "output_dir", ",", "world_model_steps_num", ")", "if", "restarter", ".", "should_skip", ":", "return", "world_model_steps_num", "with", "restarter", ".", "training_loop", "(", ")", ":", "train_supervised", "(", "problem", "=", "env", ",", "model_name", "=", "hparams", ".", "generative_model", ",", "hparams", "=", "model_hparams", ",", "data_dir", "=", "data_dir", ",", "output_dir", "=", "output_dir", ",", "train_steps", "=", "restarter", ".", "target_global_step", ",", "eval_steps", "=", "100", ",", "local_eval_frequency", "=", "2000", ")", "return", "world_model_steps_num" ]
Train the world model on problem_name.
[ "Train", "the", "world", "model", "on", "problem_name", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L199-L228
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
load_metrics
def load_metrics(event_dir, epoch): """Loads metrics for this epoch if they have already been written. This reads the entire event file but it's small with just per-epoch metrics. Args: event_dir: TODO(koz4k): Document this. epoch: TODO(koz4k): Document this. Returns: metrics. """ metrics = {} for filename in tf.gfile.ListDirectory(event_dir): path = os.path.join(event_dir, filename) for event in tf.train.summary_iterator(path): if event.step == epoch and event.HasField("summary"): value = event.summary.value[0] metrics[value.tag] = value.simple_value return metrics
python
def load_metrics(event_dir, epoch): """Loads metrics for this epoch if they have already been written. This reads the entire event file but it's small with just per-epoch metrics. Args: event_dir: TODO(koz4k): Document this. epoch: TODO(koz4k): Document this. Returns: metrics. """ metrics = {} for filename in tf.gfile.ListDirectory(event_dir): path = os.path.join(event_dir, filename) for event in tf.train.summary_iterator(path): if event.step == epoch and event.HasField("summary"): value = event.summary.value[0] metrics[value.tag] = value.simple_value return metrics
[ "def", "load_metrics", "(", "event_dir", ",", "epoch", ")", ":", "metrics", "=", "{", "}", "for", "filename", "in", "tf", ".", "gfile", ".", "ListDirectory", "(", "event_dir", ")", ":", "path", "=", "os", ".", "path", ".", "join", "(", "event_dir", ",", "filename", ")", "for", "event", "in", "tf", ".", "train", ".", "summary_iterator", "(", "path", ")", ":", "if", "event", ".", "step", "==", "epoch", "and", "event", ".", "HasField", "(", "\"summary\"", ")", ":", "value", "=", "event", ".", "summary", ".", "value", "[", "0", "]", "metrics", "[", "value", ".", "tag", "]", "=", "value", ".", "simple_value", "return", "metrics" ]
Loads metrics for this epoch if they have already been written. This reads the entire event file but it's small with just per-epoch metrics. Args: event_dir: TODO(koz4k): Document this. epoch: TODO(koz4k): Document this. Returns: metrics.
[ "Loads", "metrics", "for", "this", "epoch", "if", "they", "have", "already", "been", "written", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L231-L250
train
tensorflow/tensor2tensor
tensor2tensor/rl/trainer_model_based.py
training_loop
def training_loop(hparams, output_dir, report_fn=None, report_metric=None): """Run the main training loop.""" if report_fn: assert report_metric is not None # Directories subdirectories = [ "data", "tmp", "world_model", ("world_model", "debug_videos"), "policy", "eval_metrics" ] directories = setup_directories(output_dir, subdirectories) epoch = -1 data_dir = directories["data"] env = rl_utils.setup_env( hparams, batch_size=hparams.real_batch_size, max_num_noops=hparams.max_num_noops, rl_env_max_episode_steps=hparams.rl_env_max_episode_steps ) env.start_new_epoch(epoch, data_dir) if hparams.wm_policy_param_sharing: policy_model_dir = directories["world_model"] else: policy_model_dir = directories["policy"] learner = rl_utils.LEARNERS[hparams.base_algo]( hparams.frame_stack_size, policy_model_dir, policy_model_dir, hparams.epochs ) # Timing log function log_relative_time = make_relative_timing_fn() # Per-epoch state epoch_metrics = [] metrics = {} # Collect data from the real environment. policy_model_dir = directories["policy"] tf.logging.info("Initial training of the policy in real environment.") train_agent_real_env(env, learner, hparams, epoch) metrics["mean_reward/train/clipped"] = rl_utils.compute_mean_reward( env.current_epoch_rollouts(), clipped=True ) tf.logging.info("Mean training reward (initial): {}".format( metrics["mean_reward/train/clipped"] )) env.generate_data(data_dir) eval_metrics_writer = tf.summary.FileWriter( directories["eval_metrics"] ) world_model_steps_num = 0 for epoch in range(hparams.epochs): log = make_log_fn(epoch, log_relative_time) # Train world model log("Training world model") world_model_steps_num = train_world_model( env, data_dir, directories["world_model"], hparams, world_model_steps_num, epoch ) # Train agent log("Training policy in simulated environment.") train_agent(env, learner, directories["world_model"], hparams, epoch) env.start_new_epoch(epoch, data_dir) # Train agent on real env (short) log("Training policy in real environment.") train_agent_real_env(env, learner, hparams, epoch) if hparams.stop_loop_early: return 0.0 env.generate_data(data_dir) metrics = load_metrics(directories["eval_metrics"], epoch) if metrics: # Skip eval if metrics have already been written for this epoch. Otherwise # we'd overwrite them with wrong data. log("Metrics found for this epoch, skipping evaluation.") else: metrics["mean_reward/train/clipped"] = rl_utils.compute_mean_reward( env.current_epoch_rollouts(), clipped=True ) log("Mean training reward: {}".format( metrics["mean_reward/train/clipped"] )) eval_metrics = rl_utils.evaluate_all_configs(hparams, policy_model_dir) log("Agent eval metrics:\n{}".format(pprint.pformat(eval_metrics))) metrics.update(eval_metrics) if hparams.eval_world_model: debug_video_path = os.path.join( directories["world_model", "debug_videos"], "{}.avi".format(env.current_epoch) ) wm_metrics = rl_utils.evaluate_world_model( env, hparams, directories["world_model"], debug_video_path ) log("World model eval metrics:\n{}".format(pprint.pformat(wm_metrics))) metrics.update(wm_metrics) rl_utils.summarize_metrics(eval_metrics_writer, metrics, epoch) # Report metrics if report_fn: if report_metric == "mean_reward": metric_name = rl_utils.get_metric_name( sampling_temp=hparams.eval_sampling_temps[0], max_num_noops=hparams.eval_max_num_noops, clipped=False ) report_fn(eval_metrics[metric_name], epoch) else: report_fn(eval_metrics[report_metric], epoch) epoch_metrics.append(metrics) # Return the evaluation metrics from the final epoch return epoch_metrics[-1]
python
def training_loop(hparams, output_dir, report_fn=None, report_metric=None): """Run the main training loop.""" if report_fn: assert report_metric is not None # Directories subdirectories = [ "data", "tmp", "world_model", ("world_model", "debug_videos"), "policy", "eval_metrics" ] directories = setup_directories(output_dir, subdirectories) epoch = -1 data_dir = directories["data"] env = rl_utils.setup_env( hparams, batch_size=hparams.real_batch_size, max_num_noops=hparams.max_num_noops, rl_env_max_episode_steps=hparams.rl_env_max_episode_steps ) env.start_new_epoch(epoch, data_dir) if hparams.wm_policy_param_sharing: policy_model_dir = directories["world_model"] else: policy_model_dir = directories["policy"] learner = rl_utils.LEARNERS[hparams.base_algo]( hparams.frame_stack_size, policy_model_dir, policy_model_dir, hparams.epochs ) # Timing log function log_relative_time = make_relative_timing_fn() # Per-epoch state epoch_metrics = [] metrics = {} # Collect data from the real environment. policy_model_dir = directories["policy"] tf.logging.info("Initial training of the policy in real environment.") train_agent_real_env(env, learner, hparams, epoch) metrics["mean_reward/train/clipped"] = rl_utils.compute_mean_reward( env.current_epoch_rollouts(), clipped=True ) tf.logging.info("Mean training reward (initial): {}".format( metrics["mean_reward/train/clipped"] )) env.generate_data(data_dir) eval_metrics_writer = tf.summary.FileWriter( directories["eval_metrics"] ) world_model_steps_num = 0 for epoch in range(hparams.epochs): log = make_log_fn(epoch, log_relative_time) # Train world model log("Training world model") world_model_steps_num = train_world_model( env, data_dir, directories["world_model"], hparams, world_model_steps_num, epoch ) # Train agent log("Training policy in simulated environment.") train_agent(env, learner, directories["world_model"], hparams, epoch) env.start_new_epoch(epoch, data_dir) # Train agent on real env (short) log("Training policy in real environment.") train_agent_real_env(env, learner, hparams, epoch) if hparams.stop_loop_early: return 0.0 env.generate_data(data_dir) metrics = load_metrics(directories["eval_metrics"], epoch) if metrics: # Skip eval if metrics have already been written for this epoch. Otherwise # we'd overwrite them with wrong data. log("Metrics found for this epoch, skipping evaluation.") else: metrics["mean_reward/train/clipped"] = rl_utils.compute_mean_reward( env.current_epoch_rollouts(), clipped=True ) log("Mean training reward: {}".format( metrics["mean_reward/train/clipped"] )) eval_metrics = rl_utils.evaluate_all_configs(hparams, policy_model_dir) log("Agent eval metrics:\n{}".format(pprint.pformat(eval_metrics))) metrics.update(eval_metrics) if hparams.eval_world_model: debug_video_path = os.path.join( directories["world_model", "debug_videos"], "{}.avi".format(env.current_epoch) ) wm_metrics = rl_utils.evaluate_world_model( env, hparams, directories["world_model"], debug_video_path ) log("World model eval metrics:\n{}".format(pprint.pformat(wm_metrics))) metrics.update(wm_metrics) rl_utils.summarize_metrics(eval_metrics_writer, metrics, epoch) # Report metrics if report_fn: if report_metric == "mean_reward": metric_name = rl_utils.get_metric_name( sampling_temp=hparams.eval_sampling_temps[0], max_num_noops=hparams.eval_max_num_noops, clipped=False ) report_fn(eval_metrics[metric_name], epoch) else: report_fn(eval_metrics[report_metric], epoch) epoch_metrics.append(metrics) # Return the evaluation metrics from the final epoch return epoch_metrics[-1]
[ "def", "training_loop", "(", "hparams", ",", "output_dir", ",", "report_fn", "=", "None", ",", "report_metric", "=", "None", ")", ":", "if", "report_fn", ":", "assert", "report_metric", "is", "not", "None", "# Directories", "subdirectories", "=", "[", "\"data\"", ",", "\"tmp\"", ",", "\"world_model\"", ",", "(", "\"world_model\"", ",", "\"debug_videos\"", ")", ",", "\"policy\"", ",", "\"eval_metrics\"", "]", "directories", "=", "setup_directories", "(", "output_dir", ",", "subdirectories", ")", "epoch", "=", "-", "1", "data_dir", "=", "directories", "[", "\"data\"", "]", "env", "=", "rl_utils", ".", "setup_env", "(", "hparams", ",", "batch_size", "=", "hparams", ".", "real_batch_size", ",", "max_num_noops", "=", "hparams", ".", "max_num_noops", ",", "rl_env_max_episode_steps", "=", "hparams", ".", "rl_env_max_episode_steps", ")", "env", ".", "start_new_epoch", "(", "epoch", ",", "data_dir", ")", "if", "hparams", ".", "wm_policy_param_sharing", ":", "policy_model_dir", "=", "directories", "[", "\"world_model\"", "]", "else", ":", "policy_model_dir", "=", "directories", "[", "\"policy\"", "]", "learner", "=", "rl_utils", ".", "LEARNERS", "[", "hparams", ".", "base_algo", "]", "(", "hparams", ".", "frame_stack_size", ",", "policy_model_dir", ",", "policy_model_dir", ",", "hparams", ".", "epochs", ")", "# Timing log function", "log_relative_time", "=", "make_relative_timing_fn", "(", ")", "# Per-epoch state", "epoch_metrics", "=", "[", "]", "metrics", "=", "{", "}", "# Collect data from the real environment.", "policy_model_dir", "=", "directories", "[", "\"policy\"", "]", "tf", ".", "logging", ".", "info", "(", "\"Initial training of the policy in real environment.\"", ")", "train_agent_real_env", "(", "env", ",", "learner", ",", "hparams", ",", "epoch", ")", "metrics", "[", "\"mean_reward/train/clipped\"", "]", "=", "rl_utils", ".", "compute_mean_reward", "(", "env", ".", "current_epoch_rollouts", "(", ")", ",", "clipped", "=", "True", ")", "tf", ".", "logging", ".", "info", "(", "\"Mean training reward (initial): {}\"", ".", "format", "(", "metrics", "[", "\"mean_reward/train/clipped\"", "]", ")", ")", "env", ".", "generate_data", "(", "data_dir", ")", "eval_metrics_writer", "=", "tf", ".", "summary", ".", "FileWriter", "(", "directories", "[", "\"eval_metrics\"", "]", ")", "world_model_steps_num", "=", "0", "for", "epoch", "in", "range", "(", "hparams", ".", "epochs", ")", ":", "log", "=", "make_log_fn", "(", "epoch", ",", "log_relative_time", ")", "# Train world model", "log", "(", "\"Training world model\"", ")", "world_model_steps_num", "=", "train_world_model", "(", "env", ",", "data_dir", ",", "directories", "[", "\"world_model\"", "]", ",", "hparams", ",", "world_model_steps_num", ",", "epoch", ")", "# Train agent", "log", "(", "\"Training policy in simulated environment.\"", ")", "train_agent", "(", "env", ",", "learner", ",", "directories", "[", "\"world_model\"", "]", ",", "hparams", ",", "epoch", ")", "env", ".", "start_new_epoch", "(", "epoch", ",", "data_dir", ")", "# Train agent on real env (short)", "log", "(", "\"Training policy in real environment.\"", ")", "train_agent_real_env", "(", "env", ",", "learner", ",", "hparams", ",", "epoch", ")", "if", "hparams", ".", "stop_loop_early", ":", "return", "0.0", "env", ".", "generate_data", "(", "data_dir", ")", "metrics", "=", "load_metrics", "(", "directories", "[", "\"eval_metrics\"", "]", ",", "epoch", ")", "if", "metrics", ":", "# Skip eval if metrics have already been written for this epoch. Otherwise", "# we'd overwrite them with wrong data.", "log", "(", "\"Metrics found for this epoch, skipping evaluation.\"", ")", "else", ":", "metrics", "[", "\"mean_reward/train/clipped\"", "]", "=", "rl_utils", ".", "compute_mean_reward", "(", "env", ".", "current_epoch_rollouts", "(", ")", ",", "clipped", "=", "True", ")", "log", "(", "\"Mean training reward: {}\"", ".", "format", "(", "metrics", "[", "\"mean_reward/train/clipped\"", "]", ")", ")", "eval_metrics", "=", "rl_utils", ".", "evaluate_all_configs", "(", "hparams", ",", "policy_model_dir", ")", "log", "(", "\"Agent eval metrics:\\n{}\"", ".", "format", "(", "pprint", ".", "pformat", "(", "eval_metrics", ")", ")", ")", "metrics", ".", "update", "(", "eval_metrics", ")", "if", "hparams", ".", "eval_world_model", ":", "debug_video_path", "=", "os", ".", "path", ".", "join", "(", "directories", "[", "\"world_model\"", ",", "\"debug_videos\"", "]", ",", "\"{}.avi\"", ".", "format", "(", "env", ".", "current_epoch", ")", ")", "wm_metrics", "=", "rl_utils", ".", "evaluate_world_model", "(", "env", ",", "hparams", ",", "directories", "[", "\"world_model\"", "]", ",", "debug_video_path", ")", "log", "(", "\"World model eval metrics:\\n{}\"", ".", "format", "(", "pprint", ".", "pformat", "(", "wm_metrics", ")", ")", ")", "metrics", ".", "update", "(", "wm_metrics", ")", "rl_utils", ".", "summarize_metrics", "(", "eval_metrics_writer", ",", "metrics", ",", "epoch", ")", "# Report metrics", "if", "report_fn", ":", "if", "report_metric", "==", "\"mean_reward\"", ":", "metric_name", "=", "rl_utils", ".", "get_metric_name", "(", "sampling_temp", "=", "hparams", ".", "eval_sampling_temps", "[", "0", "]", ",", "max_num_noops", "=", "hparams", ".", "eval_max_num_noops", ",", "clipped", "=", "False", ")", "report_fn", "(", "eval_metrics", "[", "metric_name", "]", ",", "epoch", ")", "else", ":", "report_fn", "(", "eval_metrics", "[", "report_metric", "]", ",", "epoch", ")", "epoch_metrics", ".", "append", "(", "metrics", ")", "# Return the evaluation metrics from the final epoch", "return", "epoch_metrics", "[", "-", "1", "]" ]
Run the main training loop.
[ "Run", "the", "main", "training", "loop", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/trainer_model_based.py#L253-L378
train
tensorflow/tensor2tensor
tensor2tensor/models/research/gene_expression.py
conv_layer
def conv_layer(x, hidden_size, kernel_size, stride, pooling_window, dropout_rate, dilation_rate, name="conv"): """Single conv layer with relu, optional pooling, and dropout.""" with tf.variable_scope(name): out = x out = common_layers.conv1d_block( out, hidden_size, [(dilation_rate, kernel_size)], strides=stride, first_relu=False, padding="same") out = tf.nn.relu(out) if pooling_window: out = tf.layers.max_pooling1d( out, pooling_window, pooling_window, padding="same") out = tf.layers.dropout(out, dropout_rate) return out
python
def conv_layer(x, hidden_size, kernel_size, stride, pooling_window, dropout_rate, dilation_rate, name="conv"): """Single conv layer with relu, optional pooling, and dropout.""" with tf.variable_scope(name): out = x out = common_layers.conv1d_block( out, hidden_size, [(dilation_rate, kernel_size)], strides=stride, first_relu=False, padding="same") out = tf.nn.relu(out) if pooling_window: out = tf.layers.max_pooling1d( out, pooling_window, pooling_window, padding="same") out = tf.layers.dropout(out, dropout_rate) return out
[ "def", "conv_layer", "(", "x", ",", "hidden_size", ",", "kernel_size", ",", "stride", ",", "pooling_window", ",", "dropout_rate", ",", "dilation_rate", ",", "name", "=", "\"conv\"", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ")", ":", "out", "=", "x", "out", "=", "common_layers", ".", "conv1d_block", "(", "out", ",", "hidden_size", ",", "[", "(", "dilation_rate", ",", "kernel_size", ")", "]", ",", "strides", "=", "stride", ",", "first_relu", "=", "False", ",", "padding", "=", "\"same\"", ")", "out", "=", "tf", ".", "nn", ".", "relu", "(", "out", ")", "if", "pooling_window", ":", "out", "=", "tf", ".", "layers", ".", "max_pooling1d", "(", "out", ",", "pooling_window", ",", "pooling_window", ",", "padding", "=", "\"same\"", ")", "out", "=", "tf", ".", "layers", ".", "dropout", "(", "out", ",", "dropout_rate", ")", "return", "out" ]
Single conv layer with relu, optional pooling, and dropout.
[ "Single", "conv", "layer", "with", "relu", "optional", "pooling", "and", "dropout", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/gene_expression.py#L92-L114
train
tensorflow/tensor2tensor
tensor2tensor/models/research/gene_expression.py
gene_expression_conv_base
def gene_expression_conv_base(): """Hparams for GeneExpressionConv model.""" hparams = common_hparams.basic_params1() batch_size = 10 output_length = 2048 inputs_per_output = 128 chunk_size = 4 input_length = output_length * inputs_per_output // chunk_size hparams.batch_size = input_length * batch_size hparams.dropout = 0.1 hparams.add_hparam("num_conv_layers", 4) hparams.add_hparam("num_dconv_layers", 7) # The product of these pooling windows should match # input_length/target_length. hparams.add_hparam("pooling_windows", [2, 2, 2, 4]) hparams.hidden_size = 256 hparams.kernel_width = 20 hparams.add_hparam("stride", 1) return hparams
python
def gene_expression_conv_base(): """Hparams for GeneExpressionConv model.""" hparams = common_hparams.basic_params1() batch_size = 10 output_length = 2048 inputs_per_output = 128 chunk_size = 4 input_length = output_length * inputs_per_output // chunk_size hparams.batch_size = input_length * batch_size hparams.dropout = 0.1 hparams.add_hparam("num_conv_layers", 4) hparams.add_hparam("num_dconv_layers", 7) # The product of these pooling windows should match # input_length/target_length. hparams.add_hparam("pooling_windows", [2, 2, 2, 4]) hparams.hidden_size = 256 hparams.kernel_width = 20 hparams.add_hparam("stride", 1) return hparams
[ "def", "gene_expression_conv_base", "(", ")", ":", "hparams", "=", "common_hparams", ".", "basic_params1", "(", ")", "batch_size", "=", "10", "output_length", "=", "2048", "inputs_per_output", "=", "128", "chunk_size", "=", "4", "input_length", "=", "output_length", "*", "inputs_per_output", "//", "chunk_size", "hparams", ".", "batch_size", "=", "input_length", "*", "batch_size", "hparams", ".", "dropout", "=", "0.1", "hparams", ".", "add_hparam", "(", "\"num_conv_layers\"", ",", "4", ")", "hparams", ".", "add_hparam", "(", "\"num_dconv_layers\"", ",", "7", ")", "# The product of these pooling windows should match", "# input_length/target_length.", "hparams", ".", "add_hparam", "(", "\"pooling_windows\"", ",", "[", "2", ",", "2", ",", "2", ",", "4", "]", ")", "hparams", ".", "hidden_size", "=", "256", "hparams", ".", "kernel_width", "=", "20", "hparams", ".", "add_hparam", "(", "\"stride\"", ",", "1", ")", "return", "hparams" ]
Hparams for GeneExpressionConv model.
[ "Hparams", "for", "GeneExpressionConv", "model", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/gene_expression.py#L128-L149
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
compress_self_attention_layer
def compress_self_attention_layer(x, hparams, name=None): """Attend function.""" with tf.variable_scope(name, default_name="compress_self_attention"): x, xshape, _ = cia.maybe_reshape_4d_to_3d(x) y = common_attention.multihead_attention( common_layers.layer_preprocess(x, hparams), None, None, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout) res = common_layers.layer_postprocess(x, y, hparams) return tf.reshape(res, xshape)
python
def compress_self_attention_layer(x, hparams, name=None): """Attend function.""" with tf.variable_scope(name, default_name="compress_self_attention"): x, xshape, _ = cia.maybe_reshape_4d_to_3d(x) y = common_attention.multihead_attention( common_layers.layer_preprocess(x, hparams), None, None, hparams.attention_key_channels or hparams.hidden_size, hparams.attention_value_channels or hparams.hidden_size, hparams.hidden_size, hparams.num_heads, hparams.attention_dropout) res = common_layers.layer_postprocess(x, y, hparams) return tf.reshape(res, xshape)
[ "def", "compress_self_attention_layer", "(", "x", ",", "hparams", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"compress_self_attention\"", ")", ":", "x", ",", "xshape", ",", "_", "=", "cia", ".", "maybe_reshape_4d_to_3d", "(", "x", ")", "y", "=", "common_attention", ".", "multihead_attention", "(", "common_layers", ".", "layer_preprocess", "(", "x", ",", "hparams", ")", ",", "None", ",", "None", ",", "hparams", ".", "attention_key_channels", "or", "hparams", ".", "hidden_size", ",", "hparams", ".", "attention_value_channels", "or", "hparams", ".", "hidden_size", ",", "hparams", ".", "hidden_size", ",", "hparams", ".", "num_heads", ",", "hparams", ".", "attention_dropout", ")", "res", "=", "common_layers", ".", "layer_postprocess", "(", "x", ",", "y", ",", "hparams", ")", "return", "tf", ".", "reshape", "(", "res", ",", "xshape", ")" ]
Attend function.
[ "Attend", "function", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L35-L48
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
compute_nats_and_bits_per_dim
def compute_nats_and_bits_per_dim(data_dim, latent_dim, average_reconstruction, average_prior): """Computes negative ELBO, which is an upper bound on the negative likelihood. Args: data_dim: int-like indicating data dimensionality. latent_dim: int-like indicating latent dimensionality. average_reconstruction: Scalar Tensor indicating the reconstruction cost averaged over all data dimensions and any data batches. average_prior: Scalar Tensor indicating the negative log-prior probability averaged over all latent dimensions and any data batches. Returns: Tuple of scalar Tensors, representing the nats and bits per data dimension (e.g., subpixels) respectively. """ with tf.name_scope(None, default_name="compute_nats_per_dim"): data_dim = tf.cast(data_dim, average_reconstruction.dtype) latent_dim = tf.cast(latent_dim, average_prior.dtype) negative_log_likelihood = data_dim * average_reconstruction negative_log_prior = latent_dim * average_prior negative_elbo = negative_log_likelihood + negative_log_prior nats_per_dim = tf.divide(negative_elbo, data_dim, name="nats_per_dim") bits_per_dim = tf.divide(nats_per_dim, tf.log(2.), name="bits_per_dim") return nats_per_dim, bits_per_dim
python
def compute_nats_and_bits_per_dim(data_dim, latent_dim, average_reconstruction, average_prior): """Computes negative ELBO, which is an upper bound on the negative likelihood. Args: data_dim: int-like indicating data dimensionality. latent_dim: int-like indicating latent dimensionality. average_reconstruction: Scalar Tensor indicating the reconstruction cost averaged over all data dimensions and any data batches. average_prior: Scalar Tensor indicating the negative log-prior probability averaged over all latent dimensions and any data batches. Returns: Tuple of scalar Tensors, representing the nats and bits per data dimension (e.g., subpixels) respectively. """ with tf.name_scope(None, default_name="compute_nats_per_dim"): data_dim = tf.cast(data_dim, average_reconstruction.dtype) latent_dim = tf.cast(latent_dim, average_prior.dtype) negative_log_likelihood = data_dim * average_reconstruction negative_log_prior = latent_dim * average_prior negative_elbo = negative_log_likelihood + negative_log_prior nats_per_dim = tf.divide(negative_elbo, data_dim, name="nats_per_dim") bits_per_dim = tf.divide(nats_per_dim, tf.log(2.), name="bits_per_dim") return nats_per_dim, bits_per_dim
[ "def", "compute_nats_and_bits_per_dim", "(", "data_dim", ",", "latent_dim", ",", "average_reconstruction", ",", "average_prior", ")", ":", "with", "tf", ".", "name_scope", "(", "None", ",", "default_name", "=", "\"compute_nats_per_dim\"", ")", ":", "data_dim", "=", "tf", ".", "cast", "(", "data_dim", ",", "average_reconstruction", ".", "dtype", ")", "latent_dim", "=", "tf", ".", "cast", "(", "latent_dim", ",", "average_prior", ".", "dtype", ")", "negative_log_likelihood", "=", "data_dim", "*", "average_reconstruction", "negative_log_prior", "=", "latent_dim", "*", "average_prior", "negative_elbo", "=", "negative_log_likelihood", "+", "negative_log_prior", "nats_per_dim", "=", "tf", ".", "divide", "(", "negative_elbo", ",", "data_dim", ",", "name", "=", "\"nats_per_dim\"", ")", "bits_per_dim", "=", "tf", ".", "divide", "(", "nats_per_dim", ",", "tf", ".", "log", "(", "2.", ")", ",", "name", "=", "\"bits_per_dim\"", ")", "return", "nats_per_dim", ",", "bits_per_dim" ]
Computes negative ELBO, which is an upper bound on the negative likelihood. Args: data_dim: int-like indicating data dimensionality. latent_dim: int-like indicating latent dimensionality. average_reconstruction: Scalar Tensor indicating the reconstruction cost averaged over all data dimensions and any data batches. average_prior: Scalar Tensor indicating the negative log-prior probability averaged over all latent dimensions and any data batches. Returns: Tuple of scalar Tensors, representing the nats and bits per data dimension (e.g., subpixels) respectively.
[ "Computes", "negative", "ELBO", "which", "is", "an", "upper", "bound", "on", "the", "negative", "likelihood", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L51-L77
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
multinomial_sample
def multinomial_sample(x, vocab_size=None, sampling_method="random", temperature=1.0): """Multinomial sampling from a n-dimensional tensor. Args: x: Tensor of shape [..., vocab_size]. Parameterizes logits of multinomial. vocab_size: Number of classes in multinomial distribution. sampling_method: String, "random" or otherwise deterministic. temperature: Positive float. Returns: Tensor of shape [...]. """ vocab_size = vocab_size or common_layers.shape_list(x)[-1] if sampling_method == "random" and temperature > 0.0: samples = tf.multinomial(tf.reshape(x, [-1, vocab_size]) / temperature, 1) else: samples = tf.argmax(x, axis=-1) reshaped_samples = tf.reshape(samples, common_layers.shape_list(x)[:-1]) return reshaped_samples
python
def multinomial_sample(x, vocab_size=None, sampling_method="random", temperature=1.0): """Multinomial sampling from a n-dimensional tensor. Args: x: Tensor of shape [..., vocab_size]. Parameterizes logits of multinomial. vocab_size: Number of classes in multinomial distribution. sampling_method: String, "random" or otherwise deterministic. temperature: Positive float. Returns: Tensor of shape [...]. """ vocab_size = vocab_size or common_layers.shape_list(x)[-1] if sampling_method == "random" and temperature > 0.0: samples = tf.multinomial(tf.reshape(x, [-1, vocab_size]) / temperature, 1) else: samples = tf.argmax(x, axis=-1) reshaped_samples = tf.reshape(samples, common_layers.shape_list(x)[:-1]) return reshaped_samples
[ "def", "multinomial_sample", "(", "x", ",", "vocab_size", "=", "None", ",", "sampling_method", "=", "\"random\"", ",", "temperature", "=", "1.0", ")", ":", "vocab_size", "=", "vocab_size", "or", "common_layers", ".", "shape_list", "(", "x", ")", "[", "-", "1", "]", "if", "sampling_method", "==", "\"random\"", "and", "temperature", ">", "0.0", ":", "samples", "=", "tf", ".", "multinomial", "(", "tf", ".", "reshape", "(", "x", ",", "[", "-", "1", ",", "vocab_size", "]", ")", "/", "temperature", ",", "1", ")", "else", ":", "samples", "=", "tf", ".", "argmax", "(", "x", ",", "axis", "=", "-", "1", ")", "reshaped_samples", "=", "tf", ".", "reshape", "(", "samples", ",", "common_layers", ".", "shape_list", "(", "x", ")", "[", ":", "-", "1", "]", ")", "return", "reshaped_samples" ]
Multinomial sampling from a n-dimensional tensor. Args: x: Tensor of shape [..., vocab_size]. Parameterizes logits of multinomial. vocab_size: Number of classes in multinomial distribution. sampling_method: String, "random" or otherwise deterministic. temperature: Positive float. Returns: Tensor of shape [...].
[ "Multinomial", "sampling", "from", "a", "n", "-", "dimensional", "tensor", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L80-L99
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
ae_latent_softmax
def ae_latent_softmax(latents_pred, latents_discrete_hot, vocab_size, hparams): """Latent prediction and loss. Args: latents_pred: Tensor of shape [..., depth]. latents_discrete_hot: Tensor of shape [..., vocab_size]. vocab_size: an int representing the vocab size. hparams: HParams. Returns: sample: Tensor of shape [...], a sample from a multinomial distribution. loss: Tensor of shape [...], the softmax cross-entropy. """ with tf.variable_scope("latent_logits"): latents_logits = tf.layers.dense(latents_pred, vocab_size, name="logits_dense") if hparams.logit_normalization: latents_logits *= tf.rsqrt(1e-8 + tf.reduce_mean(tf.square(latents_logits))) loss = tf.nn.softmax_cross_entropy_with_logits_v2( labels=latents_discrete_hot, logits=latents_logits) # TODO(trandustin): tease this out from ae_latent_softmax. # we use just the loss portion to anchor prior / encoder on text. sample = multinomial_sample(latents_logits, vocab_size, hparams.sampling_method, hparams.sampling_temp) return sample, loss
python
def ae_latent_softmax(latents_pred, latents_discrete_hot, vocab_size, hparams): """Latent prediction and loss. Args: latents_pred: Tensor of shape [..., depth]. latents_discrete_hot: Tensor of shape [..., vocab_size]. vocab_size: an int representing the vocab size. hparams: HParams. Returns: sample: Tensor of shape [...], a sample from a multinomial distribution. loss: Tensor of shape [...], the softmax cross-entropy. """ with tf.variable_scope("latent_logits"): latents_logits = tf.layers.dense(latents_pred, vocab_size, name="logits_dense") if hparams.logit_normalization: latents_logits *= tf.rsqrt(1e-8 + tf.reduce_mean(tf.square(latents_logits))) loss = tf.nn.softmax_cross_entropy_with_logits_v2( labels=latents_discrete_hot, logits=latents_logits) # TODO(trandustin): tease this out from ae_latent_softmax. # we use just the loss portion to anchor prior / encoder on text. sample = multinomial_sample(latents_logits, vocab_size, hparams.sampling_method, hparams.sampling_temp) return sample, loss
[ "def", "ae_latent_softmax", "(", "latents_pred", ",", "latents_discrete_hot", ",", "vocab_size", ",", "hparams", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"latent_logits\"", ")", ":", "latents_logits", "=", "tf", ".", "layers", ".", "dense", "(", "latents_pred", ",", "vocab_size", ",", "name", "=", "\"logits_dense\"", ")", "if", "hparams", ".", "logit_normalization", ":", "latents_logits", "*=", "tf", ".", "rsqrt", "(", "1e-8", "+", "tf", ".", "reduce_mean", "(", "tf", ".", "square", "(", "latents_logits", ")", ")", ")", "loss", "=", "tf", ".", "nn", ".", "softmax_cross_entropy_with_logits_v2", "(", "labels", "=", "latents_discrete_hot", ",", "logits", "=", "latents_logits", ")", "# TODO(trandustin): tease this out from ae_latent_softmax.", "# we use just the loss portion to anchor prior / encoder on text.", "sample", "=", "multinomial_sample", "(", "latents_logits", ",", "vocab_size", ",", "hparams", ".", "sampling_method", ",", "hparams", ".", "sampling_temp", ")", "return", "sample", ",", "loss" ]
Latent prediction and loss. Args: latents_pred: Tensor of shape [..., depth]. latents_discrete_hot: Tensor of shape [..., vocab_size]. vocab_size: an int representing the vocab size. hparams: HParams. Returns: sample: Tensor of shape [...], a sample from a multinomial distribution. loss: Tensor of shape [...], the softmax cross-entropy.
[ "Latent", "prediction", "and", "loss", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L102-L130
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
ae_latent_sample_beam
def ae_latent_sample_beam(latents_dense_in, inputs, ed, embed, hparams): """Samples from the latent space in the autoencoder. Args: latents_dense_in: Tensor of shape [batch, length_q, ...]. Only the shape of its first two dimensions are used. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). inputs: Tensor of shape [batch, length_kv, hparams.hidden_size]. Encodings to attend to in decoder. ed: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. embed: Callable which embeds discrete latent hot-vectors and a hidden size and returns dense vectors. hparams: HParams. Returns: Tensor of shape [batch, length]. """ def symbols_to_logits_fn(ids): """Go from ids to logits.""" ids = tf.expand_dims(ids, axis=2) # Ids start with added all-zeros. latents_discrete = tf.pad(ids[:, 1:], [[0, 0], [0, 1], [0, 0]]) with tf.variable_scope(tf.get_variable_scope(), reuse=False): latents_dense = embed( tf.one_hot(latents_discrete, depth=2**hparams.bottleneck_bits), hparams.hidden_size) latents_pred = transformer_latent_decoder( latents_dense, inputs, ed, hparams, name="latent_prediction") logits = tf.layers.dense( latents_pred, 2**hparams.bottleneck_bits, name="logits_dense") current_output_position = common_layers.shape_list(ids)[1] - 1 logits = logits[:, current_output_position, :] return logits initial_ids = tf.zeros([tf.shape(latents_dense_in)[0]], dtype=tf.int32) length = tf.shape(latents_dense_in)[1] ids, _, _ = beam_search.beam_search( symbols_to_logits_fn, initial_ids, 1, length, 2**hparams.bottleneck_bits, alpha=0.0, eos_id=-1, stop_early=False) res = tf.expand_dims(ids[:, 0, :], axis=2) # Pick first beam. return res[:, 1:]
python
def ae_latent_sample_beam(latents_dense_in, inputs, ed, embed, hparams): """Samples from the latent space in the autoencoder. Args: latents_dense_in: Tensor of shape [batch, length_q, ...]. Only the shape of its first two dimensions are used. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). inputs: Tensor of shape [batch, length_kv, hparams.hidden_size]. Encodings to attend to in decoder. ed: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. embed: Callable which embeds discrete latent hot-vectors and a hidden size and returns dense vectors. hparams: HParams. Returns: Tensor of shape [batch, length]. """ def symbols_to_logits_fn(ids): """Go from ids to logits.""" ids = tf.expand_dims(ids, axis=2) # Ids start with added all-zeros. latents_discrete = tf.pad(ids[:, 1:], [[0, 0], [0, 1], [0, 0]]) with tf.variable_scope(tf.get_variable_scope(), reuse=False): latents_dense = embed( tf.one_hot(latents_discrete, depth=2**hparams.bottleneck_bits), hparams.hidden_size) latents_pred = transformer_latent_decoder( latents_dense, inputs, ed, hparams, name="latent_prediction") logits = tf.layers.dense( latents_pred, 2**hparams.bottleneck_bits, name="logits_dense") current_output_position = common_layers.shape_list(ids)[1] - 1 logits = logits[:, current_output_position, :] return logits initial_ids = tf.zeros([tf.shape(latents_dense_in)[0]], dtype=tf.int32) length = tf.shape(latents_dense_in)[1] ids, _, _ = beam_search.beam_search( symbols_to_logits_fn, initial_ids, 1, length, 2**hparams.bottleneck_bits, alpha=0.0, eos_id=-1, stop_early=False) res = tf.expand_dims(ids[:, 0, :], axis=2) # Pick first beam. return res[:, 1:]
[ "def", "ae_latent_sample_beam", "(", "latents_dense_in", ",", "inputs", ",", "ed", ",", "embed", ",", "hparams", ")", ":", "def", "symbols_to_logits_fn", "(", "ids", ")", ":", "\"\"\"Go from ids to logits.\"\"\"", "ids", "=", "tf", ".", "expand_dims", "(", "ids", ",", "axis", "=", "2", ")", "# Ids start with added all-zeros.", "latents_discrete", "=", "tf", ".", "pad", "(", "ids", "[", ":", ",", "1", ":", "]", ",", "[", "[", "0", ",", "0", "]", ",", "[", "0", ",", "1", "]", ",", "[", "0", ",", "0", "]", "]", ")", "with", "tf", ".", "variable_scope", "(", "tf", ".", "get_variable_scope", "(", ")", ",", "reuse", "=", "False", ")", ":", "latents_dense", "=", "embed", "(", "tf", ".", "one_hot", "(", "latents_discrete", ",", "depth", "=", "2", "**", "hparams", ".", "bottleneck_bits", ")", ",", "hparams", ".", "hidden_size", ")", "latents_pred", "=", "transformer_latent_decoder", "(", "latents_dense", ",", "inputs", ",", "ed", ",", "hparams", ",", "name", "=", "\"latent_prediction\"", ")", "logits", "=", "tf", ".", "layers", ".", "dense", "(", "latents_pred", ",", "2", "**", "hparams", ".", "bottleneck_bits", ",", "name", "=", "\"logits_dense\"", ")", "current_output_position", "=", "common_layers", ".", "shape_list", "(", "ids", ")", "[", "1", "]", "-", "1", "logits", "=", "logits", "[", ":", ",", "current_output_position", ",", ":", "]", "return", "logits", "initial_ids", "=", "tf", ".", "zeros", "(", "[", "tf", ".", "shape", "(", "latents_dense_in", ")", "[", "0", "]", "]", ",", "dtype", "=", "tf", ".", "int32", ")", "length", "=", "tf", ".", "shape", "(", "latents_dense_in", ")", "[", "1", "]", "ids", ",", "_", ",", "_", "=", "beam_search", ".", "beam_search", "(", "symbols_to_logits_fn", ",", "initial_ids", ",", "1", ",", "length", ",", "2", "**", "hparams", ".", "bottleneck_bits", ",", "alpha", "=", "0.0", ",", "eos_id", "=", "-", "1", ",", "stop_early", "=", "False", ")", "res", "=", "tf", ".", "expand_dims", "(", "ids", "[", ":", ",", "0", ",", ":", "]", ",", "axis", "=", "2", ")", "# Pick first beam.", "return", "res", "[", ":", ",", "1", ":", "]" ]
Samples from the latent space in the autoencoder. Args: latents_dense_in: Tensor of shape [batch, length_q, ...]. Only the shape of its first two dimensions are used. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). inputs: Tensor of shape [batch, length_kv, hparams.hidden_size]. Encodings to attend to in decoder. ed: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. embed: Callable which embeds discrete latent hot-vectors and a hidden size and returns dense vectors. hparams: HParams. Returns: Tensor of shape [batch, length].
[ "Samples", "from", "the", "latent", "space", "in", "the", "autoencoder", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L133-L182
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
residual_block_layer
def residual_block_layer(inputs, hparams): """Residual block over inputs. Runs a residual block consisting of conv: kernel_size x kernel_size conv: 1x1 dropout, add and normalize according to hparams.layer_postprocess_sequence. Args: inputs: Tensor of shape [batch, height, width, hparams.hidden_size]. hparams: HParams. Returns: Tensor of shape [batch, height, width, hparams.hidden_size]. """ kernel = (hparams.res_kernel_size, hparams.res_kernel_size) x = inputs for i in range(hparams.num_res_layers): with tf.variable_scope("res_conv_%d" % i): # kernel_size x kernel_size conv block y = common_layers.conv_block( common_layers.layer_norm(x, hparams.hidden_size, name="lnorm"), hparams.hidden_size, [((1, 1), kernel)], strides=(1, 1), padding="SAME", name="residual_conv") # 1x1 conv block y = common_layers.conv_block( y, hparams.hidden_size, [((1, 1), (1, 1))], strides=(1, 1), padding="SAME", name="residual_dense") x = common_layers.layer_postprocess(x, y, hparams) return x
python
def residual_block_layer(inputs, hparams): """Residual block over inputs. Runs a residual block consisting of conv: kernel_size x kernel_size conv: 1x1 dropout, add and normalize according to hparams.layer_postprocess_sequence. Args: inputs: Tensor of shape [batch, height, width, hparams.hidden_size]. hparams: HParams. Returns: Tensor of shape [batch, height, width, hparams.hidden_size]. """ kernel = (hparams.res_kernel_size, hparams.res_kernel_size) x = inputs for i in range(hparams.num_res_layers): with tf.variable_scope("res_conv_%d" % i): # kernel_size x kernel_size conv block y = common_layers.conv_block( common_layers.layer_norm(x, hparams.hidden_size, name="lnorm"), hparams.hidden_size, [((1, 1), kernel)], strides=(1, 1), padding="SAME", name="residual_conv") # 1x1 conv block y = common_layers.conv_block( y, hparams.hidden_size, [((1, 1), (1, 1))], strides=(1, 1), padding="SAME", name="residual_dense") x = common_layers.layer_postprocess(x, y, hparams) return x
[ "def", "residual_block_layer", "(", "inputs", ",", "hparams", ")", ":", "kernel", "=", "(", "hparams", ".", "res_kernel_size", ",", "hparams", ".", "res_kernel_size", ")", "x", "=", "inputs", "for", "i", "in", "range", "(", "hparams", ".", "num_res_layers", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"res_conv_%d\"", "%", "i", ")", ":", "# kernel_size x kernel_size conv block", "y", "=", "common_layers", ".", "conv_block", "(", "common_layers", ".", "layer_norm", "(", "x", ",", "hparams", ".", "hidden_size", ",", "name", "=", "\"lnorm\"", ")", ",", "hparams", ".", "hidden_size", ",", "[", "(", "(", "1", ",", "1", ")", ",", "kernel", ")", "]", ",", "strides", "=", "(", "1", ",", "1", ")", ",", "padding", "=", "\"SAME\"", ",", "name", "=", "\"residual_conv\"", ")", "# 1x1 conv block", "y", "=", "common_layers", ".", "conv_block", "(", "y", ",", "hparams", ".", "hidden_size", ",", "[", "(", "(", "1", ",", "1", ")", ",", "(", "1", ",", "1", ")", ")", "]", ",", "strides", "=", "(", "1", ",", "1", ")", ",", "padding", "=", "\"SAME\"", ",", "name", "=", "\"residual_dense\"", ")", "x", "=", "common_layers", ".", "layer_postprocess", "(", "x", ",", "y", ",", "hparams", ")", "return", "x" ]
Residual block over inputs. Runs a residual block consisting of conv: kernel_size x kernel_size conv: 1x1 dropout, add and normalize according to hparams.layer_postprocess_sequence. Args: inputs: Tensor of shape [batch, height, width, hparams.hidden_size]. hparams: HParams. Returns: Tensor of shape [batch, height, width, hparams.hidden_size].
[ "Residual", "block", "over", "inputs", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L185-L219
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
compress_encoder
def compress_encoder(inputs, hparams, strides=(2, 2), kernel_size=(3, 3), name=None): """Encoder that compresses 2-D inputs by 2**num_compress_steps. Args: inputs: Tensor of shape [batch, height, width, channels]. hparams: HParams. strides: Tuple, strides for conv block. kernel_size: Tuple, kernel window size for conv block. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * (height*width) / 2**(hparams.num_compress_steps). """ with tf.variable_scope(name, default_name="compress"): x = inputs for i in range(hparams.num_compress_steps // 2): with tf.variable_scope("compress_conv_%d" % i): y = common_layers.conv_block( common_layers.layer_norm( x, hparams.hidden_size, name="lnorm"), hparams.hidden_size, dilation_rates_and_kernel_sizes=[((1, 1), kernel_size)], strides=strides, padding="SAME", name="compress_conv_%d" % i) y = tf.nn.dropout(y, 1.0 - hparams.dropout) if hparams.do_compress_attend: y = compress_self_attention_layer( x, hparams, name="compress_selfatt_%d" % i) y += x x = y x = residual_block_layer(x, hparams) # If using multiple copies of latents, blow up the hidden size and then # reshape to increase by num_latents. shape_x = common_layers.shape_list(x) x = tf.layers.dense(x, hparams.num_latents * hparams.hidden_size, name=name + "_dense") return tf.reshape(x, [shape_x[0], shape_x[1] * shape_x[2] * hparams.num_latents, hparams.hidden_size])
python
def compress_encoder(inputs, hparams, strides=(2, 2), kernel_size=(3, 3), name=None): """Encoder that compresses 2-D inputs by 2**num_compress_steps. Args: inputs: Tensor of shape [batch, height, width, channels]. hparams: HParams. strides: Tuple, strides for conv block. kernel_size: Tuple, kernel window size for conv block. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * (height*width) / 2**(hparams.num_compress_steps). """ with tf.variable_scope(name, default_name="compress"): x = inputs for i in range(hparams.num_compress_steps // 2): with tf.variable_scope("compress_conv_%d" % i): y = common_layers.conv_block( common_layers.layer_norm( x, hparams.hidden_size, name="lnorm"), hparams.hidden_size, dilation_rates_and_kernel_sizes=[((1, 1), kernel_size)], strides=strides, padding="SAME", name="compress_conv_%d" % i) y = tf.nn.dropout(y, 1.0 - hparams.dropout) if hparams.do_compress_attend: y = compress_self_attention_layer( x, hparams, name="compress_selfatt_%d" % i) y += x x = y x = residual_block_layer(x, hparams) # If using multiple copies of latents, blow up the hidden size and then # reshape to increase by num_latents. shape_x = common_layers.shape_list(x) x = tf.layers.dense(x, hparams.num_latents * hparams.hidden_size, name=name + "_dense") return tf.reshape(x, [shape_x[0], shape_x[1] * shape_x[2] * hparams.num_latents, hparams.hidden_size])
[ "def", "compress_encoder", "(", "inputs", ",", "hparams", ",", "strides", "=", "(", "2", ",", "2", ")", ",", "kernel_size", "=", "(", "3", ",", "3", ")", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"compress\"", ")", ":", "x", "=", "inputs", "for", "i", "in", "range", "(", "hparams", ".", "num_compress_steps", "//", "2", ")", ":", "with", "tf", ".", "variable_scope", "(", "\"compress_conv_%d\"", "%", "i", ")", ":", "y", "=", "common_layers", ".", "conv_block", "(", "common_layers", ".", "layer_norm", "(", "x", ",", "hparams", ".", "hidden_size", ",", "name", "=", "\"lnorm\"", ")", ",", "hparams", ".", "hidden_size", ",", "dilation_rates_and_kernel_sizes", "=", "[", "(", "(", "1", ",", "1", ")", ",", "kernel_size", ")", "]", ",", "strides", "=", "strides", ",", "padding", "=", "\"SAME\"", ",", "name", "=", "\"compress_conv_%d\"", "%", "i", ")", "y", "=", "tf", ".", "nn", ".", "dropout", "(", "y", ",", "1.0", "-", "hparams", ".", "dropout", ")", "if", "hparams", ".", "do_compress_attend", ":", "y", "=", "compress_self_attention_layer", "(", "x", ",", "hparams", ",", "name", "=", "\"compress_selfatt_%d\"", "%", "i", ")", "y", "+=", "x", "x", "=", "y", "x", "=", "residual_block_layer", "(", "x", ",", "hparams", ")", "# If using multiple copies of latents, blow up the hidden size and then", "# reshape to increase by num_latents.", "shape_x", "=", "common_layers", ".", "shape_list", "(", "x", ")", "x", "=", "tf", ".", "layers", ".", "dense", "(", "x", ",", "hparams", ".", "num_latents", "*", "hparams", ".", "hidden_size", ",", "name", "=", "name", "+", "\"_dense\"", ")", "return", "tf", ".", "reshape", "(", "x", ",", "[", "shape_x", "[", "0", "]", ",", "shape_x", "[", "1", "]", "*", "shape_x", "[", "2", "]", "*", "hparams", ".", "num_latents", ",", "hparams", ".", "hidden_size", "]", ")" ]
Encoder that compresses 2-D inputs by 2**num_compress_steps. Args: inputs: Tensor of shape [batch, height, width, channels]. hparams: HParams. strides: Tuple, strides for conv block. kernel_size: Tuple, kernel window size for conv block. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * (height*width) / 2**(hparams.num_compress_steps).
[ "Encoder", "that", "compresses", "2", "-", "D", "inputs", "by", "2", "**", "num_compress_steps", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L222-L270
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
compress_encoder_2d
def compress_encoder_2d(x, hparams, name=None): """Encoder that compresses 2-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, height, width, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * (height*width) / 2**(hparams.num_compress_steps). """ return compress_encoder( x, hparams, strides=(2, 2), kernel_size=(hparams.kernel_size, hparams.kernel_size), name=name)
python
def compress_encoder_2d(x, hparams, name=None): """Encoder that compresses 2-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, height, width, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * (height*width) / 2**(hparams.num_compress_steps). """ return compress_encoder( x, hparams, strides=(2, 2), kernel_size=(hparams.kernel_size, hparams.kernel_size), name=name)
[ "def", "compress_encoder_2d", "(", "x", ",", "hparams", ",", "name", "=", "None", ")", ":", "return", "compress_encoder", "(", "x", ",", "hparams", ",", "strides", "=", "(", "2", ",", "2", ")", ",", "kernel_size", "=", "(", "hparams", ".", "kernel_size", ",", "hparams", ".", "kernel_size", ")", ",", "name", "=", "name", ")" ]
Encoder that compresses 2-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, height, width, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * (height*width) / 2**(hparams.num_compress_steps).
[ "Encoder", "that", "compresses", "2", "-", "D", "inputs", "by", "2", "**", "num_compress_steps", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L273-L291
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
compress_encoder_1d
def compress_encoder_1d(x, hparams, name=None): """Encoder that compresses 1-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, length, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * length / 2**hparams.num_compress_steps. """ x = tf.expand_dims(x, axis=2) return compress_encoder(x, hparams, strides=(2, 1), kernel_size=(hparams.kernel_size, 1), name=name)
python
def compress_encoder_1d(x, hparams, name=None): """Encoder that compresses 1-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, length, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * length / 2**hparams.num_compress_steps. """ x = tf.expand_dims(x, axis=2) return compress_encoder(x, hparams, strides=(2, 1), kernel_size=(hparams.kernel_size, 1), name=name)
[ "def", "compress_encoder_1d", "(", "x", ",", "hparams", ",", "name", "=", "None", ")", ":", "x", "=", "tf", ".", "expand_dims", "(", "x", ",", "axis", "=", "2", ")", "return", "compress_encoder", "(", "x", ",", "hparams", ",", "strides", "=", "(", "2", ",", "1", ")", ",", "kernel_size", "=", "(", "hparams", ".", "kernel_size", ",", "1", ")", ",", "name", "=", "name", ")" ]
Encoder that compresses 1-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, length, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, latent_length, hparams.hidden_size], where latent_length is hparams.num_latents * length / 2**hparams.num_compress_steps.
[ "Encoder", "that", "compresses", "1", "-", "D", "inputs", "by", "2", "**", "num_compress_steps", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L294-L312
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
decompress_decoder
def decompress_decoder(inputs, hparams, strides=(2, 2), kernel=(3, 3), name=None): """Decoder that decompresses 2-D inputs by 2**num_compress_steps. Args: inputs: Tensor of shape [batch, compress_height, compress_width, channels]. hparams: HParams. strides: Tuple, strides for conv block. kernel: Tuple, kernel window size for conv block. name: string, variable scope. Returns: Tensor of shape [batch, height, width, hparams.hidden_size]. """ with tf.variable_scope(name, default_name="decompress"): x = inputs x = tf.layers.dense(x, hparams.hidden_size, name=name + "_dense") x = residual_block_layer(x, hparams) for i in range(hparams.num_compress_steps // 2): j = hparams.num_compress_steps // 2 - i - 1 with tf.variable_scope(name + "_%d" % j): if hparams.do_decompress_attend: y = compress_self_attention_layer( x, hparams, name="decompress_selfatt") x += y y = tf.layers.conv2d_transpose( x, hparams.hidden_size, kernel, strides=strides, padding="SAME", activation=tf.nn.relu if i > 0 else None, name="decompress_conv") x = y return x
python
def decompress_decoder(inputs, hparams, strides=(2, 2), kernel=(3, 3), name=None): """Decoder that decompresses 2-D inputs by 2**num_compress_steps. Args: inputs: Tensor of shape [batch, compress_height, compress_width, channels]. hparams: HParams. strides: Tuple, strides for conv block. kernel: Tuple, kernel window size for conv block. name: string, variable scope. Returns: Tensor of shape [batch, height, width, hparams.hidden_size]. """ with tf.variable_scope(name, default_name="decompress"): x = inputs x = tf.layers.dense(x, hparams.hidden_size, name=name + "_dense") x = residual_block_layer(x, hparams) for i in range(hparams.num_compress_steps // 2): j = hparams.num_compress_steps // 2 - i - 1 with tf.variable_scope(name + "_%d" % j): if hparams.do_decompress_attend: y = compress_self_attention_layer( x, hparams, name="decompress_selfatt") x += y y = tf.layers.conv2d_transpose( x, hparams.hidden_size, kernel, strides=strides, padding="SAME", activation=tf.nn.relu if i > 0 else None, name="decompress_conv") x = y return x
[ "def", "decompress_decoder", "(", "inputs", ",", "hparams", ",", "strides", "=", "(", "2", ",", "2", ")", ",", "kernel", "=", "(", "3", ",", "3", ")", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"decompress\"", ")", ":", "x", "=", "inputs", "x", "=", "tf", ".", "layers", ".", "dense", "(", "x", ",", "hparams", ".", "hidden_size", ",", "name", "=", "name", "+", "\"_dense\"", ")", "x", "=", "residual_block_layer", "(", "x", ",", "hparams", ")", "for", "i", "in", "range", "(", "hparams", ".", "num_compress_steps", "//", "2", ")", ":", "j", "=", "hparams", ".", "num_compress_steps", "//", "2", "-", "i", "-", "1", "with", "tf", ".", "variable_scope", "(", "name", "+", "\"_%d\"", "%", "j", ")", ":", "if", "hparams", ".", "do_decompress_attend", ":", "y", "=", "compress_self_attention_layer", "(", "x", ",", "hparams", ",", "name", "=", "\"decompress_selfatt\"", ")", "x", "+=", "y", "y", "=", "tf", ".", "layers", ".", "conv2d_transpose", "(", "x", ",", "hparams", ".", "hidden_size", ",", "kernel", ",", "strides", "=", "strides", ",", "padding", "=", "\"SAME\"", ",", "activation", "=", "tf", ".", "nn", ".", "relu", "if", "i", ">", "0", "else", "None", ",", "name", "=", "\"decompress_conv\"", ")", "x", "=", "y", "return", "x" ]
Decoder that decompresses 2-D inputs by 2**num_compress_steps. Args: inputs: Tensor of shape [batch, compress_height, compress_width, channels]. hparams: HParams. strides: Tuple, strides for conv block. kernel: Tuple, kernel window size for conv block. name: string, variable scope. Returns: Tensor of shape [batch, height, width, hparams.hidden_size].
[ "Decoder", "that", "decompresses", "2", "-", "D", "inputs", "by", "2", "**", "num_compress_steps", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L315-L352
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
decompress_decoder_2d
def decompress_decoder_2d(x, hparams, name=None): """Decoder that decompresses 2-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, compress_height, compress_width, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, height, width, hparams.hidden_size]. """ return decompress_decoder(x, hparams, strides=(2, 2), kernel=(hparams.kernel_size, hparams.kernel_size), name=name)
python
def decompress_decoder_2d(x, hparams, name=None): """Decoder that decompresses 2-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, compress_height, compress_width, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, height, width, hparams.hidden_size]. """ return decompress_decoder(x, hparams, strides=(2, 2), kernel=(hparams.kernel_size, hparams.kernel_size), name=name)
[ "def", "decompress_decoder_2d", "(", "x", ",", "hparams", ",", "name", "=", "None", ")", ":", "return", "decompress_decoder", "(", "x", ",", "hparams", ",", "strides", "=", "(", "2", ",", "2", ")", ",", "kernel", "=", "(", "hparams", ".", "kernel_size", ",", "hparams", ".", "kernel_size", ")", ",", "name", "=", "name", ")" ]
Decoder that decompresses 2-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, compress_height, compress_width, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, height, width, hparams.hidden_size].
[ "Decoder", "that", "decompresses", "2", "-", "D", "inputs", "by", "2", "**", "num_compress_steps", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L355-L369
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
decompress_decoder_1d
def decompress_decoder_1d(x, hparams, name=None): """Decoder that decompresses 1-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, compress_length, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, length, hparams.hidden_size]. """ x = tf.expand_dims(x, axis=2) output = decompress_decoder(x, hparams, strides=(2, 1), kernel=(hparams.kernel_size, 1), name=name) return tf.squeeze(output, axis=2)
python
def decompress_decoder_1d(x, hparams, name=None): """Decoder that decompresses 1-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, compress_length, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, length, hparams.hidden_size]. """ x = tf.expand_dims(x, axis=2) output = decompress_decoder(x, hparams, strides=(2, 1), kernel=(hparams.kernel_size, 1), name=name) return tf.squeeze(output, axis=2)
[ "def", "decompress_decoder_1d", "(", "x", ",", "hparams", ",", "name", "=", "None", ")", ":", "x", "=", "tf", ".", "expand_dims", "(", "x", ",", "axis", "=", "2", ")", "output", "=", "decompress_decoder", "(", "x", ",", "hparams", ",", "strides", "=", "(", "2", ",", "1", ")", ",", "kernel", "=", "(", "hparams", ".", "kernel_size", ",", "1", ")", ",", "name", "=", "name", ")", "return", "tf", ".", "squeeze", "(", "output", ",", "axis", "=", "2", ")" ]
Decoder that decompresses 1-D inputs by 2**num_compress_steps. Args: x: Tensor of shape [batch, compress_length, channels]. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, length, hparams.hidden_size].
[ "Decoder", "that", "decompresses", "1", "-", "D", "inputs", "by", "2", "**", "num_compress_steps", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L372-L388
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
transformer_text_encoder
def transformer_text_encoder(inputs, target_space, hparams, name=None): """Transformer text encoder over inputs with unmasked full attention. Args: inputs: Tensor of shape [batch, length, 1, hparams.hidden_size]. target_space: int. Used for encoding inputs under a target space id. hparams: HParams. name: string, variable scope. Returns: encoder_output: Tensor of shape [batch, length, hparams.hidden_size]. ed: Tensor of shape [batch, 1, 1, length]. Encoder-decoder attention bias for any padded tokens. """ with tf.variable_scope(name, default_name="transformer_text_encoder"): inputs = common_layers.flatten4d3d(inputs) [ encoder_input, encoder_self_attention_bias, ed, ] = transformer_layers.transformer_prepare_encoder( inputs, target_space=target_space, hparams=hparams) encoder_input = tf.nn.dropout(encoder_input, 1.0 - hparams.dropout) encoder_output = transformer_layers.transformer_encoder( encoder_input, encoder_self_attention_bias, hparams) return encoder_output, ed
python
def transformer_text_encoder(inputs, target_space, hparams, name=None): """Transformer text encoder over inputs with unmasked full attention. Args: inputs: Tensor of shape [batch, length, 1, hparams.hidden_size]. target_space: int. Used for encoding inputs under a target space id. hparams: HParams. name: string, variable scope. Returns: encoder_output: Tensor of shape [batch, length, hparams.hidden_size]. ed: Tensor of shape [batch, 1, 1, length]. Encoder-decoder attention bias for any padded tokens. """ with tf.variable_scope(name, default_name="transformer_text_encoder"): inputs = common_layers.flatten4d3d(inputs) [ encoder_input, encoder_self_attention_bias, ed, ] = transformer_layers.transformer_prepare_encoder( inputs, target_space=target_space, hparams=hparams) encoder_input = tf.nn.dropout(encoder_input, 1.0 - hparams.dropout) encoder_output = transformer_layers.transformer_encoder( encoder_input, encoder_self_attention_bias, hparams) return encoder_output, ed
[ "def", "transformer_text_encoder", "(", "inputs", ",", "target_space", ",", "hparams", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"transformer_text_encoder\"", ")", ":", "inputs", "=", "common_layers", ".", "flatten4d3d", "(", "inputs", ")", "[", "encoder_input", ",", "encoder_self_attention_bias", ",", "ed", ",", "]", "=", "transformer_layers", ".", "transformer_prepare_encoder", "(", "inputs", ",", "target_space", "=", "target_space", ",", "hparams", "=", "hparams", ")", "encoder_input", "=", "tf", ".", "nn", ".", "dropout", "(", "encoder_input", ",", "1.0", "-", "hparams", ".", "dropout", ")", "encoder_output", "=", "transformer_layers", ".", "transformer_encoder", "(", "encoder_input", ",", "encoder_self_attention_bias", ",", "hparams", ")", "return", "encoder_output", ",", "ed" ]
Transformer text encoder over inputs with unmasked full attention. Args: inputs: Tensor of shape [batch, length, 1, hparams.hidden_size]. target_space: int. Used for encoding inputs under a target space id. hparams: HParams. name: string, variable scope. Returns: encoder_output: Tensor of shape [batch, length, hparams.hidden_size]. ed: Tensor of shape [batch, 1, 1, length]. Encoder-decoder attention bias for any padded tokens.
[ "Transformer", "text", "encoder", "over", "inputs", "with", "unmasked", "full", "attention", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L391-L419
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
transformer_image_decoder
def transformer_image_decoder(targets, encoder_output, ed_attention_bias, hparams, name=None): """Transformer image decoder over targets with local attention. Args: targets: Tensor of shape [batch, ...], and whose size is batch * height * width * hparams.num_channels * hparams.hidden_size. encoder_output: Tensor of shape [batch, length_kv, hparams.hidden_size]. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, height, width * hparams.num_channels, hparams.hidden_size]. """ with tf.variable_scope(name, default_name="transformer_dec"): batch_size = common_layers.shape_list(targets)[0] targets = tf.reshape(targets, [batch_size, hparams.img_len, hparams.img_len, hparams.num_channels * hparams.hidden_size]) decoder_input, _, _ = cia.prepare_decoder(targets, hparams) decoder_output = cia.transformer_decoder_layers( decoder_input, encoder_output, hparams.num_decoder_layers or hparams.num_hidden_layers, hparams, attention_type=hparams.dec_attention_type, encoder_decoder_attention_bias=ed_attention_bias, name="decoder") decoder_output = tf.reshape(decoder_output, [batch_size, hparams.img_len, hparams.img_len * hparams.num_channels, hparams.hidden_size]) return decoder_output
python
def transformer_image_decoder(targets, encoder_output, ed_attention_bias, hparams, name=None): """Transformer image decoder over targets with local attention. Args: targets: Tensor of shape [batch, ...], and whose size is batch * height * width * hparams.num_channels * hparams.hidden_size. encoder_output: Tensor of shape [batch, length_kv, hparams.hidden_size]. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, height, width * hparams.num_channels, hparams.hidden_size]. """ with tf.variable_scope(name, default_name="transformer_dec"): batch_size = common_layers.shape_list(targets)[0] targets = tf.reshape(targets, [batch_size, hparams.img_len, hparams.img_len, hparams.num_channels * hparams.hidden_size]) decoder_input, _, _ = cia.prepare_decoder(targets, hparams) decoder_output = cia.transformer_decoder_layers( decoder_input, encoder_output, hparams.num_decoder_layers or hparams.num_hidden_layers, hparams, attention_type=hparams.dec_attention_type, encoder_decoder_attention_bias=ed_attention_bias, name="decoder") decoder_output = tf.reshape(decoder_output, [batch_size, hparams.img_len, hparams.img_len * hparams.num_channels, hparams.hidden_size]) return decoder_output
[ "def", "transformer_image_decoder", "(", "targets", ",", "encoder_output", ",", "ed_attention_bias", ",", "hparams", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"transformer_dec\"", ")", ":", "batch_size", "=", "common_layers", ".", "shape_list", "(", "targets", ")", "[", "0", "]", "targets", "=", "tf", ".", "reshape", "(", "targets", ",", "[", "batch_size", ",", "hparams", ".", "img_len", ",", "hparams", ".", "img_len", ",", "hparams", ".", "num_channels", "*", "hparams", ".", "hidden_size", "]", ")", "decoder_input", ",", "_", ",", "_", "=", "cia", ".", "prepare_decoder", "(", "targets", ",", "hparams", ")", "decoder_output", "=", "cia", ".", "transformer_decoder_layers", "(", "decoder_input", ",", "encoder_output", ",", "hparams", ".", "num_decoder_layers", "or", "hparams", ".", "num_hidden_layers", ",", "hparams", ",", "attention_type", "=", "hparams", ".", "dec_attention_type", ",", "encoder_decoder_attention_bias", "=", "ed_attention_bias", ",", "name", "=", "\"decoder\"", ")", "decoder_output", "=", "tf", ".", "reshape", "(", "decoder_output", ",", "[", "batch_size", ",", "hparams", ".", "img_len", ",", "hparams", ".", "img_len", "*", "hparams", ".", "num_channels", ",", "hparams", ".", "hidden_size", "]", ")", "return", "decoder_output" ]
Transformer image decoder over targets with local attention. Args: targets: Tensor of shape [batch, ...], and whose size is batch * height * width * hparams.num_channels * hparams.hidden_size. encoder_output: Tensor of shape [batch, length_kv, hparams.hidden_size]. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, height, width * hparams.num_channels, hparams.hidden_size].
[ "Transformer", "image", "decoder", "over", "targets", "with", "local", "attention", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L422-L462
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
transformer_latent_decoder
def transformer_latent_decoder(x, encoder_output, ed_attention_bias, hparams, name=None): """Transformer decoder over latents using latent_attention_type. Args: x: Tensor of shape [batch, length_q, hparams.hidden_size]. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). encoder_output: Tensor of shape [batch, length_kv, hparams.hidden_size]. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, length_q, hparams.hidden_size]. """ with tf.variable_scope(name, default_name="transformer_latent_dec"): batch_size = common_layers.shape_list(x)[0] compressed_img_len = (hparams.img_len // 2**(hparams.num_compress_steps // 2)) x = tf.reshape(x, [batch_size, compressed_img_len, compressed_img_len * hparams.num_latents, hparams.hidden_size]) decoder_input, _, _ = cia.prepare_decoder(x, hparams) decoder_output = cia.transformer_decoder_layers( decoder_input, encoder_output, hparams.num_latent_layers or hparams.num_hidden_layers, hparams, attention_type=hparams.latent_attention_type, encoder_decoder_attention_bias=ed_attention_bias, name="decoder") decoder_output = tf.reshape(decoder_output, [batch_size, compressed_img_len**2 * hparams.num_latents, hparams.hidden_size]) return decoder_output
python
def transformer_latent_decoder(x, encoder_output, ed_attention_bias, hparams, name=None): """Transformer decoder over latents using latent_attention_type. Args: x: Tensor of shape [batch, length_q, hparams.hidden_size]. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). encoder_output: Tensor of shape [batch, length_kv, hparams.hidden_size]. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, length_q, hparams.hidden_size]. """ with tf.variable_scope(name, default_name="transformer_latent_dec"): batch_size = common_layers.shape_list(x)[0] compressed_img_len = (hparams.img_len // 2**(hparams.num_compress_steps // 2)) x = tf.reshape(x, [batch_size, compressed_img_len, compressed_img_len * hparams.num_latents, hparams.hidden_size]) decoder_input, _, _ = cia.prepare_decoder(x, hparams) decoder_output = cia.transformer_decoder_layers( decoder_input, encoder_output, hparams.num_latent_layers or hparams.num_hidden_layers, hparams, attention_type=hparams.latent_attention_type, encoder_decoder_attention_bias=ed_attention_bias, name="decoder") decoder_output = tf.reshape(decoder_output, [batch_size, compressed_img_len**2 * hparams.num_latents, hparams.hidden_size]) return decoder_output
[ "def", "transformer_latent_decoder", "(", "x", ",", "encoder_output", ",", "ed_attention_bias", ",", "hparams", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"transformer_latent_dec\"", ")", ":", "batch_size", "=", "common_layers", ".", "shape_list", "(", "x", ")", "[", "0", "]", "compressed_img_len", "=", "(", "hparams", ".", "img_len", "//", "2", "**", "(", "hparams", ".", "num_compress_steps", "//", "2", ")", ")", "x", "=", "tf", ".", "reshape", "(", "x", ",", "[", "batch_size", ",", "compressed_img_len", ",", "compressed_img_len", "*", "hparams", ".", "num_latents", ",", "hparams", ".", "hidden_size", "]", ")", "decoder_input", ",", "_", ",", "_", "=", "cia", ".", "prepare_decoder", "(", "x", ",", "hparams", ")", "decoder_output", "=", "cia", ".", "transformer_decoder_layers", "(", "decoder_input", ",", "encoder_output", ",", "hparams", ".", "num_latent_layers", "or", "hparams", ".", "num_hidden_layers", ",", "hparams", ",", "attention_type", "=", "hparams", ".", "latent_attention_type", ",", "encoder_decoder_attention_bias", "=", "ed_attention_bias", ",", "name", "=", "\"decoder\"", ")", "decoder_output", "=", "tf", ".", "reshape", "(", "decoder_output", ",", "[", "batch_size", ",", "compressed_img_len", "**", "2", "*", "hparams", ".", "num_latents", ",", "hparams", ".", "hidden_size", "]", ")", "return", "decoder_output" ]
Transformer decoder over latents using latent_attention_type. Args: x: Tensor of shape [batch, length_q, hparams.hidden_size]. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). encoder_output: Tensor of shape [batch, length_kv, hparams.hidden_size]. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. hparams: HParams. name: string, variable scope. Returns: Tensor of shape [batch, length_q, hparams.hidden_size].
[ "Transformer", "decoder", "over", "latents", "using", "latent_attention_type", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L465-L506
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
bottleneck_layer
def bottleneck_layer(inputs, hparams, name="discrete_bottleneck"): """Computes latents given inputs (typically, compressed targets).""" [ latents_dense, latents_discrete, extra_loss, embed_fn, _, ] = hparams.bottleneck(inputs=inputs, filter_size=hparams.compress_filter_size, name=name, mode=hparams.mode) if DO_SUMMARIES: tf.summary.histogram("discrete_latents", tf.reshape(latents_discrete, [-1])) return latents_dense, latents_discrete, extra_loss, embed_fn
python
def bottleneck_layer(inputs, hparams, name="discrete_bottleneck"): """Computes latents given inputs (typically, compressed targets).""" [ latents_dense, latents_discrete, extra_loss, embed_fn, _, ] = hparams.bottleneck(inputs=inputs, filter_size=hparams.compress_filter_size, name=name, mode=hparams.mode) if DO_SUMMARIES: tf.summary.histogram("discrete_latents", tf.reshape(latents_discrete, [-1])) return latents_dense, latents_discrete, extra_loss, embed_fn
[ "def", "bottleneck_layer", "(", "inputs", ",", "hparams", ",", "name", "=", "\"discrete_bottleneck\"", ")", ":", "[", "latents_dense", ",", "latents_discrete", ",", "extra_loss", ",", "embed_fn", ",", "_", ",", "]", "=", "hparams", ".", "bottleneck", "(", "inputs", "=", "inputs", ",", "filter_size", "=", "hparams", ".", "compress_filter_size", ",", "name", "=", "name", ",", "mode", "=", "hparams", ".", "mode", ")", "if", "DO_SUMMARIES", ":", "tf", ".", "summary", ".", "histogram", "(", "\"discrete_latents\"", ",", "tf", ".", "reshape", "(", "latents_discrete", ",", "[", "-", "1", "]", ")", ")", "return", "latents_dense", ",", "latents_discrete", ",", "extra_loss", ",", "embed_fn" ]
Computes latents given inputs (typically, compressed targets).
[ "Computes", "latents", "given", "inputs", "(", "typically", "compressed", "targets", ")", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L509-L526
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
latent_prediction_model
def latent_prediction_model(inputs, ed_attention_bias, latents_discrete, latents_dense, hparams, vocab_size=None, name=None): """Transformer-based latent prediction model. It is an autoregressive decoder over latents_discrete given inputs. Args: inputs: Tensor of shape [batch, length_kv, hparams.hidden_size]. Inputs to attend to for the decoder on latents. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. latents_discrete: Tensor of shape [batch, length_q, vocab_size]. One-hot latents to compute log-probability of given inputs. latents_dense: Tensor of shape [batch, length_q, hparams.hidden_size]. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). hparams: HParams. vocab_size: int or None. If None, it is 2**hparams.bottleneck_bits. name: string, variable scope. Returns: latents_pred: Tensor of shape [batch, length_q, hparams.hidden_size]. latents_pred_loss: Tensor of shape [batch, length_q]. """ with tf.variable_scope(name, default_name="latent_prediction"): if hparams.mode != tf.estimator.ModeKeys.PREDICT: latents_pred = transformer_latent_decoder(tf.stop_gradient(latents_dense), inputs, ed_attention_bias, hparams, name) if vocab_size is None: vocab_size = 2**hparams.bottleneck_bits if not hparams.soft_em: # TODO(trandustin): latents_discrete is not one-hot from # discrete_bottleneck unless hparams.soft_em is True. Refactor. latents_discrete = tf.one_hot(latents_discrete, depth=vocab_size) _, latent_pred_loss = ae_latent_softmax( latents_pred, tf.stop_gradient(latents_discrete), vocab_size, hparams) return latents_pred, latent_pred_loss
python
def latent_prediction_model(inputs, ed_attention_bias, latents_discrete, latents_dense, hparams, vocab_size=None, name=None): """Transformer-based latent prediction model. It is an autoregressive decoder over latents_discrete given inputs. Args: inputs: Tensor of shape [batch, length_kv, hparams.hidden_size]. Inputs to attend to for the decoder on latents. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. latents_discrete: Tensor of shape [batch, length_q, vocab_size]. One-hot latents to compute log-probability of given inputs. latents_dense: Tensor of shape [batch, length_q, hparams.hidden_size]. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). hparams: HParams. vocab_size: int or None. If None, it is 2**hparams.bottleneck_bits. name: string, variable scope. Returns: latents_pred: Tensor of shape [batch, length_q, hparams.hidden_size]. latents_pred_loss: Tensor of shape [batch, length_q]. """ with tf.variable_scope(name, default_name="latent_prediction"): if hparams.mode != tf.estimator.ModeKeys.PREDICT: latents_pred = transformer_latent_decoder(tf.stop_gradient(latents_dense), inputs, ed_attention_bias, hparams, name) if vocab_size is None: vocab_size = 2**hparams.bottleneck_bits if not hparams.soft_em: # TODO(trandustin): latents_discrete is not one-hot from # discrete_bottleneck unless hparams.soft_em is True. Refactor. latents_discrete = tf.one_hot(latents_discrete, depth=vocab_size) _, latent_pred_loss = ae_latent_softmax( latents_pred, tf.stop_gradient(latents_discrete), vocab_size, hparams) return latents_pred, latent_pred_loss
[ "def", "latent_prediction_model", "(", "inputs", ",", "ed_attention_bias", ",", "latents_discrete", ",", "latents_dense", ",", "hparams", ",", "vocab_size", "=", "None", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"latent_prediction\"", ")", ":", "if", "hparams", ".", "mode", "!=", "tf", ".", "estimator", ".", "ModeKeys", ".", "PREDICT", ":", "latents_pred", "=", "transformer_latent_decoder", "(", "tf", ".", "stop_gradient", "(", "latents_dense", ")", ",", "inputs", ",", "ed_attention_bias", ",", "hparams", ",", "name", ")", "if", "vocab_size", "is", "None", ":", "vocab_size", "=", "2", "**", "hparams", ".", "bottleneck_bits", "if", "not", "hparams", ".", "soft_em", ":", "# TODO(trandustin): latents_discrete is not one-hot from", "# discrete_bottleneck unless hparams.soft_em is True. Refactor.", "latents_discrete", "=", "tf", ".", "one_hot", "(", "latents_discrete", ",", "depth", "=", "vocab_size", ")", "_", ",", "latent_pred_loss", "=", "ae_latent_softmax", "(", "latents_pred", ",", "tf", ".", "stop_gradient", "(", "latents_discrete", ")", ",", "vocab_size", ",", "hparams", ")", "return", "latents_pred", ",", "latent_pred_loss" ]
Transformer-based latent prediction model. It is an autoregressive decoder over latents_discrete given inputs. Args: inputs: Tensor of shape [batch, length_kv, hparams.hidden_size]. Inputs to attend to for the decoder on latents. ed_attention_bias: Tensor which broadcasts with shape [batch, hparams.num_heads, length_q, length_kv]. Encoder-decoder attention bias. latents_discrete: Tensor of shape [batch, length_q, vocab_size]. One-hot latents to compute log-probability of given inputs. latents_dense: Tensor of shape [batch, length_q, hparams.hidden_size]. length_q is the latent length, which is height * width * hparams.num_latents / (2**hparams.num_compress_steps). hparams: HParams. vocab_size: int or None. If None, it is 2**hparams.bottleneck_bits. name: string, variable scope. Returns: latents_pred: Tensor of shape [batch, length_q, hparams.hidden_size]. latents_pred_loss: Tensor of shape [batch, length_q].
[ "Transformer", "-", "based", "latent", "prediction", "model", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L529-L573
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
transformer_autoencoder
def transformer_autoencoder(inputs, targets, target_space, hparams, cache=None, predict_mask=1.0): """Auto-encoder using a Transformer decoder and a prior over latent sequences. Args: inputs: Tensor of shape [batch, length, 1, hparams.hidden_size] or None. targets: Tensor of shape [batch, ..., channels]. Ellipses may be 1 or 2 dimensions denoting sequence length. target_space: int. Used for encoding inputs under a target space id. hparams: HParams. cache: Tensor of shape [batch, length] or None. predict_mask: Tensor masking whether to use gold targets or predictions. Returns: decoder_output: Tensor of shape [batch, ..., hparams.hidden_size] presenting pre-logit activations. After a transformation (`top` in `T2TModel`), it is used with targets to compute the "training" (reconstruction) loss. losses: dict of str to Tensors. There are three loss terms: "extra", "extra_loss", and "latent_pred". The first is hard-coded to 0. The latter two are Tensors of shape [batch]. cache: Tensor of shape [batch, length], either the same as cache, or newly computed if the cache input is None. """ original_targets_shape = common_layers.shape_list(targets) batch_size = original_targets_shape[0] if len(original_targets_shape) == 4: compress_fn = compress_encoder_2d decompress_fn = decompress_decoder_2d else: compress_fn = compress_encoder_1d decompress_fn = decompress_decoder_1d ed_attention_bias = None if inputs is not None: inputs, ed_attention_bias = transformer_text_encoder( inputs, target_space, hparams, name="input_encoder") losses = {"extra": 0., "extra_loss": 0., "latent_pred": 0.} if hparams.mode != tf.estimator.ModeKeys.PREDICT: targets_compressed = compress_fn(targets, hparams, name="compress") if hparams.mode == tf.estimator.ModeKeys.TRAIN: scale = common_layers.inverse_exp_decay(hparams.startup_steps) else: scale = 1.0 scale = tf.to_float(tf.less(tf.random_uniform([batch_size]), scale)) latents_dense, latents_discrete, extra_loss, _ = bottleneck_layer( targets_compressed, hparams) extra_loss = scale * tf.reduce_mean(extra_loss) _, latents_pred_loss = latent_prediction_model( inputs, ed_attention_bias, latents_discrete, latents_dense, hparams, name="latent_pred") latent_time = tf.less(hparams.mask_startup_steps, tf.to_int32(tf.train.get_global_step())) latents_pred_loss = scale * tf.reduce_mean(latents_pred_loss) latents_pred_loss *= tf.to_float(latent_time) # Apply dropout noise for each data point and time step. latents_dense_shape = common_layers.shape_list(latents_dense) latents_dense = tf.nn.dropout( latents_dense, keep_prob=1 - hparams.latent_dropout, noise_shape=[latents_dense_shape[0], latents_dense_shape[1], 1]) # TODO(trandustin): Can we combine extra and extra_loss? losses = {"extra": 0., "extra_loss": extra_loss, "latent_pred": latents_pred_loss} else: # Set the latent length, which is num_latents times the number of latent # pixels. The number of latent pixels is determined by a compression factor # on the number of image pixels. latent_len = ((hparams.img_len * hparams.img_len * hparams.num_latents) / (2**hparams.num_compress_steps)) _, _, _, embed_fn = bottleneck_layer(targets_compressed, hparams) latents_dense = tf.zeros([batch_size, latent_len, 1, hparams.hidden_size]) if cache is None: cache = ae_latent_sample_beam(latents_dense, inputs, ed_attention_bias, embed_fn, hparams) cache_one_hot = tf.one_hot(cache, depth=2**hparams.bottleneck_bits) latents_dense = embed_fn(cache_one_hot, hparams.hidden_size) if len(original_targets_shape) == 4: compressed_img_len = (hparams.img_len // 2**(hparams.num_compress_steps // 2)) latents_dense = tf.reshape(latents_dense, [batch_size, compressed_img_len, compressed_img_len, hparams.num_latents * hparams.hidden_size]) latents_dense = decompress_fn(latents_dense, hparams, name="decompress") latents_dense = tf.reshape( latents_dense, [-1, hparams.img_len, hparams.img_len, hparams.hidden_size]) if hparams.use_gold_targets: if hparams.mode == tf.estimator.ModeKeys.PREDICT: masking = predict_mask else: masking = common_layers.inverse_exp_decay(hparams.mask_startup_steps) targets, _, _ = cia.maybe_reshape_4d_to_3d(targets) mask = tf.less(masking, tf.random_uniform(common_layers.shape_list(targets)[:-1])) mask = tf.expand_dims(tf.to_float(mask), 2) latents_dense = mask * targets + (1.0 - mask) * latents_dense latents_dense = tf.reshape(latents_dense, original_targets_shape) if hparams.decode_autoregressive: decoder_output = transformer_image_decoder( latents_dense, inputs, ed_attention_bias, hparams, name="decoder") else: decoder_output = latents_dense return decoder_output, losses, cache
python
def transformer_autoencoder(inputs, targets, target_space, hparams, cache=None, predict_mask=1.0): """Auto-encoder using a Transformer decoder and a prior over latent sequences. Args: inputs: Tensor of shape [batch, length, 1, hparams.hidden_size] or None. targets: Tensor of shape [batch, ..., channels]. Ellipses may be 1 or 2 dimensions denoting sequence length. target_space: int. Used for encoding inputs under a target space id. hparams: HParams. cache: Tensor of shape [batch, length] or None. predict_mask: Tensor masking whether to use gold targets or predictions. Returns: decoder_output: Tensor of shape [batch, ..., hparams.hidden_size] presenting pre-logit activations. After a transformation (`top` in `T2TModel`), it is used with targets to compute the "training" (reconstruction) loss. losses: dict of str to Tensors. There are three loss terms: "extra", "extra_loss", and "latent_pred". The first is hard-coded to 0. The latter two are Tensors of shape [batch]. cache: Tensor of shape [batch, length], either the same as cache, or newly computed if the cache input is None. """ original_targets_shape = common_layers.shape_list(targets) batch_size = original_targets_shape[0] if len(original_targets_shape) == 4: compress_fn = compress_encoder_2d decompress_fn = decompress_decoder_2d else: compress_fn = compress_encoder_1d decompress_fn = decompress_decoder_1d ed_attention_bias = None if inputs is not None: inputs, ed_attention_bias = transformer_text_encoder( inputs, target_space, hparams, name="input_encoder") losses = {"extra": 0., "extra_loss": 0., "latent_pred": 0.} if hparams.mode != tf.estimator.ModeKeys.PREDICT: targets_compressed = compress_fn(targets, hparams, name="compress") if hparams.mode == tf.estimator.ModeKeys.TRAIN: scale = common_layers.inverse_exp_decay(hparams.startup_steps) else: scale = 1.0 scale = tf.to_float(tf.less(tf.random_uniform([batch_size]), scale)) latents_dense, latents_discrete, extra_loss, _ = bottleneck_layer( targets_compressed, hparams) extra_loss = scale * tf.reduce_mean(extra_loss) _, latents_pred_loss = latent_prediction_model( inputs, ed_attention_bias, latents_discrete, latents_dense, hparams, name="latent_pred") latent_time = tf.less(hparams.mask_startup_steps, tf.to_int32(tf.train.get_global_step())) latents_pred_loss = scale * tf.reduce_mean(latents_pred_loss) latents_pred_loss *= tf.to_float(latent_time) # Apply dropout noise for each data point and time step. latents_dense_shape = common_layers.shape_list(latents_dense) latents_dense = tf.nn.dropout( latents_dense, keep_prob=1 - hparams.latent_dropout, noise_shape=[latents_dense_shape[0], latents_dense_shape[1], 1]) # TODO(trandustin): Can we combine extra and extra_loss? losses = {"extra": 0., "extra_loss": extra_loss, "latent_pred": latents_pred_loss} else: # Set the latent length, which is num_latents times the number of latent # pixels. The number of latent pixels is determined by a compression factor # on the number of image pixels. latent_len = ((hparams.img_len * hparams.img_len * hparams.num_latents) / (2**hparams.num_compress_steps)) _, _, _, embed_fn = bottleneck_layer(targets_compressed, hparams) latents_dense = tf.zeros([batch_size, latent_len, 1, hparams.hidden_size]) if cache is None: cache = ae_latent_sample_beam(latents_dense, inputs, ed_attention_bias, embed_fn, hparams) cache_one_hot = tf.one_hot(cache, depth=2**hparams.bottleneck_bits) latents_dense = embed_fn(cache_one_hot, hparams.hidden_size) if len(original_targets_shape) == 4: compressed_img_len = (hparams.img_len // 2**(hparams.num_compress_steps // 2)) latents_dense = tf.reshape(latents_dense, [batch_size, compressed_img_len, compressed_img_len, hparams.num_latents * hparams.hidden_size]) latents_dense = decompress_fn(latents_dense, hparams, name="decompress") latents_dense = tf.reshape( latents_dense, [-1, hparams.img_len, hparams.img_len, hparams.hidden_size]) if hparams.use_gold_targets: if hparams.mode == tf.estimator.ModeKeys.PREDICT: masking = predict_mask else: masking = common_layers.inverse_exp_decay(hparams.mask_startup_steps) targets, _, _ = cia.maybe_reshape_4d_to_3d(targets) mask = tf.less(masking, tf.random_uniform(common_layers.shape_list(targets)[:-1])) mask = tf.expand_dims(tf.to_float(mask), 2) latents_dense = mask * targets + (1.0 - mask) * latents_dense latents_dense = tf.reshape(latents_dense, original_targets_shape) if hparams.decode_autoregressive: decoder_output = transformer_image_decoder( latents_dense, inputs, ed_attention_bias, hparams, name="decoder") else: decoder_output = latents_dense return decoder_output, losses, cache
[ "def", "transformer_autoencoder", "(", "inputs", ",", "targets", ",", "target_space", ",", "hparams", ",", "cache", "=", "None", ",", "predict_mask", "=", "1.0", ")", ":", "original_targets_shape", "=", "common_layers", ".", "shape_list", "(", "targets", ")", "batch_size", "=", "original_targets_shape", "[", "0", "]", "if", "len", "(", "original_targets_shape", ")", "==", "4", ":", "compress_fn", "=", "compress_encoder_2d", "decompress_fn", "=", "decompress_decoder_2d", "else", ":", "compress_fn", "=", "compress_encoder_1d", "decompress_fn", "=", "decompress_decoder_1d", "ed_attention_bias", "=", "None", "if", "inputs", "is", "not", "None", ":", "inputs", ",", "ed_attention_bias", "=", "transformer_text_encoder", "(", "inputs", ",", "target_space", ",", "hparams", ",", "name", "=", "\"input_encoder\"", ")", "losses", "=", "{", "\"extra\"", ":", "0.", ",", "\"extra_loss\"", ":", "0.", ",", "\"latent_pred\"", ":", "0.", "}", "if", "hparams", ".", "mode", "!=", "tf", ".", "estimator", ".", "ModeKeys", ".", "PREDICT", ":", "targets_compressed", "=", "compress_fn", "(", "targets", ",", "hparams", ",", "name", "=", "\"compress\"", ")", "if", "hparams", ".", "mode", "==", "tf", ".", "estimator", ".", "ModeKeys", ".", "TRAIN", ":", "scale", "=", "common_layers", ".", "inverse_exp_decay", "(", "hparams", ".", "startup_steps", ")", "else", ":", "scale", "=", "1.0", "scale", "=", "tf", ".", "to_float", "(", "tf", ".", "less", "(", "tf", ".", "random_uniform", "(", "[", "batch_size", "]", ")", ",", "scale", ")", ")", "latents_dense", ",", "latents_discrete", ",", "extra_loss", ",", "_", "=", "bottleneck_layer", "(", "targets_compressed", ",", "hparams", ")", "extra_loss", "=", "scale", "*", "tf", ".", "reduce_mean", "(", "extra_loss", ")", "_", ",", "latents_pred_loss", "=", "latent_prediction_model", "(", "inputs", ",", "ed_attention_bias", ",", "latents_discrete", ",", "latents_dense", ",", "hparams", ",", "name", "=", "\"latent_pred\"", ")", "latent_time", "=", "tf", ".", "less", "(", "hparams", ".", "mask_startup_steps", ",", "tf", ".", "to_int32", "(", "tf", ".", "train", ".", "get_global_step", "(", ")", ")", ")", "latents_pred_loss", "=", "scale", "*", "tf", ".", "reduce_mean", "(", "latents_pred_loss", ")", "latents_pred_loss", "*=", "tf", ".", "to_float", "(", "latent_time", ")", "# Apply dropout noise for each data point and time step.", "latents_dense_shape", "=", "common_layers", ".", "shape_list", "(", "latents_dense", ")", "latents_dense", "=", "tf", ".", "nn", ".", "dropout", "(", "latents_dense", ",", "keep_prob", "=", "1", "-", "hparams", ".", "latent_dropout", ",", "noise_shape", "=", "[", "latents_dense_shape", "[", "0", "]", ",", "latents_dense_shape", "[", "1", "]", ",", "1", "]", ")", "# TODO(trandustin): Can we combine extra and extra_loss?", "losses", "=", "{", "\"extra\"", ":", "0.", ",", "\"extra_loss\"", ":", "extra_loss", ",", "\"latent_pred\"", ":", "latents_pred_loss", "}", "else", ":", "# Set the latent length, which is num_latents times the number of latent", "# pixels. The number of latent pixels is determined by a compression factor", "# on the number of image pixels.", "latent_len", "=", "(", "(", "hparams", ".", "img_len", "*", "hparams", ".", "img_len", "*", "hparams", ".", "num_latents", ")", "/", "(", "2", "**", "hparams", ".", "num_compress_steps", ")", ")", "_", ",", "_", ",", "_", ",", "embed_fn", "=", "bottleneck_layer", "(", "targets_compressed", ",", "hparams", ")", "latents_dense", "=", "tf", ".", "zeros", "(", "[", "batch_size", ",", "latent_len", ",", "1", ",", "hparams", ".", "hidden_size", "]", ")", "if", "cache", "is", "None", ":", "cache", "=", "ae_latent_sample_beam", "(", "latents_dense", ",", "inputs", ",", "ed_attention_bias", ",", "embed_fn", ",", "hparams", ")", "cache_one_hot", "=", "tf", ".", "one_hot", "(", "cache", ",", "depth", "=", "2", "**", "hparams", ".", "bottleneck_bits", ")", "latents_dense", "=", "embed_fn", "(", "cache_one_hot", ",", "hparams", ".", "hidden_size", ")", "if", "len", "(", "original_targets_shape", ")", "==", "4", ":", "compressed_img_len", "=", "(", "hparams", ".", "img_len", "//", "2", "**", "(", "hparams", ".", "num_compress_steps", "//", "2", ")", ")", "latents_dense", "=", "tf", ".", "reshape", "(", "latents_dense", ",", "[", "batch_size", ",", "compressed_img_len", ",", "compressed_img_len", ",", "hparams", ".", "num_latents", "*", "hparams", ".", "hidden_size", "]", ")", "latents_dense", "=", "decompress_fn", "(", "latents_dense", ",", "hparams", ",", "name", "=", "\"decompress\"", ")", "latents_dense", "=", "tf", ".", "reshape", "(", "latents_dense", ",", "[", "-", "1", ",", "hparams", ".", "img_len", ",", "hparams", ".", "img_len", ",", "hparams", ".", "hidden_size", "]", ")", "if", "hparams", ".", "use_gold_targets", ":", "if", "hparams", ".", "mode", "==", "tf", ".", "estimator", ".", "ModeKeys", ".", "PREDICT", ":", "masking", "=", "predict_mask", "else", ":", "masking", "=", "common_layers", ".", "inverse_exp_decay", "(", "hparams", ".", "mask_startup_steps", ")", "targets", ",", "_", ",", "_", "=", "cia", ".", "maybe_reshape_4d_to_3d", "(", "targets", ")", "mask", "=", "tf", ".", "less", "(", "masking", ",", "tf", ".", "random_uniform", "(", "common_layers", ".", "shape_list", "(", "targets", ")", "[", ":", "-", "1", "]", ")", ")", "mask", "=", "tf", ".", "expand_dims", "(", "tf", ".", "to_float", "(", "mask", ")", ",", "2", ")", "latents_dense", "=", "mask", "*", "targets", "+", "(", "1.0", "-", "mask", ")", "*", "latents_dense", "latents_dense", "=", "tf", ".", "reshape", "(", "latents_dense", ",", "original_targets_shape", ")", "if", "hparams", ".", "decode_autoregressive", ":", "decoder_output", "=", "transformer_image_decoder", "(", "latents_dense", ",", "inputs", ",", "ed_attention_bias", ",", "hparams", ",", "name", "=", "\"decoder\"", ")", "else", ":", "decoder_output", "=", "latents_dense", "return", "decoder_output", ",", "losses", ",", "cache" ]
Auto-encoder using a Transformer decoder and a prior over latent sequences. Args: inputs: Tensor of shape [batch, length, 1, hparams.hidden_size] or None. targets: Tensor of shape [batch, ..., channels]. Ellipses may be 1 or 2 dimensions denoting sequence length. target_space: int. Used for encoding inputs under a target space id. hparams: HParams. cache: Tensor of shape [batch, length] or None. predict_mask: Tensor masking whether to use gold targets or predictions. Returns: decoder_output: Tensor of shape [batch, ..., hparams.hidden_size] presenting pre-logit activations. After a transformation (`top` in `T2TModel`), it is used with targets to compute the "training" (reconstruction) loss. losses: dict of str to Tensors. There are three loss terms: "extra", "extra_loss", and "latent_pred". The first is hard-coded to 0. The latter two are Tensors of shape [batch]. cache: Tensor of shape [batch, length], either the same as cache, or newly computed if the cache input is None.
[ "Auto", "-", "encoder", "using", "a", "Transformer", "decoder", "and", "a", "prior", "over", "latent", "sequences", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L576-L700
train
tensorflow/tensor2tensor
tensor2tensor/layers/latent_layers.py
iaf_flow
def iaf_flow(one_hot_assignments, scale_weights, scale_bias, num_codes, summary=True, name=None): """Performs a single IAF flow using scale and normalization transformations. Args: one_hot_assignments: Assignments Tensor with shape [num_samples, batch_size, latent_size, num_codes]. scale_weights: Tensor corresponding to lower triangular matrix used to autoregressively generate scale matrix from assignments. To ensure the lower-triangular matrix has length of latent_size, scale_weights should be a rank-one tensor with size latent_size * (latent_size + 1) / 2. scale_bias: Bias tensor to be added to scale tensor, with shape [latent_size, num_codes]. If scale weights are zero, initialize scale_bias to be log(exp(1.) / 2. - 1) so initial transformation is identity. num_codes: Number of codes in codebook. summary: Whether to save summaries. name: String used for name scope. Returns: flow_output: Transformed one-hot assignments. inverse_log_det_jacobian: Inverse log deteriminant of Jacobian corresponding to transformation. """ with tf.name_scope(name, default_name="iaf"): # Pad the one_hot_assignments by zeroing out the first latent dimension and # shifting the rest down by one (and removing the last dimension). padded_assignments = tf.pad( one_hot_assignments, [[0, 0], [0, 0], [1, 0], [0, 0]])[:, :, :-1, :] scale_bijector = tfp.distributions.bijectors.Affine( scale_tril=tfp.distributions.fill_triangular(scale_weights)) scale = scale_bijector.forward( tf.transpose(padded_assignments, [0, 1, 3, 2])) # Transpose the bijector output since it performs a batch matmul. scale = tf.transpose(scale, [0, 1, 3, 2]) scale = tf.nn.softplus(scale) scale = scale + tf.nn.softplus(scale_bias[tf.newaxis, tf.newaxis, ...]) # Don't need last dimension since the transformation keeps it constant. scale = scale[..., :-1] z = one_hot_assignments[..., :-1] unnormalized_probs = tf.concat([z * scale, one_hot_assignments[..., -1, tf.newaxis]], axis=-1) normalizer = tf.reduce_sum(unnormalized_probs, axis=-1) flow_output = unnormalized_probs / (normalizer[..., tf.newaxis]) inverse_log_det_jacobian = (-tf.reduce_sum(tf.log(scale), axis=-1) + num_codes * tf.log(normalizer)) if summary: tf.summary.histogram("iaf/scale", tf.reshape(scale, [-1])) tf.summary.histogram("iaf/inverse_log_det_jacobian", tf.reshape(inverse_log_det_jacobian, [-1])) return flow_output, inverse_log_det_jacobian
python
def iaf_flow(one_hot_assignments, scale_weights, scale_bias, num_codes, summary=True, name=None): """Performs a single IAF flow using scale and normalization transformations. Args: one_hot_assignments: Assignments Tensor with shape [num_samples, batch_size, latent_size, num_codes]. scale_weights: Tensor corresponding to lower triangular matrix used to autoregressively generate scale matrix from assignments. To ensure the lower-triangular matrix has length of latent_size, scale_weights should be a rank-one tensor with size latent_size * (latent_size + 1) / 2. scale_bias: Bias tensor to be added to scale tensor, with shape [latent_size, num_codes]. If scale weights are zero, initialize scale_bias to be log(exp(1.) / 2. - 1) so initial transformation is identity. num_codes: Number of codes in codebook. summary: Whether to save summaries. name: String used for name scope. Returns: flow_output: Transformed one-hot assignments. inverse_log_det_jacobian: Inverse log deteriminant of Jacobian corresponding to transformation. """ with tf.name_scope(name, default_name="iaf"): # Pad the one_hot_assignments by zeroing out the first latent dimension and # shifting the rest down by one (and removing the last dimension). padded_assignments = tf.pad( one_hot_assignments, [[0, 0], [0, 0], [1, 0], [0, 0]])[:, :, :-1, :] scale_bijector = tfp.distributions.bijectors.Affine( scale_tril=tfp.distributions.fill_triangular(scale_weights)) scale = scale_bijector.forward( tf.transpose(padded_assignments, [0, 1, 3, 2])) # Transpose the bijector output since it performs a batch matmul. scale = tf.transpose(scale, [0, 1, 3, 2]) scale = tf.nn.softplus(scale) scale = scale + tf.nn.softplus(scale_bias[tf.newaxis, tf.newaxis, ...]) # Don't need last dimension since the transformation keeps it constant. scale = scale[..., :-1] z = one_hot_assignments[..., :-1] unnormalized_probs = tf.concat([z * scale, one_hot_assignments[..., -1, tf.newaxis]], axis=-1) normalizer = tf.reduce_sum(unnormalized_probs, axis=-1) flow_output = unnormalized_probs / (normalizer[..., tf.newaxis]) inverse_log_det_jacobian = (-tf.reduce_sum(tf.log(scale), axis=-1) + num_codes * tf.log(normalizer)) if summary: tf.summary.histogram("iaf/scale", tf.reshape(scale, [-1])) tf.summary.histogram("iaf/inverse_log_det_jacobian", tf.reshape(inverse_log_det_jacobian, [-1])) return flow_output, inverse_log_det_jacobian
[ "def", "iaf_flow", "(", "one_hot_assignments", ",", "scale_weights", ",", "scale_bias", ",", "num_codes", ",", "summary", "=", "True", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "name_scope", "(", "name", ",", "default_name", "=", "\"iaf\"", ")", ":", "# Pad the one_hot_assignments by zeroing out the first latent dimension and", "# shifting the rest down by one (and removing the last dimension).", "padded_assignments", "=", "tf", ".", "pad", "(", "one_hot_assignments", ",", "[", "[", "0", ",", "0", "]", ",", "[", "0", ",", "0", "]", ",", "[", "1", ",", "0", "]", ",", "[", "0", ",", "0", "]", "]", ")", "[", ":", ",", ":", ",", ":", "-", "1", ",", ":", "]", "scale_bijector", "=", "tfp", ".", "distributions", ".", "bijectors", ".", "Affine", "(", "scale_tril", "=", "tfp", ".", "distributions", ".", "fill_triangular", "(", "scale_weights", ")", ")", "scale", "=", "scale_bijector", ".", "forward", "(", "tf", ".", "transpose", "(", "padded_assignments", ",", "[", "0", ",", "1", ",", "3", ",", "2", "]", ")", ")", "# Transpose the bijector output since it performs a batch matmul.", "scale", "=", "tf", ".", "transpose", "(", "scale", ",", "[", "0", ",", "1", ",", "3", ",", "2", "]", ")", "scale", "=", "tf", ".", "nn", ".", "softplus", "(", "scale", ")", "scale", "=", "scale", "+", "tf", ".", "nn", ".", "softplus", "(", "scale_bias", "[", "tf", ".", "newaxis", ",", "tf", ".", "newaxis", ",", "...", "]", ")", "# Don't need last dimension since the transformation keeps it constant.", "scale", "=", "scale", "[", "...", ",", ":", "-", "1", "]", "z", "=", "one_hot_assignments", "[", "...", ",", ":", "-", "1", "]", "unnormalized_probs", "=", "tf", ".", "concat", "(", "[", "z", "*", "scale", ",", "one_hot_assignments", "[", "...", ",", "-", "1", ",", "tf", ".", "newaxis", "]", "]", ",", "axis", "=", "-", "1", ")", "normalizer", "=", "tf", ".", "reduce_sum", "(", "unnormalized_probs", ",", "axis", "=", "-", "1", ")", "flow_output", "=", "unnormalized_probs", "/", "(", "normalizer", "[", "...", ",", "tf", ".", "newaxis", "]", ")", "inverse_log_det_jacobian", "=", "(", "-", "tf", ".", "reduce_sum", "(", "tf", ".", "log", "(", "scale", ")", ",", "axis", "=", "-", "1", ")", "+", "num_codes", "*", "tf", ".", "log", "(", "normalizer", ")", ")", "if", "summary", ":", "tf", ".", "summary", ".", "histogram", "(", "\"iaf/scale\"", ",", "tf", ".", "reshape", "(", "scale", ",", "[", "-", "1", "]", ")", ")", "tf", ".", "summary", ".", "histogram", "(", "\"iaf/inverse_log_det_jacobian\"", ",", "tf", ".", "reshape", "(", "inverse_log_det_jacobian", ",", "[", "-", "1", "]", ")", ")", "return", "flow_output", ",", "inverse_log_det_jacobian" ]
Performs a single IAF flow using scale and normalization transformations. Args: one_hot_assignments: Assignments Tensor with shape [num_samples, batch_size, latent_size, num_codes]. scale_weights: Tensor corresponding to lower triangular matrix used to autoregressively generate scale matrix from assignments. To ensure the lower-triangular matrix has length of latent_size, scale_weights should be a rank-one tensor with size latent_size * (latent_size + 1) / 2. scale_bias: Bias tensor to be added to scale tensor, with shape [latent_size, num_codes]. If scale weights are zero, initialize scale_bias to be log(exp(1.) / 2. - 1) so initial transformation is identity. num_codes: Number of codes in codebook. summary: Whether to save summaries. name: String used for name scope. Returns: flow_output: Transformed one-hot assignments. inverse_log_det_jacobian: Inverse log deteriminant of Jacobian corresponding to transformation.
[ "Performs", "a", "single", "IAF", "flow", "using", "scale", "and", "normalization", "transformations", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/latent_layers.py#L703-L758
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/image_lsun.py
_get_lsun
def _get_lsun(directory, category, split_name): """Downloads all lsun files to directory unless they are there.""" generator_utils.maybe_download(directory, _LSUN_DATA_FILENAME % (category, split_name), _LSUN_URL % (category, split_name))
python
def _get_lsun(directory, category, split_name): """Downloads all lsun files to directory unless they are there.""" generator_utils.maybe_download(directory, _LSUN_DATA_FILENAME % (category, split_name), _LSUN_URL % (category, split_name))
[ "def", "_get_lsun", "(", "directory", ",", "category", ",", "split_name", ")", ":", "generator_utils", ".", "maybe_download", "(", "directory", ",", "_LSUN_DATA_FILENAME", "%", "(", "category", ",", "split_name", ")", ",", "_LSUN_URL", "%", "(", "category", ",", "split_name", ")", ")" ]
Downloads all lsun files to directory unless they are there.
[ "Downloads", "all", "lsun", "files", "to", "directory", "unless", "they", "are", "there", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/image_lsun.py#L40-L44
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
_mixed_precision_is_enabled
def _mixed_precision_is_enabled(hparams): """Should be the same as in common_attention, avoiding import.""" activation_dtype = hparams.activation_dtype weight_dtype = hparams.weight_dtype return activation_dtype == tf.float16 and weight_dtype == tf.float32
python
def _mixed_precision_is_enabled(hparams): """Should be the same as in common_attention, avoiding import.""" activation_dtype = hparams.activation_dtype weight_dtype = hparams.weight_dtype return activation_dtype == tf.float16 and weight_dtype == tf.float32
[ "def", "_mixed_precision_is_enabled", "(", "hparams", ")", ":", "activation_dtype", "=", "hparams", ".", "activation_dtype", "weight_dtype", "=", "hparams", ".", "weight_dtype", "return", "activation_dtype", "==", "tf", ".", "float16", "and", "weight_dtype", "==", "tf", ".", "float32" ]
Should be the same as in common_attention, avoiding import.
[ "Should", "be", "the", "same", "as", "in", "common_attention", "avoiding", "import", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L36-L40
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
optimize
def optimize(loss, learning_rate, hparams, use_tpu=False, variables=None): """Minimize loss.""" loss = weight_decay_and_noise(loss, hparams, learning_rate) loss = tf.identity(loss, name="total_loss") if variables is None: variables = tf.trainable_variables() # Print trainable variables. log_variable_sizes(variables, verbose=hparams.summarize_vars) # Print non-trainable variables. non_trainable_variables = list( set(tf.global_variables()) - set(variables)) log_variable_sizes(non_trainable_variables, tag="Non-trainable variables", verbose=hparams.summarize_vars) if hparams.summarize_vars: summarize_variables(variables) # Summarize non-trainable variables as well summarize_variables(non_trainable_variables, tag="Non-trainable variables") diet_vars = [ v for v in tf.global_variables() if v.dtype == dtypes.float16_ref ] log_variable_sizes( diet_vars, "Diet Variables", verbose=hparams.summarize_vars) opt = ConditionalOptimizer(hparams.optimizer, learning_rate, hparams, use_tpu) if use_tpu: opt = tf.contrib.tpu.CrossShardOptimizer(opt) opt_summaries = [] if common_layers.should_generate_summaries(): tf.summary.scalar("learning_rate", learning_rate) opt_summaries.append("loss") if hparams.summarize_grads: tf.logging.info("Summarizing gradients") opt_summaries.extend( ["gradients", "gradient_norm", "global_gradient_norm"]) if hparams.clip_grad_norm: tf.logging.info("Clipping gradients, norm: %0.5f", hparams.clip_grad_norm) if hparams.grad_noise_scale: tf.logging.info("Adding noise to gradients, noise scale: %0.5f", hparams.grad_noise_scale) train_op = tf.contrib.layers.optimize_loss( name="training", loss=loss, global_step=tf.train.get_or_create_global_step(), learning_rate=learning_rate, clip_gradients=hparams.clip_grad_norm or None, gradient_noise_scale=hparams.grad_noise_scale or None, optimizer=opt, summaries=opt_summaries, colocate_gradients_with_ops=True, variables=variables) return train_op
python
def optimize(loss, learning_rate, hparams, use_tpu=False, variables=None): """Minimize loss.""" loss = weight_decay_and_noise(loss, hparams, learning_rate) loss = tf.identity(loss, name="total_loss") if variables is None: variables = tf.trainable_variables() # Print trainable variables. log_variable_sizes(variables, verbose=hparams.summarize_vars) # Print non-trainable variables. non_trainable_variables = list( set(tf.global_variables()) - set(variables)) log_variable_sizes(non_trainable_variables, tag="Non-trainable variables", verbose=hparams.summarize_vars) if hparams.summarize_vars: summarize_variables(variables) # Summarize non-trainable variables as well summarize_variables(non_trainable_variables, tag="Non-trainable variables") diet_vars = [ v for v in tf.global_variables() if v.dtype == dtypes.float16_ref ] log_variable_sizes( diet_vars, "Diet Variables", verbose=hparams.summarize_vars) opt = ConditionalOptimizer(hparams.optimizer, learning_rate, hparams, use_tpu) if use_tpu: opt = tf.contrib.tpu.CrossShardOptimizer(opt) opt_summaries = [] if common_layers.should_generate_summaries(): tf.summary.scalar("learning_rate", learning_rate) opt_summaries.append("loss") if hparams.summarize_grads: tf.logging.info("Summarizing gradients") opt_summaries.extend( ["gradients", "gradient_norm", "global_gradient_norm"]) if hparams.clip_grad_norm: tf.logging.info("Clipping gradients, norm: %0.5f", hparams.clip_grad_norm) if hparams.grad_noise_scale: tf.logging.info("Adding noise to gradients, noise scale: %0.5f", hparams.grad_noise_scale) train_op = tf.contrib.layers.optimize_loss( name="training", loss=loss, global_step=tf.train.get_or_create_global_step(), learning_rate=learning_rate, clip_gradients=hparams.clip_grad_norm or None, gradient_noise_scale=hparams.grad_noise_scale or None, optimizer=opt, summaries=opt_summaries, colocate_gradients_with_ops=True, variables=variables) return train_op
[ "def", "optimize", "(", "loss", ",", "learning_rate", ",", "hparams", ",", "use_tpu", "=", "False", ",", "variables", "=", "None", ")", ":", "loss", "=", "weight_decay_and_noise", "(", "loss", ",", "hparams", ",", "learning_rate", ")", "loss", "=", "tf", ".", "identity", "(", "loss", ",", "name", "=", "\"total_loss\"", ")", "if", "variables", "is", "None", ":", "variables", "=", "tf", ".", "trainable_variables", "(", ")", "# Print trainable variables.", "log_variable_sizes", "(", "variables", ",", "verbose", "=", "hparams", ".", "summarize_vars", ")", "# Print non-trainable variables.", "non_trainable_variables", "=", "list", "(", "set", "(", "tf", ".", "global_variables", "(", ")", ")", "-", "set", "(", "variables", ")", ")", "log_variable_sizes", "(", "non_trainable_variables", ",", "tag", "=", "\"Non-trainable variables\"", ",", "verbose", "=", "hparams", ".", "summarize_vars", ")", "if", "hparams", ".", "summarize_vars", ":", "summarize_variables", "(", "variables", ")", "# Summarize non-trainable variables as well", "summarize_variables", "(", "non_trainable_variables", ",", "tag", "=", "\"Non-trainable variables\"", ")", "diet_vars", "=", "[", "v", "for", "v", "in", "tf", ".", "global_variables", "(", ")", "if", "v", ".", "dtype", "==", "dtypes", ".", "float16_ref", "]", "log_variable_sizes", "(", "diet_vars", ",", "\"Diet Variables\"", ",", "verbose", "=", "hparams", ".", "summarize_vars", ")", "opt", "=", "ConditionalOptimizer", "(", "hparams", ".", "optimizer", ",", "learning_rate", ",", "hparams", ",", "use_tpu", ")", "if", "use_tpu", ":", "opt", "=", "tf", ".", "contrib", ".", "tpu", ".", "CrossShardOptimizer", "(", "opt", ")", "opt_summaries", "=", "[", "]", "if", "common_layers", ".", "should_generate_summaries", "(", ")", ":", "tf", ".", "summary", ".", "scalar", "(", "\"learning_rate\"", ",", "learning_rate", ")", "opt_summaries", ".", "append", "(", "\"loss\"", ")", "if", "hparams", ".", "summarize_grads", ":", "tf", ".", "logging", ".", "info", "(", "\"Summarizing gradients\"", ")", "opt_summaries", ".", "extend", "(", "[", "\"gradients\"", ",", "\"gradient_norm\"", ",", "\"global_gradient_norm\"", "]", ")", "if", "hparams", ".", "clip_grad_norm", ":", "tf", ".", "logging", ".", "info", "(", "\"Clipping gradients, norm: %0.5f\"", ",", "hparams", ".", "clip_grad_norm", ")", "if", "hparams", ".", "grad_noise_scale", ":", "tf", ".", "logging", ".", "info", "(", "\"Adding noise to gradients, noise scale: %0.5f\"", ",", "hparams", ".", "grad_noise_scale", ")", "train_op", "=", "tf", ".", "contrib", ".", "layers", ".", "optimize_loss", "(", "name", "=", "\"training\"", ",", "loss", "=", "loss", ",", "global_step", "=", "tf", ".", "train", ".", "get_or_create_global_step", "(", ")", ",", "learning_rate", "=", "learning_rate", ",", "clip_gradients", "=", "hparams", ".", "clip_grad_norm", "or", "None", ",", "gradient_noise_scale", "=", "hparams", ".", "grad_noise_scale", "or", "None", ",", "optimizer", "=", "opt", ",", "summaries", "=", "opt_summaries", ",", "colocate_gradients_with_ops", "=", "True", ",", "variables", "=", "variables", ")", "return", "train_op" ]
Minimize loss.
[ "Minimize", "loss", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L43-L94
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
weight_decay_and_noise
def weight_decay_and_noise(loss, hparams, learning_rate, var_list=None): """Apply weight decay and weight noise.""" if var_list is None: var_list = tf.trainable_variables() decay_vars = [v for v in var_list] noise_vars = [v for v in var_list if "/body/" in v.name] weight_decay_loss = weight_decay(hparams.weight_decay, decay_vars) if hparams.weight_decay and common_layers.should_generate_summaries(): tf.summary.scalar("losses/weight_decay", weight_decay_loss) weight_noise_ops = weight_noise(hparams.weight_noise, learning_rate, noise_vars) with tf.control_dependencies(weight_noise_ops): loss = tf.identity(loss) loss += weight_decay_loss return loss
python
def weight_decay_and_noise(loss, hparams, learning_rate, var_list=None): """Apply weight decay and weight noise.""" if var_list is None: var_list = tf.trainable_variables() decay_vars = [v for v in var_list] noise_vars = [v for v in var_list if "/body/" in v.name] weight_decay_loss = weight_decay(hparams.weight_decay, decay_vars) if hparams.weight_decay and common_layers.should_generate_summaries(): tf.summary.scalar("losses/weight_decay", weight_decay_loss) weight_noise_ops = weight_noise(hparams.weight_noise, learning_rate, noise_vars) with tf.control_dependencies(weight_noise_ops): loss = tf.identity(loss) loss += weight_decay_loss return loss
[ "def", "weight_decay_and_noise", "(", "loss", ",", "hparams", ",", "learning_rate", ",", "var_list", "=", "None", ")", ":", "if", "var_list", "is", "None", ":", "var_list", "=", "tf", ".", "trainable_variables", "(", ")", "decay_vars", "=", "[", "v", "for", "v", "in", "var_list", "]", "noise_vars", "=", "[", "v", "for", "v", "in", "var_list", "if", "\"/body/\"", "in", "v", ".", "name", "]", "weight_decay_loss", "=", "weight_decay", "(", "hparams", ".", "weight_decay", ",", "decay_vars", ")", "if", "hparams", ".", "weight_decay", "and", "common_layers", ".", "should_generate_summaries", "(", ")", ":", "tf", ".", "summary", ".", "scalar", "(", "\"losses/weight_decay\"", ",", "weight_decay_loss", ")", "weight_noise_ops", "=", "weight_noise", "(", "hparams", ".", "weight_noise", ",", "learning_rate", ",", "noise_vars", ")", "with", "tf", ".", "control_dependencies", "(", "weight_noise_ops", ")", ":", "loss", "=", "tf", ".", "identity", "(", "loss", ")", "loss", "+=", "weight_decay_loss", "return", "loss" ]
Apply weight decay and weight noise.
[ "Apply", "weight", "decay", "and", "weight", "noise", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L238-L256
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
weight_noise
def weight_noise(noise_rate, learning_rate, var_list): """Apply weight noise to vars in var_list.""" if not noise_rate: return [tf.no_op()] tf.logging.info("Applying weight noise scaled by learning rate, " "noise_rate: %0.5f", noise_rate) noise_ops = [] for v in var_list: with tf.device(v.device): # pylint: disable=protected-access scale = noise_rate * learning_rate * 0.001 if common_layers.should_generate_summaries(): tf.summary.scalar("weight_noise_scale", scale) noise = tf.truncated_normal(v.shape) * scale noise_op = v.assign_add(noise) noise_ops.append(noise_op) return noise_ops
python
def weight_noise(noise_rate, learning_rate, var_list): """Apply weight noise to vars in var_list.""" if not noise_rate: return [tf.no_op()] tf.logging.info("Applying weight noise scaled by learning rate, " "noise_rate: %0.5f", noise_rate) noise_ops = [] for v in var_list: with tf.device(v.device): # pylint: disable=protected-access scale = noise_rate * learning_rate * 0.001 if common_layers.should_generate_summaries(): tf.summary.scalar("weight_noise_scale", scale) noise = tf.truncated_normal(v.shape) * scale noise_op = v.assign_add(noise) noise_ops.append(noise_op) return noise_ops
[ "def", "weight_noise", "(", "noise_rate", ",", "learning_rate", ",", "var_list", ")", ":", "if", "not", "noise_rate", ":", "return", "[", "tf", ".", "no_op", "(", ")", "]", "tf", ".", "logging", ".", "info", "(", "\"Applying weight noise scaled by learning rate, \"", "\"noise_rate: %0.5f\"", ",", "noise_rate", ")", "noise_ops", "=", "[", "]", "for", "v", "in", "var_list", ":", "with", "tf", ".", "device", "(", "v", ".", "device", ")", ":", "# pylint: disable=protected-access", "scale", "=", "noise_rate", "*", "learning_rate", "*", "0.001", "if", "common_layers", ".", "should_generate_summaries", "(", ")", ":", "tf", ".", "summary", ".", "scalar", "(", "\"weight_noise_scale\"", ",", "scale", ")", "noise", "=", "tf", ".", "truncated_normal", "(", "v", ".", "shape", ")", "*", "scale", "noise_op", "=", "v", ".", "assign_add", "(", "noise", ")", "noise_ops", ".", "append", "(", "noise_op", ")", "return", "noise_ops" ]
Apply weight noise to vars in var_list.
[ "Apply", "weight", "noise", "to", "vars", "in", "var_list", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L259-L278
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
weight_decay
def weight_decay(decay_rate, var_list, skip_biases=True): """Apply weight decay to vars in var_list.""" if not decay_rate: return 0. tf.logging.info("Applying weight decay, decay_rate: %0.5f", decay_rate) weight_decays = [] for v in var_list: # Weight decay. # This is a heuristic way to detect biases that works for main tf.layers. is_bias = len(v.shape.as_list()) == 1 and v.name.endswith("bias:0") if not (skip_biases and is_bias): with tf.device(v.device): v_loss = tf.nn.l2_loss(v) weight_decays.append(v_loss) return tf.add_n(weight_decays) * decay_rate
python
def weight_decay(decay_rate, var_list, skip_biases=True): """Apply weight decay to vars in var_list.""" if not decay_rate: return 0. tf.logging.info("Applying weight decay, decay_rate: %0.5f", decay_rate) weight_decays = [] for v in var_list: # Weight decay. # This is a heuristic way to detect biases that works for main tf.layers. is_bias = len(v.shape.as_list()) == 1 and v.name.endswith("bias:0") if not (skip_biases and is_bias): with tf.device(v.device): v_loss = tf.nn.l2_loss(v) weight_decays.append(v_loss) return tf.add_n(weight_decays) * decay_rate
[ "def", "weight_decay", "(", "decay_rate", ",", "var_list", ",", "skip_biases", "=", "True", ")", ":", "if", "not", "decay_rate", ":", "return", "0.", "tf", ".", "logging", ".", "info", "(", "\"Applying weight decay, decay_rate: %0.5f\"", ",", "decay_rate", ")", "weight_decays", "=", "[", "]", "for", "v", "in", "var_list", ":", "# Weight decay.", "# This is a heuristic way to detect biases that works for main tf.layers.", "is_bias", "=", "len", "(", "v", ".", "shape", ".", "as_list", "(", ")", ")", "==", "1", "and", "v", ".", "name", ".", "endswith", "(", "\"bias:0\"", ")", "if", "not", "(", "skip_biases", "and", "is_bias", ")", ":", "with", "tf", ".", "device", "(", "v", ".", "device", ")", ":", "v_loss", "=", "tf", ".", "nn", ".", "l2_loss", "(", "v", ")", "weight_decays", ".", "append", "(", "v_loss", ")", "return", "tf", ".", "add_n", "(", "weight_decays", ")", "*", "decay_rate" ]
Apply weight decay to vars in var_list.
[ "Apply", "weight", "decay", "to", "vars", "in", "var_list", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L281-L298
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
log_variable_sizes
def log_variable_sizes(var_list=None, tag=None, verbose=False): """Log the sizes and shapes of variables, and the total size. Args: var_list: a list of variables; defaults to trainable_variables tag: a string; defaults to "Trainable Variables" verbose: bool, if True, log every weight; otherwise, log total size only. """ if var_list is None: var_list = tf.trainable_variables() if tag is None: tag = "Trainable Variables" if not var_list: return name_to_var = {v.name: v for v in var_list} total_size = 0 for v_name in sorted(list(name_to_var)): v = name_to_var[v_name] v_size = int(np.prod(np.array(v.shape.as_list()))) if verbose: tf.logging.info("Weight %s\tshape %s\tsize %d", v.name[:-2].ljust(80), str(v.shape).ljust(20), v_size) total_size += v_size tf.logging.info("%s Total size: %d", tag, total_size)
python
def log_variable_sizes(var_list=None, tag=None, verbose=False): """Log the sizes and shapes of variables, and the total size. Args: var_list: a list of variables; defaults to trainable_variables tag: a string; defaults to "Trainable Variables" verbose: bool, if True, log every weight; otherwise, log total size only. """ if var_list is None: var_list = tf.trainable_variables() if tag is None: tag = "Trainable Variables" if not var_list: return name_to_var = {v.name: v for v in var_list} total_size = 0 for v_name in sorted(list(name_to_var)): v = name_to_var[v_name] v_size = int(np.prod(np.array(v.shape.as_list()))) if verbose: tf.logging.info("Weight %s\tshape %s\tsize %d", v.name[:-2].ljust(80), str(v.shape).ljust(20), v_size) total_size += v_size tf.logging.info("%s Total size: %d", tag, total_size)
[ "def", "log_variable_sizes", "(", "var_list", "=", "None", ",", "tag", "=", "None", ",", "verbose", "=", "False", ")", ":", "if", "var_list", "is", "None", ":", "var_list", "=", "tf", ".", "trainable_variables", "(", ")", "if", "tag", "is", "None", ":", "tag", "=", "\"Trainable Variables\"", "if", "not", "var_list", ":", "return", "name_to_var", "=", "{", "v", ".", "name", ":", "v", "for", "v", "in", "var_list", "}", "total_size", "=", "0", "for", "v_name", "in", "sorted", "(", "list", "(", "name_to_var", ")", ")", ":", "v", "=", "name_to_var", "[", "v_name", "]", "v_size", "=", "int", "(", "np", ".", "prod", "(", "np", ".", "array", "(", "v", ".", "shape", ".", "as_list", "(", ")", ")", ")", ")", "if", "verbose", ":", "tf", ".", "logging", ".", "info", "(", "\"Weight %s\\tshape %s\\tsize %d\"", ",", "v", ".", "name", "[", ":", "-", "2", "]", ".", "ljust", "(", "80", ")", ",", "str", "(", "v", ".", "shape", ")", ".", "ljust", "(", "20", ")", ",", "v_size", ")", "total_size", "+=", "v_size", "tf", ".", "logging", ".", "info", "(", "\"%s Total size: %d\"", ",", "tag", ",", "total_size", ")" ]
Log the sizes and shapes of variables, and the total size. Args: var_list: a list of variables; defaults to trainable_variables tag: a string; defaults to "Trainable Variables" verbose: bool, if True, log every weight; otherwise, log total size only.
[ "Log", "the", "sizes", "and", "shapes", "of", "variables", "and", "the", "total", "size", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L301-L327
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
summarize_variables
def summarize_variables(var_list=None, tag=None): """Summarize the variables. Args: var_list: a list of variables; defaults to trainable_variables. tag: name scope of the summary; defaults to training_variables/. """ if var_list is None: var_list = tf.trainable_variables() if tag is None: tag = "training_variables/" name_to_var = {v.name: v for v in var_list} for v_name in list(name_to_var): v = name_to_var[v_name] tf.summary.histogram(tag + v_name, v)
python
def summarize_variables(var_list=None, tag=None): """Summarize the variables. Args: var_list: a list of variables; defaults to trainable_variables. tag: name scope of the summary; defaults to training_variables/. """ if var_list is None: var_list = tf.trainable_variables() if tag is None: tag = "training_variables/" name_to_var = {v.name: v for v in var_list} for v_name in list(name_to_var): v = name_to_var[v_name] tf.summary.histogram(tag + v_name, v)
[ "def", "summarize_variables", "(", "var_list", "=", "None", ",", "tag", "=", "None", ")", ":", "if", "var_list", "is", "None", ":", "var_list", "=", "tf", ".", "trainable_variables", "(", ")", "if", "tag", "is", "None", ":", "tag", "=", "\"training_variables/\"", "name_to_var", "=", "{", "v", ".", "name", ":", "v", "for", "v", "in", "var_list", "}", "for", "v_name", "in", "list", "(", "name_to_var", ")", ":", "v", "=", "name_to_var", "[", "v_name", "]", "tf", ".", "summary", ".", "histogram", "(", "tag", "+", "v_name", ",", "v", ")" ]
Summarize the variables. Args: var_list: a list of variables; defaults to trainable_variables. tag: name scope of the summary; defaults to training_variables/.
[ "Summarize", "the", "variables", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L330-L345
train
tensorflow/tensor2tensor
tensor2tensor/utils/optimize.py
get_variable_initializer
def get_variable_initializer(hparams): """Get variable initializer from hparams.""" if not hparams.initializer: return None mlperf_log.transformer_print(key=mlperf_log.MODEL_HP_INITIALIZER_GAIN, value=hparams.initializer_gain, hparams=hparams) if not tf.executing_eagerly(): tf.logging.info("Using variable initializer: %s", hparams.initializer) if hparams.initializer == "orthogonal": return tf.orthogonal_initializer(gain=hparams.initializer_gain) elif hparams.initializer == "uniform": max_val = 0.1 * hparams.initializer_gain return tf.random_uniform_initializer(-max_val, max_val) elif hparams.initializer == "normal_unit_scaling": return tf.variance_scaling_initializer( hparams.initializer_gain, mode="fan_avg", distribution="normal") elif hparams.initializer == "uniform_unit_scaling": return tf.variance_scaling_initializer( hparams.initializer_gain, mode="fan_avg", distribution="uniform") elif hparams.initializer == "xavier": return tf.initializers.glorot_uniform() else: raise ValueError("Unrecognized initializer: %s" % hparams.initializer)
python
def get_variable_initializer(hparams): """Get variable initializer from hparams.""" if not hparams.initializer: return None mlperf_log.transformer_print(key=mlperf_log.MODEL_HP_INITIALIZER_GAIN, value=hparams.initializer_gain, hparams=hparams) if not tf.executing_eagerly(): tf.logging.info("Using variable initializer: %s", hparams.initializer) if hparams.initializer == "orthogonal": return tf.orthogonal_initializer(gain=hparams.initializer_gain) elif hparams.initializer == "uniform": max_val = 0.1 * hparams.initializer_gain return tf.random_uniform_initializer(-max_val, max_val) elif hparams.initializer == "normal_unit_scaling": return tf.variance_scaling_initializer( hparams.initializer_gain, mode="fan_avg", distribution="normal") elif hparams.initializer == "uniform_unit_scaling": return tf.variance_scaling_initializer( hparams.initializer_gain, mode="fan_avg", distribution="uniform") elif hparams.initializer == "xavier": return tf.initializers.glorot_uniform() else: raise ValueError("Unrecognized initializer: %s" % hparams.initializer)
[ "def", "get_variable_initializer", "(", "hparams", ")", ":", "if", "not", "hparams", ".", "initializer", ":", "return", "None", "mlperf_log", ".", "transformer_print", "(", "key", "=", "mlperf_log", ".", "MODEL_HP_INITIALIZER_GAIN", ",", "value", "=", "hparams", ".", "initializer_gain", ",", "hparams", "=", "hparams", ")", "if", "not", "tf", ".", "executing_eagerly", "(", ")", ":", "tf", ".", "logging", ".", "info", "(", "\"Using variable initializer: %s\"", ",", "hparams", ".", "initializer", ")", "if", "hparams", ".", "initializer", "==", "\"orthogonal\"", ":", "return", "tf", ".", "orthogonal_initializer", "(", "gain", "=", "hparams", ".", "initializer_gain", ")", "elif", "hparams", ".", "initializer", "==", "\"uniform\"", ":", "max_val", "=", "0.1", "*", "hparams", ".", "initializer_gain", "return", "tf", ".", "random_uniform_initializer", "(", "-", "max_val", ",", "max_val", ")", "elif", "hparams", ".", "initializer", "==", "\"normal_unit_scaling\"", ":", "return", "tf", ".", "variance_scaling_initializer", "(", "hparams", ".", "initializer_gain", ",", "mode", "=", "\"fan_avg\"", ",", "distribution", "=", "\"normal\"", ")", "elif", "hparams", ".", "initializer", "==", "\"uniform_unit_scaling\"", ":", "return", "tf", ".", "variance_scaling_initializer", "(", "hparams", ".", "initializer_gain", ",", "mode", "=", "\"fan_avg\"", ",", "distribution", "=", "\"uniform\"", ")", "elif", "hparams", ".", "initializer", "==", "\"xavier\"", ":", "return", "tf", ".", "initializers", ".", "glorot_uniform", "(", ")", "else", ":", "raise", "ValueError", "(", "\"Unrecognized initializer: %s\"", "%", "hparams", ".", "initializer", ")" ]
Get variable initializer from hparams.
[ "Get", "variable", "initializer", "from", "hparams", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/optimize.py#L348-L373
train
tensorflow/tensor2tensor
tensor2tensor/layers/vqa_layers.py
summarize_tensors
def summarize_tensors(tensor_dict, tag=None): """Summarize the tensors. Args: tensor_dict: a dictionary of tensors. tag: name scope of the summary; defaults to tensors/. """ if tag is None: tag = "tensors/" for t_name in list(tensor_dict): t = tensor_dict[t_name] tf.summary.histogram(tag + t_name, t)
python
def summarize_tensors(tensor_dict, tag=None): """Summarize the tensors. Args: tensor_dict: a dictionary of tensors. tag: name scope of the summary; defaults to tensors/. """ if tag is None: tag = "tensors/" for t_name in list(tensor_dict): t = tensor_dict[t_name] tf.summary.histogram(tag + t_name, t)
[ "def", "summarize_tensors", "(", "tensor_dict", ",", "tag", "=", "None", ")", ":", "if", "tag", "is", "None", ":", "tag", "=", "\"tensors/\"", "for", "t_name", "in", "list", "(", "tensor_dict", ")", ":", "t", "=", "tensor_dict", "[", "t_name", "]", "tf", ".", "summary", ".", "histogram", "(", "tag", "+", "t_name", ",", "t", ")" ]
Summarize the tensors. Args: tensor_dict: a dictionary of tensors. tag: name scope of the summary; defaults to tensors/.
[ "Summarize", "the", "tensors", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/vqa_layers.py#L33-L45
train
tensorflow/tensor2tensor
tensor2tensor/layers/vqa_layers.py
image_embedding
def image_embedding(images, model_fn=resnet_v1_152, trainable=True, is_training=True, weight_decay=0.0001, batch_norm_decay=0.997, batch_norm_epsilon=1e-5, batch_norm_scale=True, add_summaries=False, reuse=False): """Extract image features from pretrained resnet model.""" is_resnet_training = trainable and is_training batch_norm_params = { "is_training": is_resnet_training, "trainable": trainable, "decay": batch_norm_decay, "epsilon": batch_norm_epsilon, "scale": batch_norm_scale, } if trainable: weights_regularizer = tf.contrib.layers.l2_regularizer(weight_decay) else: weights_regularizer = None with tf.variable_scope(model_fn.__name__, [images], reuse=reuse) as scope: with slim.arg_scope( [slim.conv2d], weights_regularizer=weights_regularizer, trainable=trainable): with slim.arg_scope( [slim.conv2d], weights_initializer=slim.variance_scaling_initializer(), activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): with slim.arg_scope([slim.batch_norm], is_training=is_resnet_training, trainable=trainable): with slim.arg_scope([slim.max_pool2d], padding="SAME"): net, end_points = model_fn( images, num_classes=None, global_pool=False, is_training=is_resnet_training, reuse=reuse, scope=scope) if add_summaries: for v in end_points.values(): tf.contrib.layers.summaries.summarize_activation(v) return net
python
def image_embedding(images, model_fn=resnet_v1_152, trainable=True, is_training=True, weight_decay=0.0001, batch_norm_decay=0.997, batch_norm_epsilon=1e-5, batch_norm_scale=True, add_summaries=False, reuse=False): """Extract image features from pretrained resnet model.""" is_resnet_training = trainable and is_training batch_norm_params = { "is_training": is_resnet_training, "trainable": trainable, "decay": batch_norm_decay, "epsilon": batch_norm_epsilon, "scale": batch_norm_scale, } if trainable: weights_regularizer = tf.contrib.layers.l2_regularizer(weight_decay) else: weights_regularizer = None with tf.variable_scope(model_fn.__name__, [images], reuse=reuse) as scope: with slim.arg_scope( [slim.conv2d], weights_regularizer=weights_regularizer, trainable=trainable): with slim.arg_scope( [slim.conv2d], weights_initializer=slim.variance_scaling_initializer(), activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): with slim.arg_scope([slim.batch_norm], is_training=is_resnet_training, trainable=trainable): with slim.arg_scope([slim.max_pool2d], padding="SAME"): net, end_points = model_fn( images, num_classes=None, global_pool=False, is_training=is_resnet_training, reuse=reuse, scope=scope) if add_summaries: for v in end_points.values(): tf.contrib.layers.summaries.summarize_activation(v) return net
[ "def", "image_embedding", "(", "images", ",", "model_fn", "=", "resnet_v1_152", ",", "trainable", "=", "True", ",", "is_training", "=", "True", ",", "weight_decay", "=", "0.0001", ",", "batch_norm_decay", "=", "0.997", ",", "batch_norm_epsilon", "=", "1e-5", ",", "batch_norm_scale", "=", "True", ",", "add_summaries", "=", "False", ",", "reuse", "=", "False", ")", ":", "is_resnet_training", "=", "trainable", "and", "is_training", "batch_norm_params", "=", "{", "\"is_training\"", ":", "is_resnet_training", ",", "\"trainable\"", ":", "trainable", ",", "\"decay\"", ":", "batch_norm_decay", ",", "\"epsilon\"", ":", "batch_norm_epsilon", ",", "\"scale\"", ":", "batch_norm_scale", ",", "}", "if", "trainable", ":", "weights_regularizer", "=", "tf", ".", "contrib", ".", "layers", ".", "l2_regularizer", "(", "weight_decay", ")", "else", ":", "weights_regularizer", "=", "None", "with", "tf", ".", "variable_scope", "(", "model_fn", ".", "__name__", ",", "[", "images", "]", ",", "reuse", "=", "reuse", ")", "as", "scope", ":", "with", "slim", ".", "arg_scope", "(", "[", "slim", ".", "conv2d", "]", ",", "weights_regularizer", "=", "weights_regularizer", ",", "trainable", "=", "trainable", ")", ":", "with", "slim", ".", "arg_scope", "(", "[", "slim", ".", "conv2d", "]", ",", "weights_initializer", "=", "slim", ".", "variance_scaling_initializer", "(", ")", ",", "activation_fn", "=", "tf", ".", "nn", ".", "relu", ",", "normalizer_fn", "=", "slim", ".", "batch_norm", ",", "normalizer_params", "=", "batch_norm_params", ")", ":", "with", "slim", ".", "arg_scope", "(", "[", "slim", ".", "batch_norm", "]", ",", "is_training", "=", "is_resnet_training", ",", "trainable", "=", "trainable", ")", ":", "with", "slim", ".", "arg_scope", "(", "[", "slim", ".", "max_pool2d", "]", ",", "padding", "=", "\"SAME\"", ")", ":", "net", ",", "end_points", "=", "model_fn", "(", "images", ",", "num_classes", "=", "None", ",", "global_pool", "=", "False", ",", "is_training", "=", "is_resnet_training", ",", "reuse", "=", "reuse", ",", "scope", "=", "scope", ")", "if", "add_summaries", ":", "for", "v", "in", "end_points", ".", "values", "(", ")", ":", "tf", ".", "contrib", ".", "layers", ".", "summaries", ".", "summarize_activation", "(", "v", ")", "return", "net" ]
Extract image features from pretrained resnet model.
[ "Extract", "image", "features", "from", "pretrained", "resnet", "model", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/vqa_layers.py#L48-L99
train
tensorflow/tensor2tensor
tensor2tensor/layers/vqa_layers.py
multihead_attention
def multihead_attention(query_antecedent, memory_antecedent, bias, total_key_depth, total_value_depth, output_depth, num_heads, dropout_rate, shared_rel=False, max_relative_position=None, image_shapes=None, attention_type="dot_product", block_length=128, block_width=128, q_filter_width=1, kv_filter_width=1, q_padding="VALID", kv_padding="VALID", cache=None, gap_size=0, num_memory_blocks=2, name="multihead_attention", save_weights_to=None, make_image_summary=True, dropout_broadcast_dims=None, max_length=None, vars_3d=False, scale_dotproduct=True, **kwargs): """Multihead scaled-dot-product attention with input/output transformations. Args: query_antecedent: a Tensor with shape [batch, length_q, channels] memory_antecedent: a Tensor with shape [batch, length_m, channels] or None bias: bias Tensor (see attention_bias()) total_key_depth: an integer total_value_depth: an integer output_depth: an integer num_heads: an integer dividing total_key_depth and total_value_depth dropout_rate: a floating point number shared_rel: boolean to share relative embeddings max_relative_position: Maximum distance between inputs to generate unique relation embeddings for. Only relevant when using "dot_product_relative" attention. image_shapes: optional tuple of integer scalars. see comments for attention_image_summary() attention_type: a string, either "dot_product", "dot_product_relative", "local_mask_right", "local_unmasked", "masked_dilated_1d", "unmasked_dilated_1d", graph, or any attention function with the signature (query, key, value, **kwargs) block_length: an integer - relevant for "local_mask_right" block_width: an integer - relevant for "local_unmasked" q_filter_width: An integer specifying how wide you want the query to be. kv_filter_width: An integer specifying how wide you want the keys and values to be. q_padding: One of "VALID", "SAME" or "LEFT". Default is VALID: No padding. kv_padding: One of "VALID", "SAME" or "LEFT". Default is "VALID": no padding. cache: dict containing Tensors which are the results of previous attentions, used for fast decoding. Expects the dict to contrain two keys ('k' and 'v'), for the initial call the values for these keys should be empty Tensors of the appropriate shape. 'k' [batch_size, 0, key_channels] 'v' [batch_size, 0, value_channels] gap_size: Integer option for dilated attention to indicate spacing between memory blocks. num_memory_blocks: Integer option to indicate how many memory blocks to look at. name: an optional string. save_weights_to: an optional dictionary to capture attention weights for vizualization; the weights tensor will be appended there under a string key created from the variable scope (including name). make_image_summary: Whether to make an attention image summary. dropout_broadcast_dims: an optional list of integers less than 4 specifying in which dimensions to broadcast the dropout decisions. saves memory. max_length: an integer - needed by relative attention vars_3d: use 3-dimensional variables for input/output transformations scale_dotproduct: whether to normalize the attention product. **kwargs (dict): Parameters for the attention function Caching: WARNING: For decoder self-attention, i.e. when memory_antecedent == None, the caching assumes that the bias contains future masking. The caching works by saving all the previous key and value values so that you are able to send just the last query location to this attention function. I.e. if the cache dict is provided it assumes the query is of the shape [batch_size, 1, hidden_dim] rather than the full memory. Returns: The result of the attention transformation. The output shape is [batch_size, length_q, hidden_dim] unless the cache dict is provided in which case only the last memory position is calculated and the output shape is [batch_size, 1, hidden_dim] Optionally returns an additional loss parameters (ex: load balance loss for the experts) returned by the attention_type function. Raises: ValueError: if the key depth or value depth are not divisible by the number of attention heads. """ if total_key_depth % num_heads != 0: raise ValueError("Key depth (%d) must be divisible by the number of " "attention heads (%d)." % (total_key_depth, num_heads)) if total_value_depth % num_heads != 0: raise ValueError("Value depth (%d) must be divisible by the number of " "attention heads (%d)." % (total_value_depth, num_heads)) vars_3d_num_heads = num_heads if vars_3d else 0 with tf.variable_scope(name, default_name="multihead_attention", values=[query_antecedent, memory_antecedent]): if cache is None or memory_antecedent is None: q, k, v = common_attention.compute_qkv( query_antecedent, memory_antecedent, total_key_depth, total_value_depth, q_filter_width, kv_filter_width, q_padding, kv_padding, vars_3d_num_heads=vars_3d_num_heads) if cache is not None: if attention_type != "dot_product": # TODO(petershaw): Support caching when using relative position # representations, i.e. "dot_product_relative" attention. raise NotImplementedError( "Caching is not guaranteed to work with attention types other than" " dot_product.") if bias is None: raise ValueError("Bias required for caching. See function docstring " "for details.") if memory_antecedent is not None: # Encoder-Decoder Attention Cache q = common_attention.compute_attention_component( query_antecedent, total_key_depth, q_filter_width, q_padding, "q", vars_3d_num_heads=vars_3d_num_heads) k = cache["k_encdec"] v = cache["v_encdec"] else: k = common_attention.split_heads(k, num_heads) v = common_attention.split_heads(v, num_heads) decode_loop_step = kwargs.get("decode_loop_step") if decode_loop_step is None: k = cache["k"] = tf.concat([cache["k"], k], axis=2) v = cache["v"] = tf.concat([cache["v"], v], axis=2) else: # Inplace update is required for inference on TPU. # Inplace_ops only supports inplace_update on the first dimension. # The performance of current implementation is better than updating # the tensor by adding the result of matmul(one_hot, # update_in_current_step) tmp_k = tf.transpose(cache["k"], perm=[2, 0, 1, 3]) tmp_k = inplace_ops.alias_inplace_update( tmp_k, decode_loop_step, tf.squeeze(k, axis=2)) k = cache["k"] = tf.transpose(tmp_k, perm=[1, 2, 0, 3]) tmp_v = tf.transpose(cache["v"], perm=[2, 0, 1, 3]) tmp_v = inplace_ops.alias_inplace_update( tmp_v, decode_loop_step, tf.squeeze(v, axis=2)) v = cache["v"] = tf.transpose(tmp_v, perm=[1, 2, 0, 3]) q = common_attention.split_heads(q, num_heads) if cache is None: k = common_attention.split_heads(k, num_heads) v = common_attention.split_heads(v, num_heads) key_depth_per_head = total_key_depth // num_heads if not vars_3d: if scale_dotproduct: q *= key_depth_per_head**-0.5 additional_returned_value = None if callable(attention_type): # Generic way to extend multihead_attention x = attention_type(q, k, v, **kwargs) if isinstance(x, tuple): x, additional_returned_value = x # Unpack elif attention_type == "dot_product": x = common_attention.dot_product_attention( q, k, v, bias, dropout_rate, image_shapes, save_weights_to=save_weights_to, make_image_summary=make_image_summary, dropout_broadcast_dims=dropout_broadcast_dims) elif attention_type == "dot_product_relative": x = common_attention.dot_product_attention_relative( q, k, v, bias, max_relative_position, dropout_rate, image_shapes, make_image_summary=make_image_summary) elif attention_type == "dot_product_relative_v2": x = common_attention.dot_product_self_attention_relative_v2( q, k, v, bias, max_length, dropout_rate, image_shapes, make_image_summary=make_image_summary, dropout_broadcast_dims=dropout_broadcast_dims) elif attention_type == "local_within_block_mask_right": x = common_attention.masked_within_block_local_attention_1d( q, k, v, block_length=block_length) elif attention_type == "rel_local_mask_right": x = common_attention.masked_rel_local_attention_1d( q, k, v, block_length=block_length, make_image_summary=make_image_summary, dropout_rate=dropout_rate, share_rel_embed=shared_rel) elif attention_type == "local_mask_right": x = common_attention.masked_local_attention_1d( q, k, v, block_length=block_length, make_image_summary=make_image_summary) elif attention_type == "local_unmasked": x = common_attention.local_attention_1d( q, k, v, block_length=block_length, filter_width=block_width) elif attention_type == "masked_dilated_1d": x = common_attention.masked_dilated_self_attention_1d( q, k, v, block_length, block_width, gap_size, num_memory_blocks) else: assert attention_type == "unmasked_dilated_1d" x = common_attention.dilated_self_attention_1d( q, k, v, block_length, block_width, gap_size, num_memory_blocks) x = common_attention.combine_heads(x) # Set last dim specifically. x.set_shape(x.shape.as_list()[:-1] + [total_value_depth]) if vars_3d: o_var = tf.get_variable( "o", [num_heads, total_value_depth // num_heads, output_depth]) o_var = tf.cast(o_var, x.dtype) o_var = tf.reshape(o_var, [total_value_depth, output_depth]) x = tf.tensordot(x, o_var, axes=1) else: x = common_layers.dense( x, output_depth, use_bias=False, name="output_transform") if additional_returned_value is not None: return x, additional_returned_value return x
python
def multihead_attention(query_antecedent, memory_antecedent, bias, total_key_depth, total_value_depth, output_depth, num_heads, dropout_rate, shared_rel=False, max_relative_position=None, image_shapes=None, attention_type="dot_product", block_length=128, block_width=128, q_filter_width=1, kv_filter_width=1, q_padding="VALID", kv_padding="VALID", cache=None, gap_size=0, num_memory_blocks=2, name="multihead_attention", save_weights_to=None, make_image_summary=True, dropout_broadcast_dims=None, max_length=None, vars_3d=False, scale_dotproduct=True, **kwargs): """Multihead scaled-dot-product attention with input/output transformations. Args: query_antecedent: a Tensor with shape [batch, length_q, channels] memory_antecedent: a Tensor with shape [batch, length_m, channels] or None bias: bias Tensor (see attention_bias()) total_key_depth: an integer total_value_depth: an integer output_depth: an integer num_heads: an integer dividing total_key_depth and total_value_depth dropout_rate: a floating point number shared_rel: boolean to share relative embeddings max_relative_position: Maximum distance between inputs to generate unique relation embeddings for. Only relevant when using "dot_product_relative" attention. image_shapes: optional tuple of integer scalars. see comments for attention_image_summary() attention_type: a string, either "dot_product", "dot_product_relative", "local_mask_right", "local_unmasked", "masked_dilated_1d", "unmasked_dilated_1d", graph, or any attention function with the signature (query, key, value, **kwargs) block_length: an integer - relevant for "local_mask_right" block_width: an integer - relevant for "local_unmasked" q_filter_width: An integer specifying how wide you want the query to be. kv_filter_width: An integer specifying how wide you want the keys and values to be. q_padding: One of "VALID", "SAME" or "LEFT". Default is VALID: No padding. kv_padding: One of "VALID", "SAME" or "LEFT". Default is "VALID": no padding. cache: dict containing Tensors which are the results of previous attentions, used for fast decoding. Expects the dict to contrain two keys ('k' and 'v'), for the initial call the values for these keys should be empty Tensors of the appropriate shape. 'k' [batch_size, 0, key_channels] 'v' [batch_size, 0, value_channels] gap_size: Integer option for dilated attention to indicate spacing between memory blocks. num_memory_blocks: Integer option to indicate how many memory blocks to look at. name: an optional string. save_weights_to: an optional dictionary to capture attention weights for vizualization; the weights tensor will be appended there under a string key created from the variable scope (including name). make_image_summary: Whether to make an attention image summary. dropout_broadcast_dims: an optional list of integers less than 4 specifying in which dimensions to broadcast the dropout decisions. saves memory. max_length: an integer - needed by relative attention vars_3d: use 3-dimensional variables for input/output transformations scale_dotproduct: whether to normalize the attention product. **kwargs (dict): Parameters for the attention function Caching: WARNING: For decoder self-attention, i.e. when memory_antecedent == None, the caching assumes that the bias contains future masking. The caching works by saving all the previous key and value values so that you are able to send just the last query location to this attention function. I.e. if the cache dict is provided it assumes the query is of the shape [batch_size, 1, hidden_dim] rather than the full memory. Returns: The result of the attention transformation. The output shape is [batch_size, length_q, hidden_dim] unless the cache dict is provided in which case only the last memory position is calculated and the output shape is [batch_size, 1, hidden_dim] Optionally returns an additional loss parameters (ex: load balance loss for the experts) returned by the attention_type function. Raises: ValueError: if the key depth or value depth are not divisible by the number of attention heads. """ if total_key_depth % num_heads != 0: raise ValueError("Key depth (%d) must be divisible by the number of " "attention heads (%d)." % (total_key_depth, num_heads)) if total_value_depth % num_heads != 0: raise ValueError("Value depth (%d) must be divisible by the number of " "attention heads (%d)." % (total_value_depth, num_heads)) vars_3d_num_heads = num_heads if vars_3d else 0 with tf.variable_scope(name, default_name="multihead_attention", values=[query_antecedent, memory_antecedent]): if cache is None or memory_antecedent is None: q, k, v = common_attention.compute_qkv( query_antecedent, memory_antecedent, total_key_depth, total_value_depth, q_filter_width, kv_filter_width, q_padding, kv_padding, vars_3d_num_heads=vars_3d_num_heads) if cache is not None: if attention_type != "dot_product": # TODO(petershaw): Support caching when using relative position # representations, i.e. "dot_product_relative" attention. raise NotImplementedError( "Caching is not guaranteed to work with attention types other than" " dot_product.") if bias is None: raise ValueError("Bias required for caching. See function docstring " "for details.") if memory_antecedent is not None: # Encoder-Decoder Attention Cache q = common_attention.compute_attention_component( query_antecedent, total_key_depth, q_filter_width, q_padding, "q", vars_3d_num_heads=vars_3d_num_heads) k = cache["k_encdec"] v = cache["v_encdec"] else: k = common_attention.split_heads(k, num_heads) v = common_attention.split_heads(v, num_heads) decode_loop_step = kwargs.get("decode_loop_step") if decode_loop_step is None: k = cache["k"] = tf.concat([cache["k"], k], axis=2) v = cache["v"] = tf.concat([cache["v"], v], axis=2) else: # Inplace update is required for inference on TPU. # Inplace_ops only supports inplace_update on the first dimension. # The performance of current implementation is better than updating # the tensor by adding the result of matmul(one_hot, # update_in_current_step) tmp_k = tf.transpose(cache["k"], perm=[2, 0, 1, 3]) tmp_k = inplace_ops.alias_inplace_update( tmp_k, decode_loop_step, tf.squeeze(k, axis=2)) k = cache["k"] = tf.transpose(tmp_k, perm=[1, 2, 0, 3]) tmp_v = tf.transpose(cache["v"], perm=[2, 0, 1, 3]) tmp_v = inplace_ops.alias_inplace_update( tmp_v, decode_loop_step, tf.squeeze(v, axis=2)) v = cache["v"] = tf.transpose(tmp_v, perm=[1, 2, 0, 3]) q = common_attention.split_heads(q, num_heads) if cache is None: k = common_attention.split_heads(k, num_heads) v = common_attention.split_heads(v, num_heads) key_depth_per_head = total_key_depth // num_heads if not vars_3d: if scale_dotproduct: q *= key_depth_per_head**-0.5 additional_returned_value = None if callable(attention_type): # Generic way to extend multihead_attention x = attention_type(q, k, v, **kwargs) if isinstance(x, tuple): x, additional_returned_value = x # Unpack elif attention_type == "dot_product": x = common_attention.dot_product_attention( q, k, v, bias, dropout_rate, image_shapes, save_weights_to=save_weights_to, make_image_summary=make_image_summary, dropout_broadcast_dims=dropout_broadcast_dims) elif attention_type == "dot_product_relative": x = common_attention.dot_product_attention_relative( q, k, v, bias, max_relative_position, dropout_rate, image_shapes, make_image_summary=make_image_summary) elif attention_type == "dot_product_relative_v2": x = common_attention.dot_product_self_attention_relative_v2( q, k, v, bias, max_length, dropout_rate, image_shapes, make_image_summary=make_image_summary, dropout_broadcast_dims=dropout_broadcast_dims) elif attention_type == "local_within_block_mask_right": x = common_attention.masked_within_block_local_attention_1d( q, k, v, block_length=block_length) elif attention_type == "rel_local_mask_right": x = common_attention.masked_rel_local_attention_1d( q, k, v, block_length=block_length, make_image_summary=make_image_summary, dropout_rate=dropout_rate, share_rel_embed=shared_rel) elif attention_type == "local_mask_right": x = common_attention.masked_local_attention_1d( q, k, v, block_length=block_length, make_image_summary=make_image_summary) elif attention_type == "local_unmasked": x = common_attention.local_attention_1d( q, k, v, block_length=block_length, filter_width=block_width) elif attention_type == "masked_dilated_1d": x = common_attention.masked_dilated_self_attention_1d( q, k, v, block_length, block_width, gap_size, num_memory_blocks) else: assert attention_type == "unmasked_dilated_1d" x = common_attention.dilated_self_attention_1d( q, k, v, block_length, block_width, gap_size, num_memory_blocks) x = common_attention.combine_heads(x) # Set last dim specifically. x.set_shape(x.shape.as_list()[:-1] + [total_value_depth]) if vars_3d: o_var = tf.get_variable( "o", [num_heads, total_value_depth // num_heads, output_depth]) o_var = tf.cast(o_var, x.dtype) o_var = tf.reshape(o_var, [total_value_depth, output_depth]) x = tf.tensordot(x, o_var, axes=1) else: x = common_layers.dense( x, output_depth, use_bias=False, name="output_transform") if additional_returned_value is not None: return x, additional_returned_value return x
[ "def", "multihead_attention", "(", "query_antecedent", ",", "memory_antecedent", ",", "bias", ",", "total_key_depth", ",", "total_value_depth", ",", "output_depth", ",", "num_heads", ",", "dropout_rate", ",", "shared_rel", "=", "False", ",", "max_relative_position", "=", "None", ",", "image_shapes", "=", "None", ",", "attention_type", "=", "\"dot_product\"", ",", "block_length", "=", "128", ",", "block_width", "=", "128", ",", "q_filter_width", "=", "1", ",", "kv_filter_width", "=", "1", ",", "q_padding", "=", "\"VALID\"", ",", "kv_padding", "=", "\"VALID\"", ",", "cache", "=", "None", ",", "gap_size", "=", "0", ",", "num_memory_blocks", "=", "2", ",", "name", "=", "\"multihead_attention\"", ",", "save_weights_to", "=", "None", ",", "make_image_summary", "=", "True", ",", "dropout_broadcast_dims", "=", "None", ",", "max_length", "=", "None", ",", "vars_3d", "=", "False", ",", "scale_dotproduct", "=", "True", ",", "*", "*", "kwargs", ")", ":", "if", "total_key_depth", "%", "num_heads", "!=", "0", ":", "raise", "ValueError", "(", "\"Key depth (%d) must be divisible by the number of \"", "\"attention heads (%d).\"", "%", "(", "total_key_depth", ",", "num_heads", ")", ")", "if", "total_value_depth", "%", "num_heads", "!=", "0", ":", "raise", "ValueError", "(", "\"Value depth (%d) must be divisible by the number of \"", "\"attention heads (%d).\"", "%", "(", "total_value_depth", ",", "num_heads", ")", ")", "vars_3d_num_heads", "=", "num_heads", "if", "vars_3d", "else", "0", "with", "tf", ".", "variable_scope", "(", "name", ",", "default_name", "=", "\"multihead_attention\"", ",", "values", "=", "[", "query_antecedent", ",", "memory_antecedent", "]", ")", ":", "if", "cache", "is", "None", "or", "memory_antecedent", "is", "None", ":", "q", ",", "k", ",", "v", "=", "common_attention", ".", "compute_qkv", "(", "query_antecedent", ",", "memory_antecedent", ",", "total_key_depth", ",", "total_value_depth", ",", "q_filter_width", ",", "kv_filter_width", ",", "q_padding", ",", "kv_padding", ",", "vars_3d_num_heads", "=", "vars_3d_num_heads", ")", "if", "cache", "is", "not", "None", ":", "if", "attention_type", "!=", "\"dot_product\"", ":", "# TODO(petershaw): Support caching when using relative position", "# representations, i.e. \"dot_product_relative\" attention.", "raise", "NotImplementedError", "(", "\"Caching is not guaranteed to work with attention types other than\"", "\" dot_product.\"", ")", "if", "bias", "is", "None", ":", "raise", "ValueError", "(", "\"Bias required for caching. See function docstring \"", "\"for details.\"", ")", "if", "memory_antecedent", "is", "not", "None", ":", "# Encoder-Decoder Attention Cache", "q", "=", "common_attention", ".", "compute_attention_component", "(", "query_antecedent", ",", "total_key_depth", ",", "q_filter_width", ",", "q_padding", ",", "\"q\"", ",", "vars_3d_num_heads", "=", "vars_3d_num_heads", ")", "k", "=", "cache", "[", "\"k_encdec\"", "]", "v", "=", "cache", "[", "\"v_encdec\"", "]", "else", ":", "k", "=", "common_attention", ".", "split_heads", "(", "k", ",", "num_heads", ")", "v", "=", "common_attention", ".", "split_heads", "(", "v", ",", "num_heads", ")", "decode_loop_step", "=", "kwargs", ".", "get", "(", "\"decode_loop_step\"", ")", "if", "decode_loop_step", "is", "None", ":", "k", "=", "cache", "[", "\"k\"", "]", "=", "tf", ".", "concat", "(", "[", "cache", "[", "\"k\"", "]", ",", "k", "]", ",", "axis", "=", "2", ")", "v", "=", "cache", "[", "\"v\"", "]", "=", "tf", ".", "concat", "(", "[", "cache", "[", "\"v\"", "]", ",", "v", "]", ",", "axis", "=", "2", ")", "else", ":", "# Inplace update is required for inference on TPU.", "# Inplace_ops only supports inplace_update on the first dimension.", "# The performance of current implementation is better than updating", "# the tensor by adding the result of matmul(one_hot,", "# update_in_current_step)", "tmp_k", "=", "tf", ".", "transpose", "(", "cache", "[", "\"k\"", "]", ",", "perm", "=", "[", "2", ",", "0", ",", "1", ",", "3", "]", ")", "tmp_k", "=", "inplace_ops", ".", "alias_inplace_update", "(", "tmp_k", ",", "decode_loop_step", ",", "tf", ".", "squeeze", "(", "k", ",", "axis", "=", "2", ")", ")", "k", "=", "cache", "[", "\"k\"", "]", "=", "tf", ".", "transpose", "(", "tmp_k", ",", "perm", "=", "[", "1", ",", "2", ",", "0", ",", "3", "]", ")", "tmp_v", "=", "tf", ".", "transpose", "(", "cache", "[", "\"v\"", "]", ",", "perm", "=", "[", "2", ",", "0", ",", "1", ",", "3", "]", ")", "tmp_v", "=", "inplace_ops", ".", "alias_inplace_update", "(", "tmp_v", ",", "decode_loop_step", ",", "tf", ".", "squeeze", "(", "v", ",", "axis", "=", "2", ")", ")", "v", "=", "cache", "[", "\"v\"", "]", "=", "tf", ".", "transpose", "(", "tmp_v", ",", "perm", "=", "[", "1", ",", "2", ",", "0", ",", "3", "]", ")", "q", "=", "common_attention", ".", "split_heads", "(", "q", ",", "num_heads", ")", "if", "cache", "is", "None", ":", "k", "=", "common_attention", ".", "split_heads", "(", "k", ",", "num_heads", ")", "v", "=", "common_attention", ".", "split_heads", "(", "v", ",", "num_heads", ")", "key_depth_per_head", "=", "total_key_depth", "//", "num_heads", "if", "not", "vars_3d", ":", "if", "scale_dotproduct", ":", "q", "*=", "key_depth_per_head", "**", "-", "0.5", "additional_returned_value", "=", "None", "if", "callable", "(", "attention_type", ")", ":", "# Generic way to extend multihead_attention", "x", "=", "attention_type", "(", "q", ",", "k", ",", "v", ",", "*", "*", "kwargs", ")", "if", "isinstance", "(", "x", ",", "tuple", ")", ":", "x", ",", "additional_returned_value", "=", "x", "# Unpack", "elif", "attention_type", "==", "\"dot_product\"", ":", "x", "=", "common_attention", ".", "dot_product_attention", "(", "q", ",", "k", ",", "v", ",", "bias", ",", "dropout_rate", ",", "image_shapes", ",", "save_weights_to", "=", "save_weights_to", ",", "make_image_summary", "=", "make_image_summary", ",", "dropout_broadcast_dims", "=", "dropout_broadcast_dims", ")", "elif", "attention_type", "==", "\"dot_product_relative\"", ":", "x", "=", "common_attention", ".", "dot_product_attention_relative", "(", "q", ",", "k", ",", "v", ",", "bias", ",", "max_relative_position", ",", "dropout_rate", ",", "image_shapes", ",", "make_image_summary", "=", "make_image_summary", ")", "elif", "attention_type", "==", "\"dot_product_relative_v2\"", ":", "x", "=", "common_attention", ".", "dot_product_self_attention_relative_v2", "(", "q", ",", "k", ",", "v", ",", "bias", ",", "max_length", ",", "dropout_rate", ",", "image_shapes", ",", "make_image_summary", "=", "make_image_summary", ",", "dropout_broadcast_dims", "=", "dropout_broadcast_dims", ")", "elif", "attention_type", "==", "\"local_within_block_mask_right\"", ":", "x", "=", "common_attention", ".", "masked_within_block_local_attention_1d", "(", "q", ",", "k", ",", "v", ",", "block_length", "=", "block_length", ")", "elif", "attention_type", "==", "\"rel_local_mask_right\"", ":", "x", "=", "common_attention", ".", "masked_rel_local_attention_1d", "(", "q", ",", "k", ",", "v", ",", "block_length", "=", "block_length", ",", "make_image_summary", "=", "make_image_summary", ",", "dropout_rate", "=", "dropout_rate", ",", "share_rel_embed", "=", "shared_rel", ")", "elif", "attention_type", "==", "\"local_mask_right\"", ":", "x", "=", "common_attention", ".", "masked_local_attention_1d", "(", "q", ",", "k", ",", "v", ",", "block_length", "=", "block_length", ",", "make_image_summary", "=", "make_image_summary", ")", "elif", "attention_type", "==", "\"local_unmasked\"", ":", "x", "=", "common_attention", ".", "local_attention_1d", "(", "q", ",", "k", ",", "v", ",", "block_length", "=", "block_length", ",", "filter_width", "=", "block_width", ")", "elif", "attention_type", "==", "\"masked_dilated_1d\"", ":", "x", "=", "common_attention", ".", "masked_dilated_self_attention_1d", "(", "q", ",", "k", ",", "v", ",", "block_length", ",", "block_width", ",", "gap_size", ",", "num_memory_blocks", ")", "else", ":", "assert", "attention_type", "==", "\"unmasked_dilated_1d\"", "x", "=", "common_attention", ".", "dilated_self_attention_1d", "(", "q", ",", "k", ",", "v", ",", "block_length", ",", "block_width", ",", "gap_size", ",", "num_memory_blocks", ")", "x", "=", "common_attention", ".", "combine_heads", "(", "x", ")", "# Set last dim specifically.", "x", ".", "set_shape", "(", "x", ".", "shape", ".", "as_list", "(", ")", "[", ":", "-", "1", "]", "+", "[", "total_value_depth", "]", ")", "if", "vars_3d", ":", "o_var", "=", "tf", ".", "get_variable", "(", "\"o\"", ",", "[", "num_heads", ",", "total_value_depth", "//", "num_heads", ",", "output_depth", "]", ")", "o_var", "=", "tf", ".", "cast", "(", "o_var", ",", "x", ".", "dtype", ")", "o_var", "=", "tf", ".", "reshape", "(", "o_var", ",", "[", "total_value_depth", ",", "output_depth", "]", ")", "x", "=", "tf", ".", "tensordot", "(", "x", ",", "o_var", ",", "axes", "=", "1", ")", "else", ":", "x", "=", "common_layers", ".", "dense", "(", "x", ",", "output_depth", ",", "use_bias", "=", "False", ",", "name", "=", "\"output_transform\"", ")", "if", "additional_returned_value", "is", "not", "None", ":", "return", "x", ",", "additional_returned_value", "return", "x" ]
Multihead scaled-dot-product attention with input/output transformations. Args: query_antecedent: a Tensor with shape [batch, length_q, channels] memory_antecedent: a Tensor with shape [batch, length_m, channels] or None bias: bias Tensor (see attention_bias()) total_key_depth: an integer total_value_depth: an integer output_depth: an integer num_heads: an integer dividing total_key_depth and total_value_depth dropout_rate: a floating point number shared_rel: boolean to share relative embeddings max_relative_position: Maximum distance between inputs to generate unique relation embeddings for. Only relevant when using "dot_product_relative" attention. image_shapes: optional tuple of integer scalars. see comments for attention_image_summary() attention_type: a string, either "dot_product", "dot_product_relative", "local_mask_right", "local_unmasked", "masked_dilated_1d", "unmasked_dilated_1d", graph, or any attention function with the signature (query, key, value, **kwargs) block_length: an integer - relevant for "local_mask_right" block_width: an integer - relevant for "local_unmasked" q_filter_width: An integer specifying how wide you want the query to be. kv_filter_width: An integer specifying how wide you want the keys and values to be. q_padding: One of "VALID", "SAME" or "LEFT". Default is VALID: No padding. kv_padding: One of "VALID", "SAME" or "LEFT". Default is "VALID": no padding. cache: dict containing Tensors which are the results of previous attentions, used for fast decoding. Expects the dict to contrain two keys ('k' and 'v'), for the initial call the values for these keys should be empty Tensors of the appropriate shape. 'k' [batch_size, 0, key_channels] 'v' [batch_size, 0, value_channels] gap_size: Integer option for dilated attention to indicate spacing between memory blocks. num_memory_blocks: Integer option to indicate how many memory blocks to look at. name: an optional string. save_weights_to: an optional dictionary to capture attention weights for vizualization; the weights tensor will be appended there under a string key created from the variable scope (including name). make_image_summary: Whether to make an attention image summary. dropout_broadcast_dims: an optional list of integers less than 4 specifying in which dimensions to broadcast the dropout decisions. saves memory. max_length: an integer - needed by relative attention vars_3d: use 3-dimensional variables for input/output transformations scale_dotproduct: whether to normalize the attention product. **kwargs (dict): Parameters for the attention function Caching: WARNING: For decoder self-attention, i.e. when memory_antecedent == None, the caching assumes that the bias contains future masking. The caching works by saving all the previous key and value values so that you are able to send just the last query location to this attention function. I.e. if the cache dict is provided it assumes the query is of the shape [batch_size, 1, hidden_dim] rather than the full memory. Returns: The result of the attention transformation. The output shape is [batch_size, length_q, hidden_dim] unless the cache dict is provided in which case only the last memory position is calculated and the output shape is [batch_size, 1, hidden_dim] Optionally returns an additional loss parameters (ex: load balance loss for the experts) returned by the attention_type function. Raises: ValueError: if the key depth or value depth are not divisible by the number of attention heads.
[ "Multihead", "scaled", "-", "dot", "-", "product", "attention", "with", "input", "/", "output", "transformations", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/layers/vqa_layers.py#L102-L347
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/audio.py
_get_timit
def _get_timit(directory): """Extract TIMIT datasets to directory unless directory/timit exists.""" if os.path.exists(os.path.join(directory, "timit")): return assert FLAGS.timit_paths for path in FLAGS.timit_paths.split(","): with tf.gfile.GFile(path) as f: with tarfile.open(fileobj=f, mode="r:gz") as timit_compressed: timit_compressed.extractall(directory)
python
def _get_timit(directory): """Extract TIMIT datasets to directory unless directory/timit exists.""" if os.path.exists(os.path.join(directory, "timit")): return assert FLAGS.timit_paths for path in FLAGS.timit_paths.split(","): with tf.gfile.GFile(path) as f: with tarfile.open(fileobj=f, mode="r:gz") as timit_compressed: timit_compressed.extractall(directory)
[ "def", "_get_timit", "(", "directory", ")", ":", "if", "os", ".", "path", ".", "exists", "(", "os", ".", "path", ".", "join", "(", "directory", ",", "\"timit\"", ")", ")", ":", "return", "assert", "FLAGS", ".", "timit_paths", "for", "path", "in", "FLAGS", ".", "timit_paths", ".", "split", "(", "\",\"", ")", ":", "with", "tf", ".", "gfile", ".", "GFile", "(", "path", ")", "as", "f", ":", "with", "tarfile", ".", "open", "(", "fileobj", "=", "f", ",", "mode", "=", "\"r:gz\"", ")", "as", "timit_compressed", ":", "timit_compressed", ".", "extractall", "(", "directory", ")" ]
Extract TIMIT datasets to directory unless directory/timit exists.
[ "Extract", "TIMIT", "datasets", "to", "directory", "unless", "directory", "/", "timit", "exists", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/audio.py#L44-L53
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/audio.py
_collect_data
def _collect_data(directory, input_ext, target_ext): """Traverses directory collecting input and target files.""" # Directory from string to tuple pair of strings # key: the filepath to a datafile including the datafile's basename. Example, # if the datafile was "/path/to/datafile.wav" then the key would be # "/path/to/datafile" # value: a pair of strings (input_filepath, target_filepath) data_files = {} for root, _, filenames in os.walk(directory): input_files = [filename for filename in filenames if input_ext in filename] for input_filename in input_files: basename = input_filename.strip(input_ext) input_file = os.path.join(root, input_filename) target_file = os.path.join(root, basename + target_ext) key = os.path.join(root, basename) assert os.path.exists(target_file) assert key not in data_files data_files[key] = (input_file, target_file) return data_files
python
def _collect_data(directory, input_ext, target_ext): """Traverses directory collecting input and target files.""" # Directory from string to tuple pair of strings # key: the filepath to a datafile including the datafile's basename. Example, # if the datafile was "/path/to/datafile.wav" then the key would be # "/path/to/datafile" # value: a pair of strings (input_filepath, target_filepath) data_files = {} for root, _, filenames in os.walk(directory): input_files = [filename for filename in filenames if input_ext in filename] for input_filename in input_files: basename = input_filename.strip(input_ext) input_file = os.path.join(root, input_filename) target_file = os.path.join(root, basename + target_ext) key = os.path.join(root, basename) assert os.path.exists(target_file) assert key not in data_files data_files[key] = (input_file, target_file) return data_files
[ "def", "_collect_data", "(", "directory", ",", "input_ext", ",", "target_ext", ")", ":", "# Directory from string to tuple pair of strings", "# key: the filepath to a datafile including the datafile's basename. Example,", "# if the datafile was \"/path/to/datafile.wav\" then the key would be", "# \"/path/to/datafile\"", "# value: a pair of strings (input_filepath, target_filepath)", "data_files", "=", "{", "}", "for", "root", ",", "_", ",", "filenames", "in", "os", ".", "walk", "(", "directory", ")", ":", "input_files", "=", "[", "filename", "for", "filename", "in", "filenames", "if", "input_ext", "in", "filename", "]", "for", "input_filename", "in", "input_files", ":", "basename", "=", "input_filename", ".", "strip", "(", "input_ext", ")", "input_file", "=", "os", ".", "path", ".", "join", "(", "root", ",", "input_filename", ")", "target_file", "=", "os", ".", "path", ".", "join", "(", "root", ",", "basename", "+", "target_ext", ")", "key", "=", "os", ".", "path", ".", "join", "(", "root", ",", "basename", ")", "assert", "os", ".", "path", ".", "exists", "(", "target_file", ")", "assert", "key", "not", "in", "data_files", "data_files", "[", "key", "]", "=", "(", "input_file", ",", "target_file", ")", "return", "data_files" ]
Traverses directory collecting input and target files.
[ "Traverses", "directory", "collecting", "input", "and", "target", "files", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/audio.py#L56-L74
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/audio.py
timit_generator
def timit_generator(data_dir, tmp_dir, training, how_many, start_from=0, eos_list=None, vocab_filename=None, vocab_size=0): """Data generator for TIMIT transcription problem. Args: data_dir: path to the data directory. tmp_dir: path to temporary storage directory. training: a Boolean; if true, we use the train set, otherwise the test set. how_many: how many inputs and labels to generate. start_from: from which input to start. eos_list: optional list of end of sentence tokens, otherwise use default value `1`. vocab_filename: file within `tmp_dir` to read vocabulary from. If this is not provided then the target sentence will be encoded by character. vocab_size: integer target to generate vocabulary size to. Yields: A dictionary representing the images with the following fields: * inputs: a float sequence containing the audio data * audio/channel_count: an integer * audio/sample_count: an integer * audio/sample_width: an integer * targets: an integer sequence representing the encoded sentence """ del data_dir eos_list = [1] if eos_list is None else eos_list if vocab_filename is not None: # TODO(lukaszkaiser): Correct this call to generate a vocabulary. No data # sources are being passed. # vocab_symbolizer = generator_utils.get_or_generate_vocab( # data_dir, tmp_dir, vocab_filename, vocab_size) del vocab_size vocab_symbolizer = None assert False _get_timit(tmp_dir) datasets = (_TIMIT_TRAIN_DATASETS if training else _TIMIT_TEST_DATASETS) i = 0 for timit_data_dir, (audio_ext, transcription_ext) in datasets: timit_data_dir = os.path.join(tmp_dir, timit_data_dir) data_files = _collect_data(timit_data_dir, audio_ext, transcription_ext) data_pairs = data_files.values() for input_file, target_file in sorted(data_pairs)[start_from:]: if i == how_many: return i += 1 audio_data, sample_count, sample_width, num_channels = _get_audio_data( input_file) text_data = _get_text_data(target_file) if vocab_filename is None: label = [ord(c) for c in text_data] + eos_list else: label = vocab_symbolizer.encode(text_data) + eos_list yield { "inputs": audio_data, "audio/channel_count": [num_channels], "audio/sample_count": [sample_count], "audio/sample_width": [sample_width], "targets": label }
python
def timit_generator(data_dir, tmp_dir, training, how_many, start_from=0, eos_list=None, vocab_filename=None, vocab_size=0): """Data generator for TIMIT transcription problem. Args: data_dir: path to the data directory. tmp_dir: path to temporary storage directory. training: a Boolean; if true, we use the train set, otherwise the test set. how_many: how many inputs and labels to generate. start_from: from which input to start. eos_list: optional list of end of sentence tokens, otherwise use default value `1`. vocab_filename: file within `tmp_dir` to read vocabulary from. If this is not provided then the target sentence will be encoded by character. vocab_size: integer target to generate vocabulary size to. Yields: A dictionary representing the images with the following fields: * inputs: a float sequence containing the audio data * audio/channel_count: an integer * audio/sample_count: an integer * audio/sample_width: an integer * targets: an integer sequence representing the encoded sentence """ del data_dir eos_list = [1] if eos_list is None else eos_list if vocab_filename is not None: # TODO(lukaszkaiser): Correct this call to generate a vocabulary. No data # sources are being passed. # vocab_symbolizer = generator_utils.get_or_generate_vocab( # data_dir, tmp_dir, vocab_filename, vocab_size) del vocab_size vocab_symbolizer = None assert False _get_timit(tmp_dir) datasets = (_TIMIT_TRAIN_DATASETS if training else _TIMIT_TEST_DATASETS) i = 0 for timit_data_dir, (audio_ext, transcription_ext) in datasets: timit_data_dir = os.path.join(tmp_dir, timit_data_dir) data_files = _collect_data(timit_data_dir, audio_ext, transcription_ext) data_pairs = data_files.values() for input_file, target_file in sorted(data_pairs)[start_from:]: if i == how_many: return i += 1 audio_data, sample_count, sample_width, num_channels = _get_audio_data( input_file) text_data = _get_text_data(target_file) if vocab_filename is None: label = [ord(c) for c in text_data] + eos_list else: label = vocab_symbolizer.encode(text_data) + eos_list yield { "inputs": audio_data, "audio/channel_count": [num_channels], "audio/sample_count": [sample_count], "audio/sample_width": [sample_width], "targets": label }
[ "def", "timit_generator", "(", "data_dir", ",", "tmp_dir", ",", "training", ",", "how_many", ",", "start_from", "=", "0", ",", "eos_list", "=", "None", ",", "vocab_filename", "=", "None", ",", "vocab_size", "=", "0", ")", ":", "del", "data_dir", "eos_list", "=", "[", "1", "]", "if", "eos_list", "is", "None", "else", "eos_list", "if", "vocab_filename", "is", "not", "None", ":", "# TODO(lukaszkaiser): Correct this call to generate a vocabulary. No data", "# sources are being passed.", "# vocab_symbolizer = generator_utils.get_or_generate_vocab(", "# data_dir, tmp_dir, vocab_filename, vocab_size)", "del", "vocab_size", "vocab_symbolizer", "=", "None", "assert", "False", "_get_timit", "(", "tmp_dir", ")", "datasets", "=", "(", "_TIMIT_TRAIN_DATASETS", "if", "training", "else", "_TIMIT_TEST_DATASETS", ")", "i", "=", "0", "for", "timit_data_dir", ",", "(", "audio_ext", ",", "transcription_ext", ")", "in", "datasets", ":", "timit_data_dir", "=", "os", ".", "path", ".", "join", "(", "tmp_dir", ",", "timit_data_dir", ")", "data_files", "=", "_collect_data", "(", "timit_data_dir", ",", "audio_ext", ",", "transcription_ext", ")", "data_pairs", "=", "data_files", ".", "values", "(", ")", "for", "input_file", ",", "target_file", "in", "sorted", "(", "data_pairs", ")", "[", "start_from", ":", "]", ":", "if", "i", "==", "how_many", ":", "return", "i", "+=", "1", "audio_data", ",", "sample_count", ",", "sample_width", ",", "num_channels", "=", "_get_audio_data", "(", "input_file", ")", "text_data", "=", "_get_text_data", "(", "target_file", ")", "if", "vocab_filename", "is", "None", ":", "label", "=", "[", "ord", "(", "c", ")", "for", "c", "in", "text_data", "]", "+", "eos_list", "else", ":", "label", "=", "vocab_symbolizer", ".", "encode", "(", "text_data", ")", "+", "eos_list", "yield", "{", "\"inputs\"", ":", "audio_data", ",", "\"audio/channel_count\"", ":", "[", "num_channels", "]", ",", "\"audio/sample_count\"", ":", "[", "sample_count", "]", ",", "\"audio/sample_width\"", ":", "[", "sample_width", "]", ",", "\"targets\"", ":", "label", "}" ]
Data generator for TIMIT transcription problem. Args: data_dir: path to the data directory. tmp_dir: path to temporary storage directory. training: a Boolean; if true, we use the train set, otherwise the test set. how_many: how many inputs and labels to generate. start_from: from which input to start. eos_list: optional list of end of sentence tokens, otherwise use default value `1`. vocab_filename: file within `tmp_dir` to read vocabulary from. If this is not provided then the target sentence will be encoded by character. vocab_size: integer target to generate vocabulary size to. Yields: A dictionary representing the images with the following fields: * inputs: a float sequence containing the audio data * audio/channel_count: an integer * audio/sample_count: an integer * audio/sample_width: an integer * targets: an integer sequence representing the encoded sentence
[ "Data", "generator", "for", "TIMIT", "transcription", "problem", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/audio.py#L98-L162
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/wikitext103.py
_build_vocab
def _build_vocab(filename, vocab_dir, vocab_name): """Reads a file to build a vocabulary. Args: filename: file to read list of words from. vocab_dir: directory where to save the vocabulary. vocab_name: vocab file name. Returns: text encoder. """ vocab_path = os.path.join(vocab_dir, vocab_name) if not tf.gfile.Exists(vocab_path): with tf.gfile.GFile(filename, "r") as f: data = f.read().split() counter = collections.Counter(data) count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0])) words, _ = list(zip(*count_pairs)) encoder = text_encoder.TokenTextEncoder(None, vocab_list=words) encoder.store_to_file(vocab_path) else: encoder = text_encoder.TokenTextEncoder(vocab_path) return encoder
python
def _build_vocab(filename, vocab_dir, vocab_name): """Reads a file to build a vocabulary. Args: filename: file to read list of words from. vocab_dir: directory where to save the vocabulary. vocab_name: vocab file name. Returns: text encoder. """ vocab_path = os.path.join(vocab_dir, vocab_name) if not tf.gfile.Exists(vocab_path): with tf.gfile.GFile(filename, "r") as f: data = f.read().split() counter = collections.Counter(data) count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0])) words, _ = list(zip(*count_pairs)) encoder = text_encoder.TokenTextEncoder(None, vocab_list=words) encoder.store_to_file(vocab_path) else: encoder = text_encoder.TokenTextEncoder(vocab_path) return encoder
[ "def", "_build_vocab", "(", "filename", ",", "vocab_dir", ",", "vocab_name", ")", ":", "vocab_path", "=", "os", ".", "path", ".", "join", "(", "vocab_dir", ",", "vocab_name", ")", "if", "not", "tf", ".", "gfile", ".", "Exists", "(", "vocab_path", ")", ":", "with", "tf", ".", "gfile", ".", "GFile", "(", "filename", ",", "\"r\"", ")", "as", "f", ":", "data", "=", "f", ".", "read", "(", ")", ".", "split", "(", ")", "counter", "=", "collections", ".", "Counter", "(", "data", ")", "count_pairs", "=", "sorted", "(", "counter", ".", "items", "(", ")", ",", "key", "=", "lambda", "x", ":", "(", "-", "x", "[", "1", "]", ",", "x", "[", "0", "]", ")", ")", "words", ",", "_", "=", "list", "(", "zip", "(", "*", "count_pairs", ")", ")", "encoder", "=", "text_encoder", ".", "TokenTextEncoder", "(", "None", ",", "vocab_list", "=", "words", ")", "encoder", ".", "store_to_file", "(", "vocab_path", ")", "else", ":", "encoder", "=", "text_encoder", ".", "TokenTextEncoder", "(", "vocab_path", ")", "return", "encoder" ]
Reads a file to build a vocabulary. Args: filename: file to read list of words from. vocab_dir: directory where to save the vocabulary. vocab_name: vocab file name. Returns: text encoder.
[ "Reads", "a", "file", "to", "build", "a", "vocabulary", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikitext103.py#L37-L59
train
tensorflow/tensor2tensor
tensor2tensor/data_generators/wikitext103.py
_maybe_download_corpus
def _maybe_download_corpus(tmp_dir, vocab_type): """Download and unpack the corpus. Args: tmp_dir: directory containing dataset. vocab_type: which vocabulary are we using. Returns: The list of names of files. """ if vocab_type == text_problems.VocabType.CHARACTER: dataset_url = ("https://s3.amazonaws.com/research.metamind.io/wikitext" "/wikitext-103-raw-v1.zip") dir_name = "wikitext-103-raw" else: dataset_url = ("https://s3.amazonaws.com/research.metamind.io/wikitext" "/wikitext-103-v1.zip") dir_name = "wikitext-103" fname = os.path.basename(dataset_url) compressed_filepath = generator_utils.maybe_download(tmp_dir, fname, dataset_url) zip_ref = zipfile.ZipFile(compressed_filepath, "r") zip_ref.extractall(tmp_dir) zip_ref.close() files = os.path.join(tmp_dir, dir_name, "*") train_file, valid_file, test_file = None, None, None for f in tf.gfile.Glob(files): fname = os.path.basename(f) if "train" in fname: train_file = f elif "valid" in fname: valid_file = f elif "test" in fname: test_file = f assert train_file, "Training file not found" assert valid_file, "Validation file not found" assert test_file, "Testing file not found" return train_file, valid_file, test_file
python
def _maybe_download_corpus(tmp_dir, vocab_type): """Download and unpack the corpus. Args: tmp_dir: directory containing dataset. vocab_type: which vocabulary are we using. Returns: The list of names of files. """ if vocab_type == text_problems.VocabType.CHARACTER: dataset_url = ("https://s3.amazonaws.com/research.metamind.io/wikitext" "/wikitext-103-raw-v1.zip") dir_name = "wikitext-103-raw" else: dataset_url = ("https://s3.amazonaws.com/research.metamind.io/wikitext" "/wikitext-103-v1.zip") dir_name = "wikitext-103" fname = os.path.basename(dataset_url) compressed_filepath = generator_utils.maybe_download(tmp_dir, fname, dataset_url) zip_ref = zipfile.ZipFile(compressed_filepath, "r") zip_ref.extractall(tmp_dir) zip_ref.close() files = os.path.join(tmp_dir, dir_name, "*") train_file, valid_file, test_file = None, None, None for f in tf.gfile.Glob(files): fname = os.path.basename(f) if "train" in fname: train_file = f elif "valid" in fname: valid_file = f elif "test" in fname: test_file = f assert train_file, "Training file not found" assert valid_file, "Validation file not found" assert test_file, "Testing file not found" return train_file, valid_file, test_file
[ "def", "_maybe_download_corpus", "(", "tmp_dir", ",", "vocab_type", ")", ":", "if", "vocab_type", "==", "text_problems", ".", "VocabType", ".", "CHARACTER", ":", "dataset_url", "=", "(", "\"https://s3.amazonaws.com/research.metamind.io/wikitext\"", "\"/wikitext-103-raw-v1.zip\"", ")", "dir_name", "=", "\"wikitext-103-raw\"", "else", ":", "dataset_url", "=", "(", "\"https://s3.amazonaws.com/research.metamind.io/wikitext\"", "\"/wikitext-103-v1.zip\"", ")", "dir_name", "=", "\"wikitext-103\"", "fname", "=", "os", ".", "path", ".", "basename", "(", "dataset_url", ")", "compressed_filepath", "=", "generator_utils", ".", "maybe_download", "(", "tmp_dir", ",", "fname", ",", "dataset_url", ")", "zip_ref", "=", "zipfile", ".", "ZipFile", "(", "compressed_filepath", ",", "\"r\"", ")", "zip_ref", ".", "extractall", "(", "tmp_dir", ")", "zip_ref", ".", "close", "(", ")", "files", "=", "os", ".", "path", ".", "join", "(", "tmp_dir", ",", "dir_name", ",", "\"*\"", ")", "train_file", ",", "valid_file", ",", "test_file", "=", "None", ",", "None", ",", "None", "for", "f", "in", "tf", ".", "gfile", ".", "Glob", "(", "files", ")", ":", "fname", "=", "os", ".", "path", ".", "basename", "(", "f", ")", "if", "\"train\"", "in", "fname", ":", "train_file", "=", "f", "elif", "\"valid\"", "in", "fname", ":", "valid_file", "=", "f", "elif", "\"test\"", "in", "fname", ":", "test_file", "=", "f", "assert", "train_file", ",", "\"Training file not found\"", "assert", "valid_file", ",", "\"Validation file not found\"", "assert", "test_file", ",", "\"Testing file not found\"", "return", "train_file", ",", "valid_file", ",", "test_file" ]
Download and unpack the corpus. Args: tmp_dir: directory containing dataset. vocab_type: which vocabulary are we using. Returns: The list of names of files.
[ "Download", "and", "unpack", "the", "corpus", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikitext103.py#L62-L104
train
tensorflow/tensor2tensor
tensor2tensor/models/research/aligned.py
get_batch_coordinate
def get_batch_coordinate(x): """Return a flat int32 tensor of shape [1, batch_size*length, 1].""" # Compute the batch coordinate before flattening all batches batch_coordinate = tf.expand_dims( common_attention.coordinate_tensor( common_layers.shape_list(x)[:-1], axis=0), axis=-1) return batch_coordinate
python
def get_batch_coordinate(x): """Return a flat int32 tensor of shape [1, batch_size*length, 1].""" # Compute the batch coordinate before flattening all batches batch_coordinate = tf.expand_dims( common_attention.coordinate_tensor( common_layers.shape_list(x)[:-1], axis=0), axis=-1) return batch_coordinate
[ "def", "get_batch_coordinate", "(", "x", ")", ":", "# Compute the batch coordinate before flattening all batches", "batch_coordinate", "=", "tf", ".", "expand_dims", "(", "common_attention", ".", "coordinate_tensor", "(", "common_layers", ".", "shape_list", "(", "x", ")", "[", ":", "-", "1", "]", ",", "axis", "=", "0", ")", ",", "axis", "=", "-", "1", ")", "return", "batch_coordinate" ]
Return a flat int32 tensor of shape [1, batch_size*length, 1].
[ "Return", "a", "flat", "int32", "tensor", "of", "shape", "[", "1", "batch_size", "*", "length", "1", "]", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/aligned.py#L228-L235
train
tensorflow/tensor2tensor
tensor2tensor/models/research/aligned.py
aligned_base
def aligned_base(): """Set of hyperparameters. languagemodel_wiki_scramble1k50, 1gpu, 7k steps (10min): log(ppl)_eval = 2.60 12.0 steps/sec on P100 8gpu (8x batch), 7k steps: log(ppl)_eval = 2.00 Returns: a hparams object """ hparams = common_hparams.basic_params1() hparams.hidden_size = 512 hparams.batch_size = 5000 hparams.max_length = 0 hparams.min_length_bucket = 1024 hparams.dropout = 0.0 hparams.layer_prepostprocess_dropout = 0.0 hparams.label_smoothing = 0.0 hparams.clip_grad_norm = 0. # i.e. no gradient clipping hparams.optimizer_adam_epsilon = 1e-9 hparams.learning_rate_decay_scheme = "noam" hparams.learning_rate = 0.1 hparams.learning_rate_warmup_steps = 2000 hparams.initializer_gain = 1.0 hparams.initializer = "uniform_unit_scaling" hparams.weight_decay = 0.0 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.98 hparams.shared_embedding_and_softmax_weights = True hparams.add_hparam("ffn_hidden_sizes", "2048") # Add new ones like this. hparams.moe_num_experts = 32 hparams.layer_preprocess_sequence = "n" hparams.layer_postprocess_sequence = "da" hparams.add_hparam("layers", "timing," + "conv,att,ffn," * 2) # attention-related flags hparams.add_hparam("num_heads", 8) hparams.add_hparam("attention_key_channels", 0) hparams.add_hparam("attention_value_channels", 0) # All hyperparameters ending in "dropout" are automatically set to 0.0 # when not in training mode. hparams.add_hparam("attention_dropout", 0.0) hparams.add_hparam("pos", "timing") # timing, none # moe params. local attention moe. hparams.add_hparam("attention_local", False) hparams.add_hparam("attention_moe_k", 2) hparams.add_hparam("attention_num_experts", 16) hparams.add_hparam("attention_split_batch", False) # Key, query and value dimensions for the attention hparams.add_hparam("attention_kq_size", 128) hparams.add_hparam("attention_v_size", 256) # Loss coef for load balancing hparams.add_hparam("attention_load_balance", 2e-2) hparams.add_hparam("diet_experts", False) hparams.add_hparam("memory_efficient_ffn", False) hparams.add_hparam("local_attention_window", 128) hparams.add_hparam("attention_num_groups", 8) hparams.add_hparam("memory_target_density", 2.0) hparams.add_hparam("multiplicative_overhead", 1.25) hparams.add_hparam("multiplicative_overhead_eval", 2.0) hparams.add_hparam("attention_image_summary", True) # LSH params hparams.add_hparam("lsh_truncated", True) # For testing right-masking. # This is not implemented in all layers. hparams.add_hparam("mask_right", False) return hparams
python
def aligned_base(): """Set of hyperparameters. languagemodel_wiki_scramble1k50, 1gpu, 7k steps (10min): log(ppl)_eval = 2.60 12.0 steps/sec on P100 8gpu (8x batch), 7k steps: log(ppl)_eval = 2.00 Returns: a hparams object """ hparams = common_hparams.basic_params1() hparams.hidden_size = 512 hparams.batch_size = 5000 hparams.max_length = 0 hparams.min_length_bucket = 1024 hparams.dropout = 0.0 hparams.layer_prepostprocess_dropout = 0.0 hparams.label_smoothing = 0.0 hparams.clip_grad_norm = 0. # i.e. no gradient clipping hparams.optimizer_adam_epsilon = 1e-9 hparams.learning_rate_decay_scheme = "noam" hparams.learning_rate = 0.1 hparams.learning_rate_warmup_steps = 2000 hparams.initializer_gain = 1.0 hparams.initializer = "uniform_unit_scaling" hparams.weight_decay = 0.0 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.98 hparams.shared_embedding_and_softmax_weights = True hparams.add_hparam("ffn_hidden_sizes", "2048") # Add new ones like this. hparams.moe_num_experts = 32 hparams.layer_preprocess_sequence = "n" hparams.layer_postprocess_sequence = "da" hparams.add_hparam("layers", "timing," + "conv,att,ffn," * 2) # attention-related flags hparams.add_hparam("num_heads", 8) hparams.add_hparam("attention_key_channels", 0) hparams.add_hparam("attention_value_channels", 0) # All hyperparameters ending in "dropout" are automatically set to 0.0 # when not in training mode. hparams.add_hparam("attention_dropout", 0.0) hparams.add_hparam("pos", "timing") # timing, none # moe params. local attention moe. hparams.add_hparam("attention_local", False) hparams.add_hparam("attention_moe_k", 2) hparams.add_hparam("attention_num_experts", 16) hparams.add_hparam("attention_split_batch", False) # Key, query and value dimensions for the attention hparams.add_hparam("attention_kq_size", 128) hparams.add_hparam("attention_v_size", 256) # Loss coef for load balancing hparams.add_hparam("attention_load_balance", 2e-2) hparams.add_hparam("diet_experts", False) hparams.add_hparam("memory_efficient_ffn", False) hparams.add_hparam("local_attention_window", 128) hparams.add_hparam("attention_num_groups", 8) hparams.add_hparam("memory_target_density", 2.0) hparams.add_hparam("multiplicative_overhead", 1.25) hparams.add_hparam("multiplicative_overhead_eval", 2.0) hparams.add_hparam("attention_image_summary", True) # LSH params hparams.add_hparam("lsh_truncated", True) # For testing right-masking. # This is not implemented in all layers. hparams.add_hparam("mask_right", False) return hparams
[ "def", "aligned_base", "(", ")", ":", "hparams", "=", "common_hparams", ".", "basic_params1", "(", ")", "hparams", ".", "hidden_size", "=", "512", "hparams", ".", "batch_size", "=", "5000", "hparams", ".", "max_length", "=", "0", "hparams", ".", "min_length_bucket", "=", "1024", "hparams", ".", "dropout", "=", "0.0", "hparams", ".", "layer_prepostprocess_dropout", "=", "0.0", "hparams", ".", "label_smoothing", "=", "0.0", "hparams", ".", "clip_grad_norm", "=", "0.", "# i.e. no gradient clipping", "hparams", ".", "optimizer_adam_epsilon", "=", "1e-9", "hparams", ".", "learning_rate_decay_scheme", "=", "\"noam\"", "hparams", ".", "learning_rate", "=", "0.1", "hparams", ".", "learning_rate_warmup_steps", "=", "2000", "hparams", ".", "initializer_gain", "=", "1.0", "hparams", ".", "initializer", "=", "\"uniform_unit_scaling\"", "hparams", ".", "weight_decay", "=", "0.0", "hparams", ".", "optimizer_adam_beta1", "=", "0.9", "hparams", ".", "optimizer_adam_beta2", "=", "0.98", "hparams", ".", "shared_embedding_and_softmax_weights", "=", "True", "hparams", ".", "add_hparam", "(", "\"ffn_hidden_sizes\"", ",", "\"2048\"", ")", "# Add new ones like this.", "hparams", ".", "moe_num_experts", "=", "32", "hparams", ".", "layer_preprocess_sequence", "=", "\"n\"", "hparams", ".", "layer_postprocess_sequence", "=", "\"da\"", "hparams", ".", "add_hparam", "(", "\"layers\"", ",", "\"timing,\"", "+", "\"conv,att,ffn,\"", "*", "2", ")", "# attention-related flags", "hparams", ".", "add_hparam", "(", "\"num_heads\"", ",", "8", ")", "hparams", ".", "add_hparam", "(", "\"attention_key_channels\"", ",", "0", ")", "hparams", ".", "add_hparam", "(", "\"attention_value_channels\"", ",", "0", ")", "# All hyperparameters ending in \"dropout\" are automatically set to 0.0", "# when not in training mode.", "hparams", ".", "add_hparam", "(", "\"attention_dropout\"", ",", "0.0", ")", "hparams", ".", "add_hparam", "(", "\"pos\"", ",", "\"timing\"", ")", "# timing, none", "# moe params. local attention moe.", "hparams", ".", "add_hparam", "(", "\"attention_local\"", ",", "False", ")", "hparams", ".", "add_hparam", "(", "\"attention_moe_k\"", ",", "2", ")", "hparams", ".", "add_hparam", "(", "\"attention_num_experts\"", ",", "16", ")", "hparams", ".", "add_hparam", "(", "\"attention_split_batch\"", ",", "False", ")", "# Key, query and value dimensions for the attention", "hparams", ".", "add_hparam", "(", "\"attention_kq_size\"", ",", "128", ")", "hparams", ".", "add_hparam", "(", "\"attention_v_size\"", ",", "256", ")", "# Loss coef for load balancing", "hparams", ".", "add_hparam", "(", "\"attention_load_balance\"", ",", "2e-2", ")", "hparams", ".", "add_hparam", "(", "\"diet_experts\"", ",", "False", ")", "hparams", ".", "add_hparam", "(", "\"memory_efficient_ffn\"", ",", "False", ")", "hparams", ".", "add_hparam", "(", "\"local_attention_window\"", ",", "128", ")", "hparams", ".", "add_hparam", "(", "\"attention_num_groups\"", ",", "8", ")", "hparams", ".", "add_hparam", "(", "\"memory_target_density\"", ",", "2.0", ")", "hparams", ".", "add_hparam", "(", "\"multiplicative_overhead\"", ",", "1.25", ")", "hparams", ".", "add_hparam", "(", "\"multiplicative_overhead_eval\"", ",", "2.0", ")", "hparams", ".", "add_hparam", "(", "\"attention_image_summary\"", ",", "True", ")", "# LSH params", "hparams", ".", "add_hparam", "(", "\"lsh_truncated\"", ",", "True", ")", "# For testing right-masking.", "# This is not implemented in all layers.", "hparams", ".", "add_hparam", "(", "\"mask_right\"", ",", "False", ")", "return", "hparams" ]
Set of hyperparameters. languagemodel_wiki_scramble1k50, 1gpu, 7k steps (10min): log(ppl)_eval = 2.60 12.0 steps/sec on P100 8gpu (8x batch), 7k steps: log(ppl)_eval = 2.00 Returns: a hparams object
[ "Set", "of", "hyperparameters", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/aligned.py#L239-L305
train
tensorflow/tensor2tensor
tensor2tensor/models/research/aligned.py
aligned_8k_grouped
def aligned_8k_grouped(): """version for languagemodel_wiki_scramble8k50. languagemodel_wiki_scramble1k50, 1gpu, 7k steps: log(ppl)_eval = 2.92 3.3 steps/sec on P100 8gpu (8x batch), 7k steps: log(ppl)_eval = 2.15 Returns: a hparams object """ hparams = aligned_grouped() hparams.batch_size = 8192 # hparams.attention_image_summary = False hparams.num_groups = 16 hparams.multiplicative_overhead = 1.1 return hparams
python
def aligned_8k_grouped(): """version for languagemodel_wiki_scramble8k50. languagemodel_wiki_scramble1k50, 1gpu, 7k steps: log(ppl)_eval = 2.92 3.3 steps/sec on P100 8gpu (8x batch), 7k steps: log(ppl)_eval = 2.15 Returns: a hparams object """ hparams = aligned_grouped() hparams.batch_size = 8192 # hparams.attention_image_summary = False hparams.num_groups = 16 hparams.multiplicative_overhead = 1.1 return hparams
[ "def", "aligned_8k_grouped", "(", ")", ":", "hparams", "=", "aligned_grouped", "(", ")", "hparams", ".", "batch_size", "=", "8192", "# hparams.attention_image_summary = False", "hparams", ".", "num_groups", "=", "16", "hparams", ".", "multiplicative_overhead", "=", "1.1", "return", "hparams" ]
version for languagemodel_wiki_scramble8k50. languagemodel_wiki_scramble1k50, 1gpu, 7k steps: log(ppl)_eval = 2.92 3.3 steps/sec on P100 8gpu (8x batch), 7k steps: log(ppl)_eval = 2.15 Returns: a hparams object
[ "version", "for", "languagemodel_wiki_scramble8k50", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/aligned.py#L512-L527
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
_merge_beam_dim
def _merge_beam_dim(tensor): """Reshapes first two dimensions in to single dimension. Args: tensor: Tensor to reshape of shape [A, B, ...] Returns: Reshaped tensor of shape [A*B, ...] """ shape = common_layers.shape_list(tensor) shape[0] *= shape[1] # batch -> batch * beam_size shape.pop(1) # Remove beam dim return tf.reshape(tensor, shape)
python
def _merge_beam_dim(tensor): """Reshapes first two dimensions in to single dimension. Args: tensor: Tensor to reshape of shape [A, B, ...] Returns: Reshaped tensor of shape [A*B, ...] """ shape = common_layers.shape_list(tensor) shape[0] *= shape[1] # batch -> batch * beam_size shape.pop(1) # Remove beam dim return tf.reshape(tensor, shape)
[ "def", "_merge_beam_dim", "(", "tensor", ")", ":", "shape", "=", "common_layers", ".", "shape_list", "(", "tensor", ")", "shape", "[", "0", "]", "*=", "shape", "[", "1", "]", "# batch -> batch * beam_size", "shape", ".", "pop", "(", "1", ")", "# Remove beam dim", "return", "tf", ".", "reshape", "(", "tensor", ",", "shape", ")" ]
Reshapes first two dimensions in to single dimension. Args: tensor: Tensor to reshape of shape [A, B, ...] Returns: Reshaped tensor of shape [A*B, ...]
[ "Reshapes", "first", "two", "dimensions", "in", "to", "single", "dimension", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L37-L49
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
_unmerge_beam_dim
def _unmerge_beam_dim(tensor, batch_size, beam_size): """Reshapes first dimension back to [batch_size, beam_size]. Args: tensor: Tensor to reshape of shape [batch_size*beam_size, ...] batch_size: Tensor, original batch size. beam_size: int, original beam size. Returns: Reshaped tensor of shape [batch_size, beam_size, ...] """ shape = common_layers.shape_list(tensor) new_shape = [batch_size] + [beam_size] + shape[1:] return tf.reshape(tensor, new_shape)
python
def _unmerge_beam_dim(tensor, batch_size, beam_size): """Reshapes first dimension back to [batch_size, beam_size]. Args: tensor: Tensor to reshape of shape [batch_size*beam_size, ...] batch_size: Tensor, original batch size. beam_size: int, original beam size. Returns: Reshaped tensor of shape [batch_size, beam_size, ...] """ shape = common_layers.shape_list(tensor) new_shape = [batch_size] + [beam_size] + shape[1:] return tf.reshape(tensor, new_shape)
[ "def", "_unmerge_beam_dim", "(", "tensor", ",", "batch_size", ",", "beam_size", ")", ":", "shape", "=", "common_layers", ".", "shape_list", "(", "tensor", ")", "new_shape", "=", "[", "batch_size", "]", "+", "[", "beam_size", "]", "+", "shape", "[", "1", ":", "]", "return", "tf", ".", "reshape", "(", "tensor", ",", "new_shape", ")" ]
Reshapes first dimension back to [batch_size, beam_size]. Args: tensor: Tensor to reshape of shape [batch_size*beam_size, ...] batch_size: Tensor, original batch size. beam_size: int, original beam size. Returns: Reshaped tensor of shape [batch_size, beam_size, ...]
[ "Reshapes", "first", "dimension", "back", "to", "[", "batch_size", "beam_size", "]", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L52-L65
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
_expand_to_beam_size
def _expand_to_beam_size(tensor, beam_size): """Tiles a given tensor by beam_size. Args: tensor: tensor to tile [batch_size, ...] beam_size: How much to tile the tensor by. Returns: Tiled tensor [batch_size, beam_size, ...] """ tensor = tf.expand_dims(tensor, axis=1) tile_dims = [1] * tensor.shape.ndims tile_dims[1] = beam_size return tf.tile(tensor, tile_dims)
python
def _expand_to_beam_size(tensor, beam_size): """Tiles a given tensor by beam_size. Args: tensor: tensor to tile [batch_size, ...] beam_size: How much to tile the tensor by. Returns: Tiled tensor [batch_size, beam_size, ...] """ tensor = tf.expand_dims(tensor, axis=1) tile_dims = [1] * tensor.shape.ndims tile_dims[1] = beam_size return tf.tile(tensor, tile_dims)
[ "def", "_expand_to_beam_size", "(", "tensor", ",", "beam_size", ")", ":", "tensor", "=", "tf", ".", "expand_dims", "(", "tensor", ",", "axis", "=", "1", ")", "tile_dims", "=", "[", "1", "]", "*", "tensor", ".", "shape", ".", "ndims", "tile_dims", "[", "1", "]", "=", "beam_size", "return", "tf", ".", "tile", "(", "tensor", ",", "tile_dims", ")" ]
Tiles a given tensor by beam_size. Args: tensor: tensor to tile [batch_size, ...] beam_size: How much to tile the tensor by. Returns: Tiled tensor [batch_size, beam_size, ...]
[ "Tiles", "a", "given", "tensor", "by", "beam_size", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L68-L82
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
get_state_shape_invariants
def get_state_shape_invariants(tensor): """Returns the shape of the tensor but sets middle dims to None.""" shape = tensor.shape.as_list() for i in range(1, len(shape) - 1): shape[i] = None return tf.TensorShape(shape)
python
def get_state_shape_invariants(tensor): """Returns the shape of the tensor but sets middle dims to None.""" shape = tensor.shape.as_list() for i in range(1, len(shape) - 1): shape[i] = None return tf.TensorShape(shape)
[ "def", "get_state_shape_invariants", "(", "tensor", ")", ":", "shape", "=", "tensor", ".", "shape", ".", "as_list", "(", ")", "for", "i", "in", "range", "(", "1", ",", "len", "(", "shape", ")", "-", "1", ")", ":", "shape", "[", "i", "]", "=", "None", "return", "tf", ".", "TensorShape", "(", "shape", ")" ]
Returns the shape of the tensor but sets middle dims to None.
[ "Returns", "the", "shape", "of", "the", "tensor", "but", "sets", "middle", "dims", "to", "None", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L85-L90
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
compute_batch_indices
def compute_batch_indices(batch_size, beam_size): """Computes the i'th coordinate that contains the batch index for gathers. Batch pos is a tensor like [[0,0,0,0,],[1,1,1,1],..]. It says which batch the beam item is in. This will create the i of the i,j coordinate needed for the gather. Args: batch_size: Batch size beam_size: Size of the beam. Returns: batch_pos: [batch_size, beam_size] tensor of ids """ batch_pos = tf.range(batch_size * beam_size) // beam_size batch_pos = tf.reshape(batch_pos, [batch_size, beam_size]) return batch_pos
python
def compute_batch_indices(batch_size, beam_size): """Computes the i'th coordinate that contains the batch index for gathers. Batch pos is a tensor like [[0,0,0,0,],[1,1,1,1],..]. It says which batch the beam item is in. This will create the i of the i,j coordinate needed for the gather. Args: batch_size: Batch size beam_size: Size of the beam. Returns: batch_pos: [batch_size, beam_size] tensor of ids """ batch_pos = tf.range(batch_size * beam_size) // beam_size batch_pos = tf.reshape(batch_pos, [batch_size, beam_size]) return batch_pos
[ "def", "compute_batch_indices", "(", "batch_size", ",", "beam_size", ")", ":", "batch_pos", "=", "tf", ".", "range", "(", "batch_size", "*", "beam_size", ")", "//", "beam_size", "batch_pos", "=", "tf", ".", "reshape", "(", "batch_pos", ",", "[", "batch_size", ",", "beam_size", "]", ")", "return", "batch_pos" ]
Computes the i'th coordinate that contains the batch index for gathers. Batch pos is a tensor like [[0,0,0,0,],[1,1,1,1],..]. It says which batch the beam item is in. This will create the i of the i,j coordinate needed for the gather. Args: batch_size: Batch size beam_size: Size of the beam. Returns: batch_pos: [batch_size, beam_size] tensor of ids
[ "Computes", "the", "i", "th", "coordinate", "that", "contains", "the", "batch", "index", "for", "gathers", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L93-L108
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
fast_tpu_gather
def fast_tpu_gather(params, indices, name=None): """Fast gather implementation for models running on TPU. This function use one_hot and batch matmul to do gather, which is faster than gather_nd on TPU. For params that have dtype of int32 (sequences to gather from), batch_gather is used to keep accuracy. Args: params: A tensor from which to gather values. [batch_size, original_size, ...] indices: A tensor used as the index to gather values. [batch_size, selected_size]. name: A string, name of the operation (optional). Returns: gather_result: A tensor that has the same rank as params. [batch_size, selected_size, ...] """ with tf.name_scope(name): dtype = params.dtype def _gather(params, indices): """Fast gather using one_hot and batch matmul.""" if dtype != tf.float32: params = tf.to_float(params) shape = common_layers.shape_list(params) indices_shape = common_layers.shape_list(indices) ndims = params.shape.ndims # Adjust the shape of params to match one-hot indices, which is the # requirement of Batch MatMul. if ndims == 2: params = tf.expand_dims(params, axis=-1) if ndims > 3: params = tf.reshape(params, [shape[0], shape[1], -1]) gather_result = tf.matmul( tf.one_hot(indices, shape[1], dtype=params.dtype), params) if ndims == 2: gather_result = tf.squeeze(gather_result, axis=-1) if ndims > 3: shape[1] = indices_shape[1] gather_result = tf.reshape(gather_result, shape) if dtype != tf.float32: gather_result = tf.cast(gather_result, dtype) return gather_result # If the dtype is int, use the gather instead of one_hot matmul to avoid # precision loss. The max int value can be represented by bfloat16 in MXU is # 256, which is smaller than the possible id values. Encoding/decoding can # potentially used to make it work, but the benenfit is small right now. if dtype.is_integer: gather_result = tf.batch_gather(params, indices) else: gather_result = _gather(params, indices) return gather_result
python
def fast_tpu_gather(params, indices, name=None): """Fast gather implementation for models running on TPU. This function use one_hot and batch matmul to do gather, which is faster than gather_nd on TPU. For params that have dtype of int32 (sequences to gather from), batch_gather is used to keep accuracy. Args: params: A tensor from which to gather values. [batch_size, original_size, ...] indices: A tensor used as the index to gather values. [batch_size, selected_size]. name: A string, name of the operation (optional). Returns: gather_result: A tensor that has the same rank as params. [batch_size, selected_size, ...] """ with tf.name_scope(name): dtype = params.dtype def _gather(params, indices): """Fast gather using one_hot and batch matmul.""" if dtype != tf.float32: params = tf.to_float(params) shape = common_layers.shape_list(params) indices_shape = common_layers.shape_list(indices) ndims = params.shape.ndims # Adjust the shape of params to match one-hot indices, which is the # requirement of Batch MatMul. if ndims == 2: params = tf.expand_dims(params, axis=-1) if ndims > 3: params = tf.reshape(params, [shape[0], shape[1], -1]) gather_result = tf.matmul( tf.one_hot(indices, shape[1], dtype=params.dtype), params) if ndims == 2: gather_result = tf.squeeze(gather_result, axis=-1) if ndims > 3: shape[1] = indices_shape[1] gather_result = tf.reshape(gather_result, shape) if dtype != tf.float32: gather_result = tf.cast(gather_result, dtype) return gather_result # If the dtype is int, use the gather instead of one_hot matmul to avoid # precision loss. The max int value can be represented by bfloat16 in MXU is # 256, which is smaller than the possible id values. Encoding/decoding can # potentially used to make it work, but the benenfit is small right now. if dtype.is_integer: gather_result = tf.batch_gather(params, indices) else: gather_result = _gather(params, indices) return gather_result
[ "def", "fast_tpu_gather", "(", "params", ",", "indices", ",", "name", "=", "None", ")", ":", "with", "tf", ".", "name_scope", "(", "name", ")", ":", "dtype", "=", "params", ".", "dtype", "def", "_gather", "(", "params", ",", "indices", ")", ":", "\"\"\"Fast gather using one_hot and batch matmul.\"\"\"", "if", "dtype", "!=", "tf", ".", "float32", ":", "params", "=", "tf", ".", "to_float", "(", "params", ")", "shape", "=", "common_layers", ".", "shape_list", "(", "params", ")", "indices_shape", "=", "common_layers", ".", "shape_list", "(", "indices", ")", "ndims", "=", "params", ".", "shape", ".", "ndims", "# Adjust the shape of params to match one-hot indices, which is the", "# requirement of Batch MatMul.", "if", "ndims", "==", "2", ":", "params", "=", "tf", ".", "expand_dims", "(", "params", ",", "axis", "=", "-", "1", ")", "if", "ndims", ">", "3", ":", "params", "=", "tf", ".", "reshape", "(", "params", ",", "[", "shape", "[", "0", "]", ",", "shape", "[", "1", "]", ",", "-", "1", "]", ")", "gather_result", "=", "tf", ".", "matmul", "(", "tf", ".", "one_hot", "(", "indices", ",", "shape", "[", "1", "]", ",", "dtype", "=", "params", ".", "dtype", ")", ",", "params", ")", "if", "ndims", "==", "2", ":", "gather_result", "=", "tf", ".", "squeeze", "(", "gather_result", ",", "axis", "=", "-", "1", ")", "if", "ndims", ">", "3", ":", "shape", "[", "1", "]", "=", "indices_shape", "[", "1", "]", "gather_result", "=", "tf", ".", "reshape", "(", "gather_result", ",", "shape", ")", "if", "dtype", "!=", "tf", ".", "float32", ":", "gather_result", "=", "tf", ".", "cast", "(", "gather_result", ",", "dtype", ")", "return", "gather_result", "# If the dtype is int, use the gather instead of one_hot matmul to avoid", "# precision loss. The max int value can be represented by bfloat16 in MXU is", "# 256, which is smaller than the possible id values. Encoding/decoding can", "# potentially used to make it work, but the benenfit is small right now.", "if", "dtype", ".", "is_integer", ":", "gather_result", "=", "tf", ".", "batch_gather", "(", "params", ",", "indices", ")", "else", ":", "gather_result", "=", "_gather", "(", "params", ",", "indices", ")", "return", "gather_result" ]
Fast gather implementation for models running on TPU. This function use one_hot and batch matmul to do gather, which is faster than gather_nd on TPU. For params that have dtype of int32 (sequences to gather from), batch_gather is used to keep accuracy. Args: params: A tensor from which to gather values. [batch_size, original_size, ...] indices: A tensor used as the index to gather values. [batch_size, selected_size]. name: A string, name of the operation (optional). Returns: gather_result: A tensor that has the same rank as params. [batch_size, selected_size, ...]
[ "Fast", "gather", "implementation", "for", "models", "running", "on", "TPU", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L111-L165
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
_create_make_unique
def _create_make_unique(inputs): """Replaces the lower bits of each element with iota. The iota is used to derive the index, and also serves the purpose to make each element unique to break ties. Args: inputs: A tensor with rank of 2 and dtype of tf.float32. [batch_size, original_size]. Returns: A tensor after element wise transformation, with dtype the same as inputs. [batch_size, original_size]. Raises: ValueError: If the rank of the input tensor does not equal 2. """ if inputs.shape.ndims != 2: raise ValueError("Input of top_k_with_unique must be rank-2 " "but got: %s" % inputs.shape) height = inputs.shape[0] width = inputs.shape[1] zeros = tf.zeros([height, width], dtype=tf.int32) # Count_mask is used to mask away the low order bits to ensure that every # element is distinct. log2_ceiling = int(math.ceil(math.log(int(width), 2))) next_power_of_two = 1 << log2_ceiling count_mask = ~(next_power_of_two - 1) count_mask_r0 = tf.constant(count_mask) count_mask_r2 = tf.fill([height, width], count_mask_r0) # Smallest_normal is the bit representation of the smallest positive normal # floating point number. The sign is zero, exponent is one, and the fraction # is zero. smallest_normal = 1 << 23 smallest_normal_r0 = tf.constant(smallest_normal, dtype=tf.int32) smallest_normal_r2 = tf.fill([height, width], smallest_normal_r0) # Low_bit_mask is used to mask away the sign bit when computing the absolute # value. low_bit_mask = ~(1 << 31) low_bit_mask_r0 = tf.constant(low_bit_mask, dtype=tf.int32) low_bit_mask_r2 = tf.fill([height, width], low_bit_mask_r0) iota = tf.tile(tf.expand_dims(tf.range(width, dtype=tf.int32), 0), [height, 1]) # Compare the absolute value with positive zero to handle negative zero. input_r2 = tf.bitcast(inputs, tf.int32) abs_r2 = tf.bitwise.bitwise_and(input_r2, low_bit_mask_r2) if_zero_r2 = tf.equal(abs_r2, zeros) smallest_normal_preserving_sign_r2 = tf.bitwise.bitwise_or( input_r2, smallest_normal_r2) input_no_zeros_r2 = tf.where( if_zero_r2, smallest_normal_preserving_sign_r2, input_r2) # Discard the low-order bits and replace with iota. and_r2 = tf.bitwise.bitwise_and(input_no_zeros_r2, count_mask_r2) or_r2 = tf.bitwise.bitwise_or(and_r2, iota) return tf.bitcast(or_r2, tf.float32)
python
def _create_make_unique(inputs): """Replaces the lower bits of each element with iota. The iota is used to derive the index, and also serves the purpose to make each element unique to break ties. Args: inputs: A tensor with rank of 2 and dtype of tf.float32. [batch_size, original_size]. Returns: A tensor after element wise transformation, with dtype the same as inputs. [batch_size, original_size]. Raises: ValueError: If the rank of the input tensor does not equal 2. """ if inputs.shape.ndims != 2: raise ValueError("Input of top_k_with_unique must be rank-2 " "but got: %s" % inputs.shape) height = inputs.shape[0] width = inputs.shape[1] zeros = tf.zeros([height, width], dtype=tf.int32) # Count_mask is used to mask away the low order bits to ensure that every # element is distinct. log2_ceiling = int(math.ceil(math.log(int(width), 2))) next_power_of_two = 1 << log2_ceiling count_mask = ~(next_power_of_two - 1) count_mask_r0 = tf.constant(count_mask) count_mask_r2 = tf.fill([height, width], count_mask_r0) # Smallest_normal is the bit representation of the smallest positive normal # floating point number. The sign is zero, exponent is one, and the fraction # is zero. smallest_normal = 1 << 23 smallest_normal_r0 = tf.constant(smallest_normal, dtype=tf.int32) smallest_normal_r2 = tf.fill([height, width], smallest_normal_r0) # Low_bit_mask is used to mask away the sign bit when computing the absolute # value. low_bit_mask = ~(1 << 31) low_bit_mask_r0 = tf.constant(low_bit_mask, dtype=tf.int32) low_bit_mask_r2 = tf.fill([height, width], low_bit_mask_r0) iota = tf.tile(tf.expand_dims(tf.range(width, dtype=tf.int32), 0), [height, 1]) # Compare the absolute value with positive zero to handle negative zero. input_r2 = tf.bitcast(inputs, tf.int32) abs_r2 = tf.bitwise.bitwise_and(input_r2, low_bit_mask_r2) if_zero_r2 = tf.equal(abs_r2, zeros) smallest_normal_preserving_sign_r2 = tf.bitwise.bitwise_or( input_r2, smallest_normal_r2) input_no_zeros_r2 = tf.where( if_zero_r2, smallest_normal_preserving_sign_r2, input_r2) # Discard the low-order bits and replace with iota. and_r2 = tf.bitwise.bitwise_and(input_no_zeros_r2, count_mask_r2) or_r2 = tf.bitwise.bitwise_or(and_r2, iota) return tf.bitcast(or_r2, tf.float32)
[ "def", "_create_make_unique", "(", "inputs", ")", ":", "if", "inputs", ".", "shape", ".", "ndims", "!=", "2", ":", "raise", "ValueError", "(", "\"Input of top_k_with_unique must be rank-2 \"", "\"but got: %s\"", "%", "inputs", ".", "shape", ")", "height", "=", "inputs", ".", "shape", "[", "0", "]", "width", "=", "inputs", ".", "shape", "[", "1", "]", "zeros", "=", "tf", ".", "zeros", "(", "[", "height", ",", "width", "]", ",", "dtype", "=", "tf", ".", "int32", ")", "# Count_mask is used to mask away the low order bits to ensure that every", "# element is distinct.", "log2_ceiling", "=", "int", "(", "math", ".", "ceil", "(", "math", ".", "log", "(", "int", "(", "width", ")", ",", "2", ")", ")", ")", "next_power_of_two", "=", "1", "<<", "log2_ceiling", "count_mask", "=", "~", "(", "next_power_of_two", "-", "1", ")", "count_mask_r0", "=", "tf", ".", "constant", "(", "count_mask", ")", "count_mask_r2", "=", "tf", ".", "fill", "(", "[", "height", ",", "width", "]", ",", "count_mask_r0", ")", "# Smallest_normal is the bit representation of the smallest positive normal", "# floating point number. The sign is zero, exponent is one, and the fraction", "# is zero.", "smallest_normal", "=", "1", "<<", "23", "smallest_normal_r0", "=", "tf", ".", "constant", "(", "smallest_normal", ",", "dtype", "=", "tf", ".", "int32", ")", "smallest_normal_r2", "=", "tf", ".", "fill", "(", "[", "height", ",", "width", "]", ",", "smallest_normal_r0", ")", "# Low_bit_mask is used to mask away the sign bit when computing the absolute", "# value.", "low_bit_mask", "=", "~", "(", "1", "<<", "31", ")", "low_bit_mask_r0", "=", "tf", ".", "constant", "(", "low_bit_mask", ",", "dtype", "=", "tf", ".", "int32", ")", "low_bit_mask_r2", "=", "tf", ".", "fill", "(", "[", "height", ",", "width", "]", ",", "low_bit_mask_r0", ")", "iota", "=", "tf", ".", "tile", "(", "tf", ".", "expand_dims", "(", "tf", ".", "range", "(", "width", ",", "dtype", "=", "tf", ".", "int32", ")", ",", "0", ")", ",", "[", "height", ",", "1", "]", ")", "# Compare the absolute value with positive zero to handle negative zero.", "input_r2", "=", "tf", ".", "bitcast", "(", "inputs", ",", "tf", ".", "int32", ")", "abs_r2", "=", "tf", ".", "bitwise", ".", "bitwise_and", "(", "input_r2", ",", "low_bit_mask_r2", ")", "if_zero_r2", "=", "tf", ".", "equal", "(", "abs_r2", ",", "zeros", ")", "smallest_normal_preserving_sign_r2", "=", "tf", ".", "bitwise", ".", "bitwise_or", "(", "input_r2", ",", "smallest_normal_r2", ")", "input_no_zeros_r2", "=", "tf", ".", "where", "(", "if_zero_r2", ",", "smallest_normal_preserving_sign_r2", ",", "input_r2", ")", "# Discard the low-order bits and replace with iota.", "and_r2", "=", "tf", ".", "bitwise", ".", "bitwise_and", "(", "input_no_zeros_r2", ",", "count_mask_r2", ")", "or_r2", "=", "tf", ".", "bitwise", ".", "bitwise_or", "(", "and_r2", ",", "iota", ")", "return", "tf", ".", "bitcast", "(", "or_r2", ",", "tf", ".", "float32", ")" ]
Replaces the lower bits of each element with iota. The iota is used to derive the index, and also serves the purpose to make each element unique to break ties. Args: inputs: A tensor with rank of 2 and dtype of tf.float32. [batch_size, original_size]. Returns: A tensor after element wise transformation, with dtype the same as inputs. [batch_size, original_size]. Raises: ValueError: If the rank of the input tensor does not equal 2.
[ "Replaces", "the", "lower", "bits", "of", "each", "element", "with", "iota", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L168-L229
train
tensorflow/tensor2tensor
tensor2tensor/utils/beam_search.py
_create_topk_unique
def _create_topk_unique(inputs, k): """Creates the top k values in sorted order with indices. Args: inputs: A tensor with rank of 2. [batch_size, original_size]. k: An integer, number of top elements to select. Returns: topk_r2: A tensor, the k largest elements. [batch_size, k]. topk_indices_r2: A tensor, indices of the top k values. [batch_size, k]. """ height = inputs.shape[0] width = inputs.shape[1] neg_inf_r0 = tf.constant(-np.inf, dtype=tf.float32) ones = tf.ones([height, width], dtype=tf.float32) neg_inf_r2 = ones * neg_inf_r0 inputs = tf.where(tf.is_nan(inputs), neg_inf_r2, inputs) # Select the current largest value k times and keep them in topk_r2. The # selected largest values are marked as the smallest value to avoid being # selected again. tmp = inputs topk_r2 = tf.zeros([height, k], dtype=tf.float32) for i in range(k): kth_order_statistic = tf.reduce_max(tmp, axis=1, keepdims=True) k_mask = tf.tile(tf.expand_dims(tf.equal(tf.range(k), tf.fill([k], i)), 0), [height, 1]) topk_r2 = tf.where(k_mask, tf.tile(kth_order_statistic, [1, k]), topk_r2) ge_r2 = tf.greater_equal(inputs, tf.tile(kth_order_statistic, [1, width])) tmp = tf.where(ge_r2, neg_inf_r2, inputs) log2_ceiling = int(math.ceil(math.log(float(int(width)), 2))) next_power_of_two = 1 << log2_ceiling count_mask = next_power_of_two - 1 mask_r0 = tf.constant(count_mask) mask_r2 = tf.fill([height, k], mask_r0) topk_r2_s32 = tf.bitcast(topk_r2, tf.int32) topk_indices_r2 = tf.bitwise.bitwise_and(topk_r2_s32, mask_r2) return topk_r2, topk_indices_r2
python
def _create_topk_unique(inputs, k): """Creates the top k values in sorted order with indices. Args: inputs: A tensor with rank of 2. [batch_size, original_size]. k: An integer, number of top elements to select. Returns: topk_r2: A tensor, the k largest elements. [batch_size, k]. topk_indices_r2: A tensor, indices of the top k values. [batch_size, k]. """ height = inputs.shape[0] width = inputs.shape[1] neg_inf_r0 = tf.constant(-np.inf, dtype=tf.float32) ones = tf.ones([height, width], dtype=tf.float32) neg_inf_r2 = ones * neg_inf_r0 inputs = tf.where(tf.is_nan(inputs), neg_inf_r2, inputs) # Select the current largest value k times and keep them in topk_r2. The # selected largest values are marked as the smallest value to avoid being # selected again. tmp = inputs topk_r2 = tf.zeros([height, k], dtype=tf.float32) for i in range(k): kth_order_statistic = tf.reduce_max(tmp, axis=1, keepdims=True) k_mask = tf.tile(tf.expand_dims(tf.equal(tf.range(k), tf.fill([k], i)), 0), [height, 1]) topk_r2 = tf.where(k_mask, tf.tile(kth_order_statistic, [1, k]), topk_r2) ge_r2 = tf.greater_equal(inputs, tf.tile(kth_order_statistic, [1, width])) tmp = tf.where(ge_r2, neg_inf_r2, inputs) log2_ceiling = int(math.ceil(math.log(float(int(width)), 2))) next_power_of_two = 1 << log2_ceiling count_mask = next_power_of_two - 1 mask_r0 = tf.constant(count_mask) mask_r2 = tf.fill([height, k], mask_r0) topk_r2_s32 = tf.bitcast(topk_r2, tf.int32) topk_indices_r2 = tf.bitwise.bitwise_and(topk_r2_s32, mask_r2) return topk_r2, topk_indices_r2
[ "def", "_create_topk_unique", "(", "inputs", ",", "k", ")", ":", "height", "=", "inputs", ".", "shape", "[", "0", "]", "width", "=", "inputs", ".", "shape", "[", "1", "]", "neg_inf_r0", "=", "tf", ".", "constant", "(", "-", "np", ".", "inf", ",", "dtype", "=", "tf", ".", "float32", ")", "ones", "=", "tf", ".", "ones", "(", "[", "height", ",", "width", "]", ",", "dtype", "=", "tf", ".", "float32", ")", "neg_inf_r2", "=", "ones", "*", "neg_inf_r0", "inputs", "=", "tf", ".", "where", "(", "tf", ".", "is_nan", "(", "inputs", ")", ",", "neg_inf_r2", ",", "inputs", ")", "# Select the current largest value k times and keep them in topk_r2. The", "# selected largest values are marked as the smallest value to avoid being", "# selected again.", "tmp", "=", "inputs", "topk_r2", "=", "tf", ".", "zeros", "(", "[", "height", ",", "k", "]", ",", "dtype", "=", "tf", ".", "float32", ")", "for", "i", "in", "range", "(", "k", ")", ":", "kth_order_statistic", "=", "tf", ".", "reduce_max", "(", "tmp", ",", "axis", "=", "1", ",", "keepdims", "=", "True", ")", "k_mask", "=", "tf", ".", "tile", "(", "tf", ".", "expand_dims", "(", "tf", ".", "equal", "(", "tf", ".", "range", "(", "k", ")", ",", "tf", ".", "fill", "(", "[", "k", "]", ",", "i", ")", ")", ",", "0", ")", ",", "[", "height", ",", "1", "]", ")", "topk_r2", "=", "tf", ".", "where", "(", "k_mask", ",", "tf", ".", "tile", "(", "kth_order_statistic", ",", "[", "1", ",", "k", "]", ")", ",", "topk_r2", ")", "ge_r2", "=", "tf", ".", "greater_equal", "(", "inputs", ",", "tf", ".", "tile", "(", "kth_order_statistic", ",", "[", "1", ",", "width", "]", ")", ")", "tmp", "=", "tf", ".", "where", "(", "ge_r2", ",", "neg_inf_r2", ",", "inputs", ")", "log2_ceiling", "=", "int", "(", "math", ".", "ceil", "(", "math", ".", "log", "(", "float", "(", "int", "(", "width", ")", ")", ",", "2", ")", ")", ")", "next_power_of_two", "=", "1", "<<", "log2_ceiling", "count_mask", "=", "next_power_of_two", "-", "1", "mask_r0", "=", "tf", ".", "constant", "(", "count_mask", ")", "mask_r2", "=", "tf", ".", "fill", "(", "[", "height", ",", "k", "]", ",", "mask_r0", ")", "topk_r2_s32", "=", "tf", ".", "bitcast", "(", "topk_r2", ",", "tf", ".", "int32", ")", "topk_indices_r2", "=", "tf", ".", "bitwise", ".", "bitwise_and", "(", "topk_r2_s32", ",", "mask_r2", ")", "return", "topk_r2", ",", "topk_indices_r2" ]
Creates the top k values in sorted order with indices. Args: inputs: A tensor with rank of 2. [batch_size, original_size]. k: An integer, number of top elements to select. Returns: topk_r2: A tensor, the k largest elements. [batch_size, k]. topk_indices_r2: A tensor, indices of the top k values. [batch_size, k].
[ "Creates", "the", "top", "k", "values", "in", "sorted", "order", "with", "indices", "." ]
272500b6efe353aeb638d2745ed56e519462ca31
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/utils/beam_search.py#L232-L270
train