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/rl/restarter.py
|
Restarter.training_loop
|
def training_loop(self):
"""Context manager wrapping the training loop, updates step counters."""
if not self.restarting:
self._write_counters(self._local_step_at_start, self._global_step)
tf.logging.info(
"Training %s up to %d, %d to go", self.model_mode,
self.target_local_step, self.steps_to_go
)
yield
self._write_counters(self.target_local_step, -1)
|
python
|
def training_loop(self):
"""Context manager wrapping the training loop, updates step counters."""
if not self.restarting:
self._write_counters(self._local_step_at_start, self._global_step)
tf.logging.info(
"Training %s up to %d, %d to go", self.model_mode,
self.target_local_step, self.steps_to_go
)
yield
self._write_counters(self.target_local_step, -1)
|
[
"def",
"training_loop",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"restarting",
":",
"self",
".",
"_write_counters",
"(",
"self",
".",
"_local_step_at_start",
",",
"self",
".",
"_global_step",
")",
"tf",
".",
"logging",
".",
"info",
"(",
"\"Training %s up to %d, %d to go\"",
",",
"self",
".",
"model_mode",
",",
"self",
".",
"target_local_step",
",",
"self",
".",
"steps_to_go",
")",
"yield",
"self",
".",
"_write_counters",
"(",
"self",
".",
"target_local_step",
",",
"-",
"1",
")"
] |
Context manager wrapping the training loop, updates step counters.
|
[
"Context",
"manager",
"wrapping",
"the",
"training",
"loop",
"updates",
"step",
"counters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/rl/restarter.py#L90-L102
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/ptb.py
|
_read_words
|
def _read_words(filename):
"""Reads words from a file."""
with tf.gfile.GFile(filename, "r") as f:
if sys.version_info[0] >= 3:
return f.read().replace("\n", " %s " % EOS).split()
else:
return f.read().decode("utf-8").replace("\n", " %s " % EOS).split()
|
python
|
def _read_words(filename):
"""Reads words from a file."""
with tf.gfile.GFile(filename, "r") as f:
if sys.version_info[0] >= 3:
return f.read().replace("\n", " %s " % EOS).split()
else:
return f.read().decode("utf-8").replace("\n", " %s " % EOS).split()
|
[
"def",
"_read_words",
"(",
"filename",
")",
":",
"with",
"tf",
".",
"gfile",
".",
"GFile",
"(",
"filename",
",",
"\"r\"",
")",
"as",
"f",
":",
"if",
"sys",
".",
"version_info",
"[",
"0",
"]",
">=",
"3",
":",
"return",
"f",
".",
"read",
"(",
")",
".",
"replace",
"(",
"\"\\n\"",
",",
"\" %s \"",
"%",
"EOS",
")",
".",
"split",
"(",
")",
"else",
":",
"return",
"f",
".",
"read",
"(",
")",
".",
"decode",
"(",
"\"utf-8\"",
")",
".",
"replace",
"(",
"\"\\n\"",
",",
"\" %s \"",
"%",
"EOS",
")",
".",
"split",
"(",
")"
] |
Reads words from a file.
|
[
"Reads",
"words",
"from",
"a",
"file",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/ptb.py#L39-L45
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/ptb.py
|
_build_vocab
|
def _build_vocab(filename, vocab_path, vocab_size):
"""Reads a file to build a vocabulary of `vocab_size` most common words.
The vocabulary is sorted by occurrence count and has one word per line.
Originally from:
https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/reader.py
Args:
filename: file to read list of words from.
vocab_path: path where to save the vocabulary.
vocab_size: size of the vocabulary to generate.
"""
data = _read_words(filename)
counter = collections.Counter(data)
count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))
words, _ = list(zip(*count_pairs))
words = words[:vocab_size]
with open(vocab_path, "w") as f:
f.write("\n".join(words))
|
python
|
def _build_vocab(filename, vocab_path, vocab_size):
"""Reads a file to build a vocabulary of `vocab_size` most common words.
The vocabulary is sorted by occurrence count and has one word per line.
Originally from:
https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/reader.py
Args:
filename: file to read list of words from.
vocab_path: path where to save the vocabulary.
vocab_size: size of the vocabulary to generate.
"""
data = _read_words(filename)
counter = collections.Counter(data)
count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))
words, _ = list(zip(*count_pairs))
words = words[:vocab_size]
with open(vocab_path, "w") as f:
f.write("\n".join(words))
|
[
"def",
"_build_vocab",
"(",
"filename",
",",
"vocab_path",
",",
"vocab_size",
")",
":",
"data",
"=",
"_read_words",
"(",
"filename",
")",
"counter",
"=",
"collections",
".",
"Counter",
"(",
"data",
")",
"count_pairs",
"=",
"sorted",
"(",
"counter",
".",
"items",
"(",
")",
",",
"key",
"=",
"lambda",
"x",
":",
"(",
"-",
"x",
"[",
"1",
"]",
",",
"x",
"[",
"0",
"]",
")",
")",
"words",
",",
"_",
"=",
"list",
"(",
"zip",
"(",
"*",
"count_pairs",
")",
")",
"words",
"=",
"words",
"[",
":",
"vocab_size",
"]",
"with",
"open",
"(",
"vocab_path",
",",
"\"w\"",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"\"\\n\"",
".",
"join",
"(",
"words",
")",
")"
] |
Reads a file to build a vocabulary of `vocab_size` most common words.
The vocabulary is sorted by occurrence count and has one word per line.
Originally from:
https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/reader.py
Args:
filename: file to read list of words from.
vocab_path: path where to save the vocabulary.
vocab_size: size of the vocabulary to generate.
|
[
"Reads",
"a",
"file",
"to",
"build",
"a",
"vocabulary",
"of",
"vocab_size",
"most",
"common",
"words",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/ptb.py#L48-L66
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/ptb.py
|
_get_token_encoder
|
def _get_token_encoder(vocab_dir, vocab_name, filename):
"""Reads from file and returns a `TokenTextEncoder` for the vocabulary."""
vocab_path = os.path.join(vocab_dir, vocab_name)
if not tf.gfile.Exists(vocab_path):
_build_vocab(filename, vocab_path, 10000)
return text_encoder.TokenTextEncoder(vocab_path)
|
python
|
def _get_token_encoder(vocab_dir, vocab_name, filename):
"""Reads from file and returns a `TokenTextEncoder` for the vocabulary."""
vocab_path = os.path.join(vocab_dir, vocab_name)
if not tf.gfile.Exists(vocab_path):
_build_vocab(filename, vocab_path, 10000)
return text_encoder.TokenTextEncoder(vocab_path)
|
[
"def",
"_get_token_encoder",
"(",
"vocab_dir",
",",
"vocab_name",
",",
"filename",
")",
":",
"vocab_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"vocab_dir",
",",
"vocab_name",
")",
"if",
"not",
"tf",
".",
"gfile",
".",
"Exists",
"(",
"vocab_path",
")",
":",
"_build_vocab",
"(",
"filename",
",",
"vocab_path",
",",
"10000",
")",
"return",
"text_encoder",
".",
"TokenTextEncoder",
"(",
"vocab_path",
")"
] |
Reads from file and returns a `TokenTextEncoder` for the vocabulary.
|
[
"Reads",
"from",
"file",
"and",
"returns",
"a",
"TokenTextEncoder",
"for",
"the",
"vocabulary",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/ptb.py#L69-L74
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/ptb.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.
"""
filename = os.path.basename(PTB_URL)
compressed_filepath = generator_utils.maybe_download(
tmp_dir, filename, PTB_URL)
ptb_files = []
ptb_char_files = []
with tarfile.open(compressed_filepath, "r:gz") as tgz:
files = []
# Selecting only relevant files.
for m in tgz.getmembers():
if "ptb" in m.name and ".txt" in m.name:
if "char" in m.name:
ptb_char_files += [m.name]
else:
ptb_files += [m.name]
files += [m]
tgz.extractall(tmp_dir, members=files)
if vocab_type == text_problems.VocabType.CHARACTER:
return ptb_char_files
else:
return ptb_files
|
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.
"""
filename = os.path.basename(PTB_URL)
compressed_filepath = generator_utils.maybe_download(
tmp_dir, filename, PTB_URL)
ptb_files = []
ptb_char_files = []
with tarfile.open(compressed_filepath, "r:gz") as tgz:
files = []
# Selecting only relevant files.
for m in tgz.getmembers():
if "ptb" in m.name and ".txt" in m.name:
if "char" in m.name:
ptb_char_files += [m.name]
else:
ptb_files += [m.name]
files += [m]
tgz.extractall(tmp_dir, members=files)
if vocab_type == text_problems.VocabType.CHARACTER:
return ptb_char_files
else:
return ptb_files
|
[
"def",
"_maybe_download_corpus",
"(",
"tmp_dir",
",",
"vocab_type",
")",
":",
"filename",
"=",
"os",
".",
"path",
".",
"basename",
"(",
"PTB_URL",
")",
"compressed_filepath",
"=",
"generator_utils",
".",
"maybe_download",
"(",
"tmp_dir",
",",
"filename",
",",
"PTB_URL",
")",
"ptb_files",
"=",
"[",
"]",
"ptb_char_files",
"=",
"[",
"]",
"with",
"tarfile",
".",
"open",
"(",
"compressed_filepath",
",",
"\"r:gz\"",
")",
"as",
"tgz",
":",
"files",
"=",
"[",
"]",
"# Selecting only relevant files.",
"for",
"m",
"in",
"tgz",
".",
"getmembers",
"(",
")",
":",
"if",
"\"ptb\"",
"in",
"m",
".",
"name",
"and",
"\".txt\"",
"in",
"m",
".",
"name",
":",
"if",
"\"char\"",
"in",
"m",
".",
"name",
":",
"ptb_char_files",
"+=",
"[",
"m",
".",
"name",
"]",
"else",
":",
"ptb_files",
"+=",
"[",
"m",
".",
"name",
"]",
"files",
"+=",
"[",
"m",
"]",
"tgz",
".",
"extractall",
"(",
"tmp_dir",
",",
"members",
"=",
"files",
")",
"if",
"vocab_type",
"==",
"text_problems",
".",
"VocabType",
".",
"CHARACTER",
":",
"return",
"ptb_char_files",
"else",
":",
"return",
"ptb_files"
] |
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/ptb.py#L77-L109
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/visualization/attention.py
|
resize
|
def resize(att_mat, max_length=None):
"""Normalize attention matrices and reshape as necessary."""
for i, att in enumerate(att_mat):
# Add extra batch dim for viz code to work.
if att.ndim == 3:
att = np.expand_dims(att, axis=0)
if max_length is not None:
# Sum across different attention values for each token.
att = att[:, :, :max_length, :max_length]
row_sums = np.sum(att, axis=2)
# Normalize
att /= row_sums[:, :, np.newaxis]
att_mat[i] = att
return att_mat
|
python
|
def resize(att_mat, max_length=None):
"""Normalize attention matrices and reshape as necessary."""
for i, att in enumerate(att_mat):
# Add extra batch dim for viz code to work.
if att.ndim == 3:
att = np.expand_dims(att, axis=0)
if max_length is not None:
# Sum across different attention values for each token.
att = att[:, :, :max_length, :max_length]
row_sums = np.sum(att, axis=2)
# Normalize
att /= row_sums[:, :, np.newaxis]
att_mat[i] = att
return att_mat
|
[
"def",
"resize",
"(",
"att_mat",
",",
"max_length",
"=",
"None",
")",
":",
"for",
"i",
",",
"att",
"in",
"enumerate",
"(",
"att_mat",
")",
":",
"# Add extra batch dim for viz code to work.",
"if",
"att",
".",
"ndim",
"==",
"3",
":",
"att",
"=",
"np",
".",
"expand_dims",
"(",
"att",
",",
"axis",
"=",
"0",
")",
"if",
"max_length",
"is",
"not",
"None",
":",
"# Sum across different attention values for each token.",
"att",
"=",
"att",
"[",
":",
",",
":",
",",
":",
"max_length",
",",
":",
"max_length",
"]",
"row_sums",
"=",
"np",
".",
"sum",
"(",
"att",
",",
"axis",
"=",
"2",
")",
"# Normalize",
"att",
"/=",
"row_sums",
"[",
":",
",",
":",
",",
"np",
".",
"newaxis",
"]",
"att_mat",
"[",
"i",
"]",
"=",
"att",
"return",
"att_mat"
] |
Normalize attention matrices and reshape as necessary.
|
[
"Normalize",
"attention",
"matrices",
"and",
"reshape",
"as",
"necessary",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/visualization/attention.py#L62-L75
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/visualization/attention.py
|
_get_attention
|
def _get_attention(inp_text, out_text, enc_atts, dec_atts, encdec_atts):
"""Compute representation of the attention ready for the d3 visualization.
Args:
inp_text: list of strings, words to be displayed on the left of the vis
out_text: list of strings, words to be displayed on the right of the vis
enc_atts: numpy array, encoder self-attentions
[num_layers, batch_size, num_heads, enc_length, enc_length]
dec_atts: numpy array, decoder self-attentions
[num_layers, batch_size, num_heads, dec_length, dec_length]
encdec_atts: numpy array, encoder-decoder attentions
[num_layers, batch_size, num_heads, dec_length, enc_length]
Returns:
Dictionary of attention representations with the structure:
{
'all': Representations for showing all attentions at the same time.
'inp_inp': Representations for showing encoder self-attentions
'inp_out': Representations for showing encoder-decoder attentions
'out_out': Representations for showing decoder self-attentions
}
and each sub-dictionary has structure:
{
'att': list of inter attentions matrices, one for each attention head
'top_text': list of strings, words to be displayed on the left of the vis
'bot_text': list of strings, words to be displayed on the right of the vis
}
"""
def get_full_attention(layer):
"""Get the full input+output - input+output attentions."""
enc_att = enc_atts[layer][0]
dec_att = dec_atts[layer][0]
encdec_att = encdec_atts[layer][0]
enc_att = np.transpose(enc_att, [0, 2, 1])
dec_att = np.transpose(dec_att, [0, 2, 1])
encdec_att = np.transpose(encdec_att, [0, 2, 1])
# [heads, query_length, memory_length]
enc_length = enc_att.shape[1]
dec_length = dec_att.shape[1]
num_heads = enc_att.shape[0]
first = np.concatenate([enc_att, encdec_att], axis=2)
second = np.concatenate(
[np.zeros((num_heads, dec_length, enc_length)), dec_att], axis=2)
full_att = np.concatenate([first, second], axis=1)
return [ha.T.tolist() for ha in full_att]
def get_inp_inp_attention(layer):
att = np.transpose(enc_atts[layer][0], (0, 2, 1))
return [ha.T.tolist() for ha in att]
def get_out_inp_attention(layer):
att = np.transpose(encdec_atts[layer][0], (0, 2, 1))
return [ha.T.tolist() for ha in att]
def get_out_out_attention(layer):
att = np.transpose(dec_atts[layer][0], (0, 2, 1))
return [ha.T.tolist() for ha in att]
def get_attentions(get_attention_fn):
num_layers = len(enc_atts)
return [get_attention_fn(i) for i in range(num_layers)]
attentions = {
'all': {
'att': get_attentions(get_full_attention),
'top_text': inp_text + out_text,
'bot_text': inp_text + out_text,
},
'inp_inp': {
'att': get_attentions(get_inp_inp_attention),
'top_text': inp_text,
'bot_text': inp_text,
},
'inp_out': {
'att': get_attentions(get_out_inp_attention),
'top_text': inp_text,
'bot_text': out_text,
},
'out_out': {
'att': get_attentions(get_out_out_attention),
'top_text': out_text,
'bot_text': out_text,
},
}
return attentions
|
python
|
def _get_attention(inp_text, out_text, enc_atts, dec_atts, encdec_atts):
"""Compute representation of the attention ready for the d3 visualization.
Args:
inp_text: list of strings, words to be displayed on the left of the vis
out_text: list of strings, words to be displayed on the right of the vis
enc_atts: numpy array, encoder self-attentions
[num_layers, batch_size, num_heads, enc_length, enc_length]
dec_atts: numpy array, decoder self-attentions
[num_layers, batch_size, num_heads, dec_length, dec_length]
encdec_atts: numpy array, encoder-decoder attentions
[num_layers, batch_size, num_heads, dec_length, enc_length]
Returns:
Dictionary of attention representations with the structure:
{
'all': Representations for showing all attentions at the same time.
'inp_inp': Representations for showing encoder self-attentions
'inp_out': Representations for showing encoder-decoder attentions
'out_out': Representations for showing decoder self-attentions
}
and each sub-dictionary has structure:
{
'att': list of inter attentions matrices, one for each attention head
'top_text': list of strings, words to be displayed on the left of the vis
'bot_text': list of strings, words to be displayed on the right of the vis
}
"""
def get_full_attention(layer):
"""Get the full input+output - input+output attentions."""
enc_att = enc_atts[layer][0]
dec_att = dec_atts[layer][0]
encdec_att = encdec_atts[layer][0]
enc_att = np.transpose(enc_att, [0, 2, 1])
dec_att = np.transpose(dec_att, [0, 2, 1])
encdec_att = np.transpose(encdec_att, [0, 2, 1])
# [heads, query_length, memory_length]
enc_length = enc_att.shape[1]
dec_length = dec_att.shape[1]
num_heads = enc_att.shape[0]
first = np.concatenate([enc_att, encdec_att], axis=2)
second = np.concatenate(
[np.zeros((num_heads, dec_length, enc_length)), dec_att], axis=2)
full_att = np.concatenate([first, second], axis=1)
return [ha.T.tolist() for ha in full_att]
def get_inp_inp_attention(layer):
att = np.transpose(enc_atts[layer][0], (0, 2, 1))
return [ha.T.tolist() for ha in att]
def get_out_inp_attention(layer):
att = np.transpose(encdec_atts[layer][0], (0, 2, 1))
return [ha.T.tolist() for ha in att]
def get_out_out_attention(layer):
att = np.transpose(dec_atts[layer][0], (0, 2, 1))
return [ha.T.tolist() for ha in att]
def get_attentions(get_attention_fn):
num_layers = len(enc_atts)
return [get_attention_fn(i) for i in range(num_layers)]
attentions = {
'all': {
'att': get_attentions(get_full_attention),
'top_text': inp_text + out_text,
'bot_text': inp_text + out_text,
},
'inp_inp': {
'att': get_attentions(get_inp_inp_attention),
'top_text': inp_text,
'bot_text': inp_text,
},
'inp_out': {
'att': get_attentions(get_out_inp_attention),
'top_text': inp_text,
'bot_text': out_text,
},
'out_out': {
'att': get_attentions(get_out_out_attention),
'top_text': out_text,
'bot_text': out_text,
},
}
return attentions
|
[
"def",
"_get_attention",
"(",
"inp_text",
",",
"out_text",
",",
"enc_atts",
",",
"dec_atts",
",",
"encdec_atts",
")",
":",
"def",
"get_full_attention",
"(",
"layer",
")",
":",
"\"\"\"Get the full input+output - input+output attentions.\"\"\"",
"enc_att",
"=",
"enc_atts",
"[",
"layer",
"]",
"[",
"0",
"]",
"dec_att",
"=",
"dec_atts",
"[",
"layer",
"]",
"[",
"0",
"]",
"encdec_att",
"=",
"encdec_atts",
"[",
"layer",
"]",
"[",
"0",
"]",
"enc_att",
"=",
"np",
".",
"transpose",
"(",
"enc_att",
",",
"[",
"0",
",",
"2",
",",
"1",
"]",
")",
"dec_att",
"=",
"np",
".",
"transpose",
"(",
"dec_att",
",",
"[",
"0",
",",
"2",
",",
"1",
"]",
")",
"encdec_att",
"=",
"np",
".",
"transpose",
"(",
"encdec_att",
",",
"[",
"0",
",",
"2",
",",
"1",
"]",
")",
"# [heads, query_length, memory_length]",
"enc_length",
"=",
"enc_att",
".",
"shape",
"[",
"1",
"]",
"dec_length",
"=",
"dec_att",
".",
"shape",
"[",
"1",
"]",
"num_heads",
"=",
"enc_att",
".",
"shape",
"[",
"0",
"]",
"first",
"=",
"np",
".",
"concatenate",
"(",
"[",
"enc_att",
",",
"encdec_att",
"]",
",",
"axis",
"=",
"2",
")",
"second",
"=",
"np",
".",
"concatenate",
"(",
"[",
"np",
".",
"zeros",
"(",
"(",
"num_heads",
",",
"dec_length",
",",
"enc_length",
")",
")",
",",
"dec_att",
"]",
",",
"axis",
"=",
"2",
")",
"full_att",
"=",
"np",
".",
"concatenate",
"(",
"[",
"first",
",",
"second",
"]",
",",
"axis",
"=",
"1",
")",
"return",
"[",
"ha",
".",
"T",
".",
"tolist",
"(",
")",
"for",
"ha",
"in",
"full_att",
"]",
"def",
"get_inp_inp_attention",
"(",
"layer",
")",
":",
"att",
"=",
"np",
".",
"transpose",
"(",
"enc_atts",
"[",
"layer",
"]",
"[",
"0",
"]",
",",
"(",
"0",
",",
"2",
",",
"1",
")",
")",
"return",
"[",
"ha",
".",
"T",
".",
"tolist",
"(",
")",
"for",
"ha",
"in",
"att",
"]",
"def",
"get_out_inp_attention",
"(",
"layer",
")",
":",
"att",
"=",
"np",
".",
"transpose",
"(",
"encdec_atts",
"[",
"layer",
"]",
"[",
"0",
"]",
",",
"(",
"0",
",",
"2",
",",
"1",
")",
")",
"return",
"[",
"ha",
".",
"T",
".",
"tolist",
"(",
")",
"for",
"ha",
"in",
"att",
"]",
"def",
"get_out_out_attention",
"(",
"layer",
")",
":",
"att",
"=",
"np",
".",
"transpose",
"(",
"dec_atts",
"[",
"layer",
"]",
"[",
"0",
"]",
",",
"(",
"0",
",",
"2",
",",
"1",
")",
")",
"return",
"[",
"ha",
".",
"T",
".",
"tolist",
"(",
")",
"for",
"ha",
"in",
"att",
"]",
"def",
"get_attentions",
"(",
"get_attention_fn",
")",
":",
"num_layers",
"=",
"len",
"(",
"enc_atts",
")",
"return",
"[",
"get_attention_fn",
"(",
"i",
")",
"for",
"i",
"in",
"range",
"(",
"num_layers",
")",
"]",
"attentions",
"=",
"{",
"'all'",
":",
"{",
"'att'",
":",
"get_attentions",
"(",
"get_full_attention",
")",
",",
"'top_text'",
":",
"inp_text",
"+",
"out_text",
",",
"'bot_text'",
":",
"inp_text",
"+",
"out_text",
",",
"}",
",",
"'inp_inp'",
":",
"{",
"'att'",
":",
"get_attentions",
"(",
"get_inp_inp_attention",
")",
",",
"'top_text'",
":",
"inp_text",
",",
"'bot_text'",
":",
"inp_text",
",",
"}",
",",
"'inp_out'",
":",
"{",
"'att'",
":",
"get_attentions",
"(",
"get_out_inp_attention",
")",
",",
"'top_text'",
":",
"inp_text",
",",
"'bot_text'",
":",
"out_text",
",",
"}",
",",
"'out_out'",
":",
"{",
"'att'",
":",
"get_attentions",
"(",
"get_out_out_attention",
")",
",",
"'top_text'",
":",
"out_text",
",",
"'bot_text'",
":",
"out_text",
",",
"}",
",",
"}",
"return",
"attentions"
] |
Compute representation of the attention ready for the d3 visualization.
Args:
inp_text: list of strings, words to be displayed on the left of the vis
out_text: list of strings, words to be displayed on the right of the vis
enc_atts: numpy array, encoder self-attentions
[num_layers, batch_size, num_heads, enc_length, enc_length]
dec_atts: numpy array, decoder self-attentions
[num_layers, batch_size, num_heads, dec_length, dec_length]
encdec_atts: numpy array, encoder-decoder attentions
[num_layers, batch_size, num_heads, dec_length, enc_length]
Returns:
Dictionary of attention representations with the structure:
{
'all': Representations for showing all attentions at the same time.
'inp_inp': Representations for showing encoder self-attentions
'inp_out': Representations for showing encoder-decoder attentions
'out_out': Representations for showing decoder self-attentions
}
and each sub-dictionary has structure:
{
'att': list of inter attentions matrices, one for each attention head
'top_text': list of strings, words to be displayed on the left of the vis
'bot_text': list of strings, words to be displayed on the right of the vis
}
|
[
"Compute",
"representation",
"of",
"the",
"attention",
"ready",
"for",
"the",
"d3",
"visualization",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/visualization/attention.py#L78-L163
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/tokenizer.py
|
decode
|
def decode(tokens):
"""Decode a list of tokens to a unicode string.
Args:
tokens: a list of Unicode strings
Returns:
a unicode string
"""
token_is_alnum = [t[0] in _ALPHANUMERIC_CHAR_SET for t in tokens]
ret = []
for i, token in enumerate(tokens):
if i > 0 and token_is_alnum[i - 1] and token_is_alnum[i]:
ret.append(u" ")
ret.append(token)
return "".join(ret)
|
python
|
def decode(tokens):
"""Decode a list of tokens to a unicode string.
Args:
tokens: a list of Unicode strings
Returns:
a unicode string
"""
token_is_alnum = [t[0] in _ALPHANUMERIC_CHAR_SET for t in tokens]
ret = []
for i, token in enumerate(tokens):
if i > 0 and token_is_alnum[i - 1] and token_is_alnum[i]:
ret.append(u" ")
ret.append(token)
return "".join(ret)
|
[
"def",
"decode",
"(",
"tokens",
")",
":",
"token_is_alnum",
"=",
"[",
"t",
"[",
"0",
"]",
"in",
"_ALPHANUMERIC_CHAR_SET",
"for",
"t",
"in",
"tokens",
"]",
"ret",
"=",
"[",
"]",
"for",
"i",
",",
"token",
"in",
"enumerate",
"(",
"tokens",
")",
":",
"if",
"i",
">",
"0",
"and",
"token_is_alnum",
"[",
"i",
"-",
"1",
"]",
"and",
"token_is_alnum",
"[",
"i",
"]",
":",
"ret",
".",
"append",
"(",
"u\" \"",
")",
"ret",
".",
"append",
"(",
"token",
")",
"return",
"\"\"",
".",
"join",
"(",
"ret",
")"
] |
Decode a list of tokens to a unicode string.
Args:
tokens: a list of Unicode strings
Returns:
a unicode string
|
[
"Decode",
"a",
"list",
"of",
"tokens",
"to",
"a",
"unicode",
"string",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/tokenizer.py#L91-L105
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/tokenizer.py
|
_read_filepattern
|
def _read_filepattern(filepattern, max_lines=None, split_on_newlines=True):
"""Reads files matching a wildcard pattern, yielding the contents.
Args:
filepattern: A wildcard pattern matching one or more files.
max_lines: If set, stop reading after reading this many lines.
split_on_newlines: A boolean. If true, then split files by lines and strip
leading and trailing whitespace from each line. Otherwise, treat each
file as a single string.
Yields:
The contents of the files as lines, if split_on_newlines is True, or
the entire contents of each file if False.
"""
filenames = sorted(tf.gfile.Glob(filepattern))
lines_read = 0
for filename in filenames:
with tf.gfile.Open(filename) as f:
if split_on_newlines:
for line in f:
yield line.strip()
lines_read += 1
if max_lines and lines_read >= max_lines:
return
else:
if max_lines:
doc = []
for line in f:
doc.append(line)
lines_read += 1
if max_lines and lines_read >= max_lines:
yield "".join(doc)
return
yield "".join(doc)
else:
yield f.read()
|
python
|
def _read_filepattern(filepattern, max_lines=None, split_on_newlines=True):
"""Reads files matching a wildcard pattern, yielding the contents.
Args:
filepattern: A wildcard pattern matching one or more files.
max_lines: If set, stop reading after reading this many lines.
split_on_newlines: A boolean. If true, then split files by lines and strip
leading and trailing whitespace from each line. Otherwise, treat each
file as a single string.
Yields:
The contents of the files as lines, if split_on_newlines is True, or
the entire contents of each file if False.
"""
filenames = sorted(tf.gfile.Glob(filepattern))
lines_read = 0
for filename in filenames:
with tf.gfile.Open(filename) as f:
if split_on_newlines:
for line in f:
yield line.strip()
lines_read += 1
if max_lines and lines_read >= max_lines:
return
else:
if max_lines:
doc = []
for line in f:
doc.append(line)
lines_read += 1
if max_lines and lines_read >= max_lines:
yield "".join(doc)
return
yield "".join(doc)
else:
yield f.read()
|
[
"def",
"_read_filepattern",
"(",
"filepattern",
",",
"max_lines",
"=",
"None",
",",
"split_on_newlines",
"=",
"True",
")",
":",
"filenames",
"=",
"sorted",
"(",
"tf",
".",
"gfile",
".",
"Glob",
"(",
"filepattern",
")",
")",
"lines_read",
"=",
"0",
"for",
"filename",
"in",
"filenames",
":",
"with",
"tf",
".",
"gfile",
".",
"Open",
"(",
"filename",
")",
"as",
"f",
":",
"if",
"split_on_newlines",
":",
"for",
"line",
"in",
"f",
":",
"yield",
"line",
".",
"strip",
"(",
")",
"lines_read",
"+=",
"1",
"if",
"max_lines",
"and",
"lines_read",
">=",
"max_lines",
":",
"return",
"else",
":",
"if",
"max_lines",
":",
"doc",
"=",
"[",
"]",
"for",
"line",
"in",
"f",
":",
"doc",
".",
"append",
"(",
"line",
")",
"lines_read",
"+=",
"1",
"if",
"max_lines",
"and",
"lines_read",
">=",
"max_lines",
":",
"yield",
"\"\"",
".",
"join",
"(",
"doc",
")",
"return",
"yield",
"\"\"",
".",
"join",
"(",
"doc",
")",
"else",
":",
"yield",
"f",
".",
"read",
"(",
")"
] |
Reads files matching a wildcard pattern, yielding the contents.
Args:
filepattern: A wildcard pattern matching one or more files.
max_lines: If set, stop reading after reading this many lines.
split_on_newlines: A boolean. If true, then split files by lines and strip
leading and trailing whitespace from each line. Otherwise, treat each
file as a single string.
Yields:
The contents of the files as lines, if split_on_newlines is True, or
the entire contents of each file if False.
|
[
"Reads",
"files",
"matching",
"a",
"wildcard",
"pattern",
"yielding",
"the",
"contents",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/tokenizer.py#L108-L145
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/tokenizer.py
|
corpus_token_counts
|
def corpus_token_counts(
text_filepattern, corpus_max_lines, split_on_newlines=True):
"""Read the corpus and compute a dictionary of token counts.
Args:
text_filepattern: A pattern matching one or more files.
corpus_max_lines: An integer; maximum total lines to read.
split_on_newlines: A boolean. If true, then split files by lines and strip
leading and trailing whitespace from each line. Otherwise, treat each
file as a single string.
Returns:
a dictionary mapping token to count.
"""
counts = collections.Counter()
for doc in _read_filepattern(
text_filepattern,
max_lines=corpus_max_lines,
split_on_newlines=split_on_newlines):
counts.update(encode(_native_to_unicode(doc)))
mlperf_log.transformer_print(
key=mlperf_log.PREPROC_VOCAB_SIZE, value=len(counts))
return counts
|
python
|
def corpus_token_counts(
text_filepattern, corpus_max_lines, split_on_newlines=True):
"""Read the corpus and compute a dictionary of token counts.
Args:
text_filepattern: A pattern matching one or more files.
corpus_max_lines: An integer; maximum total lines to read.
split_on_newlines: A boolean. If true, then split files by lines and strip
leading and trailing whitespace from each line. Otherwise, treat each
file as a single string.
Returns:
a dictionary mapping token to count.
"""
counts = collections.Counter()
for doc in _read_filepattern(
text_filepattern,
max_lines=corpus_max_lines,
split_on_newlines=split_on_newlines):
counts.update(encode(_native_to_unicode(doc)))
mlperf_log.transformer_print(
key=mlperf_log.PREPROC_VOCAB_SIZE, value=len(counts))
return counts
|
[
"def",
"corpus_token_counts",
"(",
"text_filepattern",
",",
"corpus_max_lines",
",",
"split_on_newlines",
"=",
"True",
")",
":",
"counts",
"=",
"collections",
".",
"Counter",
"(",
")",
"for",
"doc",
"in",
"_read_filepattern",
"(",
"text_filepattern",
",",
"max_lines",
"=",
"corpus_max_lines",
",",
"split_on_newlines",
"=",
"split_on_newlines",
")",
":",
"counts",
".",
"update",
"(",
"encode",
"(",
"_native_to_unicode",
"(",
"doc",
")",
")",
")",
"mlperf_log",
".",
"transformer_print",
"(",
"key",
"=",
"mlperf_log",
".",
"PREPROC_VOCAB_SIZE",
",",
"value",
"=",
"len",
"(",
"counts",
")",
")",
"return",
"counts"
] |
Read the corpus and compute a dictionary of token counts.
Args:
text_filepattern: A pattern matching one or more files.
corpus_max_lines: An integer; maximum total lines to read.
split_on_newlines: A boolean. If true, then split files by lines and strip
leading and trailing whitespace from each line. Otherwise, treat each
file as a single string.
Returns:
a dictionary mapping token to count.
|
[
"Read",
"the",
"corpus",
"and",
"compute",
"a",
"dictionary",
"of",
"token",
"counts",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/tokenizer.py#L148-L171
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/tokenizer.py
|
vocab_token_counts
|
def vocab_token_counts(text_filepattern, max_lines):
"""Read a vocab file and return a dictionary of token counts.
Reads a two-column CSV file of tokens and their frequency in a dataset. The
tokens are presumed to be generated by encode() or the equivalent.
Args:
text_filepattern: A pattern matching one or more files.
max_lines: An integer; maximum total lines to read.
Returns:
a dictionary mapping token to count.
"""
ret = {}
for i, line in enumerate(
_read_filepattern(text_filepattern, max_lines=max_lines)):
if "," not in line:
tf.logging.warning("Malformed vocab line #%d '%s'", i, line)
continue
token, count = line.rsplit(",", 1)
ret[_native_to_unicode(token)] = int(count)
return ret
|
python
|
def vocab_token_counts(text_filepattern, max_lines):
"""Read a vocab file and return a dictionary of token counts.
Reads a two-column CSV file of tokens and their frequency in a dataset. The
tokens are presumed to be generated by encode() or the equivalent.
Args:
text_filepattern: A pattern matching one or more files.
max_lines: An integer; maximum total lines to read.
Returns:
a dictionary mapping token to count.
"""
ret = {}
for i, line in enumerate(
_read_filepattern(text_filepattern, max_lines=max_lines)):
if "," not in line:
tf.logging.warning("Malformed vocab line #%d '%s'", i, line)
continue
token, count = line.rsplit(",", 1)
ret[_native_to_unicode(token)] = int(count)
return ret
|
[
"def",
"vocab_token_counts",
"(",
"text_filepattern",
",",
"max_lines",
")",
":",
"ret",
"=",
"{",
"}",
"for",
"i",
",",
"line",
"in",
"enumerate",
"(",
"_read_filepattern",
"(",
"text_filepattern",
",",
"max_lines",
"=",
"max_lines",
")",
")",
":",
"if",
"\",\"",
"not",
"in",
"line",
":",
"tf",
".",
"logging",
".",
"warning",
"(",
"\"Malformed vocab line #%d '%s'\"",
",",
"i",
",",
"line",
")",
"continue",
"token",
",",
"count",
"=",
"line",
".",
"rsplit",
"(",
"\",\"",
",",
"1",
")",
"ret",
"[",
"_native_to_unicode",
"(",
"token",
")",
"]",
"=",
"int",
"(",
"count",
")",
"return",
"ret"
] |
Read a vocab file and return a dictionary of token counts.
Reads a two-column CSV file of tokens and their frequency in a dataset. The
tokens are presumed to be generated by encode() or the equivalent.
Args:
text_filepattern: A pattern matching one or more files.
max_lines: An integer; maximum total lines to read.
Returns:
a dictionary mapping token to count.
|
[
"Read",
"a",
"vocab",
"file",
"and",
"return",
"a",
"dictionary",
"of",
"token",
"counts",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/tokenizer.py#L174-L197
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/serving/serving_utils.py
|
_make_example
|
def _make_example(input_ids, problem, input_feature_name="inputs"):
"""Make a tf.train.Example for the problem.
features[input_feature_name] = input_ids
Also fills in any other required features with dummy values.
Args:
input_ids: list<int>.
problem: Problem.
input_feature_name: name of feature for input_ids.
Returns:
tf.train.Example
"""
features = {
input_feature_name:
tf.train.Feature(int64_list=tf.train.Int64List(value=input_ids))
}
# Fill in dummy values for any other required features that presumably
# will not actually be used for prediction.
data_fields, _ = problem.example_reading_spec()
for fname, ftype in data_fields.items():
if fname == input_feature_name:
continue
if not isinstance(ftype, tf.FixedLenFeature):
# Only FixedLenFeatures are required
continue
if ftype.default_value is not None:
# If there's a default value, no need to fill it in
continue
num_elements = functools.reduce(lambda acc, el: acc * el, ftype.shape, 1)
if ftype.dtype in [tf.int32, tf.int64]:
value = tf.train.Feature(
int64_list=tf.train.Int64List(value=[0] * num_elements))
if ftype.dtype in [tf.float32, tf.float64]:
value = tf.train.Feature(
float_list=tf.train.FloatList(value=[0.] * num_elements))
if ftype.dtype == tf.bytes:
value = tf.train.Feature(
bytes_list=tf.train.BytesList(value=[""] * num_elements))
tf.logging.info("Adding dummy value for feature %s as it is required by "
"the Problem.", fname)
features[fname] = value
return tf.train.Example(features=tf.train.Features(feature=features))
|
python
|
def _make_example(input_ids, problem, input_feature_name="inputs"):
"""Make a tf.train.Example for the problem.
features[input_feature_name] = input_ids
Also fills in any other required features with dummy values.
Args:
input_ids: list<int>.
problem: Problem.
input_feature_name: name of feature for input_ids.
Returns:
tf.train.Example
"""
features = {
input_feature_name:
tf.train.Feature(int64_list=tf.train.Int64List(value=input_ids))
}
# Fill in dummy values for any other required features that presumably
# will not actually be used for prediction.
data_fields, _ = problem.example_reading_spec()
for fname, ftype in data_fields.items():
if fname == input_feature_name:
continue
if not isinstance(ftype, tf.FixedLenFeature):
# Only FixedLenFeatures are required
continue
if ftype.default_value is not None:
# If there's a default value, no need to fill it in
continue
num_elements = functools.reduce(lambda acc, el: acc * el, ftype.shape, 1)
if ftype.dtype in [tf.int32, tf.int64]:
value = tf.train.Feature(
int64_list=tf.train.Int64List(value=[0] * num_elements))
if ftype.dtype in [tf.float32, tf.float64]:
value = tf.train.Feature(
float_list=tf.train.FloatList(value=[0.] * num_elements))
if ftype.dtype == tf.bytes:
value = tf.train.Feature(
bytes_list=tf.train.BytesList(value=[""] * num_elements))
tf.logging.info("Adding dummy value for feature %s as it is required by "
"the Problem.", fname)
features[fname] = value
return tf.train.Example(features=tf.train.Features(feature=features))
|
[
"def",
"_make_example",
"(",
"input_ids",
",",
"problem",
",",
"input_feature_name",
"=",
"\"inputs\"",
")",
":",
"features",
"=",
"{",
"input_feature_name",
":",
"tf",
".",
"train",
".",
"Feature",
"(",
"int64_list",
"=",
"tf",
".",
"train",
".",
"Int64List",
"(",
"value",
"=",
"input_ids",
")",
")",
"}",
"# Fill in dummy values for any other required features that presumably",
"# will not actually be used for prediction.",
"data_fields",
",",
"_",
"=",
"problem",
".",
"example_reading_spec",
"(",
")",
"for",
"fname",
",",
"ftype",
"in",
"data_fields",
".",
"items",
"(",
")",
":",
"if",
"fname",
"==",
"input_feature_name",
":",
"continue",
"if",
"not",
"isinstance",
"(",
"ftype",
",",
"tf",
".",
"FixedLenFeature",
")",
":",
"# Only FixedLenFeatures are required",
"continue",
"if",
"ftype",
".",
"default_value",
"is",
"not",
"None",
":",
"# If there's a default value, no need to fill it in",
"continue",
"num_elements",
"=",
"functools",
".",
"reduce",
"(",
"lambda",
"acc",
",",
"el",
":",
"acc",
"*",
"el",
",",
"ftype",
".",
"shape",
",",
"1",
")",
"if",
"ftype",
".",
"dtype",
"in",
"[",
"tf",
".",
"int32",
",",
"tf",
".",
"int64",
"]",
":",
"value",
"=",
"tf",
".",
"train",
".",
"Feature",
"(",
"int64_list",
"=",
"tf",
".",
"train",
".",
"Int64List",
"(",
"value",
"=",
"[",
"0",
"]",
"*",
"num_elements",
")",
")",
"if",
"ftype",
".",
"dtype",
"in",
"[",
"tf",
".",
"float32",
",",
"tf",
".",
"float64",
"]",
":",
"value",
"=",
"tf",
".",
"train",
".",
"Feature",
"(",
"float_list",
"=",
"tf",
".",
"train",
".",
"FloatList",
"(",
"value",
"=",
"[",
"0.",
"]",
"*",
"num_elements",
")",
")",
"if",
"ftype",
".",
"dtype",
"==",
"tf",
".",
"bytes",
":",
"value",
"=",
"tf",
".",
"train",
".",
"Feature",
"(",
"bytes_list",
"=",
"tf",
".",
"train",
".",
"BytesList",
"(",
"value",
"=",
"[",
"\"\"",
"]",
"*",
"num_elements",
")",
")",
"tf",
".",
"logging",
".",
"info",
"(",
"\"Adding dummy value for feature %s as it is required by \"",
"\"the Problem.\"",
",",
"fname",
")",
"features",
"[",
"fname",
"]",
"=",
"value",
"return",
"tf",
".",
"train",
".",
"Example",
"(",
"features",
"=",
"tf",
".",
"train",
".",
"Features",
"(",
"feature",
"=",
"features",
")",
")"
] |
Make a tf.train.Example for the problem.
features[input_feature_name] = input_ids
Also fills in any other required features with dummy values.
Args:
input_ids: list<int>.
problem: Problem.
input_feature_name: name of feature for input_ids.
Returns:
tf.train.Example
|
[
"Make",
"a",
"tf",
".",
"train",
".",
"Example",
"for",
"the",
"problem",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/serving/serving_utils.py#L36-L81
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/serving/serving_utils.py
|
make_grpc_request_fn
|
def make_grpc_request_fn(servable_name, server, timeout_secs):
"""Wraps function to make grpc requests with runtime args."""
stub = _create_stub(server)
def _make_grpc_request(examples):
"""Builds and sends request to TensorFlow model server."""
request = predict_pb2.PredictRequest()
request.model_spec.name = servable_name
request.inputs["input"].CopyFrom(
tf.make_tensor_proto(
[ex.SerializeToString() for ex in examples], shape=[len(examples)]))
response = stub.Predict(request, timeout_secs)
outputs = tf.make_ndarray(response.outputs["outputs"])
scores = tf.make_ndarray(response.outputs["scores"])
assert len(outputs) == len(scores)
return [{ # pylint: disable=g-complex-comprehension
"outputs": output,
"scores": score
} for output, score in zip(outputs, scores)]
return _make_grpc_request
|
python
|
def make_grpc_request_fn(servable_name, server, timeout_secs):
"""Wraps function to make grpc requests with runtime args."""
stub = _create_stub(server)
def _make_grpc_request(examples):
"""Builds and sends request to TensorFlow model server."""
request = predict_pb2.PredictRequest()
request.model_spec.name = servable_name
request.inputs["input"].CopyFrom(
tf.make_tensor_proto(
[ex.SerializeToString() for ex in examples], shape=[len(examples)]))
response = stub.Predict(request, timeout_secs)
outputs = tf.make_ndarray(response.outputs["outputs"])
scores = tf.make_ndarray(response.outputs["scores"])
assert len(outputs) == len(scores)
return [{ # pylint: disable=g-complex-comprehension
"outputs": output,
"scores": score
} for output, score in zip(outputs, scores)]
return _make_grpc_request
|
[
"def",
"make_grpc_request_fn",
"(",
"servable_name",
",",
"server",
",",
"timeout_secs",
")",
":",
"stub",
"=",
"_create_stub",
"(",
"server",
")",
"def",
"_make_grpc_request",
"(",
"examples",
")",
":",
"\"\"\"Builds and sends request to TensorFlow model server.\"\"\"",
"request",
"=",
"predict_pb2",
".",
"PredictRequest",
"(",
")",
"request",
".",
"model_spec",
".",
"name",
"=",
"servable_name",
"request",
".",
"inputs",
"[",
"\"input\"",
"]",
".",
"CopyFrom",
"(",
"tf",
".",
"make_tensor_proto",
"(",
"[",
"ex",
".",
"SerializeToString",
"(",
")",
"for",
"ex",
"in",
"examples",
"]",
",",
"shape",
"=",
"[",
"len",
"(",
"examples",
")",
"]",
")",
")",
"response",
"=",
"stub",
".",
"Predict",
"(",
"request",
",",
"timeout_secs",
")",
"outputs",
"=",
"tf",
".",
"make_ndarray",
"(",
"response",
".",
"outputs",
"[",
"\"outputs\"",
"]",
")",
"scores",
"=",
"tf",
".",
"make_ndarray",
"(",
"response",
".",
"outputs",
"[",
"\"scores\"",
"]",
")",
"assert",
"len",
"(",
"outputs",
")",
"==",
"len",
"(",
"scores",
")",
"return",
"[",
"{",
"# pylint: disable=g-complex-comprehension",
"\"outputs\"",
":",
"output",
",",
"\"scores\"",
":",
"score",
"}",
"for",
"output",
",",
"score",
"in",
"zip",
"(",
"outputs",
",",
"scores",
")",
"]",
"return",
"_make_grpc_request"
] |
Wraps function to make grpc requests with runtime args.
|
[
"Wraps",
"function",
"to",
"make",
"grpc",
"requests",
"with",
"runtime",
"args",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/serving/serving_utils.py#L105-L125
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/serving/serving_utils.py
|
make_cloud_mlengine_request_fn
|
def make_cloud_mlengine_request_fn(credentials, model_name, version):
"""Wraps function to make CloudML Engine requests with runtime args."""
def _make_cloud_mlengine_request(examples):
"""Builds and sends requests to Cloud ML Engine."""
api = discovery.build("ml", "v1", credentials=credentials)
parent = "projects/%s/models/%s/versions/%s" % (cloud.default_project(),
model_name, version)
input_data = {
"instances": [{ # pylint: disable=g-complex-comprehension
"input": {
"b64": base64.b64encode(ex.SerializeToString())
}
} for ex in examples]
}
prediction = api.projects().predict(body=input_data, name=parent).execute()
return prediction["predictions"]
return _make_cloud_mlengine_request
|
python
|
def make_cloud_mlengine_request_fn(credentials, model_name, version):
"""Wraps function to make CloudML Engine requests with runtime args."""
def _make_cloud_mlengine_request(examples):
"""Builds and sends requests to Cloud ML Engine."""
api = discovery.build("ml", "v1", credentials=credentials)
parent = "projects/%s/models/%s/versions/%s" % (cloud.default_project(),
model_name, version)
input_data = {
"instances": [{ # pylint: disable=g-complex-comprehension
"input": {
"b64": base64.b64encode(ex.SerializeToString())
}
} for ex in examples]
}
prediction = api.projects().predict(body=input_data, name=parent).execute()
return prediction["predictions"]
return _make_cloud_mlengine_request
|
[
"def",
"make_cloud_mlengine_request_fn",
"(",
"credentials",
",",
"model_name",
",",
"version",
")",
":",
"def",
"_make_cloud_mlengine_request",
"(",
"examples",
")",
":",
"\"\"\"Builds and sends requests to Cloud ML Engine.\"\"\"",
"api",
"=",
"discovery",
".",
"build",
"(",
"\"ml\"",
",",
"\"v1\"",
",",
"credentials",
"=",
"credentials",
")",
"parent",
"=",
"\"projects/%s/models/%s/versions/%s\"",
"%",
"(",
"cloud",
".",
"default_project",
"(",
")",
",",
"model_name",
",",
"version",
")",
"input_data",
"=",
"{",
"\"instances\"",
":",
"[",
"{",
"# pylint: disable=g-complex-comprehension",
"\"input\"",
":",
"{",
"\"b64\"",
":",
"base64",
".",
"b64encode",
"(",
"ex",
".",
"SerializeToString",
"(",
")",
")",
"}",
"}",
"for",
"ex",
"in",
"examples",
"]",
"}",
"prediction",
"=",
"api",
".",
"projects",
"(",
")",
".",
"predict",
"(",
"body",
"=",
"input_data",
",",
"name",
"=",
"parent",
")",
".",
"execute",
"(",
")",
"return",
"prediction",
"[",
"\"predictions\"",
"]",
"return",
"_make_cloud_mlengine_request"
] |
Wraps function to make CloudML Engine requests with runtime args.
|
[
"Wraps",
"function",
"to",
"make",
"CloudML",
"Engine",
"requests",
"with",
"runtime",
"args",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/serving/serving_utils.py#L128-L146
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/serving/serving_utils.py
|
predict
|
def predict(inputs_list, problem, request_fn):
"""Encodes inputs, makes request to deployed TF model, and decodes outputs."""
assert isinstance(inputs_list, list)
fname = "inputs" if problem.has_inputs else "targets"
input_encoder = problem.feature_info[fname].encoder
input_ids_list = [
_encode(inputs, input_encoder, add_eos=problem.has_inputs)
for inputs in inputs_list
]
examples = [_make_example(input_ids, problem, fname)
for input_ids in input_ids_list]
predictions = request_fn(examples)
output_decoder = problem.feature_info["targets"].encoder
outputs = [
(_decode(prediction["outputs"], output_decoder),
prediction["scores"])
for prediction in predictions
]
return outputs
|
python
|
def predict(inputs_list, problem, request_fn):
"""Encodes inputs, makes request to deployed TF model, and decodes outputs."""
assert isinstance(inputs_list, list)
fname = "inputs" if problem.has_inputs else "targets"
input_encoder = problem.feature_info[fname].encoder
input_ids_list = [
_encode(inputs, input_encoder, add_eos=problem.has_inputs)
for inputs in inputs_list
]
examples = [_make_example(input_ids, problem, fname)
for input_ids in input_ids_list]
predictions = request_fn(examples)
output_decoder = problem.feature_info["targets"].encoder
outputs = [
(_decode(prediction["outputs"], output_decoder),
prediction["scores"])
for prediction in predictions
]
return outputs
|
[
"def",
"predict",
"(",
"inputs_list",
",",
"problem",
",",
"request_fn",
")",
":",
"assert",
"isinstance",
"(",
"inputs_list",
",",
"list",
")",
"fname",
"=",
"\"inputs\"",
"if",
"problem",
".",
"has_inputs",
"else",
"\"targets\"",
"input_encoder",
"=",
"problem",
".",
"feature_info",
"[",
"fname",
"]",
".",
"encoder",
"input_ids_list",
"=",
"[",
"_encode",
"(",
"inputs",
",",
"input_encoder",
",",
"add_eos",
"=",
"problem",
".",
"has_inputs",
")",
"for",
"inputs",
"in",
"inputs_list",
"]",
"examples",
"=",
"[",
"_make_example",
"(",
"input_ids",
",",
"problem",
",",
"fname",
")",
"for",
"input_ids",
"in",
"input_ids_list",
"]",
"predictions",
"=",
"request_fn",
"(",
"examples",
")",
"output_decoder",
"=",
"problem",
".",
"feature_info",
"[",
"\"targets\"",
"]",
".",
"encoder",
"outputs",
"=",
"[",
"(",
"_decode",
"(",
"prediction",
"[",
"\"outputs\"",
"]",
",",
"output_decoder",
")",
",",
"prediction",
"[",
"\"scores\"",
"]",
")",
"for",
"prediction",
"in",
"predictions",
"]",
"return",
"outputs"
] |
Encodes inputs, makes request to deployed TF model, and decodes outputs.
|
[
"Encodes",
"inputs",
"makes",
"request",
"to",
"deployed",
"TF",
"model",
"and",
"decodes",
"outputs",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/serving/serving_utils.py#L149-L167
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/video/basic_recurrent.py
|
next_frame_basic_recurrent
|
def next_frame_basic_recurrent():
"""Basic 2-frame recurrent model with stochastic tower."""
hparams = basic_stochastic.next_frame_basic_stochastic_discrete()
hparams.filter_double_steps = 2
hparams.hidden_size = 64
hparams.video_num_input_frames = 4
hparams.video_num_target_frames = 4
hparams.concat_internal_states = False
hparams.add_hparam("num_lstm_layers", 2)
hparams.add_hparam("num_lstm_filters", 256)
return hparams
|
python
|
def next_frame_basic_recurrent():
"""Basic 2-frame recurrent model with stochastic tower."""
hparams = basic_stochastic.next_frame_basic_stochastic_discrete()
hparams.filter_double_steps = 2
hparams.hidden_size = 64
hparams.video_num_input_frames = 4
hparams.video_num_target_frames = 4
hparams.concat_internal_states = False
hparams.add_hparam("num_lstm_layers", 2)
hparams.add_hparam("num_lstm_filters", 256)
return hparams
|
[
"def",
"next_frame_basic_recurrent",
"(",
")",
":",
"hparams",
"=",
"basic_stochastic",
".",
"next_frame_basic_stochastic_discrete",
"(",
")",
"hparams",
".",
"filter_double_steps",
"=",
"2",
"hparams",
".",
"hidden_size",
"=",
"64",
"hparams",
".",
"video_num_input_frames",
"=",
"4",
"hparams",
".",
"video_num_target_frames",
"=",
"4",
"hparams",
".",
"concat_internal_states",
"=",
"False",
"hparams",
".",
"add_hparam",
"(",
"\"num_lstm_layers\"",
",",
"2",
")",
"hparams",
".",
"add_hparam",
"(",
"\"num_lstm_filters\"",
",",
"256",
")",
"return",
"hparams"
] |
Basic 2-frame recurrent model with stochastic tower.
|
[
"Basic",
"2",
"-",
"frame",
"recurrent",
"model",
"with",
"stochastic",
"tower",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/video/basic_recurrent.py#L52-L62
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/bin/t2t_distill.py
|
create_teacher_experiment
|
def create_teacher_experiment(run_config, hparams, argv):
"""Creates experiment function."""
tf.logging.info("training teacher")
tf.logging.set_verbosity(tf.logging.INFO)
trainer_lib.set_random_seed(FLAGS.random_seed)
usr_dir.import_usr_dir(FLAGS.t2t_usr_dir)
t2t_trainer.maybe_log_registry_and_exit()
if FLAGS.cloud_mlengine:
return cloud_mlengine.launch()
if FLAGS.generate_data:
t2t_trainer.generate_data()
if cloud_mlengine.job_dir():
FLAGS.output_dir = cloud_mlengine.job_dir()
if argv:
t2t_trainer.set_hparams_from_args(argv[1:])
hparams.distill_phase = "train"
exp_fn = t2t_trainer.create_experiment_fn()
exp = exp_fn(run_config, hparams)
return exp
|
python
|
def create_teacher_experiment(run_config, hparams, argv):
"""Creates experiment function."""
tf.logging.info("training teacher")
tf.logging.set_verbosity(tf.logging.INFO)
trainer_lib.set_random_seed(FLAGS.random_seed)
usr_dir.import_usr_dir(FLAGS.t2t_usr_dir)
t2t_trainer.maybe_log_registry_and_exit()
if FLAGS.cloud_mlengine:
return cloud_mlengine.launch()
if FLAGS.generate_data:
t2t_trainer.generate_data()
if cloud_mlengine.job_dir():
FLAGS.output_dir = cloud_mlengine.job_dir()
if argv:
t2t_trainer.set_hparams_from_args(argv[1:])
hparams.distill_phase = "train"
exp_fn = t2t_trainer.create_experiment_fn()
exp = exp_fn(run_config, hparams)
return exp
|
[
"def",
"create_teacher_experiment",
"(",
"run_config",
",",
"hparams",
",",
"argv",
")",
":",
"tf",
".",
"logging",
".",
"info",
"(",
"\"training teacher\"",
")",
"tf",
".",
"logging",
".",
"set_verbosity",
"(",
"tf",
".",
"logging",
".",
"INFO",
")",
"trainer_lib",
".",
"set_random_seed",
"(",
"FLAGS",
".",
"random_seed",
")",
"usr_dir",
".",
"import_usr_dir",
"(",
"FLAGS",
".",
"t2t_usr_dir",
")",
"t2t_trainer",
".",
"maybe_log_registry_and_exit",
"(",
")",
"if",
"FLAGS",
".",
"cloud_mlengine",
":",
"return",
"cloud_mlengine",
".",
"launch",
"(",
")",
"if",
"FLAGS",
".",
"generate_data",
":",
"t2t_trainer",
".",
"generate_data",
"(",
")",
"if",
"cloud_mlengine",
".",
"job_dir",
"(",
")",
":",
"FLAGS",
".",
"output_dir",
"=",
"cloud_mlengine",
".",
"job_dir",
"(",
")",
"if",
"argv",
":",
"t2t_trainer",
".",
"set_hparams_from_args",
"(",
"argv",
"[",
"1",
":",
"]",
")",
"hparams",
".",
"distill_phase",
"=",
"\"train\"",
"exp_fn",
"=",
"t2t_trainer",
".",
"create_experiment_fn",
"(",
")",
"exp",
"=",
"exp_fn",
"(",
"run_config",
",",
"hparams",
")",
"return",
"exp"
] |
Creates experiment function.
|
[
"Creates",
"experiment",
"function",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/bin/t2t_distill.py#L91-L114
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/ice_parsing.py
|
tabbed_parsing_token_generator
|
def tabbed_parsing_token_generator(data_dir, tmp_dir, train, prefix,
source_vocab_size, target_vocab_size):
"""Generate source and target data from a single file."""
filename = "parsing_{0}.pairs".format("train" if train else "dev")
source_vocab = generator_utils.get_or_generate_tabbed_vocab(
data_dir, tmp_dir, filename, 0,
prefix + "_source.tokens.vocab.%d" % source_vocab_size, source_vocab_size)
target_vocab = generator_utils.get_or_generate_tabbed_vocab(
data_dir, tmp_dir, filename, 1,
prefix + "_target.tokens.vocab.%d" % target_vocab_size, target_vocab_size)
pair_filepath = os.path.join(tmp_dir, filename)
return text_problems.text2text_generate_encoded(
text_problems.text2text_txt_tab_iterator(pair_filepath), source_vocab,
target_vocab)
|
python
|
def tabbed_parsing_token_generator(data_dir, tmp_dir, train, prefix,
source_vocab_size, target_vocab_size):
"""Generate source and target data from a single file."""
filename = "parsing_{0}.pairs".format("train" if train else "dev")
source_vocab = generator_utils.get_or_generate_tabbed_vocab(
data_dir, tmp_dir, filename, 0,
prefix + "_source.tokens.vocab.%d" % source_vocab_size, source_vocab_size)
target_vocab = generator_utils.get_or_generate_tabbed_vocab(
data_dir, tmp_dir, filename, 1,
prefix + "_target.tokens.vocab.%d" % target_vocab_size, target_vocab_size)
pair_filepath = os.path.join(tmp_dir, filename)
return text_problems.text2text_generate_encoded(
text_problems.text2text_txt_tab_iterator(pair_filepath), source_vocab,
target_vocab)
|
[
"def",
"tabbed_parsing_token_generator",
"(",
"data_dir",
",",
"tmp_dir",
",",
"train",
",",
"prefix",
",",
"source_vocab_size",
",",
"target_vocab_size",
")",
":",
"filename",
"=",
"\"parsing_{0}.pairs\"",
".",
"format",
"(",
"\"train\"",
"if",
"train",
"else",
"\"dev\"",
")",
"source_vocab",
"=",
"generator_utils",
".",
"get_or_generate_tabbed_vocab",
"(",
"data_dir",
",",
"tmp_dir",
",",
"filename",
",",
"0",
",",
"prefix",
"+",
"\"_source.tokens.vocab.%d\"",
"%",
"source_vocab_size",
",",
"source_vocab_size",
")",
"target_vocab",
"=",
"generator_utils",
".",
"get_or_generate_tabbed_vocab",
"(",
"data_dir",
",",
"tmp_dir",
",",
"filename",
",",
"1",
",",
"prefix",
"+",
"\"_target.tokens.vocab.%d\"",
"%",
"target_vocab_size",
",",
"target_vocab_size",
")",
"pair_filepath",
"=",
"os",
".",
"path",
".",
"join",
"(",
"tmp_dir",
",",
"filename",
")",
"return",
"text_problems",
".",
"text2text_generate_encoded",
"(",
"text_problems",
".",
"text2text_txt_tab_iterator",
"(",
"pair_filepath",
")",
",",
"source_vocab",
",",
"target_vocab",
")"
] |
Generate source and target data from a single file.
|
[
"Generate",
"source",
"and",
"target",
"data",
"from",
"a",
"single",
"file",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/ice_parsing.py#L37-L50
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/ice_parsing.py
|
tabbed_parsing_character_generator
|
def tabbed_parsing_character_generator(tmp_dir, train):
"""Generate source and target data from a single file."""
character_vocab = text_encoder.ByteTextEncoder()
filename = "parsing_{0}.pairs".format("train" if train else "dev")
pair_filepath = os.path.join(tmp_dir, filename)
return text_problems.text2text_generate_encoded(
text_problems.text2text_txt_tab_iterator(pair_filepath), character_vocab)
|
python
|
def tabbed_parsing_character_generator(tmp_dir, train):
"""Generate source and target data from a single file."""
character_vocab = text_encoder.ByteTextEncoder()
filename = "parsing_{0}.pairs".format("train" if train else "dev")
pair_filepath = os.path.join(tmp_dir, filename)
return text_problems.text2text_generate_encoded(
text_problems.text2text_txt_tab_iterator(pair_filepath), character_vocab)
|
[
"def",
"tabbed_parsing_character_generator",
"(",
"tmp_dir",
",",
"train",
")",
":",
"character_vocab",
"=",
"text_encoder",
".",
"ByteTextEncoder",
"(",
")",
"filename",
"=",
"\"parsing_{0}.pairs\"",
".",
"format",
"(",
"\"train\"",
"if",
"train",
"else",
"\"dev\"",
")",
"pair_filepath",
"=",
"os",
".",
"path",
".",
"join",
"(",
"tmp_dir",
",",
"filename",
")",
"return",
"text_problems",
".",
"text2text_generate_encoded",
"(",
"text_problems",
".",
"text2text_txt_tab_iterator",
"(",
"pair_filepath",
")",
",",
"character_vocab",
")"
] |
Generate source and target data from a single file.
|
[
"Generate",
"source",
"and",
"target",
"data",
"from",
"a",
"single",
"file",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/ice_parsing.py#L53-L59
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
_make_list
|
def _make_list(predictions, targets):
"""Helper: make predictions and targets lists, check they match on length."""
# Our models sometimes return predictions in lists, make it a list always.
# TODO(lukaszkaiser): make abstractions for nested structures and refactor.
if not isinstance(predictions, (list, tuple)):
if isinstance(targets, (list, tuple)):
raise ValueError("Targets are a list or tuple but predictions are not.")
predictions, targets = [predictions], [targets]
if len(predictions) != len(targets):
raise ValueError("Predictions and targets have different lengths.")
return list(predictions), list(targets)
|
python
|
def _make_list(predictions, targets):
"""Helper: make predictions and targets lists, check they match on length."""
# Our models sometimes return predictions in lists, make it a list always.
# TODO(lukaszkaiser): make abstractions for nested structures and refactor.
if not isinstance(predictions, (list, tuple)):
if isinstance(targets, (list, tuple)):
raise ValueError("Targets are a list or tuple but predictions are not.")
predictions, targets = [predictions], [targets]
if len(predictions) != len(targets):
raise ValueError("Predictions and targets have different lengths.")
return list(predictions), list(targets)
|
[
"def",
"_make_list",
"(",
"predictions",
",",
"targets",
")",
":",
"# Our models sometimes return predictions in lists, make it a list always.",
"# TODO(lukaszkaiser): make abstractions for nested structures and refactor.",
"if",
"not",
"isinstance",
"(",
"predictions",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"if",
"isinstance",
"(",
"targets",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"raise",
"ValueError",
"(",
"\"Targets are a list or tuple but predictions are not.\"",
")",
"predictions",
",",
"targets",
"=",
"[",
"predictions",
"]",
",",
"[",
"targets",
"]",
"if",
"len",
"(",
"predictions",
")",
"!=",
"len",
"(",
"targets",
")",
":",
"raise",
"ValueError",
"(",
"\"Predictions and targets have different lengths.\"",
")",
"return",
"list",
"(",
"predictions",
")",
",",
"list",
"(",
"targets",
")"
] |
Helper: make predictions and targets lists, check they match on length.
|
[
"Helper",
":",
"make",
"predictions",
"and",
"targets",
"lists",
"check",
"they",
"match",
"on",
"length",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L54-L64
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
masked_mean
|
def masked_mean(inputs, targets, mask_id=None):
"""Mean of the inputs but counting only those where targets != mask_id."""
inputs = [x.astype(np.float32) for x in inputs]
# We assume all elements in the list contribute equally.
# TODO(lukaszkaiser): remove this assumption (e.g., when masks differ).
length = len(inputs)
if mask_id is None:
# TODO(lukaszkaiser): can we just divide the sum by length? XLA optimizes?
return sum([np.mean(x) / length for x in inputs])
unmask = [1.0 - np.equal(t, mask_id).astype(np.float32) for t in targets]
return sum([np.sum(x * m) / (length * np.sum(m))
for x, m in zip(inputs, unmask)])
|
python
|
def masked_mean(inputs, targets, mask_id=None):
"""Mean of the inputs but counting only those where targets != mask_id."""
inputs = [x.astype(np.float32) for x in inputs]
# We assume all elements in the list contribute equally.
# TODO(lukaszkaiser): remove this assumption (e.g., when masks differ).
length = len(inputs)
if mask_id is None:
# TODO(lukaszkaiser): can we just divide the sum by length? XLA optimizes?
return sum([np.mean(x) / length for x in inputs])
unmask = [1.0 - np.equal(t, mask_id).astype(np.float32) for t in targets]
return sum([np.sum(x * m) / (length * np.sum(m))
for x, m in zip(inputs, unmask)])
|
[
"def",
"masked_mean",
"(",
"inputs",
",",
"targets",
",",
"mask_id",
"=",
"None",
")",
":",
"inputs",
"=",
"[",
"x",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"for",
"x",
"in",
"inputs",
"]",
"# We assume all elements in the list contribute equally.",
"# TODO(lukaszkaiser): remove this assumption (e.g., when masks differ).",
"length",
"=",
"len",
"(",
"inputs",
")",
"if",
"mask_id",
"is",
"None",
":",
"# TODO(lukaszkaiser): can we just divide the sum by length? XLA optimizes?",
"return",
"sum",
"(",
"[",
"np",
".",
"mean",
"(",
"x",
")",
"/",
"length",
"for",
"x",
"in",
"inputs",
"]",
")",
"unmask",
"=",
"[",
"1.0",
"-",
"np",
".",
"equal",
"(",
"t",
",",
"mask_id",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"for",
"t",
"in",
"targets",
"]",
"return",
"sum",
"(",
"[",
"np",
".",
"sum",
"(",
"x",
"*",
"m",
")",
"/",
"(",
"length",
"*",
"np",
".",
"sum",
"(",
"m",
")",
")",
"for",
"x",
",",
"m",
"in",
"zip",
"(",
"inputs",
",",
"unmask",
")",
"]",
")"
] |
Mean of the inputs but counting only those where targets != mask_id.
|
[
"Mean",
"of",
"the",
"inputs",
"but",
"counting",
"only",
"those",
"where",
"targets",
"!",
"=",
"mask_id",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L68-L79
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
accuracy
|
def accuracy(batch, model_predictions):
"""Calculate accuracy."""
_, targets = batch
model_predictions, targets = _make_list(model_predictions, targets)
correct = []
for (prediction, target) in zip(model_predictions, targets):
predicted_class = np.argmax(prediction, axis=-1)
correct.append(np.equal(predicted_class, target))
return masked_mean(correct, targets)
|
python
|
def accuracy(batch, model_predictions):
"""Calculate accuracy."""
_, targets = batch
model_predictions, targets = _make_list(model_predictions, targets)
correct = []
for (prediction, target) in zip(model_predictions, targets):
predicted_class = np.argmax(prediction, axis=-1)
correct.append(np.equal(predicted_class, target))
return masked_mean(correct, targets)
|
[
"def",
"accuracy",
"(",
"batch",
",",
"model_predictions",
")",
":",
"_",
",",
"targets",
"=",
"batch",
"model_predictions",
",",
"targets",
"=",
"_make_list",
"(",
"model_predictions",
",",
"targets",
")",
"correct",
"=",
"[",
"]",
"for",
"(",
"prediction",
",",
"target",
")",
"in",
"zip",
"(",
"model_predictions",
",",
"targets",
")",
":",
"predicted_class",
"=",
"np",
".",
"argmax",
"(",
"prediction",
",",
"axis",
"=",
"-",
"1",
")",
"correct",
".",
"append",
"(",
"np",
".",
"equal",
"(",
"predicted_class",
",",
"target",
")",
")",
"return",
"masked_mean",
"(",
"correct",
",",
"targets",
")"
] |
Calculate accuracy.
|
[
"Calculate",
"accuracy",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L82-L90
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
neg_log_perplexity
|
def neg_log_perplexity(batch, model_predictions):
"""Calculate negative log perplexity."""
_, targets = batch
model_predictions, targets = _make_list(model_predictions, targets)
xent = []
for (prediction, target) in zip(model_predictions, targets):
hot_target = layers.one_hot(target, prediction.shape[-1])
xent.append(np.sum(prediction * hot_target, axis=-1))
return masked_mean(xent, targets)
|
python
|
def neg_log_perplexity(batch, model_predictions):
"""Calculate negative log perplexity."""
_, targets = batch
model_predictions, targets = _make_list(model_predictions, targets)
xent = []
for (prediction, target) in zip(model_predictions, targets):
hot_target = layers.one_hot(target, prediction.shape[-1])
xent.append(np.sum(prediction * hot_target, axis=-1))
return masked_mean(xent, targets)
|
[
"def",
"neg_log_perplexity",
"(",
"batch",
",",
"model_predictions",
")",
":",
"_",
",",
"targets",
"=",
"batch",
"model_predictions",
",",
"targets",
"=",
"_make_list",
"(",
"model_predictions",
",",
"targets",
")",
"xent",
"=",
"[",
"]",
"for",
"(",
"prediction",
",",
"target",
")",
"in",
"zip",
"(",
"model_predictions",
",",
"targets",
")",
":",
"hot_target",
"=",
"layers",
".",
"one_hot",
"(",
"target",
",",
"prediction",
".",
"shape",
"[",
"-",
"1",
"]",
")",
"xent",
".",
"append",
"(",
"np",
".",
"sum",
"(",
"prediction",
"*",
"hot_target",
",",
"axis",
"=",
"-",
"1",
")",
")",
"return",
"masked_mean",
"(",
"xent",
",",
"targets",
")"
] |
Calculate negative log perplexity.
|
[
"Calculate",
"negative",
"log",
"perplexity",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L93-L101
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
loss
|
def loss(params, batch, model_predict, rng):
"""Calculate loss."""
inputs, targets = batch
predictions = model_predict(inputs, params, rng=rng)
predictions, targets = _make_list(predictions, targets)
xent = []
for (pred, target) in zip(predictions, targets):
xent.append(np.sum(pred * layers.one_hot(target, pred.shape[-1]), axis=-1))
return - masked_mean(xent, targets)
|
python
|
def loss(params, batch, model_predict, rng):
"""Calculate loss."""
inputs, targets = batch
predictions = model_predict(inputs, params, rng=rng)
predictions, targets = _make_list(predictions, targets)
xent = []
for (pred, target) in zip(predictions, targets):
xent.append(np.sum(pred * layers.one_hot(target, pred.shape[-1]), axis=-1))
return - masked_mean(xent, targets)
|
[
"def",
"loss",
"(",
"params",
",",
"batch",
",",
"model_predict",
",",
"rng",
")",
":",
"inputs",
",",
"targets",
"=",
"batch",
"predictions",
"=",
"model_predict",
"(",
"inputs",
",",
"params",
",",
"rng",
"=",
"rng",
")",
"predictions",
",",
"targets",
"=",
"_make_list",
"(",
"predictions",
",",
"targets",
")",
"xent",
"=",
"[",
"]",
"for",
"(",
"pred",
",",
"target",
")",
"in",
"zip",
"(",
"predictions",
",",
"targets",
")",
":",
"xent",
".",
"append",
"(",
"np",
".",
"sum",
"(",
"pred",
"*",
"layers",
".",
"one_hot",
"(",
"target",
",",
"pred",
".",
"shape",
"[",
"-",
"1",
"]",
")",
",",
"axis",
"=",
"-",
"1",
")",
")",
"return",
"-",
"masked_mean",
"(",
"xent",
",",
"targets",
")"
] |
Calculate loss.
|
[
"Calculate",
"loss",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L104-L112
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
restore_state
|
def restore_state(output_dir):
"""Restore State."""
params_file = os.path.join(output_dir, "model.pkl")
if not gfile.exists(params_file):
return State(step=None, params=None, history=trax_history.History())
with gfile.GFile(params_file, "rb") as f:
(params, step, history) = pickle.load(f)
log("Model loaded from %s at step %d" % (params_file, step))
logging.debug("From loaded model : history = %s", history)
return State(step=step, params=params, history=history)
|
python
|
def restore_state(output_dir):
"""Restore State."""
params_file = os.path.join(output_dir, "model.pkl")
if not gfile.exists(params_file):
return State(step=None, params=None, history=trax_history.History())
with gfile.GFile(params_file, "rb") as f:
(params, step, history) = pickle.load(f)
log("Model loaded from %s at step %d" % (params_file, step))
logging.debug("From loaded model : history = %s", history)
return State(step=step, params=params, history=history)
|
[
"def",
"restore_state",
"(",
"output_dir",
")",
":",
"params_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"\"model.pkl\"",
")",
"if",
"not",
"gfile",
".",
"exists",
"(",
"params_file",
")",
":",
"return",
"State",
"(",
"step",
"=",
"None",
",",
"params",
"=",
"None",
",",
"history",
"=",
"trax_history",
".",
"History",
"(",
")",
")",
"with",
"gfile",
".",
"GFile",
"(",
"params_file",
",",
"\"rb\"",
")",
"as",
"f",
":",
"(",
"params",
",",
"step",
",",
"history",
")",
"=",
"pickle",
".",
"load",
"(",
"f",
")",
"log",
"(",
"\"Model loaded from %s at step %d\"",
"%",
"(",
"params_file",
",",
"step",
")",
")",
"logging",
".",
"debug",
"(",
"\"From loaded model : history = %s\"",
",",
"history",
")",
"return",
"State",
"(",
"step",
"=",
"step",
",",
"params",
"=",
"params",
",",
"history",
"=",
"history",
")"
] |
Restore State.
|
[
"Restore",
"State",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L129-L139
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
save_state
|
def save_state(state, output_dir, keep=False):
"""Save State and optionally gin config."""
params_file = os.path.join(output_dir, "model.pkl")
with gfile.GFile(params_file, "wb") as f:
pickle.dump((state.params, state.step, state.history), f)
if keep:
params_file = os.path.join(output_dir, "model_{}.pkl".format(state.step))
with gfile.GFile(params_file, "wb") as f:
pickle.dump((state.params, state.step, state.history), f)
log("Model saved to %s" % params_file, stdout=False)
|
python
|
def save_state(state, output_dir, keep=False):
"""Save State and optionally gin config."""
params_file = os.path.join(output_dir, "model.pkl")
with gfile.GFile(params_file, "wb") as f:
pickle.dump((state.params, state.step, state.history), f)
if keep:
params_file = os.path.join(output_dir, "model_{}.pkl".format(state.step))
with gfile.GFile(params_file, "wb") as f:
pickle.dump((state.params, state.step, state.history), f)
log("Model saved to %s" % params_file, stdout=False)
|
[
"def",
"save_state",
"(",
"state",
",",
"output_dir",
",",
"keep",
"=",
"False",
")",
":",
"params_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"\"model.pkl\"",
")",
"with",
"gfile",
".",
"GFile",
"(",
"params_file",
",",
"\"wb\"",
")",
"as",
"f",
":",
"pickle",
".",
"dump",
"(",
"(",
"state",
".",
"params",
",",
"state",
".",
"step",
",",
"state",
".",
"history",
")",
",",
"f",
")",
"if",
"keep",
":",
"params_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"\"model_{}.pkl\"",
".",
"format",
"(",
"state",
".",
"step",
")",
")",
"with",
"gfile",
".",
"GFile",
"(",
"params_file",
",",
"\"wb\"",
")",
"as",
"f",
":",
"pickle",
".",
"dump",
"(",
"(",
"state",
".",
"params",
",",
"state",
".",
"step",
",",
"state",
".",
"history",
")",
",",
"f",
")",
"log",
"(",
"\"Model saved to %s\"",
"%",
"params_file",
",",
"stdout",
"=",
"False",
")"
] |
Save State and optionally gin config.
|
[
"Save",
"State",
"and",
"optionally",
"gin",
"config",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L152-L161
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
evaluate_train_and_eval
|
def evaluate_train_and_eval(step, inputs, predict_fun, eval_steps, rng,
train_sw=None, eval_sw=None, history=None):
"""Evalaute on train and eval data, and log metrics."""
step_log(step, "Evaluation")
train_metrics, eval_metrics = [
evaluate( # pylint: disable=g-complex-comprehension
itertools.islice(input_stream(), eval_steps),
predict_fun,
_METRICS,
rng)
for input_stream in
[inputs.train_eval_stream, inputs.eval_stream]]
if train_sw:
log_metrics(train_metrics, train_sw, "train", step, history=history)
if eval_sw:
log_metrics(eval_metrics, eval_sw, "eval", step, history=history)
step_log(step, "Finished evaluation")
return train_metrics, eval_metrics
|
python
|
def evaluate_train_and_eval(step, inputs, predict_fun, eval_steps, rng,
train_sw=None, eval_sw=None, history=None):
"""Evalaute on train and eval data, and log metrics."""
step_log(step, "Evaluation")
train_metrics, eval_metrics = [
evaluate( # pylint: disable=g-complex-comprehension
itertools.islice(input_stream(), eval_steps),
predict_fun,
_METRICS,
rng)
for input_stream in
[inputs.train_eval_stream, inputs.eval_stream]]
if train_sw:
log_metrics(train_metrics, train_sw, "train", step, history=history)
if eval_sw:
log_metrics(eval_metrics, eval_sw, "eval", step, history=history)
step_log(step, "Finished evaluation")
return train_metrics, eval_metrics
|
[
"def",
"evaluate_train_and_eval",
"(",
"step",
",",
"inputs",
",",
"predict_fun",
",",
"eval_steps",
",",
"rng",
",",
"train_sw",
"=",
"None",
",",
"eval_sw",
"=",
"None",
",",
"history",
"=",
"None",
")",
":",
"step_log",
"(",
"step",
",",
"\"Evaluation\"",
")",
"train_metrics",
",",
"eval_metrics",
"=",
"[",
"evaluate",
"(",
"# pylint: disable=g-complex-comprehension",
"itertools",
".",
"islice",
"(",
"input_stream",
"(",
")",
",",
"eval_steps",
")",
",",
"predict_fun",
",",
"_METRICS",
",",
"rng",
")",
"for",
"input_stream",
"in",
"[",
"inputs",
".",
"train_eval_stream",
",",
"inputs",
".",
"eval_stream",
"]",
"]",
"if",
"train_sw",
":",
"log_metrics",
"(",
"train_metrics",
",",
"train_sw",
",",
"\"train\"",
",",
"step",
",",
"history",
"=",
"history",
")",
"if",
"eval_sw",
":",
"log_metrics",
"(",
"eval_metrics",
",",
"eval_sw",
",",
"\"eval\"",
",",
"step",
",",
"history",
"=",
"history",
")",
"step_log",
"(",
"step",
",",
"\"Finished evaluation\"",
")",
"return",
"train_metrics",
",",
"eval_metrics"
] |
Evalaute on train and eval data, and log metrics.
|
[
"Evalaute",
"on",
"train",
"and",
"eval",
"data",
"and",
"log",
"metrics",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L172-L189
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
evaluate
|
def evaluate(inputs_stream, predict_fun, metric_funs, rng):
"""Evaluate.
Args:
inputs_stream: iterable of inputs to evaluate on.
predict_fun: function from inputs to predictions. params should already be
partially applied.
metric_funs: dict from metric name to metric function, which takes inputs
and predictions and returns a scalar metric value.
rng: random number generator.
Returns:
metrics: dict from metric name to metric value averaged over the number of
inputs.
"""
metrics = collections.defaultdict(float)
count = 0
for inp in inputs_stream:
count += 1
rng, subrng = jax_random.split(rng)
preds = predict_fun(inp[0], rng=subrng)
for m, f in six.iteritems(metric_funs):
metrics[m] += f(inp, preds)
return {m: v / count for (m, v) in six.iteritems(metrics)}
|
python
|
def evaluate(inputs_stream, predict_fun, metric_funs, rng):
"""Evaluate.
Args:
inputs_stream: iterable of inputs to evaluate on.
predict_fun: function from inputs to predictions. params should already be
partially applied.
metric_funs: dict from metric name to metric function, which takes inputs
and predictions and returns a scalar metric value.
rng: random number generator.
Returns:
metrics: dict from metric name to metric value averaged over the number of
inputs.
"""
metrics = collections.defaultdict(float)
count = 0
for inp in inputs_stream:
count += 1
rng, subrng = jax_random.split(rng)
preds = predict_fun(inp[0], rng=subrng)
for m, f in six.iteritems(metric_funs):
metrics[m] += f(inp, preds)
return {m: v / count for (m, v) in six.iteritems(metrics)}
|
[
"def",
"evaluate",
"(",
"inputs_stream",
",",
"predict_fun",
",",
"metric_funs",
",",
"rng",
")",
":",
"metrics",
"=",
"collections",
".",
"defaultdict",
"(",
"float",
")",
"count",
"=",
"0",
"for",
"inp",
"in",
"inputs_stream",
":",
"count",
"+=",
"1",
"rng",
",",
"subrng",
"=",
"jax_random",
".",
"split",
"(",
"rng",
")",
"preds",
"=",
"predict_fun",
"(",
"inp",
"[",
"0",
"]",
",",
"rng",
"=",
"subrng",
")",
"for",
"m",
",",
"f",
"in",
"six",
".",
"iteritems",
"(",
"metric_funs",
")",
":",
"metrics",
"[",
"m",
"]",
"+=",
"f",
"(",
"inp",
",",
"preds",
")",
"return",
"{",
"m",
":",
"v",
"/",
"count",
"for",
"(",
"m",
",",
"v",
")",
"in",
"six",
".",
"iteritems",
"(",
"metrics",
")",
"}"
] |
Evaluate.
Args:
inputs_stream: iterable of inputs to evaluate on.
predict_fun: function from inputs to predictions. params should already be
partially applied.
metric_funs: dict from metric name to metric function, which takes inputs
and predictions and returns a scalar metric value.
rng: random number generator.
Returns:
metrics: dict from metric name to metric value averaged over the number of
inputs.
|
[
"Evaluate",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L192-L215
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
log_metrics
|
def log_metrics(metrics, summ_writer, log_prefix, step, history=None):
"""Log metrics to summary writer and history."""
rjust_len = max([len(name) for name in metrics])
for name, value in six.iteritems(metrics):
step_log(step, "%s %s | % .8f" % (
log_prefix.ljust(5), name.rjust(rjust_len), value))
full_name = "metrics/" + name
if history:
history.append(log_prefix, full_name, step, value)
if summ_writer:
summ_writer.scalar(full_name, value, step)
|
python
|
def log_metrics(metrics, summ_writer, log_prefix, step, history=None):
"""Log metrics to summary writer and history."""
rjust_len = max([len(name) for name in metrics])
for name, value in six.iteritems(metrics):
step_log(step, "%s %s | % .8f" % (
log_prefix.ljust(5), name.rjust(rjust_len), value))
full_name = "metrics/" + name
if history:
history.append(log_prefix, full_name, step, value)
if summ_writer:
summ_writer.scalar(full_name, value, step)
|
[
"def",
"log_metrics",
"(",
"metrics",
",",
"summ_writer",
",",
"log_prefix",
",",
"step",
",",
"history",
"=",
"None",
")",
":",
"rjust_len",
"=",
"max",
"(",
"[",
"len",
"(",
"name",
")",
"for",
"name",
"in",
"metrics",
"]",
")",
"for",
"name",
",",
"value",
"in",
"six",
".",
"iteritems",
"(",
"metrics",
")",
":",
"step_log",
"(",
"step",
",",
"\"%s %s | % .8f\"",
"%",
"(",
"log_prefix",
".",
"ljust",
"(",
"5",
")",
",",
"name",
".",
"rjust",
"(",
"rjust_len",
")",
",",
"value",
")",
")",
"full_name",
"=",
"\"metrics/\"",
"+",
"name",
"if",
"history",
":",
"history",
".",
"append",
"(",
"log_prefix",
",",
"full_name",
",",
"step",
",",
"value",
")",
"if",
"summ_writer",
":",
"summ_writer",
".",
"scalar",
"(",
"full_name",
",",
"value",
",",
"step",
")"
] |
Log metrics to summary writer and history.
|
[
"Log",
"metrics",
"to",
"summary",
"writer",
"and",
"history",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L218-L228
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
get_random_number_generator_and_set_seed
|
def get_random_number_generator_and_set_seed(seed=None):
"""Get a JAX random number generator and set random seed everywhere."""
random.seed(seed)
# While python random accepts None as seed and uses time/os seed then,
# some other functions expect integers so we create one here.
if seed is None:
seed = random.randint(0, 2**31 - 1)
tf.set_random_seed(seed)
numpy.random.seed(seed)
return jax_random.get_prng(seed)
|
python
|
def get_random_number_generator_and_set_seed(seed=None):
"""Get a JAX random number generator and set random seed everywhere."""
random.seed(seed)
# While python random accepts None as seed and uses time/os seed then,
# some other functions expect integers so we create one here.
if seed is None:
seed = random.randint(0, 2**31 - 1)
tf.set_random_seed(seed)
numpy.random.seed(seed)
return jax_random.get_prng(seed)
|
[
"def",
"get_random_number_generator_and_set_seed",
"(",
"seed",
"=",
"None",
")",
":",
"random",
".",
"seed",
"(",
"seed",
")",
"# While python random accepts None as seed and uses time/os seed then,",
"# some other functions expect integers so we create one here.",
"if",
"seed",
"is",
"None",
":",
"seed",
"=",
"random",
".",
"randint",
"(",
"0",
",",
"2",
"**",
"31",
"-",
"1",
")",
"tf",
".",
"set_random_seed",
"(",
"seed",
")",
"numpy",
".",
"random",
".",
"seed",
"(",
"seed",
")",
"return",
"jax_random",
".",
"get_prng",
"(",
"seed",
")"
] |
Get a JAX random number generator and set random seed everywhere.
|
[
"Get",
"a",
"JAX",
"random",
"number",
"generator",
"and",
"set",
"random",
"seed",
"everywhere",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L231-L240
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
epochs
|
def epochs(steps=None, epoch_steps=1):
"""Iterator over epochs until steps is reached. 1-indexed.
Args:
steps: int, total number of steps. Infinite if None.
epoch_steps: int, number of steps per epoch. Can also be an iterable<int> to
enable variable length epochs.
Yields:
(epoch: int, epoch id, epoch_steps: int, number of steps in this epoch)
"""
try:
iter(epoch_steps)
except TypeError:
epoch_steps = itertools.repeat(epoch_steps)
step = 0
for epoch, epoch_steps in enumerate(epoch_steps):
epoch_steps = min(epoch_steps, steps - step)
yield (epoch + 1, epoch_steps)
step += epoch_steps
if steps and step >= steps:
break
|
python
|
def epochs(steps=None, epoch_steps=1):
"""Iterator over epochs until steps is reached. 1-indexed.
Args:
steps: int, total number of steps. Infinite if None.
epoch_steps: int, number of steps per epoch. Can also be an iterable<int> to
enable variable length epochs.
Yields:
(epoch: int, epoch id, epoch_steps: int, number of steps in this epoch)
"""
try:
iter(epoch_steps)
except TypeError:
epoch_steps = itertools.repeat(epoch_steps)
step = 0
for epoch, epoch_steps in enumerate(epoch_steps):
epoch_steps = min(epoch_steps, steps - step)
yield (epoch + 1, epoch_steps)
step += epoch_steps
if steps and step >= steps:
break
|
[
"def",
"epochs",
"(",
"steps",
"=",
"None",
",",
"epoch_steps",
"=",
"1",
")",
":",
"try",
":",
"iter",
"(",
"epoch_steps",
")",
"except",
"TypeError",
":",
"epoch_steps",
"=",
"itertools",
".",
"repeat",
"(",
"epoch_steps",
")",
"step",
"=",
"0",
"for",
"epoch",
",",
"epoch_steps",
"in",
"enumerate",
"(",
"epoch_steps",
")",
":",
"epoch_steps",
"=",
"min",
"(",
"epoch_steps",
",",
"steps",
"-",
"step",
")",
"yield",
"(",
"epoch",
"+",
"1",
",",
"epoch_steps",
")",
"step",
"+=",
"epoch_steps",
"if",
"steps",
"and",
"step",
">=",
"steps",
":",
"break"
] |
Iterator over epochs until steps is reached. 1-indexed.
Args:
steps: int, total number of steps. Infinite if None.
epoch_steps: int, number of steps per epoch. Can also be an iterable<int> to
enable variable length epochs.
Yields:
(epoch: int, epoch id, epoch_steps: int, number of steps in this epoch)
|
[
"Iterator",
"over",
"epochs",
"until",
"steps",
"is",
"reached",
".",
"1",
"-",
"indexed",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L255-L277
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
_jit_predict_fun
|
def _jit_predict_fun(model_predict, num_devices):
"""Use jit on model_predict if required."""
def predict(x, params=(), rng=None):
"""Predict function jited and parallelized as requested."""
# On one device, jit and run.
if num_devices == 1:
return backend.jit(model_predict)(x, params, rng=rng)
# Multi-devices, pmap and run.
@functools.partial(backend.pmap, axis_name="batch")
def mapped_predict(x, params, rng):
return model_predict(x, params, rng=rng)
pred = mapped_predict(
reshape_by_device(x, num_devices),
params,
jax_random.split(rng, num_devices))
# Need to reduce the [device, per-device-batch, ...] tensors back to
# a [batch, ...] tensor. The tensors may be nested.
if not isinstance(x, (list, tuple)): # Not nested.
batch_size = x.shape[0]
return np.reshape(pred, [batch_size] + list(pred.shape[2:]))
batch_size = x[0].shape[0]
return [np.reshape(p, [batch_size] + list(p.shape[2:])) for p in pred]
return predict
|
python
|
def _jit_predict_fun(model_predict, num_devices):
"""Use jit on model_predict if required."""
def predict(x, params=(), rng=None):
"""Predict function jited and parallelized as requested."""
# On one device, jit and run.
if num_devices == 1:
return backend.jit(model_predict)(x, params, rng=rng)
# Multi-devices, pmap and run.
@functools.partial(backend.pmap, axis_name="batch")
def mapped_predict(x, params, rng):
return model_predict(x, params, rng=rng)
pred = mapped_predict(
reshape_by_device(x, num_devices),
params,
jax_random.split(rng, num_devices))
# Need to reduce the [device, per-device-batch, ...] tensors back to
# a [batch, ...] tensor. The tensors may be nested.
if not isinstance(x, (list, tuple)): # Not nested.
batch_size = x.shape[0]
return np.reshape(pred, [batch_size] + list(pred.shape[2:]))
batch_size = x[0].shape[0]
return [np.reshape(p, [batch_size] + list(p.shape[2:])) for p in pred]
return predict
|
[
"def",
"_jit_predict_fun",
"(",
"model_predict",
",",
"num_devices",
")",
":",
"def",
"predict",
"(",
"x",
",",
"params",
"=",
"(",
")",
",",
"rng",
"=",
"None",
")",
":",
"\"\"\"Predict function jited and parallelized as requested.\"\"\"",
"# On one device, jit and run.",
"if",
"num_devices",
"==",
"1",
":",
"return",
"backend",
".",
"jit",
"(",
"model_predict",
")",
"(",
"x",
",",
"params",
",",
"rng",
"=",
"rng",
")",
"# Multi-devices, pmap and run.",
"@",
"functools",
".",
"partial",
"(",
"backend",
".",
"pmap",
",",
"axis_name",
"=",
"\"batch\"",
")",
"def",
"mapped_predict",
"(",
"x",
",",
"params",
",",
"rng",
")",
":",
"return",
"model_predict",
"(",
"x",
",",
"params",
",",
"rng",
"=",
"rng",
")",
"pred",
"=",
"mapped_predict",
"(",
"reshape_by_device",
"(",
"x",
",",
"num_devices",
")",
",",
"params",
",",
"jax_random",
".",
"split",
"(",
"rng",
",",
"num_devices",
")",
")",
"# Need to reduce the [device, per-device-batch, ...] tensors back to",
"# a [batch, ...] tensor. The tensors may be nested.",
"if",
"not",
"isinstance",
"(",
"x",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"# Not nested.",
"batch_size",
"=",
"x",
".",
"shape",
"[",
"0",
"]",
"return",
"np",
".",
"reshape",
"(",
"pred",
",",
"[",
"batch_size",
"]",
"+",
"list",
"(",
"pred",
".",
"shape",
"[",
"2",
":",
"]",
")",
")",
"batch_size",
"=",
"x",
"[",
"0",
"]",
".",
"shape",
"[",
"0",
"]",
"return",
"[",
"np",
".",
"reshape",
"(",
"p",
",",
"[",
"batch_size",
"]",
"+",
"list",
"(",
"p",
".",
"shape",
"[",
"2",
":",
"]",
")",
")",
"for",
"p",
"in",
"pred",
"]",
"return",
"predict"
] |
Use jit on model_predict if required.
|
[
"Use",
"jit",
"on",
"model_predict",
"if",
"required",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L280-L304
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
_jit_update_fun
|
def _jit_update_fun(predict_fun, loss_fun, optimizer, lr_fun, num_devices):
"""Get jit-ed update function for loss, optimizer, learning rate function."""
if num_devices == 1: # TODO(lukaszkaiser): remove branch when not needed.
def single_update(i, opt_state, batch, rng):
rng, subrng = jax_random.split(rng[0])
_, opt_update = optimizer(lr_fun)
params = trax_opt.get_params(opt_state)
return opt_update(i, backend.grad(loss_fun)(
params, batch, predict_fun, rng), opt_state), [subrng]
return backend.jit(single_update)
@functools.partial(backend.pmap, axis_name="batch")
def mapped_update(i, opt_state, batch, rng):
"""This is a multi-device version of the update function above."""
# We assume all tensors have the first dimension = num_devices.
rng, subrng = jax_random.split(rng)
_, opt_update = optimizer(lr_fun)
params = trax_opt.get_params(opt_state)
grads = backend.grad(loss_fun)(params, batch, predict_fun, rng)
grads = jax.tree_util.tree_map(
lambda g: lax.psum(g, "batch"), grads)
return opt_update(i, grads, opt_state), subrng
def update(i, opt_state, batch, rng):
return mapped_update(jax.replicate(i), opt_state, batch, rng)
return update
|
python
|
def _jit_update_fun(predict_fun, loss_fun, optimizer, lr_fun, num_devices):
"""Get jit-ed update function for loss, optimizer, learning rate function."""
if num_devices == 1: # TODO(lukaszkaiser): remove branch when not needed.
def single_update(i, opt_state, batch, rng):
rng, subrng = jax_random.split(rng[0])
_, opt_update = optimizer(lr_fun)
params = trax_opt.get_params(opt_state)
return opt_update(i, backend.grad(loss_fun)(
params, batch, predict_fun, rng), opt_state), [subrng]
return backend.jit(single_update)
@functools.partial(backend.pmap, axis_name="batch")
def mapped_update(i, opt_state, batch, rng):
"""This is a multi-device version of the update function above."""
# We assume all tensors have the first dimension = num_devices.
rng, subrng = jax_random.split(rng)
_, opt_update = optimizer(lr_fun)
params = trax_opt.get_params(opt_state)
grads = backend.grad(loss_fun)(params, batch, predict_fun, rng)
grads = jax.tree_util.tree_map(
lambda g: lax.psum(g, "batch"), grads)
return opt_update(i, grads, opt_state), subrng
def update(i, opt_state, batch, rng):
return mapped_update(jax.replicate(i), opt_state, batch, rng)
return update
|
[
"def",
"_jit_update_fun",
"(",
"predict_fun",
",",
"loss_fun",
",",
"optimizer",
",",
"lr_fun",
",",
"num_devices",
")",
":",
"if",
"num_devices",
"==",
"1",
":",
"# TODO(lukaszkaiser): remove branch when not needed.",
"def",
"single_update",
"(",
"i",
",",
"opt_state",
",",
"batch",
",",
"rng",
")",
":",
"rng",
",",
"subrng",
"=",
"jax_random",
".",
"split",
"(",
"rng",
"[",
"0",
"]",
")",
"_",
",",
"opt_update",
"=",
"optimizer",
"(",
"lr_fun",
")",
"params",
"=",
"trax_opt",
".",
"get_params",
"(",
"opt_state",
")",
"return",
"opt_update",
"(",
"i",
",",
"backend",
".",
"grad",
"(",
"loss_fun",
")",
"(",
"params",
",",
"batch",
",",
"predict_fun",
",",
"rng",
")",
",",
"opt_state",
")",
",",
"[",
"subrng",
"]",
"return",
"backend",
".",
"jit",
"(",
"single_update",
")",
"@",
"functools",
".",
"partial",
"(",
"backend",
".",
"pmap",
",",
"axis_name",
"=",
"\"batch\"",
")",
"def",
"mapped_update",
"(",
"i",
",",
"opt_state",
",",
"batch",
",",
"rng",
")",
":",
"\"\"\"This is a multi-device version of the update function above.\"\"\"",
"# We assume all tensors have the first dimension = num_devices.",
"rng",
",",
"subrng",
"=",
"jax_random",
".",
"split",
"(",
"rng",
")",
"_",
",",
"opt_update",
"=",
"optimizer",
"(",
"lr_fun",
")",
"params",
"=",
"trax_opt",
".",
"get_params",
"(",
"opt_state",
")",
"grads",
"=",
"backend",
".",
"grad",
"(",
"loss_fun",
")",
"(",
"params",
",",
"batch",
",",
"predict_fun",
",",
"rng",
")",
"grads",
"=",
"jax",
".",
"tree_util",
".",
"tree_map",
"(",
"lambda",
"g",
":",
"lax",
".",
"psum",
"(",
"g",
",",
"\"batch\"",
")",
",",
"grads",
")",
"return",
"opt_update",
"(",
"i",
",",
"grads",
",",
"opt_state",
")",
",",
"subrng",
"def",
"update",
"(",
"i",
",",
"opt_state",
",",
"batch",
",",
"rng",
")",
":",
"return",
"mapped_update",
"(",
"jax",
".",
"replicate",
"(",
"i",
")",
",",
"opt_state",
",",
"batch",
",",
"rng",
")",
"return",
"update"
] |
Get jit-ed update function for loss, optimizer, learning rate function.
|
[
"Get",
"jit",
"-",
"ed",
"update",
"function",
"for",
"loss",
"optimizer",
"learning",
"rate",
"function",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L307-L333
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
_reshape_by_device_single
|
def _reshape_by_device_single(x, num_devices):
"""Reshape x into a shape [num_devices, ...]."""
x_shape = list(x.shape)
batch_size = x_shape[0]
batch_size_per_device = batch_size // num_devices
# We require that num_devices divides batch_size evenly.
if batch_size_per_device * num_devices != batch_size:
logging.fatal(
"We require that num_devices[%d] divides batch_size[%d] evenly.",
num_devices, batch_size)
# New shape.
new_shape_prefix = [num_devices, batch_size_per_device]
return np.reshape(x, new_shape_prefix + x_shape[1:])
|
python
|
def _reshape_by_device_single(x, num_devices):
"""Reshape x into a shape [num_devices, ...]."""
x_shape = list(x.shape)
batch_size = x_shape[0]
batch_size_per_device = batch_size // num_devices
# We require that num_devices divides batch_size evenly.
if batch_size_per_device * num_devices != batch_size:
logging.fatal(
"We require that num_devices[%d] divides batch_size[%d] evenly.",
num_devices, batch_size)
# New shape.
new_shape_prefix = [num_devices, batch_size_per_device]
return np.reshape(x, new_shape_prefix + x_shape[1:])
|
[
"def",
"_reshape_by_device_single",
"(",
"x",
",",
"num_devices",
")",
":",
"x_shape",
"=",
"list",
"(",
"x",
".",
"shape",
")",
"batch_size",
"=",
"x_shape",
"[",
"0",
"]",
"batch_size_per_device",
"=",
"batch_size",
"//",
"num_devices",
"# We require that num_devices divides batch_size evenly.",
"if",
"batch_size_per_device",
"*",
"num_devices",
"!=",
"batch_size",
":",
"logging",
".",
"fatal",
"(",
"\"We require that num_devices[%d] divides batch_size[%d] evenly.\"",
",",
"num_devices",
",",
"batch_size",
")",
"# New shape.",
"new_shape_prefix",
"=",
"[",
"num_devices",
",",
"batch_size_per_device",
"]",
"return",
"np",
".",
"reshape",
"(",
"x",
",",
"new_shape_prefix",
"+",
"x_shape",
"[",
"1",
":",
"]",
")"
] |
Reshape x into a shape [num_devices, ...].
|
[
"Reshape",
"x",
"into",
"a",
"shape",
"[",
"num_devices",
"...",
"]",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L336-L348
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
reshape_by_device
|
def reshape_by_device(x, num_devices):
"""Reshape possibly nested x into a shape [num_devices, ...]."""
return layers.nested_map(
x, lambda x: _reshape_by_device_single(x, num_devices))
|
python
|
def reshape_by_device(x, num_devices):
"""Reshape possibly nested x into a shape [num_devices, ...]."""
return layers.nested_map(
x, lambda x: _reshape_by_device_single(x, num_devices))
|
[
"def",
"reshape_by_device",
"(",
"x",
",",
"num_devices",
")",
":",
"return",
"layers",
".",
"nested_map",
"(",
"x",
",",
"lambda",
"x",
":",
"_reshape_by_device_single",
"(",
"x",
",",
"num_devices",
")",
")"
] |
Reshape possibly nested x into a shape [num_devices, ...].
|
[
"Reshape",
"possibly",
"nested",
"x",
"into",
"a",
"shape",
"[",
"num_devices",
"...",
"]",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L351-L354
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/trax.py
|
train
|
def train(output_dir,
model=gin.REQUIRED,
loss_fun=loss,
inputs=trax_inputs.inputs,
optimizer=trax_opt.adam,
lr_schedule=lr.MultifactorSchedule,
train_steps=1000,
save_steps=None,
eval_steps=10,
eval_frequency=100,
num_devices=None,
random_seed=None,
run_debug_step=False,
save_forward_graph=False):
"""Train the model on the inputs.
Args:
output_dir: Directory where to put the logs and checkpoints.
model: The model to train as a callable returning 2 callables, an init_fun
and apply_fun.
loss_fun: callable with signature: params, trax.inputs.Inputs, model, rng
-> loss.
inputs: callable returning trax.inputs.Inputs.
optimizer: The optimizer as a callable taking a learning_rate callable and
returning 2 callables, opt_init and opt_update.
lr_schedule: A learning rate schedule as a function that takes history and
returns a function from step to learning rate (a float).
train_steps: int, total number of training steps.
save_steps: list of integers. Keep a model file at each of the supplied save
steps.
eval_steps: int, num of steps per evaluation. If None or 0, eval disabled.
eval_frequency: int, how often to run evaluation (every eval_frequency
steps). If None or 0, eval disabled.
num_devices: how many devices to use (if None, default, use all available)
random_seed: the random seed to use; time/os dependent if None (default).
run_debug_step: bool, if True, will run the model and loss without @jit for
one step.
save_forward_graph: bool, if True, save forward computation graph to file.
Returns:
trax.State
"""
if save_steps is None:
save_steps = []
num_devices = num_devices or jax.lib.xla_bridge.device_count()
rng = get_random_number_generator_and_set_seed(random_seed)
gfile.makedirs(output_dir)
# Create summary writers and history.
train_sw = jaxboard.SummaryWriter(os.path.join(output_dir, "train"))
eval_sw = jaxboard.SummaryWriter(os.path.join(output_dir, "eval"))
inputs = inputs(num_devices)
# Setup optimizer and model
state = restore_state(output_dir)
history = state.history
lr_fun = lr_schedule(history)
opt_init, _ = optimizer(lr_fun)
model_train = model(mode="train")
model_predict_eval = model(mode="eval")
# Setup state
step = state.step or 0
rng, init_rng = jax_random.split(rng)
rngs = jax_random.split(rng, num_devices)
first_shape = inputs.input_shape[0]
# If the inputs are a tuple/list, add [-1] (batch) to each element.
if isinstance(first_shape, (list, tuple)):
model_input_shape = tuple(
[tuple([-1] + list(shape)) for shape in inputs.input_shape])
else: # Otherwise just add [-1] to the input shape.
model_input_shape = tuple([-1] + list(inputs.input_shape))
params = state.params or model_train.initialize(model_input_shape, init_rng)
opt_state = opt_init(params)
if num_devices > 1: # TODO(lukaszkaiser): use everywhere when pmap is stable.
opt_state = jax.replicate(opt_state)
# jit model_predict and update so they're fast
jit_model_predict_eval = _jit_predict_fun(model_predict_eval, num_devices)
jit_update_fun = _jit_update_fun(
model_train, loss_fun, optimizer, lr_fun, num_devices)
train_stream = inputs.train_stream()
epoch_steps = [train_steps] # Only training if eval_frequency is 0 or None.
if eval_frequency and eval_steps > 0:
epoch_steps = itertools.chain([1, # first epoch only 1 step
eval_frequency - 1],
itertools.repeat(eval_frequency))
step_log(step, "Starting training using %d devices" % num_devices)
# Non-compiled debug step helps find problems in models easier.
if run_debug_step:
debug_loss = loss_fun(params, next(train_stream), model_train, rng)
step_log(step, "Debug step loss %.8f" % debug_loss)
for epoch, epoch_steps in epochs(train_steps, epoch_steps):
# Log separator
print()
# Timer
start_time = time.time()
for _ in range(epoch_steps):
# Train
next_train_batch = next(train_stream)
if num_devices > 1: # TODO(lukaszkaiser): use everywhere when possible.
next_train_batch = reshape_by_device(next_train_batch, num_devices)
opt_state, rngs = jit_update_fun(step, opt_state, next_train_batch, rngs)
step += 1
if step in save_steps:
save_state(State(params=params, step=step, history=history),
output_dir,
keep=True)
# LR log
if step == 1 or step % 10 == 0:
train_sw.scalar("training/learning rate",
lr_fun(step), step=step)
# Timer
epoch_time = time.time() - start_time
step_log(step, "Ran %d train steps in %0.2f secs" %
(epoch_steps, epoch_time))
if epoch_steps > 1:
train_sw.scalar("training/steps per second",
epoch_steps / epoch_time, step=step)
# Print number of parameters
params = trax_opt.get_params(opt_state)
if step == 1:
sizes = layers.sizes(params)
total_size = layers.nested_reduce(sizes, sum)
step_log(step, "Total trainable parameters size: %d" % total_size)
# Evaluate
evaluate_train_and_eval(
step=step,
inputs=inputs,
predict_fun=functools.partial(jit_model_predict_eval, params=params),
eval_steps=eval_steps,
rng=rng,
train_sw=train_sw,
eval_sw=eval_sw,
history=history)
# Save computation graph
if save_forward_graph and step == 1:
# Dump forward computation graph to file.
computation = jax.xla_computation(model_predict_eval)(
next_train_batch[0], params=params, rng=rng)
with gfile.GFile(os.path.join(output_dir, "forward_graph.dot"), "w") as f:
f.write(computation.GetHloDotGraph())
# Save state
save_state(State(params=params, step=step, history=history), output_dir)
# Save Gin config
# Gin only tracks the used parameters, so we save it after the first epoch.
if epoch == 1:
save_gin(output_dir, train_sw)
# Update learning rate with new history
old_lr_fun = lr_fun
lr_fun = lr_schedule(history)
if lr_fun != old_lr_fun: # For performance, only jit if there is a change.
jit_update_fun = _jit_update_fun(
model_train, loss_fun, optimizer, lr_fun, num_devices)
# Flush summary writers
train_sw.flush()
eval_sw.flush()
step_log(step, "Training done")
return State(params=params, step=step, history=history)
|
python
|
def train(output_dir,
model=gin.REQUIRED,
loss_fun=loss,
inputs=trax_inputs.inputs,
optimizer=trax_opt.adam,
lr_schedule=lr.MultifactorSchedule,
train_steps=1000,
save_steps=None,
eval_steps=10,
eval_frequency=100,
num_devices=None,
random_seed=None,
run_debug_step=False,
save_forward_graph=False):
"""Train the model on the inputs.
Args:
output_dir: Directory where to put the logs and checkpoints.
model: The model to train as a callable returning 2 callables, an init_fun
and apply_fun.
loss_fun: callable with signature: params, trax.inputs.Inputs, model, rng
-> loss.
inputs: callable returning trax.inputs.Inputs.
optimizer: The optimizer as a callable taking a learning_rate callable and
returning 2 callables, opt_init and opt_update.
lr_schedule: A learning rate schedule as a function that takes history and
returns a function from step to learning rate (a float).
train_steps: int, total number of training steps.
save_steps: list of integers. Keep a model file at each of the supplied save
steps.
eval_steps: int, num of steps per evaluation. If None or 0, eval disabled.
eval_frequency: int, how often to run evaluation (every eval_frequency
steps). If None or 0, eval disabled.
num_devices: how many devices to use (if None, default, use all available)
random_seed: the random seed to use; time/os dependent if None (default).
run_debug_step: bool, if True, will run the model and loss without @jit for
one step.
save_forward_graph: bool, if True, save forward computation graph to file.
Returns:
trax.State
"""
if save_steps is None:
save_steps = []
num_devices = num_devices or jax.lib.xla_bridge.device_count()
rng = get_random_number_generator_and_set_seed(random_seed)
gfile.makedirs(output_dir)
# Create summary writers and history.
train_sw = jaxboard.SummaryWriter(os.path.join(output_dir, "train"))
eval_sw = jaxboard.SummaryWriter(os.path.join(output_dir, "eval"))
inputs = inputs(num_devices)
# Setup optimizer and model
state = restore_state(output_dir)
history = state.history
lr_fun = lr_schedule(history)
opt_init, _ = optimizer(lr_fun)
model_train = model(mode="train")
model_predict_eval = model(mode="eval")
# Setup state
step = state.step or 0
rng, init_rng = jax_random.split(rng)
rngs = jax_random.split(rng, num_devices)
first_shape = inputs.input_shape[0]
# If the inputs are a tuple/list, add [-1] (batch) to each element.
if isinstance(first_shape, (list, tuple)):
model_input_shape = tuple(
[tuple([-1] + list(shape)) for shape in inputs.input_shape])
else: # Otherwise just add [-1] to the input shape.
model_input_shape = tuple([-1] + list(inputs.input_shape))
params = state.params or model_train.initialize(model_input_shape, init_rng)
opt_state = opt_init(params)
if num_devices > 1: # TODO(lukaszkaiser): use everywhere when pmap is stable.
opt_state = jax.replicate(opt_state)
# jit model_predict and update so they're fast
jit_model_predict_eval = _jit_predict_fun(model_predict_eval, num_devices)
jit_update_fun = _jit_update_fun(
model_train, loss_fun, optimizer, lr_fun, num_devices)
train_stream = inputs.train_stream()
epoch_steps = [train_steps] # Only training if eval_frequency is 0 or None.
if eval_frequency and eval_steps > 0:
epoch_steps = itertools.chain([1, # first epoch only 1 step
eval_frequency - 1],
itertools.repeat(eval_frequency))
step_log(step, "Starting training using %d devices" % num_devices)
# Non-compiled debug step helps find problems in models easier.
if run_debug_step:
debug_loss = loss_fun(params, next(train_stream), model_train, rng)
step_log(step, "Debug step loss %.8f" % debug_loss)
for epoch, epoch_steps in epochs(train_steps, epoch_steps):
# Log separator
print()
# Timer
start_time = time.time()
for _ in range(epoch_steps):
# Train
next_train_batch = next(train_stream)
if num_devices > 1: # TODO(lukaszkaiser): use everywhere when possible.
next_train_batch = reshape_by_device(next_train_batch, num_devices)
opt_state, rngs = jit_update_fun(step, opt_state, next_train_batch, rngs)
step += 1
if step in save_steps:
save_state(State(params=params, step=step, history=history),
output_dir,
keep=True)
# LR log
if step == 1 or step % 10 == 0:
train_sw.scalar("training/learning rate",
lr_fun(step), step=step)
# Timer
epoch_time = time.time() - start_time
step_log(step, "Ran %d train steps in %0.2f secs" %
(epoch_steps, epoch_time))
if epoch_steps > 1:
train_sw.scalar("training/steps per second",
epoch_steps / epoch_time, step=step)
# Print number of parameters
params = trax_opt.get_params(opt_state)
if step == 1:
sizes = layers.sizes(params)
total_size = layers.nested_reduce(sizes, sum)
step_log(step, "Total trainable parameters size: %d" % total_size)
# Evaluate
evaluate_train_and_eval(
step=step,
inputs=inputs,
predict_fun=functools.partial(jit_model_predict_eval, params=params),
eval_steps=eval_steps,
rng=rng,
train_sw=train_sw,
eval_sw=eval_sw,
history=history)
# Save computation graph
if save_forward_graph and step == 1:
# Dump forward computation graph to file.
computation = jax.xla_computation(model_predict_eval)(
next_train_batch[0], params=params, rng=rng)
with gfile.GFile(os.path.join(output_dir, "forward_graph.dot"), "w") as f:
f.write(computation.GetHloDotGraph())
# Save state
save_state(State(params=params, step=step, history=history), output_dir)
# Save Gin config
# Gin only tracks the used parameters, so we save it after the first epoch.
if epoch == 1:
save_gin(output_dir, train_sw)
# Update learning rate with new history
old_lr_fun = lr_fun
lr_fun = lr_schedule(history)
if lr_fun != old_lr_fun: # For performance, only jit if there is a change.
jit_update_fun = _jit_update_fun(
model_train, loss_fun, optimizer, lr_fun, num_devices)
# Flush summary writers
train_sw.flush()
eval_sw.flush()
step_log(step, "Training done")
return State(params=params, step=step, history=history)
|
[
"def",
"train",
"(",
"output_dir",
",",
"model",
"=",
"gin",
".",
"REQUIRED",
",",
"loss_fun",
"=",
"loss",
",",
"inputs",
"=",
"trax_inputs",
".",
"inputs",
",",
"optimizer",
"=",
"trax_opt",
".",
"adam",
",",
"lr_schedule",
"=",
"lr",
".",
"MultifactorSchedule",
",",
"train_steps",
"=",
"1000",
",",
"save_steps",
"=",
"None",
",",
"eval_steps",
"=",
"10",
",",
"eval_frequency",
"=",
"100",
",",
"num_devices",
"=",
"None",
",",
"random_seed",
"=",
"None",
",",
"run_debug_step",
"=",
"False",
",",
"save_forward_graph",
"=",
"False",
")",
":",
"if",
"save_steps",
"is",
"None",
":",
"save_steps",
"=",
"[",
"]",
"num_devices",
"=",
"num_devices",
"or",
"jax",
".",
"lib",
".",
"xla_bridge",
".",
"device_count",
"(",
")",
"rng",
"=",
"get_random_number_generator_and_set_seed",
"(",
"random_seed",
")",
"gfile",
".",
"makedirs",
"(",
"output_dir",
")",
"# Create summary writers and history.",
"train_sw",
"=",
"jaxboard",
".",
"SummaryWriter",
"(",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"\"train\"",
")",
")",
"eval_sw",
"=",
"jaxboard",
".",
"SummaryWriter",
"(",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"\"eval\"",
")",
")",
"inputs",
"=",
"inputs",
"(",
"num_devices",
")",
"# Setup optimizer and model",
"state",
"=",
"restore_state",
"(",
"output_dir",
")",
"history",
"=",
"state",
".",
"history",
"lr_fun",
"=",
"lr_schedule",
"(",
"history",
")",
"opt_init",
",",
"_",
"=",
"optimizer",
"(",
"lr_fun",
")",
"model_train",
"=",
"model",
"(",
"mode",
"=",
"\"train\"",
")",
"model_predict_eval",
"=",
"model",
"(",
"mode",
"=",
"\"eval\"",
")",
"# Setup state",
"step",
"=",
"state",
".",
"step",
"or",
"0",
"rng",
",",
"init_rng",
"=",
"jax_random",
".",
"split",
"(",
"rng",
")",
"rngs",
"=",
"jax_random",
".",
"split",
"(",
"rng",
",",
"num_devices",
")",
"first_shape",
"=",
"inputs",
".",
"input_shape",
"[",
"0",
"]",
"# If the inputs are a tuple/list, add [-1] (batch) to each element.",
"if",
"isinstance",
"(",
"first_shape",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"model_input_shape",
"=",
"tuple",
"(",
"[",
"tuple",
"(",
"[",
"-",
"1",
"]",
"+",
"list",
"(",
"shape",
")",
")",
"for",
"shape",
"in",
"inputs",
".",
"input_shape",
"]",
")",
"else",
":",
"# Otherwise just add [-1] to the input shape.",
"model_input_shape",
"=",
"tuple",
"(",
"[",
"-",
"1",
"]",
"+",
"list",
"(",
"inputs",
".",
"input_shape",
")",
")",
"params",
"=",
"state",
".",
"params",
"or",
"model_train",
".",
"initialize",
"(",
"model_input_shape",
",",
"init_rng",
")",
"opt_state",
"=",
"opt_init",
"(",
"params",
")",
"if",
"num_devices",
">",
"1",
":",
"# TODO(lukaszkaiser): use everywhere when pmap is stable.",
"opt_state",
"=",
"jax",
".",
"replicate",
"(",
"opt_state",
")",
"# jit model_predict and update so they're fast",
"jit_model_predict_eval",
"=",
"_jit_predict_fun",
"(",
"model_predict_eval",
",",
"num_devices",
")",
"jit_update_fun",
"=",
"_jit_update_fun",
"(",
"model_train",
",",
"loss_fun",
",",
"optimizer",
",",
"lr_fun",
",",
"num_devices",
")",
"train_stream",
"=",
"inputs",
".",
"train_stream",
"(",
")",
"epoch_steps",
"=",
"[",
"train_steps",
"]",
"# Only training if eval_frequency is 0 or None.",
"if",
"eval_frequency",
"and",
"eval_steps",
">",
"0",
":",
"epoch_steps",
"=",
"itertools",
".",
"chain",
"(",
"[",
"1",
",",
"# first epoch only 1 step",
"eval_frequency",
"-",
"1",
"]",
",",
"itertools",
".",
"repeat",
"(",
"eval_frequency",
")",
")",
"step_log",
"(",
"step",
",",
"\"Starting training using %d devices\"",
"%",
"num_devices",
")",
"# Non-compiled debug step helps find problems in models easier.",
"if",
"run_debug_step",
":",
"debug_loss",
"=",
"loss_fun",
"(",
"params",
",",
"next",
"(",
"train_stream",
")",
",",
"model_train",
",",
"rng",
")",
"step_log",
"(",
"step",
",",
"\"Debug step loss %.8f\"",
"%",
"debug_loss",
")",
"for",
"epoch",
",",
"epoch_steps",
"in",
"epochs",
"(",
"train_steps",
",",
"epoch_steps",
")",
":",
"# Log separator",
"print",
"(",
")",
"# Timer",
"start_time",
"=",
"time",
".",
"time",
"(",
")",
"for",
"_",
"in",
"range",
"(",
"epoch_steps",
")",
":",
"# Train",
"next_train_batch",
"=",
"next",
"(",
"train_stream",
")",
"if",
"num_devices",
">",
"1",
":",
"# TODO(lukaszkaiser): use everywhere when possible.",
"next_train_batch",
"=",
"reshape_by_device",
"(",
"next_train_batch",
",",
"num_devices",
")",
"opt_state",
",",
"rngs",
"=",
"jit_update_fun",
"(",
"step",
",",
"opt_state",
",",
"next_train_batch",
",",
"rngs",
")",
"step",
"+=",
"1",
"if",
"step",
"in",
"save_steps",
":",
"save_state",
"(",
"State",
"(",
"params",
"=",
"params",
",",
"step",
"=",
"step",
",",
"history",
"=",
"history",
")",
",",
"output_dir",
",",
"keep",
"=",
"True",
")",
"# LR log",
"if",
"step",
"==",
"1",
"or",
"step",
"%",
"10",
"==",
"0",
":",
"train_sw",
".",
"scalar",
"(",
"\"training/learning rate\"",
",",
"lr_fun",
"(",
"step",
")",
",",
"step",
"=",
"step",
")",
"# Timer",
"epoch_time",
"=",
"time",
".",
"time",
"(",
")",
"-",
"start_time",
"step_log",
"(",
"step",
",",
"\"Ran %d train steps in %0.2f secs\"",
"%",
"(",
"epoch_steps",
",",
"epoch_time",
")",
")",
"if",
"epoch_steps",
">",
"1",
":",
"train_sw",
".",
"scalar",
"(",
"\"training/steps per second\"",
",",
"epoch_steps",
"/",
"epoch_time",
",",
"step",
"=",
"step",
")",
"# Print number of parameters",
"params",
"=",
"trax_opt",
".",
"get_params",
"(",
"opt_state",
")",
"if",
"step",
"==",
"1",
":",
"sizes",
"=",
"layers",
".",
"sizes",
"(",
"params",
")",
"total_size",
"=",
"layers",
".",
"nested_reduce",
"(",
"sizes",
",",
"sum",
")",
"step_log",
"(",
"step",
",",
"\"Total trainable parameters size: %d\"",
"%",
"total_size",
")",
"# Evaluate",
"evaluate_train_and_eval",
"(",
"step",
"=",
"step",
",",
"inputs",
"=",
"inputs",
",",
"predict_fun",
"=",
"functools",
".",
"partial",
"(",
"jit_model_predict_eval",
",",
"params",
"=",
"params",
")",
",",
"eval_steps",
"=",
"eval_steps",
",",
"rng",
"=",
"rng",
",",
"train_sw",
"=",
"train_sw",
",",
"eval_sw",
"=",
"eval_sw",
",",
"history",
"=",
"history",
")",
"# Save computation graph",
"if",
"save_forward_graph",
"and",
"step",
"==",
"1",
":",
"# Dump forward computation graph to file.",
"computation",
"=",
"jax",
".",
"xla_computation",
"(",
"model_predict_eval",
")",
"(",
"next_train_batch",
"[",
"0",
"]",
",",
"params",
"=",
"params",
",",
"rng",
"=",
"rng",
")",
"with",
"gfile",
".",
"GFile",
"(",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"\"forward_graph.dot\"",
")",
",",
"\"w\"",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"computation",
".",
"GetHloDotGraph",
"(",
")",
")",
"# Save state",
"save_state",
"(",
"State",
"(",
"params",
"=",
"params",
",",
"step",
"=",
"step",
",",
"history",
"=",
"history",
")",
",",
"output_dir",
")",
"# Save Gin config",
"# Gin only tracks the used parameters, so we save it after the first epoch.",
"if",
"epoch",
"==",
"1",
":",
"save_gin",
"(",
"output_dir",
",",
"train_sw",
")",
"# Update learning rate with new history",
"old_lr_fun",
"=",
"lr_fun",
"lr_fun",
"=",
"lr_schedule",
"(",
"history",
")",
"if",
"lr_fun",
"!=",
"old_lr_fun",
":",
"# For performance, only jit if there is a change.",
"jit_update_fun",
"=",
"_jit_update_fun",
"(",
"model_train",
",",
"loss_fun",
",",
"optimizer",
",",
"lr_fun",
",",
"num_devices",
")",
"# Flush summary writers",
"train_sw",
".",
"flush",
"(",
")",
"eval_sw",
".",
"flush",
"(",
")",
"step_log",
"(",
"step",
",",
"\"Training done\"",
")",
"return",
"State",
"(",
"params",
"=",
"params",
",",
"step",
"=",
"step",
",",
"history",
"=",
"history",
")"
] |
Train the model on the inputs.
Args:
output_dir: Directory where to put the logs and checkpoints.
model: The model to train as a callable returning 2 callables, an init_fun
and apply_fun.
loss_fun: callable with signature: params, trax.inputs.Inputs, model, rng
-> loss.
inputs: callable returning trax.inputs.Inputs.
optimizer: The optimizer as a callable taking a learning_rate callable and
returning 2 callables, opt_init and opt_update.
lr_schedule: A learning rate schedule as a function that takes history and
returns a function from step to learning rate (a float).
train_steps: int, total number of training steps.
save_steps: list of integers. Keep a model file at each of the supplied save
steps.
eval_steps: int, num of steps per evaluation. If None or 0, eval disabled.
eval_frequency: int, how often to run evaluation (every eval_frequency
steps). If None or 0, eval disabled.
num_devices: how many devices to use (if None, default, use all available)
random_seed: the random seed to use; time/os dependent if None (default).
run_debug_step: bool, if True, will run the model and loss without @jit for
one step.
save_forward_graph: bool, if True, save forward computation graph to file.
Returns:
trax.State
|
[
"Train",
"the",
"model",
"on",
"the",
"inputs",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/trax.py#L358-L532
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/keras/initializers.py
|
_compute_fans
|
def _compute_fans(shape):
"""Computes the number of input and output units for a weight shape.
Args:
shape: Integer shape tuple or TF tensor shape.
Returns:
A tuple of scalars (fan_in, fan_out).
"""
if len(shape) < 1: # Just to avoid errors for constants.
fan_in = fan_out = 1
elif len(shape) == 1:
fan_in = fan_out = shape[0]
elif len(shape) == 2:
fan_in = shape[0]
fan_out = shape[1]
else:
# Assuming convolution kernels (2D, 3D, or more).
# kernel shape: (..., input_depth, depth)
receptive_field_size = 1.
for dim in shape[:-2]:
receptive_field_size *= dim
fan_in = shape[-2] * receptive_field_size
fan_out = shape[-1] * receptive_field_size
if isinstance(fan_in, tf.Dimension):
fan_in = fan_in.value
if isinstance(fan_out, tf.Dimension):
fan_out = fan_out.value
return fan_in, fan_out
|
python
|
def _compute_fans(shape):
"""Computes the number of input and output units for a weight shape.
Args:
shape: Integer shape tuple or TF tensor shape.
Returns:
A tuple of scalars (fan_in, fan_out).
"""
if len(shape) < 1: # Just to avoid errors for constants.
fan_in = fan_out = 1
elif len(shape) == 1:
fan_in = fan_out = shape[0]
elif len(shape) == 2:
fan_in = shape[0]
fan_out = shape[1]
else:
# Assuming convolution kernels (2D, 3D, or more).
# kernel shape: (..., input_depth, depth)
receptive_field_size = 1.
for dim in shape[:-2]:
receptive_field_size *= dim
fan_in = shape[-2] * receptive_field_size
fan_out = shape[-1] * receptive_field_size
if isinstance(fan_in, tf.Dimension):
fan_in = fan_in.value
if isinstance(fan_out, tf.Dimension):
fan_out = fan_out.value
return fan_in, fan_out
|
[
"def",
"_compute_fans",
"(",
"shape",
")",
":",
"if",
"len",
"(",
"shape",
")",
"<",
"1",
":",
"# Just to avoid errors for constants.",
"fan_in",
"=",
"fan_out",
"=",
"1",
"elif",
"len",
"(",
"shape",
")",
"==",
"1",
":",
"fan_in",
"=",
"fan_out",
"=",
"shape",
"[",
"0",
"]",
"elif",
"len",
"(",
"shape",
")",
"==",
"2",
":",
"fan_in",
"=",
"shape",
"[",
"0",
"]",
"fan_out",
"=",
"shape",
"[",
"1",
"]",
"else",
":",
"# Assuming convolution kernels (2D, 3D, or more).",
"# kernel shape: (..., input_depth, depth)",
"receptive_field_size",
"=",
"1.",
"for",
"dim",
"in",
"shape",
"[",
":",
"-",
"2",
"]",
":",
"receptive_field_size",
"*=",
"dim",
"fan_in",
"=",
"shape",
"[",
"-",
"2",
"]",
"*",
"receptive_field_size",
"fan_out",
"=",
"shape",
"[",
"-",
"1",
"]",
"*",
"receptive_field_size",
"if",
"isinstance",
"(",
"fan_in",
",",
"tf",
".",
"Dimension",
")",
":",
"fan_in",
"=",
"fan_in",
".",
"value",
"if",
"isinstance",
"(",
"fan_out",
",",
"tf",
".",
"Dimension",
")",
":",
"fan_out",
"=",
"fan_out",
".",
"value",
"return",
"fan_in",
",",
"fan_out"
] |
Computes the number of input and output units for a weight shape.
Args:
shape: Integer shape tuple or TF tensor shape.
Returns:
A tuple of scalars (fan_in, fan_out).
|
[
"Computes",
"the",
"number",
"of",
"input",
"and",
"output",
"units",
"for",
"a",
"weight",
"shape",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/keras/initializers.py#L32-L60
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/keras/initializers.py
|
get
|
def get(identifier, value=None):
"""Getter for loading from strings; returns value if can't load."""
if value is None:
value = identifier
if identifier is None:
return None
elif isinstance(identifier, dict):
try:
return deserialize(identifier)
except ValueError:
return value
elif isinstance(identifier, six.string_types):
config = {'class_name': str(identifier), 'config': {}}
try:
return deserialize(config)
except ValueError:
return value
elif callable(identifier):
return identifier
return value
|
python
|
def get(identifier, value=None):
"""Getter for loading from strings; returns value if can't load."""
if value is None:
value = identifier
if identifier is None:
return None
elif isinstance(identifier, dict):
try:
return deserialize(identifier)
except ValueError:
return value
elif isinstance(identifier, six.string_types):
config = {'class_name': str(identifier), 'config': {}}
try:
return deserialize(config)
except ValueError:
return value
elif callable(identifier):
return identifier
return value
|
[
"def",
"get",
"(",
"identifier",
",",
"value",
"=",
"None",
")",
":",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"identifier",
"if",
"identifier",
"is",
"None",
":",
"return",
"None",
"elif",
"isinstance",
"(",
"identifier",
",",
"dict",
")",
":",
"try",
":",
"return",
"deserialize",
"(",
"identifier",
")",
"except",
"ValueError",
":",
"return",
"value",
"elif",
"isinstance",
"(",
"identifier",
",",
"six",
".",
"string_types",
")",
":",
"config",
"=",
"{",
"'class_name'",
":",
"str",
"(",
"identifier",
")",
",",
"'config'",
":",
"{",
"}",
"}",
"try",
":",
"return",
"deserialize",
"(",
"config",
")",
"except",
"ValueError",
":",
"return",
"value",
"elif",
"callable",
"(",
"identifier",
")",
":",
"return",
"identifier",
"return",
"value"
] |
Getter for loading from strings; returns value if can't load.
|
[
"Getter",
"for",
"loading",
"from",
"strings",
";",
"returns",
"value",
"if",
"can",
"t",
"load",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/keras/initializers.py#L279-L298
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
Trajectory.add_time_step
|
def add_time_step(self, **create_time_step_kwargs):
"""Creates a time-step and appends it to the list.
Args:
**create_time_step_kwargs: Forwarded to
time_step.TimeStep.create_time_step.
"""
ts = time_step.TimeStep.create_time_step(**create_time_step_kwargs)
assert isinstance(ts, time_step.TimeStep)
self._time_steps.append(ts)
|
python
|
def add_time_step(self, **create_time_step_kwargs):
"""Creates a time-step and appends it to the list.
Args:
**create_time_step_kwargs: Forwarded to
time_step.TimeStep.create_time_step.
"""
ts = time_step.TimeStep.create_time_step(**create_time_step_kwargs)
assert isinstance(ts, time_step.TimeStep)
self._time_steps.append(ts)
|
[
"def",
"add_time_step",
"(",
"self",
",",
"*",
"*",
"create_time_step_kwargs",
")",
":",
"ts",
"=",
"time_step",
".",
"TimeStep",
".",
"create_time_step",
"(",
"*",
"*",
"create_time_step_kwargs",
")",
"assert",
"isinstance",
"(",
"ts",
",",
"time_step",
".",
"TimeStep",
")",
"self",
".",
"_time_steps",
".",
"append",
"(",
"ts",
")"
] |
Creates a time-step and appends it to the list.
Args:
**create_time_step_kwargs: Forwarded to
time_step.TimeStep.create_time_step.
|
[
"Creates",
"a",
"time",
"-",
"step",
"and",
"appends",
"it",
"to",
"the",
"list",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L42-L51
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
Trajectory.change_last_time_step
|
def change_last_time_step(self, **replace_time_step_kwargs):
"""Replace the last time-steps with the given kwargs."""
# Pre-conditions: self._time_steps shouldn't be empty.
assert self._time_steps
self._time_steps[-1] = self._time_steps[-1].replace(
**replace_time_step_kwargs)
|
python
|
def change_last_time_step(self, **replace_time_step_kwargs):
"""Replace the last time-steps with the given kwargs."""
# Pre-conditions: self._time_steps shouldn't be empty.
assert self._time_steps
self._time_steps[-1] = self._time_steps[-1].replace(
**replace_time_step_kwargs)
|
[
"def",
"change_last_time_step",
"(",
"self",
",",
"*",
"*",
"replace_time_step_kwargs",
")",
":",
"# Pre-conditions: self._time_steps shouldn't be empty.",
"assert",
"self",
".",
"_time_steps",
"self",
".",
"_time_steps",
"[",
"-",
"1",
"]",
"=",
"self",
".",
"_time_steps",
"[",
"-",
"1",
"]",
".",
"replace",
"(",
"*",
"*",
"replace_time_step_kwargs",
")"
] |
Replace the last time-steps with the given kwargs.
|
[
"Replace",
"the",
"last",
"time",
"-",
"steps",
"with",
"the",
"given",
"kwargs",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L53-L59
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
Trajectory.reward
|
def reward(self):
"""Returns a tuple of sum of raw and processed rewards."""
raw_rewards, processed_rewards = 0, 0
for ts in self.time_steps:
# NOTE: raw_reward and processed_reward are None for the first time-step.
if ts.raw_reward is not None:
raw_rewards += ts.raw_reward
if ts.processed_reward is not None:
processed_rewards += ts.processed_reward
return raw_rewards, processed_rewards
|
python
|
def reward(self):
"""Returns a tuple of sum of raw and processed rewards."""
raw_rewards, processed_rewards = 0, 0
for ts in self.time_steps:
# NOTE: raw_reward and processed_reward are None for the first time-step.
if ts.raw_reward is not None:
raw_rewards += ts.raw_reward
if ts.processed_reward is not None:
processed_rewards += ts.processed_reward
return raw_rewards, processed_rewards
|
[
"def",
"reward",
"(",
"self",
")",
":",
"raw_rewards",
",",
"processed_rewards",
"=",
"0",
",",
"0",
"for",
"ts",
"in",
"self",
".",
"time_steps",
":",
"# NOTE: raw_reward and processed_reward are None for the first time-step.",
"if",
"ts",
".",
"raw_reward",
"is",
"not",
"None",
":",
"raw_rewards",
"+=",
"ts",
".",
"raw_reward",
"if",
"ts",
".",
"processed_reward",
"is",
"not",
"None",
":",
"processed_rewards",
"+=",
"ts",
".",
"processed_reward",
"return",
"raw_rewards",
",",
"processed_rewards"
] |
Returns a tuple of sum of raw and processed rewards.
|
[
"Returns",
"a",
"tuple",
"of",
"sum",
"of",
"raw",
"and",
"processed",
"rewards",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L85-L94
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
BatchTrajectory._complete_trajectory
|
def _complete_trajectory(self, trajectory, index):
"""Completes the given trajectory at the given index."""
assert isinstance(trajectory, Trajectory)
# This *should* be the case.
assert trajectory.last_time_step.action is None
# Add to completed trajectories.
self._completed_trajectories.append(trajectory)
# Make a new one to replace it.
self._trajectories[index] = Trajectory()
|
python
|
def _complete_trajectory(self, trajectory, index):
"""Completes the given trajectory at the given index."""
assert isinstance(trajectory, Trajectory)
# This *should* be the case.
assert trajectory.last_time_step.action is None
# Add to completed trajectories.
self._completed_trajectories.append(trajectory)
# Make a new one to replace it.
self._trajectories[index] = Trajectory()
|
[
"def",
"_complete_trajectory",
"(",
"self",
",",
"trajectory",
",",
"index",
")",
":",
"assert",
"isinstance",
"(",
"trajectory",
",",
"Trajectory",
")",
"# This *should* be the case.",
"assert",
"trajectory",
".",
"last_time_step",
".",
"action",
"is",
"None",
"# Add to completed trajectories.",
"self",
".",
"_completed_trajectories",
".",
"append",
"(",
"trajectory",
")",
"# Make a new one to replace it.",
"self",
".",
"_trajectories",
"[",
"index",
"]",
"=",
"Trajectory",
"(",
")"
] |
Completes the given trajectory at the given index.
|
[
"Completes",
"the",
"given",
"trajectory",
"at",
"the",
"given",
"index",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L133-L145
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
BatchTrajectory.reset
|
def reset(self, indices, observations):
"""Resets trajectories at given indices and populates observations.
Reset can either be called right at the beginning, when there are no
time-steps, or to reset a currently active trajectory.
If resetting a currently active trajectory then we save it in
self._completed_trajectories.
Args:
indices: 1-D np.ndarray stating the indices to reset.
observations: np.ndarray of shape (indices len, obs.shape) of observations
"""
# Pre-conditions: indices, observations are np arrays.
# : indices is one-dimensional.
# : their first dimension (batch) is the same.
assert isinstance(indices, np.ndarray)
assert len(indices.shape) == 1
assert isinstance(observations, np.ndarray)
assert indices.shape[0] == observations.shape[0]
for index, observation in zip(indices, observations):
trajectory = self._trajectories[index]
# Are we starting a new trajectory at the given index?
if not trajectory.is_active:
# Then create a new time-step here with the given observation.
trajectory.add_time_step(observation=observation)
# That's all we need to do here.
continue
# If however we are resetting a currently active trajectory then we need
# to put that in self._completed_trajectories and make a new trajectory
# with the current observation.
# TODO(afrozm): Should we mark these are done? Or is the done=False and
# this being the last time-step in the trajectory good enough to recognize
# that this was reset?
# Mark trajectory as completed and move into completed_trajectories.
self._complete_trajectory(trajectory, index)
# Put the observation in the newly created trajectory.
# TODO(afrozm): Add 0 reward.
self._trajectories[index].add_time_step(observation=observation)
|
python
|
def reset(self, indices, observations):
"""Resets trajectories at given indices and populates observations.
Reset can either be called right at the beginning, when there are no
time-steps, or to reset a currently active trajectory.
If resetting a currently active trajectory then we save it in
self._completed_trajectories.
Args:
indices: 1-D np.ndarray stating the indices to reset.
observations: np.ndarray of shape (indices len, obs.shape) of observations
"""
# Pre-conditions: indices, observations are np arrays.
# : indices is one-dimensional.
# : their first dimension (batch) is the same.
assert isinstance(indices, np.ndarray)
assert len(indices.shape) == 1
assert isinstance(observations, np.ndarray)
assert indices.shape[0] == observations.shape[0]
for index, observation in zip(indices, observations):
trajectory = self._trajectories[index]
# Are we starting a new trajectory at the given index?
if not trajectory.is_active:
# Then create a new time-step here with the given observation.
trajectory.add_time_step(observation=observation)
# That's all we need to do here.
continue
# If however we are resetting a currently active trajectory then we need
# to put that in self._completed_trajectories and make a new trajectory
# with the current observation.
# TODO(afrozm): Should we mark these are done? Or is the done=False and
# this being the last time-step in the trajectory good enough to recognize
# that this was reset?
# Mark trajectory as completed and move into completed_trajectories.
self._complete_trajectory(trajectory, index)
# Put the observation in the newly created trajectory.
# TODO(afrozm): Add 0 reward.
self._trajectories[index].add_time_step(observation=observation)
|
[
"def",
"reset",
"(",
"self",
",",
"indices",
",",
"observations",
")",
":",
"# Pre-conditions: indices, observations are np arrays.",
"# : indices is one-dimensional.",
"# : their first dimension (batch) is the same.",
"assert",
"isinstance",
"(",
"indices",
",",
"np",
".",
"ndarray",
")",
"assert",
"len",
"(",
"indices",
".",
"shape",
")",
"==",
"1",
"assert",
"isinstance",
"(",
"observations",
",",
"np",
".",
"ndarray",
")",
"assert",
"indices",
".",
"shape",
"[",
"0",
"]",
"==",
"observations",
".",
"shape",
"[",
"0",
"]",
"for",
"index",
",",
"observation",
"in",
"zip",
"(",
"indices",
",",
"observations",
")",
":",
"trajectory",
"=",
"self",
".",
"_trajectories",
"[",
"index",
"]",
"# Are we starting a new trajectory at the given index?",
"if",
"not",
"trajectory",
".",
"is_active",
":",
"# Then create a new time-step here with the given observation.",
"trajectory",
".",
"add_time_step",
"(",
"observation",
"=",
"observation",
")",
"# That's all we need to do here.",
"continue",
"# If however we are resetting a currently active trajectory then we need",
"# to put that in self._completed_trajectories and make a new trajectory",
"# with the current observation.",
"# TODO(afrozm): Should we mark these are done? Or is the done=False and",
"# this being the last time-step in the trajectory good enough to recognize",
"# that this was reset?",
"# Mark trajectory as completed and move into completed_trajectories.",
"self",
".",
"_complete_trajectory",
"(",
"trajectory",
",",
"index",
")",
"# Put the observation in the newly created trajectory.",
"# TODO(afrozm): Add 0 reward.",
"self",
".",
"_trajectories",
"[",
"index",
"]",
".",
"add_time_step",
"(",
"observation",
"=",
"observation",
")"
] |
Resets trajectories at given indices and populates observations.
Reset can either be called right at the beginning, when there are no
time-steps, or to reset a currently active trajectory.
If resetting a currently active trajectory then we save it in
self._completed_trajectories.
Args:
indices: 1-D np.ndarray stating the indices to reset.
observations: np.ndarray of shape (indices len, obs.shape) of observations
|
[
"Resets",
"trajectories",
"at",
"given",
"indices",
"and",
"populates",
"observations",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L147-L192
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
BatchTrajectory.complete_all_trajectories
|
def complete_all_trajectories(self):
"""Essentially same as reset, but we don't have observations."""
for index in range(self.batch_size):
trajectory = self._trajectories[index]
assert trajectory.is_active
self._complete_trajectory(trajectory, index)
|
python
|
def complete_all_trajectories(self):
"""Essentially same as reset, but we don't have observations."""
for index in range(self.batch_size):
trajectory = self._trajectories[index]
assert trajectory.is_active
self._complete_trajectory(trajectory, index)
|
[
"def",
"complete_all_trajectories",
"(",
"self",
")",
":",
"for",
"index",
"in",
"range",
"(",
"self",
".",
"batch_size",
")",
":",
"trajectory",
"=",
"self",
".",
"_trajectories",
"[",
"index",
"]",
"assert",
"trajectory",
".",
"is_active",
"self",
".",
"_complete_trajectory",
"(",
"trajectory",
",",
"index",
")"
] |
Essentially same as reset, but we don't have observations.
|
[
"Essentially",
"same",
"as",
"reset",
"but",
"we",
"don",
"t",
"have",
"observations",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L194-L199
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
BatchTrajectory.step
|
def step(self, observations, raw_rewards, processed_rewards, dones, actions):
"""Record the information obtained from taking a step in all envs.
Records (observation, rewards, done) in a new time-step and actions in the
current time-step.
If any trajectory gets done, we move that trajectory to
completed_trajectories.
Args:
observations: ndarray of first dimension self.batch_size, which has the
observations after we've stepped, i.e. s_{t+1} where t is the current
state.
raw_rewards: ndarray of first dimension self.batch_size containing raw
rewards i.e. r_{t+1}.
processed_rewards: ndarray of first dimension self.batch_size containing
processed rewards. i.e. r_{t+1}
dones: ndarray of first dimension self.batch_size, containing true at an
index if that env is done, i.e. d_{t+1}
actions: ndarray of first dimension self.batch_size, containing actions
applied at the current time-step, which leads to the observations
rewards and done at the next time-step, i.e. a_t
"""
# Pre-conditions
assert isinstance(observations, np.ndarray)
assert isinstance(raw_rewards, np.ndarray)
assert isinstance(processed_rewards, np.ndarray)
assert isinstance(dones, np.ndarray)
assert isinstance(actions, np.ndarray)
# We assume that we step in all envs, i.e. not like reset where we can reset
# some envs and not others.
assert self.batch_size == observations.shape[0]
assert self.batch_size == raw_rewards.shape[0]
assert self.batch_size == processed_rewards.shape[0]
assert self.batch_size == dones.shape[0]
assert self.batch_size == actions.shape[0]
for index in range(self.batch_size):
trajectory = self._trajectories[index]
# NOTE: If the trajectory isn't active, that means it doesn't have any
# time-steps in it, but we are in step, so the assumption is that it has
# a prior observation from which we are stepping away from.
# TODO(afrozm): Let's re-visit this if it becomes too restrictive.
assert trajectory.is_active
# To this trajectory's last time-step, set actions.
trajectory.change_last_time_step(action=actions[index])
# Create a new time-step to add observation, done & rewards (no actions).
trajectory.add_time_step(
observation=observations[index],
done=dones[index],
raw_reward=raw_rewards[index],
processed_reward=processed_rewards[index])
# If the trajectory is completed, i.e. dones[index] == True, then we
# account for it right-away.
if dones[index]:
self._complete_trajectory(trajectory, index)
# NOTE: The new trajectory at `index` is going to be in-active and
# `reset` should be called on it.
assert not self._trajectories[index].is_active
|
python
|
def step(self, observations, raw_rewards, processed_rewards, dones, actions):
"""Record the information obtained from taking a step in all envs.
Records (observation, rewards, done) in a new time-step and actions in the
current time-step.
If any trajectory gets done, we move that trajectory to
completed_trajectories.
Args:
observations: ndarray of first dimension self.batch_size, which has the
observations after we've stepped, i.e. s_{t+1} where t is the current
state.
raw_rewards: ndarray of first dimension self.batch_size containing raw
rewards i.e. r_{t+1}.
processed_rewards: ndarray of first dimension self.batch_size containing
processed rewards. i.e. r_{t+1}
dones: ndarray of first dimension self.batch_size, containing true at an
index if that env is done, i.e. d_{t+1}
actions: ndarray of first dimension self.batch_size, containing actions
applied at the current time-step, which leads to the observations
rewards and done at the next time-step, i.e. a_t
"""
# Pre-conditions
assert isinstance(observations, np.ndarray)
assert isinstance(raw_rewards, np.ndarray)
assert isinstance(processed_rewards, np.ndarray)
assert isinstance(dones, np.ndarray)
assert isinstance(actions, np.ndarray)
# We assume that we step in all envs, i.e. not like reset where we can reset
# some envs and not others.
assert self.batch_size == observations.shape[0]
assert self.batch_size == raw_rewards.shape[0]
assert self.batch_size == processed_rewards.shape[0]
assert self.batch_size == dones.shape[0]
assert self.batch_size == actions.shape[0]
for index in range(self.batch_size):
trajectory = self._trajectories[index]
# NOTE: If the trajectory isn't active, that means it doesn't have any
# time-steps in it, but we are in step, so the assumption is that it has
# a prior observation from which we are stepping away from.
# TODO(afrozm): Let's re-visit this if it becomes too restrictive.
assert trajectory.is_active
# To this trajectory's last time-step, set actions.
trajectory.change_last_time_step(action=actions[index])
# Create a new time-step to add observation, done & rewards (no actions).
trajectory.add_time_step(
observation=observations[index],
done=dones[index],
raw_reward=raw_rewards[index],
processed_reward=processed_rewards[index])
# If the trajectory is completed, i.e. dones[index] == True, then we
# account for it right-away.
if dones[index]:
self._complete_trajectory(trajectory, index)
# NOTE: The new trajectory at `index` is going to be in-active and
# `reset` should be called on it.
assert not self._trajectories[index].is_active
|
[
"def",
"step",
"(",
"self",
",",
"observations",
",",
"raw_rewards",
",",
"processed_rewards",
",",
"dones",
",",
"actions",
")",
":",
"# Pre-conditions",
"assert",
"isinstance",
"(",
"observations",
",",
"np",
".",
"ndarray",
")",
"assert",
"isinstance",
"(",
"raw_rewards",
",",
"np",
".",
"ndarray",
")",
"assert",
"isinstance",
"(",
"processed_rewards",
",",
"np",
".",
"ndarray",
")",
"assert",
"isinstance",
"(",
"dones",
",",
"np",
".",
"ndarray",
")",
"assert",
"isinstance",
"(",
"actions",
",",
"np",
".",
"ndarray",
")",
"# We assume that we step in all envs, i.e. not like reset where we can reset",
"# some envs and not others.",
"assert",
"self",
".",
"batch_size",
"==",
"observations",
".",
"shape",
"[",
"0",
"]",
"assert",
"self",
".",
"batch_size",
"==",
"raw_rewards",
".",
"shape",
"[",
"0",
"]",
"assert",
"self",
".",
"batch_size",
"==",
"processed_rewards",
".",
"shape",
"[",
"0",
"]",
"assert",
"self",
".",
"batch_size",
"==",
"dones",
".",
"shape",
"[",
"0",
"]",
"assert",
"self",
".",
"batch_size",
"==",
"actions",
".",
"shape",
"[",
"0",
"]",
"for",
"index",
"in",
"range",
"(",
"self",
".",
"batch_size",
")",
":",
"trajectory",
"=",
"self",
".",
"_trajectories",
"[",
"index",
"]",
"# NOTE: If the trajectory isn't active, that means it doesn't have any",
"# time-steps in it, but we are in step, so the assumption is that it has",
"# a prior observation from which we are stepping away from.",
"# TODO(afrozm): Let's re-visit this if it becomes too restrictive.",
"assert",
"trajectory",
".",
"is_active",
"# To this trajectory's last time-step, set actions.",
"trajectory",
".",
"change_last_time_step",
"(",
"action",
"=",
"actions",
"[",
"index",
"]",
")",
"# Create a new time-step to add observation, done & rewards (no actions).",
"trajectory",
".",
"add_time_step",
"(",
"observation",
"=",
"observations",
"[",
"index",
"]",
",",
"done",
"=",
"dones",
"[",
"index",
"]",
",",
"raw_reward",
"=",
"raw_rewards",
"[",
"index",
"]",
",",
"processed_reward",
"=",
"processed_rewards",
"[",
"index",
"]",
")",
"# If the trajectory is completed, i.e. dones[index] == True, then we",
"# account for it right-away.",
"if",
"dones",
"[",
"index",
"]",
":",
"self",
".",
"_complete_trajectory",
"(",
"trajectory",
",",
"index",
")",
"# NOTE: The new trajectory at `index` is going to be in-active and",
"# `reset` should be called on it.",
"assert",
"not",
"self",
".",
"_trajectories",
"[",
"index",
"]",
".",
"is_active"
] |
Record the information obtained from taking a step in all envs.
Records (observation, rewards, done) in a new time-step and actions in the
current time-step.
If any trajectory gets done, we move that trajectory to
completed_trajectories.
Args:
observations: ndarray of first dimension self.batch_size, which has the
observations after we've stepped, i.e. s_{t+1} where t is the current
state.
raw_rewards: ndarray of first dimension self.batch_size containing raw
rewards i.e. r_{t+1}.
processed_rewards: ndarray of first dimension self.batch_size containing
processed rewards. i.e. r_{t+1}
dones: ndarray of first dimension self.batch_size, containing true at an
index if that env is done, i.e. d_{t+1}
actions: ndarray of first dimension self.batch_size, containing actions
applied at the current time-step, which leads to the observations
rewards and done at the next time-step, i.e. a_t
|
[
"Record",
"the",
"information",
"obtained",
"from",
"taking",
"a",
"step",
"in",
"all",
"envs",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L201-L266
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
BatchTrajectory.num_time_steps
|
def num_time_steps(self):
"""Returns the number of time-steps in completed and incomplete trajectories."""
num_time_steps = sum(t.num_time_steps for t in self.trajectories)
return num_time_steps + self.num_completed_time_steps
|
python
|
def num_time_steps(self):
"""Returns the number of time-steps in completed and incomplete trajectories."""
num_time_steps = sum(t.num_time_steps for t in self.trajectories)
return num_time_steps + self.num_completed_time_steps
|
[
"def",
"num_time_steps",
"(",
"self",
")",
":",
"num_time_steps",
"=",
"sum",
"(",
"t",
".",
"num_time_steps",
"for",
"t",
"in",
"self",
".",
"trajectories",
")",
"return",
"num_time_steps",
"+",
"self",
".",
"num_completed_time_steps"
] |
Returns the number of time-steps in completed and incomplete trajectories.
|
[
"Returns",
"the",
"number",
"of",
"time",
"-",
"steps",
"in",
"completed",
"and",
"incomplete",
"trajectories",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L275-L279
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/trajectory.py
|
BatchTrajectory.observations_np
|
def observations_np(self, boundary=20):
"""Pads the observations in all the trajectories and returns them.
Args:
boundary: integer, Observations will be padded to (n * boundary) + 1 where
n is an integer.
Returns:
a tuple(padded_observations, time_steps), with shapes:
padded_observations: (self.batch_size, n * boundary + 1) + OBS
time_steps: integer list of length = self.batch_size
"""
list_observations_np_ts = [t.observations_np for t in self.trajectories]
# Every element in `list_observations_np_ts` is shaped (t,) + OBS
OBS = list_observations_np_ts[0].shape[1:] # pylint: disable=invalid-name
num_time_steps = [t.num_time_steps for t in self.trajectories]
t_max = max(num_time_steps)
# t_max is rounded to the next multiple of `boundary`
boundary = int(boundary)
bucket_length = boundary * int(np.ceil(float(t_max) / boundary))
def padding_config(obs):
# We're padding the first axis only, since that is the time-step.
num_to_pad = bucket_length + 1 - obs.shape[0]
return [(0, num_to_pad)] + [(0, 0)] * len(OBS)
return np.stack([
np.pad(obs, padding_config(obs), "constant")
for obs in list_observations_np_ts]), num_time_steps
|
python
|
def observations_np(self, boundary=20):
"""Pads the observations in all the trajectories and returns them.
Args:
boundary: integer, Observations will be padded to (n * boundary) + 1 where
n is an integer.
Returns:
a tuple(padded_observations, time_steps), with shapes:
padded_observations: (self.batch_size, n * boundary + 1) + OBS
time_steps: integer list of length = self.batch_size
"""
list_observations_np_ts = [t.observations_np for t in self.trajectories]
# Every element in `list_observations_np_ts` is shaped (t,) + OBS
OBS = list_observations_np_ts[0].shape[1:] # pylint: disable=invalid-name
num_time_steps = [t.num_time_steps for t in self.trajectories]
t_max = max(num_time_steps)
# t_max is rounded to the next multiple of `boundary`
boundary = int(boundary)
bucket_length = boundary * int(np.ceil(float(t_max) / boundary))
def padding_config(obs):
# We're padding the first axis only, since that is the time-step.
num_to_pad = bucket_length + 1 - obs.shape[0]
return [(0, num_to_pad)] + [(0, 0)] * len(OBS)
return np.stack([
np.pad(obs, padding_config(obs), "constant")
for obs in list_observations_np_ts]), num_time_steps
|
[
"def",
"observations_np",
"(",
"self",
",",
"boundary",
"=",
"20",
")",
":",
"list_observations_np_ts",
"=",
"[",
"t",
".",
"observations_np",
"for",
"t",
"in",
"self",
".",
"trajectories",
"]",
"# Every element in `list_observations_np_ts` is shaped (t,) + OBS",
"OBS",
"=",
"list_observations_np_ts",
"[",
"0",
"]",
".",
"shape",
"[",
"1",
":",
"]",
"# pylint: disable=invalid-name",
"num_time_steps",
"=",
"[",
"t",
".",
"num_time_steps",
"for",
"t",
"in",
"self",
".",
"trajectories",
"]",
"t_max",
"=",
"max",
"(",
"num_time_steps",
")",
"# t_max is rounded to the next multiple of `boundary`",
"boundary",
"=",
"int",
"(",
"boundary",
")",
"bucket_length",
"=",
"boundary",
"*",
"int",
"(",
"np",
".",
"ceil",
"(",
"float",
"(",
"t_max",
")",
"/",
"boundary",
")",
")",
"def",
"padding_config",
"(",
"obs",
")",
":",
"# We're padding the first axis only, since that is the time-step.",
"num_to_pad",
"=",
"bucket_length",
"+",
"1",
"-",
"obs",
".",
"shape",
"[",
"0",
"]",
"return",
"[",
"(",
"0",
",",
"num_to_pad",
")",
"]",
"+",
"[",
"(",
"0",
",",
"0",
")",
"]",
"*",
"len",
"(",
"OBS",
")",
"return",
"np",
".",
"stack",
"(",
"[",
"np",
".",
"pad",
"(",
"obs",
",",
"padding_config",
"(",
"obs",
")",
",",
"\"constant\"",
")",
"for",
"obs",
"in",
"list_observations_np_ts",
"]",
")",
",",
"num_time_steps"
] |
Pads the observations in all the trajectories and returns them.
Args:
boundary: integer, Observations will be padded to (n * boundary) + 1 where
n is an integer.
Returns:
a tuple(padded_observations, time_steps), with shapes:
padded_observations: (self.batch_size, n * boundary + 1) + OBS
time_steps: integer list of length = self.batch_size
|
[
"Pads",
"the",
"observations",
"in",
"all",
"the",
"trajectories",
"and",
"returns",
"them",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/trajectory.py#L286-L315
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/squad.py
|
_generate_examples
|
def _generate_examples(tmp_dir, dataset_split):
"""Generate squad examples.
Args:
tmp_dir: a string
dataset_split: problem.DatasetSplit.TRAIN or problem.DatasetSplit.EVAL
Yields:
dictionaries representing examples
"""
if dataset_split == problem.DatasetSplit.TRAIN:
file_name = _TRAINING_SET
else:
file_name = _DEV_SET
squad_file = generator_utils.maybe_download(tmp_dir,
file_name,
os.path.join(_URL, file_name))
with tf.gfile.GFile(squad_file, mode="r") as fp:
squad = json.load(fp)
version = squad["version"]
for article in squad["data"]:
if "title" in article:
title = article["title"].strip()
else:
title = "no title"
for paragraph in article["paragraphs"]:
context = paragraph["context"].strip()
for qa in paragraph["qas"]:
question = qa["question"].strip()
id_ = qa["id"]
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
answers = [answer["text"].strip() for answer in qa["answers"]]
# Features currently used are "context", "question", and "answers".
# Others are extracted here for the ease of future expansions.
example = {
"version": version,
"title": title,
"context": context,
"question": question,
"id": id_,
"answer_starts": answer_starts,
"answers": answers,
"num_answers": len(answers),
"is_supervised": True,
}
yield example
|
python
|
def _generate_examples(tmp_dir, dataset_split):
"""Generate squad examples.
Args:
tmp_dir: a string
dataset_split: problem.DatasetSplit.TRAIN or problem.DatasetSplit.EVAL
Yields:
dictionaries representing examples
"""
if dataset_split == problem.DatasetSplit.TRAIN:
file_name = _TRAINING_SET
else:
file_name = _DEV_SET
squad_file = generator_utils.maybe_download(tmp_dir,
file_name,
os.path.join(_URL, file_name))
with tf.gfile.GFile(squad_file, mode="r") as fp:
squad = json.load(fp)
version = squad["version"]
for article in squad["data"]:
if "title" in article:
title = article["title"].strip()
else:
title = "no title"
for paragraph in article["paragraphs"]:
context = paragraph["context"].strip()
for qa in paragraph["qas"]:
question = qa["question"].strip()
id_ = qa["id"]
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
answers = [answer["text"].strip() for answer in qa["answers"]]
# Features currently used are "context", "question", and "answers".
# Others are extracted here for the ease of future expansions.
example = {
"version": version,
"title": title,
"context": context,
"question": question,
"id": id_,
"answer_starts": answer_starts,
"answers": answers,
"num_answers": len(answers),
"is_supervised": True,
}
yield example
|
[
"def",
"_generate_examples",
"(",
"tmp_dir",
",",
"dataset_split",
")",
":",
"if",
"dataset_split",
"==",
"problem",
".",
"DatasetSplit",
".",
"TRAIN",
":",
"file_name",
"=",
"_TRAINING_SET",
"else",
":",
"file_name",
"=",
"_DEV_SET",
"squad_file",
"=",
"generator_utils",
".",
"maybe_download",
"(",
"tmp_dir",
",",
"file_name",
",",
"os",
".",
"path",
".",
"join",
"(",
"_URL",
",",
"file_name",
")",
")",
"with",
"tf",
".",
"gfile",
".",
"GFile",
"(",
"squad_file",
",",
"mode",
"=",
"\"r\"",
")",
"as",
"fp",
":",
"squad",
"=",
"json",
".",
"load",
"(",
"fp",
")",
"version",
"=",
"squad",
"[",
"\"version\"",
"]",
"for",
"article",
"in",
"squad",
"[",
"\"data\"",
"]",
":",
"if",
"\"title\"",
"in",
"article",
":",
"title",
"=",
"article",
"[",
"\"title\"",
"]",
".",
"strip",
"(",
")",
"else",
":",
"title",
"=",
"\"no title\"",
"for",
"paragraph",
"in",
"article",
"[",
"\"paragraphs\"",
"]",
":",
"context",
"=",
"paragraph",
"[",
"\"context\"",
"]",
".",
"strip",
"(",
")",
"for",
"qa",
"in",
"paragraph",
"[",
"\"qas\"",
"]",
":",
"question",
"=",
"qa",
"[",
"\"question\"",
"]",
".",
"strip",
"(",
")",
"id_",
"=",
"qa",
"[",
"\"id\"",
"]",
"answer_starts",
"=",
"[",
"answer",
"[",
"\"answer_start\"",
"]",
"for",
"answer",
"in",
"qa",
"[",
"\"answers\"",
"]",
"]",
"answers",
"=",
"[",
"answer",
"[",
"\"text\"",
"]",
".",
"strip",
"(",
")",
"for",
"answer",
"in",
"qa",
"[",
"\"answers\"",
"]",
"]",
"# Features currently used are \"context\", \"question\", and \"answers\".",
"# Others are extracted here for the ease of future expansions.",
"example",
"=",
"{",
"\"version\"",
":",
"version",
",",
"\"title\"",
":",
"title",
",",
"\"context\"",
":",
"context",
",",
"\"question\"",
":",
"question",
",",
"\"id\"",
":",
"id_",
",",
"\"answer_starts\"",
":",
"answer_starts",
",",
"\"answers\"",
":",
"answers",
",",
"\"num_answers\"",
":",
"len",
"(",
"answers",
")",
",",
"\"is_supervised\"",
":",
"True",
",",
"}",
"yield",
"example"
] |
Generate squad examples.
Args:
tmp_dir: a string
dataset_split: problem.DatasetSplit.TRAIN or problem.DatasetSplit.EVAL
Yields:
dictionaries representing examples
|
[
"Generate",
"squad",
"examples",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/squad.py#L39-L85
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
self_attention_layer
|
def self_attention_layer(hparams, prefix):
"""Create self-attention layer based on hyperparameters."""
return transformer_layers.SelfAttention(
num_heads=hparams.get(prefix + "num_heads"),
num_memory_heads=hparams.get(prefix + "num_memory_heads"),
key_value_size=hparams.d_kv,
shared_kv=hparams.get(prefix + "shared_kv", False),
attention_kwargs=attention_kwargs_from_hparams(hparams))
|
python
|
def self_attention_layer(hparams, prefix):
"""Create self-attention layer based on hyperparameters."""
return transformer_layers.SelfAttention(
num_heads=hparams.get(prefix + "num_heads"),
num_memory_heads=hparams.get(prefix + "num_memory_heads"),
key_value_size=hparams.d_kv,
shared_kv=hparams.get(prefix + "shared_kv", False),
attention_kwargs=attention_kwargs_from_hparams(hparams))
|
[
"def",
"self_attention_layer",
"(",
"hparams",
",",
"prefix",
")",
":",
"return",
"transformer_layers",
".",
"SelfAttention",
"(",
"num_heads",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"num_heads\"",
")",
",",
"num_memory_heads",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"num_memory_heads\"",
")",
",",
"key_value_size",
"=",
"hparams",
".",
"d_kv",
",",
"shared_kv",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"shared_kv\"",
",",
"False",
")",
",",
"attention_kwargs",
"=",
"attention_kwargs_from_hparams",
"(",
"hparams",
")",
")"
] |
Create self-attention layer based on hyperparameters.
|
[
"Create",
"self",
"-",
"attention",
"layer",
"based",
"on",
"hyperparameters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L311-L318
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
local_self_attention_layer
|
def local_self_attention_layer(hparams, prefix):
"""Create self-attention layer based on hyperparameters."""
return transformer_layers.LocalSelfAttention(
num_heads=hparams.get(prefix + "num_heads"),
num_memory_heads=hparams.get(prefix + "num_memory_heads"),
radius=hparams.local_attention_radius,
key_value_size=hparams.d_kv,
shared_kv=hparams.get(prefix + "shared_kv", False),
attention_kwargs=attention_kwargs_from_hparams(hparams))
|
python
|
def local_self_attention_layer(hparams, prefix):
"""Create self-attention layer based on hyperparameters."""
return transformer_layers.LocalSelfAttention(
num_heads=hparams.get(prefix + "num_heads"),
num_memory_heads=hparams.get(prefix + "num_memory_heads"),
radius=hparams.local_attention_radius,
key_value_size=hparams.d_kv,
shared_kv=hparams.get(prefix + "shared_kv", False),
attention_kwargs=attention_kwargs_from_hparams(hparams))
|
[
"def",
"local_self_attention_layer",
"(",
"hparams",
",",
"prefix",
")",
":",
"return",
"transformer_layers",
".",
"LocalSelfAttention",
"(",
"num_heads",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"num_heads\"",
")",
",",
"num_memory_heads",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"num_memory_heads\"",
")",
",",
"radius",
"=",
"hparams",
".",
"local_attention_radius",
",",
"key_value_size",
"=",
"hparams",
".",
"d_kv",
",",
"shared_kv",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"shared_kv\"",
",",
"False",
")",
",",
"attention_kwargs",
"=",
"attention_kwargs_from_hparams",
"(",
"hparams",
")",
")"
] |
Create self-attention layer based on hyperparameters.
|
[
"Create",
"self",
"-",
"attention",
"layer",
"based",
"on",
"hyperparameters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L322-L330
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
layer_stack_from_hparams
|
def layer_stack_from_hparams(hparams, prefix):
"""Create a layer stack based on the hyperparameter values."""
layers = hparams.get(prefix + "layers")
return transformer.LayerStack(
[layers_registry[l](hparams, prefix) for l in layers],
dropout_rate=hparams.layer_prepostprocess_dropout,
norm_epsilon=hparams.norm_epsilon)
|
python
|
def layer_stack_from_hparams(hparams, prefix):
"""Create a layer stack based on the hyperparameter values."""
layers = hparams.get(prefix + "layers")
return transformer.LayerStack(
[layers_registry[l](hparams, prefix) for l in layers],
dropout_rate=hparams.layer_prepostprocess_dropout,
norm_epsilon=hparams.norm_epsilon)
|
[
"def",
"layer_stack_from_hparams",
"(",
"hparams",
",",
"prefix",
")",
":",
"layers",
"=",
"hparams",
".",
"get",
"(",
"prefix",
"+",
"\"layers\"",
")",
"return",
"transformer",
".",
"LayerStack",
"(",
"[",
"layers_registry",
"[",
"l",
"]",
"(",
"hparams",
",",
"prefix",
")",
"for",
"l",
"in",
"layers",
"]",
",",
"dropout_rate",
"=",
"hparams",
".",
"layer_prepostprocess_dropout",
",",
"norm_epsilon",
"=",
"hparams",
".",
"norm_epsilon",
")"
] |
Create a layer stack based on the hyperparameter values.
|
[
"Create",
"a",
"layer",
"stack",
"based",
"on",
"the",
"hyperparameter",
"values",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L366-L372
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtf_unitransformer_base
|
def mtf_unitransformer_base():
"""Hyperparameters for single-stack Transformer."""
hparams = mtf_transformer2_base()
hparams.add_hparam("autoregressive", True)
# HYPERPARAMETERS FOR THE SINGLE LAYER STACK
hparams.add_hparam("layers", ["self_att", "drd"] * 6)
# number of heads in multihead attention
hparams.add_hparam("num_heads", 8)
# default of 0 for standard transformer behavior
# 1 means a single set of keys and values that are read by all query heads
hparams.add_hparam("num_memory_heads", 0)
# share attention keys and values
hparams.add_hparam("shared_kv", False)
# if nonzero then use local attention
hparams.add_hparam("local_attention_radius", 128)
return hparams
|
python
|
def mtf_unitransformer_base():
"""Hyperparameters for single-stack Transformer."""
hparams = mtf_transformer2_base()
hparams.add_hparam("autoregressive", True)
# HYPERPARAMETERS FOR THE SINGLE LAYER STACK
hparams.add_hparam("layers", ["self_att", "drd"] * 6)
# number of heads in multihead attention
hparams.add_hparam("num_heads", 8)
# default of 0 for standard transformer behavior
# 1 means a single set of keys and values that are read by all query heads
hparams.add_hparam("num_memory_heads", 0)
# share attention keys and values
hparams.add_hparam("shared_kv", False)
# if nonzero then use local attention
hparams.add_hparam("local_attention_radius", 128)
return hparams
|
[
"def",
"mtf_unitransformer_base",
"(",
")",
":",
"hparams",
"=",
"mtf_transformer2_base",
"(",
")",
"hparams",
".",
"add_hparam",
"(",
"\"autoregressive\"",
",",
"True",
")",
"# HYPERPARAMETERS FOR THE SINGLE LAYER STACK",
"hparams",
".",
"add_hparam",
"(",
"\"layers\"",
",",
"[",
"\"self_att\"",
",",
"\"drd\"",
"]",
"*",
"6",
")",
"# number of heads in multihead attention",
"hparams",
".",
"add_hparam",
"(",
"\"num_heads\"",
",",
"8",
")",
"# default of 0 for standard transformer behavior",
"# 1 means a single set of keys and values that are read by all query heads",
"hparams",
".",
"add_hparam",
"(",
"\"num_memory_heads\"",
",",
"0",
")",
"# share attention keys and values",
"hparams",
".",
"add_hparam",
"(",
"\"shared_kv\"",
",",
"False",
")",
"# if nonzero then use local attention",
"hparams",
".",
"add_hparam",
"(",
"\"local_attention_radius\"",
",",
"128",
")",
"return",
"hparams"
] |
Hyperparameters for single-stack Transformer.
|
[
"Hyperparameters",
"for",
"single",
"-",
"stack",
"Transformer",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L454-L469
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtf_bitransformer_base
|
def mtf_bitransformer_base():
"""Machine translation base configuration."""
hparams = mtf_transformer2_base()
hparams.max_length = 256
hparams.shared_embedding = True
# HYPERPARAMETERS FOR THE LAYER STACKS
hparams.add_hparam("encoder_layers", ["self_att", "drd"] * 6)
hparams.add_hparam("decoder_layers", ["self_att", "enc_att", "drd"] * 6)
hparams.add_hparam("encoder_num_layers", 6)
hparams.add_hparam("decoder_num_layers", 6)
# number of heads in multihead attention
hparams.add_hparam("encoder_num_heads", 8)
hparams.add_hparam("decoder_num_heads", 8)
hparams.add_hparam("local_attention_radius", 128)
# default of 0 for standard transformer behavior
# 1 means a single set of keys and values that are read by all query heads
hparams.add_hparam("encoder_num_memory_heads", 0)
hparams.add_hparam("decoder_num_memory_heads", 0)
# share attention keys and values
hparams.add_hparam("encoder_shared_kv", False)
hparams.add_hparam("decoder_shared_kv", False)
# Parameters for computing the maximum decode length in beam search.
# Maximum decode length is:
# min(max_length,
# decode_length_multiplier * input_length + decode_length_constant)
hparams.add_hparam("decode_length_multiplier", 1.5)
hparams.add_hparam("decode_length_constant", 10.0)
# used during decoding
hparams.add_hparam("alpha", 0.6)
hparams.sampling_temp = 0.0
return hparams
|
python
|
def mtf_bitransformer_base():
"""Machine translation base configuration."""
hparams = mtf_transformer2_base()
hparams.max_length = 256
hparams.shared_embedding = True
# HYPERPARAMETERS FOR THE LAYER STACKS
hparams.add_hparam("encoder_layers", ["self_att", "drd"] * 6)
hparams.add_hparam("decoder_layers", ["self_att", "enc_att", "drd"] * 6)
hparams.add_hparam("encoder_num_layers", 6)
hparams.add_hparam("decoder_num_layers", 6)
# number of heads in multihead attention
hparams.add_hparam("encoder_num_heads", 8)
hparams.add_hparam("decoder_num_heads", 8)
hparams.add_hparam("local_attention_radius", 128)
# default of 0 for standard transformer behavior
# 1 means a single set of keys and values that are read by all query heads
hparams.add_hparam("encoder_num_memory_heads", 0)
hparams.add_hparam("decoder_num_memory_heads", 0)
# share attention keys and values
hparams.add_hparam("encoder_shared_kv", False)
hparams.add_hparam("decoder_shared_kv", False)
# Parameters for computing the maximum decode length in beam search.
# Maximum decode length is:
# min(max_length,
# decode_length_multiplier * input_length + decode_length_constant)
hparams.add_hparam("decode_length_multiplier", 1.5)
hparams.add_hparam("decode_length_constant", 10.0)
# used during decoding
hparams.add_hparam("alpha", 0.6)
hparams.sampling_temp = 0.0
return hparams
|
[
"def",
"mtf_bitransformer_base",
"(",
")",
":",
"hparams",
"=",
"mtf_transformer2_base",
"(",
")",
"hparams",
".",
"max_length",
"=",
"256",
"hparams",
".",
"shared_embedding",
"=",
"True",
"# HYPERPARAMETERS FOR THE LAYER STACKS",
"hparams",
".",
"add_hparam",
"(",
"\"encoder_layers\"",
",",
"[",
"\"self_att\"",
",",
"\"drd\"",
"]",
"*",
"6",
")",
"hparams",
".",
"add_hparam",
"(",
"\"decoder_layers\"",
",",
"[",
"\"self_att\"",
",",
"\"enc_att\"",
",",
"\"drd\"",
"]",
"*",
"6",
")",
"hparams",
".",
"add_hparam",
"(",
"\"encoder_num_layers\"",
",",
"6",
")",
"hparams",
".",
"add_hparam",
"(",
"\"decoder_num_layers\"",
",",
"6",
")",
"# number of heads in multihead attention",
"hparams",
".",
"add_hparam",
"(",
"\"encoder_num_heads\"",
",",
"8",
")",
"hparams",
".",
"add_hparam",
"(",
"\"decoder_num_heads\"",
",",
"8",
")",
"hparams",
".",
"add_hparam",
"(",
"\"local_attention_radius\"",
",",
"128",
")",
"# default of 0 for standard transformer behavior",
"# 1 means a single set of keys and values that are read by all query heads",
"hparams",
".",
"add_hparam",
"(",
"\"encoder_num_memory_heads\"",
",",
"0",
")",
"hparams",
".",
"add_hparam",
"(",
"\"decoder_num_memory_heads\"",
",",
"0",
")",
"# share attention keys and values",
"hparams",
".",
"add_hparam",
"(",
"\"encoder_shared_kv\"",
",",
"False",
")",
"hparams",
".",
"add_hparam",
"(",
"\"decoder_shared_kv\"",
",",
"False",
")",
"# Parameters for computing the maximum decode length in beam search.",
"# Maximum decode length is:",
"# min(max_length,",
"# decode_length_multiplier * input_length + decode_length_constant)",
"hparams",
".",
"add_hparam",
"(",
"\"decode_length_multiplier\"",
",",
"1.5",
")",
"hparams",
".",
"add_hparam",
"(",
"\"decode_length_constant\"",
",",
"10.0",
")",
"# used during decoding",
"hparams",
".",
"add_hparam",
"(",
"\"alpha\"",
",",
"0.6",
")",
"hparams",
".",
"sampling_temp",
"=",
"0.0",
"return",
"hparams"
] |
Machine translation base configuration.
|
[
"Machine",
"translation",
"base",
"configuration",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L473-L505
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtf_bitransformer_tiny
|
def mtf_bitransformer_tiny():
"""Small encoder-decoder model for testing."""
hparams = mtf_bitransformer_base()
hparams.batch_size = 2
hparams.mesh_shape = ""
hparams.d_model = 128
hparams.encoder_layers = ["self_att", "drd"] * 2
hparams.decoder_layers = ["self_att", "enc_att", "drd"] * 2
hparams.num_heads = 4
hparams.d_ff = 512
return hparams
|
python
|
def mtf_bitransformer_tiny():
"""Small encoder-decoder model for testing."""
hparams = mtf_bitransformer_base()
hparams.batch_size = 2
hparams.mesh_shape = ""
hparams.d_model = 128
hparams.encoder_layers = ["self_att", "drd"] * 2
hparams.decoder_layers = ["self_att", "enc_att", "drd"] * 2
hparams.num_heads = 4
hparams.d_ff = 512
return hparams
|
[
"def",
"mtf_bitransformer_tiny",
"(",
")",
":",
"hparams",
"=",
"mtf_bitransformer_base",
"(",
")",
"hparams",
".",
"batch_size",
"=",
"2",
"hparams",
".",
"mesh_shape",
"=",
"\"\"",
"hparams",
".",
"d_model",
"=",
"128",
"hparams",
".",
"encoder_layers",
"=",
"[",
"\"self_att\"",
",",
"\"drd\"",
"]",
"*",
"2",
"hparams",
".",
"decoder_layers",
"=",
"[",
"\"self_att\"",
",",
"\"enc_att\"",
",",
"\"drd\"",
"]",
"*",
"2",
"hparams",
".",
"num_heads",
"=",
"4",
"hparams",
".",
"d_ff",
"=",
"512",
"return",
"hparams"
] |
Small encoder-decoder model for testing.
|
[
"Small",
"encoder",
"-",
"decoder",
"model",
"for",
"testing",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L521-L531
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtf_unitransformer_all_layers_tiny
|
def mtf_unitransformer_all_layers_tiny():
"""Test out all the layers on local CPU."""
hparams = mtf_unitransformer_tiny()
hparams.moe_num_experts = 4
hparams.moe_expert_x = 4
hparams.moe_expert_y = 4
hparams.moe_hidden_size = 512
hparams.layers = ["self_att", "local_self_att", "moe_1d", "moe_2d", "drd"]
return hparams
|
python
|
def mtf_unitransformer_all_layers_tiny():
"""Test out all the layers on local CPU."""
hparams = mtf_unitransformer_tiny()
hparams.moe_num_experts = 4
hparams.moe_expert_x = 4
hparams.moe_expert_y = 4
hparams.moe_hidden_size = 512
hparams.layers = ["self_att", "local_self_att", "moe_1d", "moe_2d", "drd"]
return hparams
|
[
"def",
"mtf_unitransformer_all_layers_tiny",
"(",
")",
":",
"hparams",
"=",
"mtf_unitransformer_tiny",
"(",
")",
"hparams",
".",
"moe_num_experts",
"=",
"4",
"hparams",
".",
"moe_expert_x",
"=",
"4",
"hparams",
".",
"moe_expert_y",
"=",
"4",
"hparams",
".",
"moe_hidden_size",
"=",
"512",
"hparams",
".",
"layers",
"=",
"[",
"\"self_att\"",
",",
"\"local_self_att\"",
",",
"\"moe_1d\"",
",",
"\"moe_2d\"",
",",
"\"drd\"",
"]",
"return",
"hparams"
] |
Test out all the layers on local CPU.
|
[
"Test",
"out",
"all",
"the",
"layers",
"on",
"local",
"CPU",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L535-L543
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtf_bitransformer_all_layers_tiny
|
def mtf_bitransformer_all_layers_tiny():
"""Test out all the layers on local CPU."""
hparams = mtf_bitransformer_tiny()
hparams.moe_num_experts = 4
hparams.moe_expert_x = 4
hparams.moe_expert_y = 4
hparams.moe_hidden_size = 512
hparams.encoder_layers = [
"self_att", "local_self_att", "moe_1d", "moe_2d", "drd"]
hparams.decoder_layers = [
"self_att", "local_self_att", "enc_att", "moe_1d", "moe_2d", "drd"]
return hparams
|
python
|
def mtf_bitransformer_all_layers_tiny():
"""Test out all the layers on local CPU."""
hparams = mtf_bitransformer_tiny()
hparams.moe_num_experts = 4
hparams.moe_expert_x = 4
hparams.moe_expert_y = 4
hparams.moe_hidden_size = 512
hparams.encoder_layers = [
"self_att", "local_self_att", "moe_1d", "moe_2d", "drd"]
hparams.decoder_layers = [
"self_att", "local_self_att", "enc_att", "moe_1d", "moe_2d", "drd"]
return hparams
|
[
"def",
"mtf_bitransformer_all_layers_tiny",
"(",
")",
":",
"hparams",
"=",
"mtf_bitransformer_tiny",
"(",
")",
"hparams",
".",
"moe_num_experts",
"=",
"4",
"hparams",
".",
"moe_expert_x",
"=",
"4",
"hparams",
".",
"moe_expert_y",
"=",
"4",
"hparams",
".",
"moe_hidden_size",
"=",
"512",
"hparams",
".",
"encoder_layers",
"=",
"[",
"\"self_att\"",
",",
"\"local_self_att\"",
",",
"\"moe_1d\"",
",",
"\"moe_2d\"",
",",
"\"drd\"",
"]",
"hparams",
".",
"decoder_layers",
"=",
"[",
"\"self_att\"",
",",
"\"local_self_att\"",
",",
"\"enc_att\"",
",",
"\"moe_1d\"",
",",
"\"moe_2d\"",
",",
"\"drd\"",
"]",
"return",
"hparams"
] |
Test out all the layers on local CPU.
|
[
"Test",
"out",
"all",
"the",
"layers",
"on",
"local",
"CPU",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L547-L558
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtr_lm_dense
|
def mtr_lm_dense(sz):
"""Series of architectures for language modeling.
We assume infinite training data, so no dropout necessary.
You can use languagemodel_wiki_noref_v32k_l1k.
(1 epoch = ~46000 steps).
TODO(noam): find a large enough dataset for these experiments.
Args:
sz: an integer
Returns:
a hparams
"""
n = 2 ** sz
hparams = mtf_unitransformer_base()
hparams.d_model = 1024
hparams.max_length = 1024
hparams.batch_size = 128
# Parameters for my_layer_stack()
hparams.num_hidden_layers = 6
hparams.d_ff = 8192 * n
hparams.d_kv = 256
hparams.num_heads = 8 * n
hparams.learning_rate_decay_steps = 65536
hparams.layout = "batch:batch;vocab:model;d_ff:model;heads:model"
hparams.mesh_shape = "batch:32"
return hparams
|
python
|
def mtr_lm_dense(sz):
"""Series of architectures for language modeling.
We assume infinite training data, so no dropout necessary.
You can use languagemodel_wiki_noref_v32k_l1k.
(1 epoch = ~46000 steps).
TODO(noam): find a large enough dataset for these experiments.
Args:
sz: an integer
Returns:
a hparams
"""
n = 2 ** sz
hparams = mtf_unitransformer_base()
hparams.d_model = 1024
hparams.max_length = 1024
hparams.batch_size = 128
# Parameters for my_layer_stack()
hparams.num_hidden_layers = 6
hparams.d_ff = 8192 * n
hparams.d_kv = 256
hparams.num_heads = 8 * n
hparams.learning_rate_decay_steps = 65536
hparams.layout = "batch:batch;vocab:model;d_ff:model;heads:model"
hparams.mesh_shape = "batch:32"
return hparams
|
[
"def",
"mtr_lm_dense",
"(",
"sz",
")",
":",
"n",
"=",
"2",
"**",
"sz",
"hparams",
"=",
"mtf_unitransformer_base",
"(",
")",
"hparams",
".",
"d_model",
"=",
"1024",
"hparams",
".",
"max_length",
"=",
"1024",
"hparams",
".",
"batch_size",
"=",
"128",
"# Parameters for my_layer_stack()",
"hparams",
".",
"num_hidden_layers",
"=",
"6",
"hparams",
".",
"d_ff",
"=",
"8192",
"*",
"n",
"hparams",
".",
"d_kv",
"=",
"256",
"hparams",
".",
"num_heads",
"=",
"8",
"*",
"n",
"hparams",
".",
"learning_rate_decay_steps",
"=",
"65536",
"hparams",
".",
"layout",
"=",
"\"batch:batch;vocab:model;d_ff:model;heads:model\"",
"hparams",
".",
"mesh_shape",
"=",
"\"batch:32\"",
"return",
"hparams"
] |
Series of architectures for language modeling.
We assume infinite training data, so no dropout necessary.
You can use languagemodel_wiki_noref_v32k_l1k.
(1 epoch = ~46000 steps).
TODO(noam): find a large enough dataset for these experiments.
Args:
sz: an integer
Returns:
a hparams
|
[
"Series",
"of",
"architectures",
"for",
"language",
"modeling",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L562-L590
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtr_lm_v1
|
def mtr_lm_v1():
"""Model incorporating mixture-of-experts, local and global attention.
~6B parameters
32 experts in 3 hierarchichal moe layers.
Returns:
a hparams
"""
hparams = mtr_lm_dense(0)
hparams.layers = (["local_self_att", "local_self_att", "drd",
"self_att", "drd", "local_self_att",
"local_self_att", "moe_2d"] * 4)[:-1]
hparams.d_kv = 128
hparams.moe_expert_x = 8
hparams.moe_expert_y = 4
hparams.moe_hidden_size = 32768
hparams.d_ff = 2048
hparams.num_memory_heads = 0
hparams.mesh_shape = "b0:4;b1:8"
hparams.layout = "outer_batch:b0;inner_batch:b1,expert_x:b1,expert_y:b0"
hparams.outer_batch_size = 4
return hparams
|
python
|
def mtr_lm_v1():
"""Model incorporating mixture-of-experts, local and global attention.
~6B parameters
32 experts in 3 hierarchichal moe layers.
Returns:
a hparams
"""
hparams = mtr_lm_dense(0)
hparams.layers = (["local_self_att", "local_self_att", "drd",
"self_att", "drd", "local_self_att",
"local_self_att", "moe_2d"] * 4)[:-1]
hparams.d_kv = 128
hparams.moe_expert_x = 8
hparams.moe_expert_y = 4
hparams.moe_hidden_size = 32768
hparams.d_ff = 2048
hparams.num_memory_heads = 0
hparams.mesh_shape = "b0:4;b1:8"
hparams.layout = "outer_batch:b0;inner_batch:b1,expert_x:b1,expert_y:b0"
hparams.outer_batch_size = 4
return hparams
|
[
"def",
"mtr_lm_v1",
"(",
")",
":",
"hparams",
"=",
"mtr_lm_dense",
"(",
"0",
")",
"hparams",
".",
"layers",
"=",
"(",
"[",
"\"local_self_att\"",
",",
"\"local_self_att\"",
",",
"\"drd\"",
",",
"\"self_att\"",
",",
"\"drd\"",
",",
"\"local_self_att\"",
",",
"\"local_self_att\"",
",",
"\"moe_2d\"",
"]",
"*",
"4",
")",
"[",
":",
"-",
"1",
"]",
"hparams",
".",
"d_kv",
"=",
"128",
"hparams",
".",
"moe_expert_x",
"=",
"8",
"hparams",
".",
"moe_expert_y",
"=",
"4",
"hparams",
".",
"moe_hidden_size",
"=",
"32768",
"hparams",
".",
"d_ff",
"=",
"2048",
"hparams",
".",
"num_memory_heads",
"=",
"0",
"hparams",
".",
"mesh_shape",
"=",
"\"b0:4;b1:8\"",
"hparams",
".",
"layout",
"=",
"\"outer_batch:b0;inner_batch:b1,expert_x:b1,expert_y:b0\"",
"hparams",
".",
"outer_batch_size",
"=",
"4",
"return",
"hparams"
] |
Model incorporating mixture-of-experts, local and global attention.
~6B parameters
32 experts in 3 hierarchichal moe layers.
Returns:
a hparams
|
[
"Model",
"incorporating",
"mixture",
"-",
"of",
"-",
"experts",
"local",
"and",
"global",
"attention",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L626-L649
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtr_tr_dense
|
def mtr_tr_dense(sz):
"""Series of machine translation models.
All models are trained on sequences of 256 tokens.
You can use the dataset translate_enfr_wmt32k_packed.
154000 steps = 3 epochs.
Args:
sz: an integer
Returns:
a hparams
"""
n = 2 ** sz
hparams = mtf_bitransformer_base()
hparams.d_model = 1024
hparams.max_length = 256
hparams.batch_size = 128
hparams.d_ff = int(4096 * n)
hparams.d_kv = 128
hparams.encoder_num_heads = int(8 * n)
hparams.decoder_num_heads = int(8 * n)
# one epoch for translate_enfr_wmt32k_packed = 51400 steps
hparams.learning_rate_decay_steps = 51400
hparams.layout = "batch:batch;vocab:model;d_ff:model;heads:model"
hparams.mesh_shape = "batch:32"
hparams.label_smoothing = 0.1
hparams.layer_prepostprocess_dropout = 0.1
hparams.attention_dropout = 0.1
hparams.relu_dropout = 0.1
return hparams
|
python
|
def mtr_tr_dense(sz):
"""Series of machine translation models.
All models are trained on sequences of 256 tokens.
You can use the dataset translate_enfr_wmt32k_packed.
154000 steps = 3 epochs.
Args:
sz: an integer
Returns:
a hparams
"""
n = 2 ** sz
hparams = mtf_bitransformer_base()
hparams.d_model = 1024
hparams.max_length = 256
hparams.batch_size = 128
hparams.d_ff = int(4096 * n)
hparams.d_kv = 128
hparams.encoder_num_heads = int(8 * n)
hparams.decoder_num_heads = int(8 * n)
# one epoch for translate_enfr_wmt32k_packed = 51400 steps
hparams.learning_rate_decay_steps = 51400
hparams.layout = "batch:batch;vocab:model;d_ff:model;heads:model"
hparams.mesh_shape = "batch:32"
hparams.label_smoothing = 0.1
hparams.layer_prepostprocess_dropout = 0.1
hparams.attention_dropout = 0.1
hparams.relu_dropout = 0.1
return hparams
|
[
"def",
"mtr_tr_dense",
"(",
"sz",
")",
":",
"n",
"=",
"2",
"**",
"sz",
"hparams",
"=",
"mtf_bitransformer_base",
"(",
")",
"hparams",
".",
"d_model",
"=",
"1024",
"hparams",
".",
"max_length",
"=",
"256",
"hparams",
".",
"batch_size",
"=",
"128",
"hparams",
".",
"d_ff",
"=",
"int",
"(",
"4096",
"*",
"n",
")",
"hparams",
".",
"d_kv",
"=",
"128",
"hparams",
".",
"encoder_num_heads",
"=",
"int",
"(",
"8",
"*",
"n",
")",
"hparams",
".",
"decoder_num_heads",
"=",
"int",
"(",
"8",
"*",
"n",
")",
"# one epoch for translate_enfr_wmt32k_packed = 51400 steps",
"hparams",
".",
"learning_rate_decay_steps",
"=",
"51400",
"hparams",
".",
"layout",
"=",
"\"batch:batch;vocab:model;d_ff:model;heads:model\"",
"hparams",
".",
"mesh_shape",
"=",
"\"batch:32\"",
"hparams",
".",
"label_smoothing",
"=",
"0.1",
"hparams",
".",
"layer_prepostprocess_dropout",
"=",
"0.1",
"hparams",
".",
"attention_dropout",
"=",
"0.1",
"hparams",
".",
"relu_dropout",
"=",
"0.1",
"return",
"hparams"
] |
Series of machine translation models.
All models are trained on sequences of 256 tokens.
You can use the dataset translate_enfr_wmt32k_packed.
154000 steps = 3 epochs.
Args:
sz: an integer
Returns:
a hparams
|
[
"Series",
"of",
"machine",
"translation",
"models",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L660-L691
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_transformer2.py
|
mtr_tr_dense_local
|
def mtr_tr_dense_local(sz):
"""With local self-attention in the decoder."""
hparams = mtr_tr_dense(sz)
hparams.decoder_layers = ["local_self_att", "enc_att", "drd"] * 6
hparams.local_attention_radius = 32
return hparams
|
python
|
def mtr_tr_dense_local(sz):
"""With local self-attention in the decoder."""
hparams = mtr_tr_dense(sz)
hparams.decoder_layers = ["local_self_att", "enc_att", "drd"] * 6
hparams.local_attention_radius = 32
return hparams
|
[
"def",
"mtr_tr_dense_local",
"(",
"sz",
")",
":",
"hparams",
"=",
"mtr_tr_dense",
"(",
"sz",
")",
"hparams",
".",
"decoder_layers",
"=",
"[",
"\"local_self_att\"",
",",
"\"enc_att\"",
",",
"\"drd\"",
"]",
"*",
"6",
"hparams",
".",
"local_attention_radius",
"=",
"32",
"return",
"hparams"
] |
With local self-attention in the decoder.
|
[
"With",
"local",
"self",
"-",
"attention",
"in",
"the",
"decoder",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_transformer2.py#L734-L739
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/vqa_recurrent_self_attention.py
|
recurrent_transformer_decoder
|
def recurrent_transformer_decoder(
decoder_input,
encoder_output,
decoder_self_attention_bias,
encoder_decoder_attention_bias,
hparams,
name="decoder",
nonpadding=None,
save_weights_to=None,
make_image_summary=True):
"""Recurrent decoder function."""
x = decoder_input
attention_dropout_broadcast_dims = (
common_layers.comma_separated_string_to_integer_list(
getattr(hparams, "attention_dropout_broadcast_dims", "")))
with tf.variable_scope(name):
ffn_unit = functools.partial(
# use encoder ffn, since decoder ffn use left padding
universal_transformer_util.transformer_encoder_ffn_unit,
hparams=hparams,
nonpadding_mask=nonpadding)
attention_unit = functools.partial(
universal_transformer_util.transformer_decoder_attention_unit,
hparams=hparams,
encoder_output=encoder_output,
decoder_self_attention_bias=decoder_self_attention_bias,
encoder_decoder_attention_bias=encoder_decoder_attention_bias,
attention_dropout_broadcast_dims=attention_dropout_broadcast_dims,
save_weights_to=save_weights_to,
make_image_summary=make_image_summary)
x, extra_output = universal_transformer_util.universal_transformer_layer(
x, hparams, ffn_unit, attention_unit)
return common_layers.layer_preprocess(x, hparams), extra_output
|
python
|
def recurrent_transformer_decoder(
decoder_input,
encoder_output,
decoder_self_attention_bias,
encoder_decoder_attention_bias,
hparams,
name="decoder",
nonpadding=None,
save_weights_to=None,
make_image_summary=True):
"""Recurrent decoder function."""
x = decoder_input
attention_dropout_broadcast_dims = (
common_layers.comma_separated_string_to_integer_list(
getattr(hparams, "attention_dropout_broadcast_dims", "")))
with tf.variable_scope(name):
ffn_unit = functools.partial(
# use encoder ffn, since decoder ffn use left padding
universal_transformer_util.transformer_encoder_ffn_unit,
hparams=hparams,
nonpadding_mask=nonpadding)
attention_unit = functools.partial(
universal_transformer_util.transformer_decoder_attention_unit,
hparams=hparams,
encoder_output=encoder_output,
decoder_self_attention_bias=decoder_self_attention_bias,
encoder_decoder_attention_bias=encoder_decoder_attention_bias,
attention_dropout_broadcast_dims=attention_dropout_broadcast_dims,
save_weights_to=save_weights_to,
make_image_summary=make_image_summary)
x, extra_output = universal_transformer_util.universal_transformer_layer(
x, hparams, ffn_unit, attention_unit)
return common_layers.layer_preprocess(x, hparams), extra_output
|
[
"def",
"recurrent_transformer_decoder",
"(",
"decoder_input",
",",
"encoder_output",
",",
"decoder_self_attention_bias",
",",
"encoder_decoder_attention_bias",
",",
"hparams",
",",
"name",
"=",
"\"decoder\"",
",",
"nonpadding",
"=",
"None",
",",
"save_weights_to",
"=",
"None",
",",
"make_image_summary",
"=",
"True",
")",
":",
"x",
"=",
"decoder_input",
"attention_dropout_broadcast_dims",
"=",
"(",
"common_layers",
".",
"comma_separated_string_to_integer_list",
"(",
"getattr",
"(",
"hparams",
",",
"\"attention_dropout_broadcast_dims\"",
",",
"\"\"",
")",
")",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"name",
")",
":",
"ffn_unit",
"=",
"functools",
".",
"partial",
"(",
"# use encoder ffn, since decoder ffn use left padding",
"universal_transformer_util",
".",
"transformer_encoder_ffn_unit",
",",
"hparams",
"=",
"hparams",
",",
"nonpadding_mask",
"=",
"nonpadding",
")",
"attention_unit",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_util",
".",
"transformer_decoder_attention_unit",
",",
"hparams",
"=",
"hparams",
",",
"encoder_output",
"=",
"encoder_output",
",",
"decoder_self_attention_bias",
"=",
"decoder_self_attention_bias",
",",
"encoder_decoder_attention_bias",
"=",
"encoder_decoder_attention_bias",
",",
"attention_dropout_broadcast_dims",
"=",
"attention_dropout_broadcast_dims",
",",
"save_weights_to",
"=",
"save_weights_to",
",",
"make_image_summary",
"=",
"make_image_summary",
")",
"x",
",",
"extra_output",
"=",
"universal_transformer_util",
".",
"universal_transformer_layer",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
")",
"return",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"extra_output"
] |
Recurrent decoder function.
|
[
"Recurrent",
"decoder",
"function",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/vqa_recurrent_self_attention.py#L138-L173
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/vqa_recurrent_self_attention.py
|
vqa_recurrent_self_attention_base
|
def vqa_recurrent_self_attention_base():
"""VQA attention baseline hparams."""
hparams = universal_transformer.universal_transformer_base()
hparams.batch_size = 1024
hparams.use_fixed_batch_size = True
hparams.weight_decay = 0.
hparams.clip_grad_norm = 0.
# use default initializer
# hparams.initializer = "xavier"
hparams.learning_rate_schedule = (
"constant*linear_warmup*rsqrt_normalized_decay")
hparams.learning_rate_warmup_steps = 8000
hparams.learning_rate_constant = 7e-4
hparams.learning_rate_decay_rate = 0.5
hparams.learning_rate_decay_steps = 50000
# hparams.dropout = 0.5
hparams.summarize_grads = True
hparams.summarize_vars = True
# not used hparams
hparams.label_smoothing = 0.1
hparams.multiply_embedding_mode = "sqrt_depth"
# add new hparams
# use raw image as input
hparams.add_hparam("image_input_type", "feature")
hparams.add_hparam("image_model_fn", "resnet_v1_152")
hparams.add_hparam("resize_side", 512)
hparams.add_hparam("height", 448)
hparams.add_hparam("width", 448)
hparams.add_hparam("distort", True)
hparams.add_hparam("train_resnet", False)
# question hidden size
# hparams.hidden_size = 512
# hparams.filter_size = 1024
# hparams.num_hidden_layers = 4
# self attention parts
# hparams.norm_type = "layer"
# hparams.layer_preprocess_sequence = "n"
# hparams.layer_postprocess_sequence = "da"
# hparams.layer_prepostprocess_dropout = 0.1
# hparams.attention_dropout = 0.1
# hparams.relu_dropout = 0.1
# hparams.add_hparam("pos", "timing")
# hparams.add_hparam("num_encoder_layers", 0)
# hparams.add_hparam("num_decoder_layers", 0)
# hparams.add_hparam("num_heads", 8)
# hparams.add_hparam("attention_key_channels", 0)
# hparams.add_hparam("attention_value_channels", 0)
# hparams.add_hparam("self_attention_type", "dot_product")
# iterative part
hparams.transformer_ffn_type = "fc"
return hparams
|
python
|
def vqa_recurrent_self_attention_base():
"""VQA attention baseline hparams."""
hparams = universal_transformer.universal_transformer_base()
hparams.batch_size = 1024
hparams.use_fixed_batch_size = True
hparams.weight_decay = 0.
hparams.clip_grad_norm = 0.
# use default initializer
# hparams.initializer = "xavier"
hparams.learning_rate_schedule = (
"constant*linear_warmup*rsqrt_normalized_decay")
hparams.learning_rate_warmup_steps = 8000
hparams.learning_rate_constant = 7e-4
hparams.learning_rate_decay_rate = 0.5
hparams.learning_rate_decay_steps = 50000
# hparams.dropout = 0.5
hparams.summarize_grads = True
hparams.summarize_vars = True
# not used hparams
hparams.label_smoothing = 0.1
hparams.multiply_embedding_mode = "sqrt_depth"
# add new hparams
# use raw image as input
hparams.add_hparam("image_input_type", "feature")
hparams.add_hparam("image_model_fn", "resnet_v1_152")
hparams.add_hparam("resize_side", 512)
hparams.add_hparam("height", 448)
hparams.add_hparam("width", 448)
hparams.add_hparam("distort", True)
hparams.add_hparam("train_resnet", False)
# question hidden size
# hparams.hidden_size = 512
# hparams.filter_size = 1024
# hparams.num_hidden_layers = 4
# self attention parts
# hparams.norm_type = "layer"
# hparams.layer_preprocess_sequence = "n"
# hparams.layer_postprocess_sequence = "da"
# hparams.layer_prepostprocess_dropout = 0.1
# hparams.attention_dropout = 0.1
# hparams.relu_dropout = 0.1
# hparams.add_hparam("pos", "timing")
# hparams.add_hparam("num_encoder_layers", 0)
# hparams.add_hparam("num_decoder_layers", 0)
# hparams.add_hparam("num_heads", 8)
# hparams.add_hparam("attention_key_channels", 0)
# hparams.add_hparam("attention_value_channels", 0)
# hparams.add_hparam("self_attention_type", "dot_product")
# iterative part
hparams.transformer_ffn_type = "fc"
return hparams
|
[
"def",
"vqa_recurrent_self_attention_base",
"(",
")",
":",
"hparams",
"=",
"universal_transformer",
".",
"universal_transformer_base",
"(",
")",
"hparams",
".",
"batch_size",
"=",
"1024",
"hparams",
".",
"use_fixed_batch_size",
"=",
"True",
"hparams",
".",
"weight_decay",
"=",
"0.",
"hparams",
".",
"clip_grad_norm",
"=",
"0.",
"# use default initializer",
"# hparams.initializer = \"xavier\"",
"hparams",
".",
"learning_rate_schedule",
"=",
"(",
"\"constant*linear_warmup*rsqrt_normalized_decay\"",
")",
"hparams",
".",
"learning_rate_warmup_steps",
"=",
"8000",
"hparams",
".",
"learning_rate_constant",
"=",
"7e-4",
"hparams",
".",
"learning_rate_decay_rate",
"=",
"0.5",
"hparams",
".",
"learning_rate_decay_steps",
"=",
"50000",
"# hparams.dropout = 0.5",
"hparams",
".",
"summarize_grads",
"=",
"True",
"hparams",
".",
"summarize_vars",
"=",
"True",
"# not used hparams",
"hparams",
".",
"label_smoothing",
"=",
"0.1",
"hparams",
".",
"multiply_embedding_mode",
"=",
"\"sqrt_depth\"",
"# add new hparams",
"# use raw image as input",
"hparams",
".",
"add_hparam",
"(",
"\"image_input_type\"",
",",
"\"feature\"",
")",
"hparams",
".",
"add_hparam",
"(",
"\"image_model_fn\"",
",",
"\"resnet_v1_152\"",
")",
"hparams",
".",
"add_hparam",
"(",
"\"resize_side\"",
",",
"512",
")",
"hparams",
".",
"add_hparam",
"(",
"\"height\"",
",",
"448",
")",
"hparams",
".",
"add_hparam",
"(",
"\"width\"",
",",
"448",
")",
"hparams",
".",
"add_hparam",
"(",
"\"distort\"",
",",
"True",
")",
"hparams",
".",
"add_hparam",
"(",
"\"train_resnet\"",
",",
"False",
")",
"# question hidden size",
"# hparams.hidden_size = 512",
"# hparams.filter_size = 1024",
"# hparams.num_hidden_layers = 4",
"# self attention parts",
"# hparams.norm_type = \"layer\"",
"# hparams.layer_preprocess_sequence = \"n\"",
"# hparams.layer_postprocess_sequence = \"da\"",
"# hparams.layer_prepostprocess_dropout = 0.1",
"# hparams.attention_dropout = 0.1",
"# hparams.relu_dropout = 0.1",
"# hparams.add_hparam(\"pos\", \"timing\")",
"# hparams.add_hparam(\"num_encoder_layers\", 0)",
"# hparams.add_hparam(\"num_decoder_layers\", 0)",
"# hparams.add_hparam(\"num_heads\", 8)",
"# hparams.add_hparam(\"attention_key_channels\", 0)",
"# hparams.add_hparam(\"attention_value_channels\", 0)",
"# hparams.add_hparam(\"self_attention_type\", \"dot_product\")",
"# iterative part",
"hparams",
".",
"transformer_ffn_type",
"=",
"\"fc\"",
"return",
"hparams"
] |
VQA attention baseline hparams.
|
[
"VQA",
"attention",
"baseline",
"hparams",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/vqa_recurrent_self_attention.py#L177-L233
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
batch_norm_relu
|
def batch_norm_relu(inputs, is_training, relu=True):
"""Block of batch norm and relu."""
inputs = mtf.layers.batch_norm(
inputs,
is_training,
BATCH_NORM_DECAY,
epsilon=BATCH_NORM_EPSILON,
init_zero=(not relu))
if relu:
inputs = mtf.relu(inputs)
return inputs
|
python
|
def batch_norm_relu(inputs, is_training, relu=True):
"""Block of batch norm and relu."""
inputs = mtf.layers.batch_norm(
inputs,
is_training,
BATCH_NORM_DECAY,
epsilon=BATCH_NORM_EPSILON,
init_zero=(not relu))
if relu:
inputs = mtf.relu(inputs)
return inputs
|
[
"def",
"batch_norm_relu",
"(",
"inputs",
",",
"is_training",
",",
"relu",
"=",
"True",
")",
":",
"inputs",
"=",
"mtf",
".",
"layers",
".",
"batch_norm",
"(",
"inputs",
",",
"is_training",
",",
"BATCH_NORM_DECAY",
",",
"epsilon",
"=",
"BATCH_NORM_EPSILON",
",",
"init_zero",
"=",
"(",
"not",
"relu",
")",
")",
"if",
"relu",
":",
"inputs",
"=",
"mtf",
".",
"relu",
"(",
"inputs",
")",
"return",
"inputs"
] |
Block of batch norm and relu.
|
[
"Block",
"of",
"batch",
"norm",
"and",
"relu",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L38-L48
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
bottleneck_block
|
def bottleneck_block(inputs,
filters,
is_training,
strides,
projection_shortcut=None,
row_blocks_dim=None,
col_blocks_dim=None):
"""Bottleneck block variant for residual networks with BN after convolutions.
Args:
inputs: a `mtf.Tensor` of shape
`[batch_dim, row_blocks, col_blocks, rows, cols, in_channels]`.
filters: `int` number of filters for the first two convolutions. Note
that the third and final convolution will use 4 times as many filters.
is_training: `bool` for whether the model is in training mode.
strides: `int` block stride. If greater than 1, this block will ultimately
downsample the input.
projection_shortcut: `function` to use for projection shortcuts (typically
a 1x1 convolution to match the filter dimensions). If None, no
projection is used and the input is passed as unchanged through the
shortcut connection.
row_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
col_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
Returns:
The output `Tensor` of the block.
"""
shortcut = inputs
filter_h_dim = mtf.Dimension("filter_height", 3)
filter_w_dim = mtf.Dimension("filter_width", 3)
one_h_dim = mtf.Dimension("filter_height", 1)
one_w_dim = mtf.Dimension("filter_width", 1)
if projection_shortcut is not None:
filters_dim = mtf.Dimension("filtersp", filters)
kernel = mtf.get_variable(
inputs.mesh, "kernel", mtf.Shape(
[one_h_dim, one_w_dim, inputs.shape.dims[-1], filters_dim]))
shortcut = projection_shortcut(inputs, kernel)
# First conv block
filters1_dim = mtf.Dimension("filters1", filters)
kernel1 = mtf.get_variable(
inputs.mesh, "kernel1", mtf.Shape(
[one_h_dim, one_w_dim, inputs.shape.dims[-1], filters1_dim]))
inputs = mtf.conv2d_with_blocks(
inputs,
kernel1,
strides=[1, 1, 1, 1],
padding="SAME",
h_blocks_dim=None, w_blocks_dim=col_blocks_dim)
# TODO(nikip): Add Dropout?
inputs = batch_norm_relu(inputs, is_training)
# Second conv block
filters2_dim = mtf.Dimension("filters2", 4*filters)
kernel2 = mtf.get_variable(
inputs.mesh, "kernel2", mtf.Shape(
[filter_h_dim, filter_w_dim, filters1_dim, filters2_dim]))
inputs = mtf.conv2d_with_blocks(
inputs,
kernel2,
strides=[1, 1, 1, 1],
padding="SAME",
h_blocks_dim=row_blocks_dim, w_blocks_dim=col_blocks_dim)
inputs = batch_norm_relu(inputs, is_training)
# Third wide conv filter block
filters3_dim = mtf.Dimension("filters3", filters)
filters3_kernel = mtf.get_variable(
inputs.mesh, "wide_kernel", mtf.Shape(
[one_h_dim, one_w_dim, filters2_dim, filters3_dim]))
inputs = mtf.conv2d_with_blocks(
inputs,
filters3_kernel,
strides,
padding="SAME",
h_blocks_dim=None, w_blocks_dim=col_blocks_dim)
# TODO(nikip): Althought the original resnet code has this batch norm, in our
# setup this is causing no gradients to be passed. Investigate further.
# inputs = batch_norm_relu(inputs, is_training, relu=True)
# TODO(nikip): Maybe add residual with a projection?
return mtf.relu(
shortcut + mtf.rename_dimension(
inputs, inputs.shape.dims[-1].name, shortcut.shape.dims[-1].name))
|
python
|
def bottleneck_block(inputs,
filters,
is_training,
strides,
projection_shortcut=None,
row_blocks_dim=None,
col_blocks_dim=None):
"""Bottleneck block variant for residual networks with BN after convolutions.
Args:
inputs: a `mtf.Tensor` of shape
`[batch_dim, row_blocks, col_blocks, rows, cols, in_channels]`.
filters: `int` number of filters for the first two convolutions. Note
that the third and final convolution will use 4 times as many filters.
is_training: `bool` for whether the model is in training mode.
strides: `int` block stride. If greater than 1, this block will ultimately
downsample the input.
projection_shortcut: `function` to use for projection shortcuts (typically
a 1x1 convolution to match the filter dimensions). If None, no
projection is used and the input is passed as unchanged through the
shortcut connection.
row_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
col_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
Returns:
The output `Tensor` of the block.
"""
shortcut = inputs
filter_h_dim = mtf.Dimension("filter_height", 3)
filter_w_dim = mtf.Dimension("filter_width", 3)
one_h_dim = mtf.Dimension("filter_height", 1)
one_w_dim = mtf.Dimension("filter_width", 1)
if projection_shortcut is not None:
filters_dim = mtf.Dimension("filtersp", filters)
kernel = mtf.get_variable(
inputs.mesh, "kernel", mtf.Shape(
[one_h_dim, one_w_dim, inputs.shape.dims[-1], filters_dim]))
shortcut = projection_shortcut(inputs, kernel)
# First conv block
filters1_dim = mtf.Dimension("filters1", filters)
kernel1 = mtf.get_variable(
inputs.mesh, "kernel1", mtf.Shape(
[one_h_dim, one_w_dim, inputs.shape.dims[-1], filters1_dim]))
inputs = mtf.conv2d_with_blocks(
inputs,
kernel1,
strides=[1, 1, 1, 1],
padding="SAME",
h_blocks_dim=None, w_blocks_dim=col_blocks_dim)
# TODO(nikip): Add Dropout?
inputs = batch_norm_relu(inputs, is_training)
# Second conv block
filters2_dim = mtf.Dimension("filters2", 4*filters)
kernel2 = mtf.get_variable(
inputs.mesh, "kernel2", mtf.Shape(
[filter_h_dim, filter_w_dim, filters1_dim, filters2_dim]))
inputs = mtf.conv2d_with_blocks(
inputs,
kernel2,
strides=[1, 1, 1, 1],
padding="SAME",
h_blocks_dim=row_blocks_dim, w_blocks_dim=col_blocks_dim)
inputs = batch_norm_relu(inputs, is_training)
# Third wide conv filter block
filters3_dim = mtf.Dimension("filters3", filters)
filters3_kernel = mtf.get_variable(
inputs.mesh, "wide_kernel", mtf.Shape(
[one_h_dim, one_w_dim, filters2_dim, filters3_dim]))
inputs = mtf.conv2d_with_blocks(
inputs,
filters3_kernel,
strides,
padding="SAME",
h_blocks_dim=None, w_blocks_dim=col_blocks_dim)
# TODO(nikip): Althought the original resnet code has this batch norm, in our
# setup this is causing no gradients to be passed. Investigate further.
# inputs = batch_norm_relu(inputs, is_training, relu=True)
# TODO(nikip): Maybe add residual with a projection?
return mtf.relu(
shortcut + mtf.rename_dimension(
inputs, inputs.shape.dims[-1].name, shortcut.shape.dims[-1].name))
|
[
"def",
"bottleneck_block",
"(",
"inputs",
",",
"filters",
",",
"is_training",
",",
"strides",
",",
"projection_shortcut",
"=",
"None",
",",
"row_blocks_dim",
"=",
"None",
",",
"col_blocks_dim",
"=",
"None",
")",
":",
"shortcut",
"=",
"inputs",
"filter_h_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filter_height\"",
",",
"3",
")",
"filter_w_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filter_width\"",
",",
"3",
")",
"one_h_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filter_height\"",
",",
"1",
")",
"one_w_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filter_width\"",
",",
"1",
")",
"if",
"projection_shortcut",
"is",
"not",
"None",
":",
"filters_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filtersp\"",
",",
"filters",
")",
"kernel",
"=",
"mtf",
".",
"get_variable",
"(",
"inputs",
".",
"mesh",
",",
"\"kernel\"",
",",
"mtf",
".",
"Shape",
"(",
"[",
"one_h_dim",
",",
"one_w_dim",
",",
"inputs",
".",
"shape",
".",
"dims",
"[",
"-",
"1",
"]",
",",
"filters_dim",
"]",
")",
")",
"shortcut",
"=",
"projection_shortcut",
"(",
"inputs",
",",
"kernel",
")",
"# First conv block",
"filters1_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filters1\"",
",",
"filters",
")",
"kernel1",
"=",
"mtf",
".",
"get_variable",
"(",
"inputs",
".",
"mesh",
",",
"\"kernel1\"",
",",
"mtf",
".",
"Shape",
"(",
"[",
"one_h_dim",
",",
"one_w_dim",
",",
"inputs",
".",
"shape",
".",
"dims",
"[",
"-",
"1",
"]",
",",
"filters1_dim",
"]",
")",
")",
"inputs",
"=",
"mtf",
".",
"conv2d_with_blocks",
"(",
"inputs",
",",
"kernel1",
",",
"strides",
"=",
"[",
"1",
",",
"1",
",",
"1",
",",
"1",
"]",
",",
"padding",
"=",
"\"SAME\"",
",",
"h_blocks_dim",
"=",
"None",
",",
"w_blocks_dim",
"=",
"col_blocks_dim",
")",
"# TODO(nikip): Add Dropout?",
"inputs",
"=",
"batch_norm_relu",
"(",
"inputs",
",",
"is_training",
")",
"# Second conv block",
"filters2_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filters2\"",
",",
"4",
"*",
"filters",
")",
"kernel2",
"=",
"mtf",
".",
"get_variable",
"(",
"inputs",
".",
"mesh",
",",
"\"kernel2\"",
",",
"mtf",
".",
"Shape",
"(",
"[",
"filter_h_dim",
",",
"filter_w_dim",
",",
"filters1_dim",
",",
"filters2_dim",
"]",
")",
")",
"inputs",
"=",
"mtf",
".",
"conv2d_with_blocks",
"(",
"inputs",
",",
"kernel2",
",",
"strides",
"=",
"[",
"1",
",",
"1",
",",
"1",
",",
"1",
"]",
",",
"padding",
"=",
"\"SAME\"",
",",
"h_blocks_dim",
"=",
"row_blocks_dim",
",",
"w_blocks_dim",
"=",
"col_blocks_dim",
")",
"inputs",
"=",
"batch_norm_relu",
"(",
"inputs",
",",
"is_training",
")",
"# Third wide conv filter block",
"filters3_dim",
"=",
"mtf",
".",
"Dimension",
"(",
"\"filters3\"",
",",
"filters",
")",
"filters3_kernel",
"=",
"mtf",
".",
"get_variable",
"(",
"inputs",
".",
"mesh",
",",
"\"wide_kernel\"",
",",
"mtf",
".",
"Shape",
"(",
"[",
"one_h_dim",
",",
"one_w_dim",
",",
"filters2_dim",
",",
"filters3_dim",
"]",
")",
")",
"inputs",
"=",
"mtf",
".",
"conv2d_with_blocks",
"(",
"inputs",
",",
"filters3_kernel",
",",
"strides",
",",
"padding",
"=",
"\"SAME\"",
",",
"h_blocks_dim",
"=",
"None",
",",
"w_blocks_dim",
"=",
"col_blocks_dim",
")",
"# TODO(nikip): Althought the original resnet code has this batch norm, in our",
"# setup this is causing no gradients to be passed. Investigate further.",
"# inputs = batch_norm_relu(inputs, is_training, relu=True)",
"# TODO(nikip): Maybe add residual with a projection?",
"return",
"mtf",
".",
"relu",
"(",
"shortcut",
"+",
"mtf",
".",
"rename_dimension",
"(",
"inputs",
",",
"inputs",
".",
"shape",
".",
"dims",
"[",
"-",
"1",
"]",
".",
"name",
",",
"shortcut",
".",
"shape",
".",
"dims",
"[",
"-",
"1",
"]",
".",
"name",
")",
")"
] |
Bottleneck block variant for residual networks with BN after convolutions.
Args:
inputs: a `mtf.Tensor` of shape
`[batch_dim, row_blocks, col_blocks, rows, cols, in_channels]`.
filters: `int` number of filters for the first two convolutions. Note
that the third and final convolution will use 4 times as many filters.
is_training: `bool` for whether the model is in training mode.
strides: `int` block stride. If greater than 1, this block will ultimately
downsample the input.
projection_shortcut: `function` to use for projection shortcuts (typically
a 1x1 convolution to match the filter dimensions). If None, no
projection is used and the input is passed as unchanged through the
shortcut connection.
row_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
col_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
Returns:
The output `Tensor` of the block.
|
[
"Bottleneck",
"block",
"variant",
"for",
"residual",
"networks",
"with",
"BN",
"after",
"convolutions",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L51-L142
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
block_layer
|
def block_layer(inputs,
filters,
blocks,
strides,
is_training,
name,
row_blocks_dim=None,
col_blocks_dim=None):
"""Creates one layer of blocks for the ResNet model.
Args:
inputs: `Tensor` of size `[batch, channels, height, width]`.
filters: `int` number of filters for the first convolution of the layer.
blocks: `int` number of blocks contained in the layer.
strides: `int` stride to use for the first convolution of the layer. If
greater than 1, this layer will downsample the input.
is_training: `bool` for whether the model is training.
name: `str`name for the Tensor output of the block layer.
row_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
col_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
Returns:
The output `Tensor` of the block layer.
"""
with tf.variable_scope(name, default_name="block_layer"):
# Only the first block per block_layer uses projection_shortcut and strides
def projection_shortcut(inputs, kernel):
"""Project identity branch."""
inputs = mtf.conv2d_with_blocks(
inputs,
kernel,
strides=strides,
padding="SAME",
h_blocks_dim=None, w_blocks_dim=col_blocks_dim)
return batch_norm_relu(
inputs, is_training, relu=False)
inputs = bottleneck_block(
inputs,
filters,
is_training,
strides=strides,
projection_shortcut=projection_shortcut,
row_blocks_dim=row_blocks_dim,
col_blocks_dim=col_blocks_dim)
for i in range(1, blocks):
with tf.variable_scope("bottleneck_%d" % i):
inputs = bottleneck_block(
inputs,
filters,
is_training,
strides=[1, 1, 1, 1],
projection_shortcut=None,
row_blocks_dim=row_blocks_dim,
col_blocks_dim=col_blocks_dim)
return inputs
|
python
|
def block_layer(inputs,
filters,
blocks,
strides,
is_training,
name,
row_blocks_dim=None,
col_blocks_dim=None):
"""Creates one layer of blocks for the ResNet model.
Args:
inputs: `Tensor` of size `[batch, channels, height, width]`.
filters: `int` number of filters for the first convolution of the layer.
blocks: `int` number of blocks contained in the layer.
strides: `int` stride to use for the first convolution of the layer. If
greater than 1, this layer will downsample the input.
is_training: `bool` for whether the model is training.
name: `str`name for the Tensor output of the block layer.
row_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
col_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
Returns:
The output `Tensor` of the block layer.
"""
with tf.variable_scope(name, default_name="block_layer"):
# Only the first block per block_layer uses projection_shortcut and strides
def projection_shortcut(inputs, kernel):
"""Project identity branch."""
inputs = mtf.conv2d_with_blocks(
inputs,
kernel,
strides=strides,
padding="SAME",
h_blocks_dim=None, w_blocks_dim=col_blocks_dim)
return batch_norm_relu(
inputs, is_training, relu=False)
inputs = bottleneck_block(
inputs,
filters,
is_training,
strides=strides,
projection_shortcut=projection_shortcut,
row_blocks_dim=row_blocks_dim,
col_blocks_dim=col_blocks_dim)
for i in range(1, blocks):
with tf.variable_scope("bottleneck_%d" % i):
inputs = bottleneck_block(
inputs,
filters,
is_training,
strides=[1, 1, 1, 1],
projection_shortcut=None,
row_blocks_dim=row_blocks_dim,
col_blocks_dim=col_blocks_dim)
return inputs
|
[
"def",
"block_layer",
"(",
"inputs",
",",
"filters",
",",
"blocks",
",",
"strides",
",",
"is_training",
",",
"name",
",",
"row_blocks_dim",
"=",
"None",
",",
"col_blocks_dim",
"=",
"None",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"name",
",",
"default_name",
"=",
"\"block_layer\"",
")",
":",
"# Only the first block per block_layer uses projection_shortcut and strides",
"def",
"projection_shortcut",
"(",
"inputs",
",",
"kernel",
")",
":",
"\"\"\"Project identity branch.\"\"\"",
"inputs",
"=",
"mtf",
".",
"conv2d_with_blocks",
"(",
"inputs",
",",
"kernel",
",",
"strides",
"=",
"strides",
",",
"padding",
"=",
"\"SAME\"",
",",
"h_blocks_dim",
"=",
"None",
",",
"w_blocks_dim",
"=",
"col_blocks_dim",
")",
"return",
"batch_norm_relu",
"(",
"inputs",
",",
"is_training",
",",
"relu",
"=",
"False",
")",
"inputs",
"=",
"bottleneck_block",
"(",
"inputs",
",",
"filters",
",",
"is_training",
",",
"strides",
"=",
"strides",
",",
"projection_shortcut",
"=",
"projection_shortcut",
",",
"row_blocks_dim",
"=",
"row_blocks_dim",
",",
"col_blocks_dim",
"=",
"col_blocks_dim",
")",
"for",
"i",
"in",
"range",
"(",
"1",
",",
"blocks",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"bottleneck_%d\"",
"%",
"i",
")",
":",
"inputs",
"=",
"bottleneck_block",
"(",
"inputs",
",",
"filters",
",",
"is_training",
",",
"strides",
"=",
"[",
"1",
",",
"1",
",",
"1",
",",
"1",
"]",
",",
"projection_shortcut",
"=",
"None",
",",
"row_blocks_dim",
"=",
"row_blocks_dim",
",",
"col_blocks_dim",
"=",
"col_blocks_dim",
")",
"return",
"inputs"
] |
Creates one layer of blocks for the ResNet model.
Args:
inputs: `Tensor` of size `[batch, channels, height, width]`.
filters: `int` number of filters for the first convolution of the layer.
blocks: `int` number of blocks contained in the layer.
strides: `int` stride to use for the first convolution of the layer. If
greater than 1, this layer will downsample the input.
is_training: `bool` for whether the model is training.
name: `str`name for the Tensor output of the block layer.
row_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
col_blocks_dim: a mtf.Dimension, row dimension which is
spatially partitioned along mesh axis
Returns:
The output `Tensor` of the block layer.
|
[
"Creates",
"one",
"layer",
"of",
"blocks",
"for",
"the",
"ResNet",
"model",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L145-L204
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
mtf_resnet_base
|
def mtf_resnet_base():
"""Set of hyperparameters."""
hparams = common_hparams.basic_params1()
hparams.no_data_parallelism = True
hparams.use_fixed_batch_size = True
hparams.batch_size = 32
hparams.max_length = 3072
hparams.hidden_size = 256
hparams.label_smoothing = 0.0
# 8-way model-parallelism
hparams.add_hparam("mesh_shape", "batch:8")
hparams.add_hparam("layout", "batch:batch")
hparams.add_hparam("filter_size", 1024)
hparams.add_hparam("num_layers", 6)
# Share weights between input and target embeddings
hparams.shared_embedding = True
hparams.shared_embedding_and_softmax_weights = True
hparams.optimizer = "Adafactor"
hparams.learning_rate_schedule = "rsqrt_decay"
hparams.learning_rate_warmup_steps = 10000
hparams.add_hparam("d_kv", 32)
# Image related hparams
hparams.add_hparam("img_len", 32)
hparams.add_hparam("num_channels", 3)
hparams.add_hparam("row_blocks", 1)
hparams.add_hparam("col_blocks", 1)
hparams.add_hparam("rows_size", 32)
hparams.add_hparam("cols_size", 32)
# Model-specific parameters
hparams.add_hparam("layer_sizes", [3, 4, 6, 3])
hparams.add_hparam("filter_sizes", [64, 64, 128, 256, 512])
hparams.add_hparam("is_cifar", False)
# Variable init
hparams.initializer = "normal_unit_scaling"
hparams.initializer_gain = 2.
# TODO(nikip): Change optimization scheme?
hparams.learning_rate = 0.1
return hparams
|
python
|
def mtf_resnet_base():
"""Set of hyperparameters."""
hparams = common_hparams.basic_params1()
hparams.no_data_parallelism = True
hparams.use_fixed_batch_size = True
hparams.batch_size = 32
hparams.max_length = 3072
hparams.hidden_size = 256
hparams.label_smoothing = 0.0
# 8-way model-parallelism
hparams.add_hparam("mesh_shape", "batch:8")
hparams.add_hparam("layout", "batch:batch")
hparams.add_hparam("filter_size", 1024)
hparams.add_hparam("num_layers", 6)
# Share weights between input and target embeddings
hparams.shared_embedding = True
hparams.shared_embedding_and_softmax_weights = True
hparams.optimizer = "Adafactor"
hparams.learning_rate_schedule = "rsqrt_decay"
hparams.learning_rate_warmup_steps = 10000
hparams.add_hparam("d_kv", 32)
# Image related hparams
hparams.add_hparam("img_len", 32)
hparams.add_hparam("num_channels", 3)
hparams.add_hparam("row_blocks", 1)
hparams.add_hparam("col_blocks", 1)
hparams.add_hparam("rows_size", 32)
hparams.add_hparam("cols_size", 32)
# Model-specific parameters
hparams.add_hparam("layer_sizes", [3, 4, 6, 3])
hparams.add_hparam("filter_sizes", [64, 64, 128, 256, 512])
hparams.add_hparam("is_cifar", False)
# Variable init
hparams.initializer = "normal_unit_scaling"
hparams.initializer_gain = 2.
# TODO(nikip): Change optimization scheme?
hparams.learning_rate = 0.1
return hparams
|
[
"def",
"mtf_resnet_base",
"(",
")",
":",
"hparams",
"=",
"common_hparams",
".",
"basic_params1",
"(",
")",
"hparams",
".",
"no_data_parallelism",
"=",
"True",
"hparams",
".",
"use_fixed_batch_size",
"=",
"True",
"hparams",
".",
"batch_size",
"=",
"32",
"hparams",
".",
"max_length",
"=",
"3072",
"hparams",
".",
"hidden_size",
"=",
"256",
"hparams",
".",
"label_smoothing",
"=",
"0.0",
"# 8-way model-parallelism",
"hparams",
".",
"add_hparam",
"(",
"\"mesh_shape\"",
",",
"\"batch:8\"",
")",
"hparams",
".",
"add_hparam",
"(",
"\"layout\"",
",",
"\"batch:batch\"",
")",
"hparams",
".",
"add_hparam",
"(",
"\"filter_size\"",
",",
"1024",
")",
"hparams",
".",
"add_hparam",
"(",
"\"num_layers\"",
",",
"6",
")",
"# Share weights between input and target embeddings",
"hparams",
".",
"shared_embedding",
"=",
"True",
"hparams",
".",
"shared_embedding_and_softmax_weights",
"=",
"True",
"hparams",
".",
"optimizer",
"=",
"\"Adafactor\"",
"hparams",
".",
"learning_rate_schedule",
"=",
"\"rsqrt_decay\"",
"hparams",
".",
"learning_rate_warmup_steps",
"=",
"10000",
"hparams",
".",
"add_hparam",
"(",
"\"d_kv\"",
",",
"32",
")",
"# Image related hparams",
"hparams",
".",
"add_hparam",
"(",
"\"img_len\"",
",",
"32",
")",
"hparams",
".",
"add_hparam",
"(",
"\"num_channels\"",
",",
"3",
")",
"hparams",
".",
"add_hparam",
"(",
"\"row_blocks\"",
",",
"1",
")",
"hparams",
".",
"add_hparam",
"(",
"\"col_blocks\"",
",",
"1",
")",
"hparams",
".",
"add_hparam",
"(",
"\"rows_size\"",
",",
"32",
")",
"hparams",
".",
"add_hparam",
"(",
"\"cols_size\"",
",",
"32",
")",
"# Model-specific parameters",
"hparams",
".",
"add_hparam",
"(",
"\"layer_sizes\"",
",",
"[",
"3",
",",
"4",
",",
"6",
",",
"3",
"]",
")",
"hparams",
".",
"add_hparam",
"(",
"\"filter_sizes\"",
",",
"[",
"64",
",",
"64",
",",
"128",
",",
"256",
",",
"512",
"]",
")",
"hparams",
".",
"add_hparam",
"(",
"\"is_cifar\"",
",",
"False",
")",
"# Variable init",
"hparams",
".",
"initializer",
"=",
"\"normal_unit_scaling\"",
"hparams",
".",
"initializer_gain",
"=",
"2.",
"# TODO(nikip): Change optimization scheme?",
"hparams",
".",
"learning_rate",
"=",
"0.1",
"return",
"hparams"
] |
Set of hyperparameters.
|
[
"Set",
"of",
"hyperparameters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L333-L376
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
mtf_resnet_tiny
|
def mtf_resnet_tiny():
"""Catch bugs locally..."""
hparams = mtf_resnet_base()
hparams.num_layers = 2
hparams.hidden_size = 64
hparams.filter_size = 64
hparams.batch_size = 16
# data parallelism and model-parallelism
hparams.col_blocks = 1
hparams.mesh_shape = "batch:2"
hparams.layout = "batch:batch"
hparams.layer_sizes = [1, 2, 3]
hparams.filter_sizes = [64, 64, 64]
return hparams
|
python
|
def mtf_resnet_tiny():
"""Catch bugs locally..."""
hparams = mtf_resnet_base()
hparams.num_layers = 2
hparams.hidden_size = 64
hparams.filter_size = 64
hparams.batch_size = 16
# data parallelism and model-parallelism
hparams.col_blocks = 1
hparams.mesh_shape = "batch:2"
hparams.layout = "batch:batch"
hparams.layer_sizes = [1, 2, 3]
hparams.filter_sizes = [64, 64, 64]
return hparams
|
[
"def",
"mtf_resnet_tiny",
"(",
")",
":",
"hparams",
"=",
"mtf_resnet_base",
"(",
")",
"hparams",
".",
"num_layers",
"=",
"2",
"hparams",
".",
"hidden_size",
"=",
"64",
"hparams",
".",
"filter_size",
"=",
"64",
"hparams",
".",
"batch_size",
"=",
"16",
"# data parallelism and model-parallelism",
"hparams",
".",
"col_blocks",
"=",
"1",
"hparams",
".",
"mesh_shape",
"=",
"\"batch:2\"",
"hparams",
".",
"layout",
"=",
"\"batch:batch\"",
"hparams",
".",
"layer_sizes",
"=",
"[",
"1",
",",
"2",
",",
"3",
"]",
"hparams",
".",
"filter_sizes",
"=",
"[",
"64",
",",
"64",
",",
"64",
"]",
"return",
"hparams"
] |
Catch bugs locally...
|
[
"Catch",
"bugs",
"locally",
"..."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L380-L393
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
mtf_resnet_single
|
def mtf_resnet_single():
"""Small single parameters."""
hparams = mtf_resnet_tiny()
hparams.mesh_shape = ""
hparams.layout = ""
hparams.hidden_size = 32
hparams.filter_size = 32
hparams.batch_size = 1
hparams.num_encoder_layers = 1
hparams.num_layers = 1
hparams.block_length = 16
return hparams
|
python
|
def mtf_resnet_single():
"""Small single parameters."""
hparams = mtf_resnet_tiny()
hparams.mesh_shape = ""
hparams.layout = ""
hparams.hidden_size = 32
hparams.filter_size = 32
hparams.batch_size = 1
hparams.num_encoder_layers = 1
hparams.num_layers = 1
hparams.block_length = 16
return hparams
|
[
"def",
"mtf_resnet_single",
"(",
")",
":",
"hparams",
"=",
"mtf_resnet_tiny",
"(",
")",
"hparams",
".",
"mesh_shape",
"=",
"\"\"",
"hparams",
".",
"layout",
"=",
"\"\"",
"hparams",
".",
"hidden_size",
"=",
"32",
"hparams",
".",
"filter_size",
"=",
"32",
"hparams",
".",
"batch_size",
"=",
"1",
"hparams",
".",
"num_encoder_layers",
"=",
"1",
"hparams",
".",
"num_layers",
"=",
"1",
"hparams",
".",
"block_length",
"=",
"16",
"return",
"hparams"
] |
Small single parameters.
|
[
"Small",
"single",
"parameters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L397-L408
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
mtf_resnet_base_single
|
def mtf_resnet_base_single():
"""Small single parameters."""
hparams = mtf_resnet_base()
hparams.num_layers = 6
hparams.filter_size = 256
hparams.block_length = 128
hparams.mesh_shape = ""
hparams.layout = ""
return hparams
|
python
|
def mtf_resnet_base_single():
"""Small single parameters."""
hparams = mtf_resnet_base()
hparams.num_layers = 6
hparams.filter_size = 256
hparams.block_length = 128
hparams.mesh_shape = ""
hparams.layout = ""
return hparams
|
[
"def",
"mtf_resnet_base_single",
"(",
")",
":",
"hparams",
"=",
"mtf_resnet_base",
"(",
")",
"hparams",
".",
"num_layers",
"=",
"6",
"hparams",
".",
"filter_size",
"=",
"256",
"hparams",
".",
"block_length",
"=",
"128",
"hparams",
".",
"mesh_shape",
"=",
"\"\"",
"hparams",
".",
"layout",
"=",
"\"\"",
"return",
"hparams"
] |
Small single parameters.
|
[
"Small",
"single",
"parameters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L412-L420
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/mtf_resnet.py
|
mtf_resnet_base_cifar
|
def mtf_resnet_base_cifar():
"""Data parallel CIFAR parameters."""
hparams = mtf_resnet_base()
hparams.mesh_shape = "batch:32"
hparams.layoyt = "batch:batch"
hparams.batch_size = 8
hparams.num_layers = 12
hparams.block_length = 256
hparams.hidden_size = 512
hparams.filter_size = 2048
hparams.learning_rate = 0.5
hparams.learning_rate_warmup_steps = 4000
hparams.layer_preprocess_sequence = "none"
hparams.layer_postprocess_sequence = "dan"
hparams.layer_prepostprocess_dropout = 0.3
hparams.unconditional = True
return hparams
|
python
|
def mtf_resnet_base_cifar():
"""Data parallel CIFAR parameters."""
hparams = mtf_resnet_base()
hparams.mesh_shape = "batch:32"
hparams.layoyt = "batch:batch"
hparams.batch_size = 8
hparams.num_layers = 12
hparams.block_length = 256
hparams.hidden_size = 512
hparams.filter_size = 2048
hparams.learning_rate = 0.5
hparams.learning_rate_warmup_steps = 4000
hparams.layer_preprocess_sequence = "none"
hparams.layer_postprocess_sequence = "dan"
hparams.layer_prepostprocess_dropout = 0.3
hparams.unconditional = True
return hparams
|
[
"def",
"mtf_resnet_base_cifar",
"(",
")",
":",
"hparams",
"=",
"mtf_resnet_base",
"(",
")",
"hparams",
".",
"mesh_shape",
"=",
"\"batch:32\"",
"hparams",
".",
"layoyt",
"=",
"\"batch:batch\"",
"hparams",
".",
"batch_size",
"=",
"8",
"hparams",
".",
"num_layers",
"=",
"12",
"hparams",
".",
"block_length",
"=",
"256",
"hparams",
".",
"hidden_size",
"=",
"512",
"hparams",
".",
"filter_size",
"=",
"2048",
"hparams",
".",
"learning_rate",
"=",
"0.5",
"hparams",
".",
"learning_rate_warmup_steps",
"=",
"4000",
"hparams",
".",
"layer_preprocess_sequence",
"=",
"\"none\"",
"hparams",
".",
"layer_postprocess_sequence",
"=",
"\"dan\"",
"hparams",
".",
"layer_prepostprocess_dropout",
"=",
"0.3",
"hparams",
".",
"unconditional",
"=",
"True",
"return",
"hparams"
] |
Data parallel CIFAR parameters.
|
[
"Data",
"parallel",
"CIFAR",
"parameters",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/mtf_resnet.py#L424-L440
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_encoder
|
def universal_transformer_encoder(encoder_input,
encoder_self_attention_bias,
hparams,
name="encoder",
nonpadding=None,
save_weights_to=None,
make_image_summary=True):
"""Universal Transformer encoder function.
Prepares all the arguments and the inputs and passes it to a
universal_transformer_layer to encode the encoder_input.
Args:
encoder_input: a Tensor
encoder_self_attention_bias: bias Tensor for self-attention
(see common_attention.attention_bias())
hparams: hyperparameters for model
name: a string
nonpadding: optional Tensor with shape [batch_size, encoder_length]
indicating what positions are not padding. This must either be
passed in, which we do for "packed" datasets, or inferred from
encoder_self_attention_bias. The knowledge about padding is used
for pad_remover(efficiency) and to mask out padding in convoltutional
layers.
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.
Returns:
y: a Tensors as the output of the encoder
extra_output: which can be used to pass extra information to the body
"""
x = encoder_input
attention_dropout_broadcast_dims = (
common_layers.comma_separated_string_to_integer_list(
getattr(hparams, "attention_dropout_broadcast_dims", "")))
with tf.variable_scope(name):
if nonpadding is not None:
padding = 1.0 - nonpadding
else:
padding = common_attention.attention_bias_to_padding(
encoder_self_attention_bias)
nonpadding = 1.0 - padding
pad_remover = None
if hparams.use_pad_remover and not common_layers.is_xla_compiled():
pad_remover = expert_utils.PadRemover(padding)
ffn_unit = functools.partial(
transformer_encoder_ffn_unit,
hparams=hparams,
nonpadding_mask=nonpadding,
pad_remover=pad_remover)
attention_unit = functools.partial(
transformer_encoder_attention_unit,
hparams=hparams,
encoder_self_attention_bias=encoder_self_attention_bias,
attention_dropout_broadcast_dims=attention_dropout_broadcast_dims,
save_weights_to=save_weights_to,
make_image_summary=make_image_summary)
x, extra_output = universal_transformer_layer(
x, hparams, ffn_unit, attention_unit, pad_remover=pad_remover)
return common_layers.layer_preprocess(x, hparams), extra_output
|
python
|
def universal_transformer_encoder(encoder_input,
encoder_self_attention_bias,
hparams,
name="encoder",
nonpadding=None,
save_weights_to=None,
make_image_summary=True):
"""Universal Transformer encoder function.
Prepares all the arguments and the inputs and passes it to a
universal_transformer_layer to encode the encoder_input.
Args:
encoder_input: a Tensor
encoder_self_attention_bias: bias Tensor for self-attention
(see common_attention.attention_bias())
hparams: hyperparameters for model
name: a string
nonpadding: optional Tensor with shape [batch_size, encoder_length]
indicating what positions are not padding. This must either be
passed in, which we do for "packed" datasets, or inferred from
encoder_self_attention_bias. The knowledge about padding is used
for pad_remover(efficiency) and to mask out padding in convoltutional
layers.
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.
Returns:
y: a Tensors as the output of the encoder
extra_output: which can be used to pass extra information to the body
"""
x = encoder_input
attention_dropout_broadcast_dims = (
common_layers.comma_separated_string_to_integer_list(
getattr(hparams, "attention_dropout_broadcast_dims", "")))
with tf.variable_scope(name):
if nonpadding is not None:
padding = 1.0 - nonpadding
else:
padding = common_attention.attention_bias_to_padding(
encoder_self_attention_bias)
nonpadding = 1.0 - padding
pad_remover = None
if hparams.use_pad_remover and not common_layers.is_xla_compiled():
pad_remover = expert_utils.PadRemover(padding)
ffn_unit = functools.partial(
transformer_encoder_ffn_unit,
hparams=hparams,
nonpadding_mask=nonpadding,
pad_remover=pad_remover)
attention_unit = functools.partial(
transformer_encoder_attention_unit,
hparams=hparams,
encoder_self_attention_bias=encoder_self_attention_bias,
attention_dropout_broadcast_dims=attention_dropout_broadcast_dims,
save_weights_to=save_weights_to,
make_image_summary=make_image_summary)
x, extra_output = universal_transformer_layer(
x, hparams, ffn_unit, attention_unit, pad_remover=pad_remover)
return common_layers.layer_preprocess(x, hparams), extra_output
|
[
"def",
"universal_transformer_encoder",
"(",
"encoder_input",
",",
"encoder_self_attention_bias",
",",
"hparams",
",",
"name",
"=",
"\"encoder\"",
",",
"nonpadding",
"=",
"None",
",",
"save_weights_to",
"=",
"None",
",",
"make_image_summary",
"=",
"True",
")",
":",
"x",
"=",
"encoder_input",
"attention_dropout_broadcast_dims",
"=",
"(",
"common_layers",
".",
"comma_separated_string_to_integer_list",
"(",
"getattr",
"(",
"hparams",
",",
"\"attention_dropout_broadcast_dims\"",
",",
"\"\"",
")",
")",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"name",
")",
":",
"if",
"nonpadding",
"is",
"not",
"None",
":",
"padding",
"=",
"1.0",
"-",
"nonpadding",
"else",
":",
"padding",
"=",
"common_attention",
".",
"attention_bias_to_padding",
"(",
"encoder_self_attention_bias",
")",
"nonpadding",
"=",
"1.0",
"-",
"padding",
"pad_remover",
"=",
"None",
"if",
"hparams",
".",
"use_pad_remover",
"and",
"not",
"common_layers",
".",
"is_xla_compiled",
"(",
")",
":",
"pad_remover",
"=",
"expert_utils",
".",
"PadRemover",
"(",
"padding",
")",
"ffn_unit",
"=",
"functools",
".",
"partial",
"(",
"transformer_encoder_ffn_unit",
",",
"hparams",
"=",
"hparams",
",",
"nonpadding_mask",
"=",
"nonpadding",
",",
"pad_remover",
"=",
"pad_remover",
")",
"attention_unit",
"=",
"functools",
".",
"partial",
"(",
"transformer_encoder_attention_unit",
",",
"hparams",
"=",
"hparams",
",",
"encoder_self_attention_bias",
"=",
"encoder_self_attention_bias",
",",
"attention_dropout_broadcast_dims",
"=",
"attention_dropout_broadcast_dims",
",",
"save_weights_to",
"=",
"save_weights_to",
",",
"make_image_summary",
"=",
"make_image_summary",
")",
"x",
",",
"extra_output",
"=",
"universal_transformer_layer",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
"=",
"pad_remover",
")",
"return",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"extra_output"
] |
Universal Transformer encoder function.
Prepares all the arguments and the inputs and passes it to a
universal_transformer_layer to encode the encoder_input.
Args:
encoder_input: a Tensor
encoder_self_attention_bias: bias Tensor for self-attention
(see common_attention.attention_bias())
hparams: hyperparameters for model
name: a string
nonpadding: optional Tensor with shape [batch_size, encoder_length]
indicating what positions are not padding. This must either be
passed in, which we do for "packed" datasets, or inferred from
encoder_self_attention_bias. The knowledge about padding is used
for pad_remover(efficiency) and to mask out padding in convoltutional
layers.
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.
Returns:
y: a Tensors as the output of the encoder
extra_output: which can be used to pass extra information to the body
|
[
"Universal",
"Transformer",
"encoder",
"function",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L62-L128
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_layer
|
def universal_transformer_layer(x,
hparams,
ffn_unit,
attention_unit,
pad_remover=None):
"""Core function applying the universal transformer layer.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
the output tensor, extra output (can be memory, ponder time, etc.)
Raises:
ValueError: Unknown recurrence type
"""
def add_vanilla_transformer_layer(x, num_layers, name):
"""Passes the input through num_layers of vanilla transformer layers.
Args:
x: input
num_layers: number of layers
name: string, prefix of layer names
Returns:
output of vanilla_transformer_layer
"""
if hparams.add_position_timing_signal:
# In case of add_position_timing_signal=true, we set hparams.pos=None
# and add position timing signal at the beginning of each step, so for
# the vanilla transformer, we need to add timing signal here.
x = common_attention.add_timing_signal_1d(x)
for layer in range(num_layers):
with tf.variable_scope(name + "layer_%d" % layer):
x = ffn_unit(attention_unit(x))
return x
with tf.variable_scope("universal_transformer_%s" % hparams.recurrence_type):
if (hparams.mix_with_transformer and
"before_ut" in hparams.mix_with_transformer):
x = add_vanilla_transformer_layer(x, hparams.num_mixedin_layers,
"before_ut_")
if hparams.recurrence_type == "act":
output, extra_output = universal_transformer_act(
x, hparams, ffn_unit, attention_unit)
else: # for all the other recurrency types with fixed number of steps
ut_function, initializer = get_ut_layer(x, hparams, ffn_unit,
attention_unit, pad_remover)
output, _, extra_output = tf.foldl(
ut_function, tf.range(hparams.num_rec_steps),
initializer=initializer)
# Right now, this is only possible when the transition function is an lstm
if (hparams.recurrence_type == "lstm" and
hparams.get("use_memory_as_final_state", False)):
output = extra_output
if (hparams.mix_with_transformer and
"after_ut" in hparams.mix_with_transformer):
output = add_vanilla_transformer_layer(output, hparams.num_mixedin_layers,
"after_ut_")
return output, extra_output
|
python
|
def universal_transformer_layer(x,
hparams,
ffn_unit,
attention_unit,
pad_remover=None):
"""Core function applying the universal transformer layer.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
the output tensor, extra output (can be memory, ponder time, etc.)
Raises:
ValueError: Unknown recurrence type
"""
def add_vanilla_transformer_layer(x, num_layers, name):
"""Passes the input through num_layers of vanilla transformer layers.
Args:
x: input
num_layers: number of layers
name: string, prefix of layer names
Returns:
output of vanilla_transformer_layer
"""
if hparams.add_position_timing_signal:
# In case of add_position_timing_signal=true, we set hparams.pos=None
# and add position timing signal at the beginning of each step, so for
# the vanilla transformer, we need to add timing signal here.
x = common_attention.add_timing_signal_1d(x)
for layer in range(num_layers):
with tf.variable_scope(name + "layer_%d" % layer):
x = ffn_unit(attention_unit(x))
return x
with tf.variable_scope("universal_transformer_%s" % hparams.recurrence_type):
if (hparams.mix_with_transformer and
"before_ut" in hparams.mix_with_transformer):
x = add_vanilla_transformer_layer(x, hparams.num_mixedin_layers,
"before_ut_")
if hparams.recurrence_type == "act":
output, extra_output = universal_transformer_act(
x, hparams, ffn_unit, attention_unit)
else: # for all the other recurrency types with fixed number of steps
ut_function, initializer = get_ut_layer(x, hparams, ffn_unit,
attention_unit, pad_remover)
output, _, extra_output = tf.foldl(
ut_function, tf.range(hparams.num_rec_steps),
initializer=initializer)
# Right now, this is only possible when the transition function is an lstm
if (hparams.recurrence_type == "lstm" and
hparams.get("use_memory_as_final_state", False)):
output = extra_output
if (hparams.mix_with_transformer and
"after_ut" in hparams.mix_with_transformer):
output = add_vanilla_transformer_layer(output, hparams.num_mixedin_layers,
"after_ut_")
return output, extra_output
|
[
"def",
"universal_transformer_layer",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
"=",
"None",
")",
":",
"def",
"add_vanilla_transformer_layer",
"(",
"x",
",",
"num_layers",
",",
"name",
")",
":",
"\"\"\"Passes the input through num_layers of vanilla transformer layers.\n\n Args:\n x: input\n num_layers: number of layers\n name: string, prefix of layer names\n\n Returns:\n output of vanilla_transformer_layer\n \"\"\"",
"if",
"hparams",
".",
"add_position_timing_signal",
":",
"# In case of add_position_timing_signal=true, we set hparams.pos=None",
"# and add position timing signal at the beginning of each step, so for",
"# the vanilla transformer, we need to add timing signal here.",
"x",
"=",
"common_attention",
".",
"add_timing_signal_1d",
"(",
"x",
")",
"for",
"layer",
"in",
"range",
"(",
"num_layers",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"name",
"+",
"\"layer_%d\"",
"%",
"layer",
")",
":",
"x",
"=",
"ffn_unit",
"(",
"attention_unit",
"(",
"x",
")",
")",
"return",
"x",
"with",
"tf",
".",
"variable_scope",
"(",
"\"universal_transformer_%s\"",
"%",
"hparams",
".",
"recurrence_type",
")",
":",
"if",
"(",
"hparams",
".",
"mix_with_transformer",
"and",
"\"before_ut\"",
"in",
"hparams",
".",
"mix_with_transformer",
")",
":",
"x",
"=",
"add_vanilla_transformer_layer",
"(",
"x",
",",
"hparams",
".",
"num_mixedin_layers",
",",
"\"before_ut_\"",
")",
"if",
"hparams",
".",
"recurrence_type",
"==",
"\"act\"",
":",
"output",
",",
"extra_output",
"=",
"universal_transformer_act",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
")",
"else",
":",
"# for all the other recurrency types with fixed number of steps",
"ut_function",
",",
"initializer",
"=",
"get_ut_layer",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
")",
"output",
",",
"_",
",",
"extra_output",
"=",
"tf",
".",
"foldl",
"(",
"ut_function",
",",
"tf",
".",
"range",
"(",
"hparams",
".",
"num_rec_steps",
")",
",",
"initializer",
"=",
"initializer",
")",
"# Right now, this is only possible when the transition function is an lstm",
"if",
"(",
"hparams",
".",
"recurrence_type",
"==",
"\"lstm\"",
"and",
"hparams",
".",
"get",
"(",
"\"use_memory_as_final_state\"",
",",
"False",
")",
")",
":",
"output",
"=",
"extra_output",
"if",
"(",
"hparams",
".",
"mix_with_transformer",
"and",
"\"after_ut\"",
"in",
"hparams",
".",
"mix_with_transformer",
")",
":",
"output",
"=",
"add_vanilla_transformer_layer",
"(",
"output",
",",
"hparams",
".",
"num_mixedin_layers",
",",
"\"after_ut_\"",
")",
"return",
"output",
",",
"extra_output"
] |
Core function applying the universal transformer layer.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
the output tensor, extra output (can be memory, ponder time, etc.)
Raises:
ValueError: Unknown recurrence type
|
[
"Core",
"function",
"applying",
"the",
"universal",
"transformer",
"layer",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L194-L265
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
get_ut_layer
|
def get_ut_layer(x,
hparams,
ffn_unit,
attention_unit,
pad_remover=None):
"""Provides the function that is used in universal transforemr steps.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
ut_function and the ut_initializer
Raises:
ValueError: Unknown recurrence type
"""
if hparams.recurrence_type == "basic":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_basic,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit)
elif hparams.recurrence_type == "highway":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_highway,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
elif hparams.recurrence_type == "skip":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_skip,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
elif hparams.recurrence_type == "dwa":
# memory contains the original input + all the states
memory_size = hparams.num_rec_steps + 1
# prepare initializer:
memory_empty = tf.zeros([memory_size] + common_layers.shape_list(x))
# filling the first slot with the original input
memory = fill_memory_slot(memory_empty, x, 0)
ut_initializer = (x, x, memory) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_depthwise_attention,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit)
elif hparams.recurrence_type == "gru":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_with_gru_as_transition_function,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
elif hparams.recurrence_type == "lstm":
memory = tf.zeros(common_layers.shape_list(x))
ut_initializer = (x, x, memory) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_with_lstm_as_transition_function,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
else:
raise ValueError("Unknown recurrence type: %s" % hparams.recurrence_type)
return ut_function, ut_initializer
|
python
|
def get_ut_layer(x,
hparams,
ffn_unit,
attention_unit,
pad_remover=None):
"""Provides the function that is used in universal transforemr steps.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
ut_function and the ut_initializer
Raises:
ValueError: Unknown recurrence type
"""
if hparams.recurrence_type == "basic":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_basic,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit)
elif hparams.recurrence_type == "highway":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_highway,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
elif hparams.recurrence_type == "skip":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_skip,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
elif hparams.recurrence_type == "dwa":
# memory contains the original input + all the states
memory_size = hparams.num_rec_steps + 1
# prepare initializer:
memory_empty = tf.zeros([memory_size] + common_layers.shape_list(x))
# filling the first slot with the original input
memory = fill_memory_slot(memory_empty, x, 0)
ut_initializer = (x, x, memory) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_depthwise_attention,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit)
elif hparams.recurrence_type == "gru":
ut_initializer = (x, x, x) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_with_gru_as_transition_function,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
elif hparams.recurrence_type == "lstm":
memory = tf.zeros(common_layers.shape_list(x))
ut_initializer = (x, x, memory) # (state, input, memory)
ut_function = functools.partial(
universal_transformer_with_lstm_as_transition_function,
hparams=hparams,
ffn_unit=ffn_unit,
attention_unit=attention_unit,
pad_remover=pad_remover)
else:
raise ValueError("Unknown recurrence type: %s" % hparams.recurrence_type)
return ut_function, ut_initializer
|
[
"def",
"get_ut_layer",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
"=",
"None",
")",
":",
"if",
"hparams",
".",
"recurrence_type",
"==",
"\"basic\"",
":",
"ut_initializer",
"=",
"(",
"x",
",",
"x",
",",
"x",
")",
"# (state, input, memory)",
"ut_function",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_basic",
",",
"hparams",
"=",
"hparams",
",",
"ffn_unit",
"=",
"ffn_unit",
",",
"attention_unit",
"=",
"attention_unit",
")",
"elif",
"hparams",
".",
"recurrence_type",
"==",
"\"highway\"",
":",
"ut_initializer",
"=",
"(",
"x",
",",
"x",
",",
"x",
")",
"# (state, input, memory)",
"ut_function",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_highway",
",",
"hparams",
"=",
"hparams",
",",
"ffn_unit",
"=",
"ffn_unit",
",",
"attention_unit",
"=",
"attention_unit",
",",
"pad_remover",
"=",
"pad_remover",
")",
"elif",
"hparams",
".",
"recurrence_type",
"==",
"\"skip\"",
":",
"ut_initializer",
"=",
"(",
"x",
",",
"x",
",",
"x",
")",
"# (state, input, memory)",
"ut_function",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_skip",
",",
"hparams",
"=",
"hparams",
",",
"ffn_unit",
"=",
"ffn_unit",
",",
"attention_unit",
"=",
"attention_unit",
",",
"pad_remover",
"=",
"pad_remover",
")",
"elif",
"hparams",
".",
"recurrence_type",
"==",
"\"dwa\"",
":",
"# memory contains the original input + all the states",
"memory_size",
"=",
"hparams",
".",
"num_rec_steps",
"+",
"1",
"# prepare initializer:",
"memory_empty",
"=",
"tf",
".",
"zeros",
"(",
"[",
"memory_size",
"]",
"+",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
")",
"# filling the first slot with the original input",
"memory",
"=",
"fill_memory_slot",
"(",
"memory_empty",
",",
"x",
",",
"0",
")",
"ut_initializer",
"=",
"(",
"x",
",",
"x",
",",
"memory",
")",
"# (state, input, memory)",
"ut_function",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_depthwise_attention",
",",
"hparams",
"=",
"hparams",
",",
"ffn_unit",
"=",
"ffn_unit",
",",
"attention_unit",
"=",
"attention_unit",
")",
"elif",
"hparams",
".",
"recurrence_type",
"==",
"\"gru\"",
":",
"ut_initializer",
"=",
"(",
"x",
",",
"x",
",",
"x",
")",
"# (state, input, memory)",
"ut_function",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_with_gru_as_transition_function",
",",
"hparams",
"=",
"hparams",
",",
"ffn_unit",
"=",
"ffn_unit",
",",
"attention_unit",
"=",
"attention_unit",
",",
"pad_remover",
"=",
"pad_remover",
")",
"elif",
"hparams",
".",
"recurrence_type",
"==",
"\"lstm\"",
":",
"memory",
"=",
"tf",
".",
"zeros",
"(",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
")",
"ut_initializer",
"=",
"(",
"x",
",",
"x",
",",
"memory",
")",
"# (state, input, memory)",
"ut_function",
"=",
"functools",
".",
"partial",
"(",
"universal_transformer_with_lstm_as_transition_function",
",",
"hparams",
"=",
"hparams",
",",
"ffn_unit",
"=",
"ffn_unit",
",",
"attention_unit",
"=",
"attention_unit",
",",
"pad_remover",
"=",
"pad_remover",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"Unknown recurrence type: %s\"",
"%",
"hparams",
".",
"recurrence_type",
")",
"return",
"ut_function",
",",
"ut_initializer"
] |
Provides the function that is used in universal transforemr steps.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
ut_function and the ut_initializer
Raises:
ValueError: Unknown recurrence type
|
[
"Provides",
"the",
"function",
"that",
"is",
"used",
"in",
"universal",
"transforemr",
"steps",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L268-L354
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
transformer_encoder_ffn_unit
|
def transformer_encoder_ffn_unit(x,
hparams,
nonpadding_mask=None,
pad_remover=None):
"""Applies a feed-forward function which is parametrised for encoding.
Args:
x: input
hparams: model hyper-parameters
nonpadding_mask: optional Tensor with shape [batch_size, encoder_length]
indicating what positions are not padding. This is used
to mask out padding in convoltutional layers. We generally only
need this mask for "packed" datasets, because for ordinary datasets,
no padding is ever followed by nonpadding.
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
the output tensor
"""
with tf.variable_scope("ffn"):
if hparams.transformer_ffn_type == "fc":
y = transformer.transformer_ffn_layer(
common_layers.layer_preprocess(x, hparams),
hparams,
pad_remover,
conv_padding="SAME",
nonpadding_mask=nonpadding_mask)
if hparams.transformer_ffn_type == "sepconv":
assert nonpadding_mask is not None, (
"The nonpadding_mask should be provided, otherwise the model uses "
"the leaked padding information to estimate the length!")
y = common_layers.sepconv_relu_sepconv(
common_layers.layer_preprocess(x, hparams),
filter_size=hparams.filter_size,
output_size=hparams.hidden_size,
first_kernel_size=(3, 1),
second_kernel_size=(5, 1),
padding="SAME",
nonpadding_mask=nonpadding_mask,
dropout=hparams.relu_dropout)
x = common_layers.layer_postprocess(x, y, hparams)
return x
|
python
|
def transformer_encoder_ffn_unit(x,
hparams,
nonpadding_mask=None,
pad_remover=None):
"""Applies a feed-forward function which is parametrised for encoding.
Args:
x: input
hparams: model hyper-parameters
nonpadding_mask: optional Tensor with shape [batch_size, encoder_length]
indicating what positions are not padding. This is used
to mask out padding in convoltutional layers. We generally only
need this mask for "packed" datasets, because for ordinary datasets,
no padding is ever followed by nonpadding.
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
the output tensor
"""
with tf.variable_scope("ffn"):
if hparams.transformer_ffn_type == "fc":
y = transformer.transformer_ffn_layer(
common_layers.layer_preprocess(x, hparams),
hparams,
pad_remover,
conv_padding="SAME",
nonpadding_mask=nonpadding_mask)
if hparams.transformer_ffn_type == "sepconv":
assert nonpadding_mask is not None, (
"The nonpadding_mask should be provided, otherwise the model uses "
"the leaked padding information to estimate the length!")
y = common_layers.sepconv_relu_sepconv(
common_layers.layer_preprocess(x, hparams),
filter_size=hparams.filter_size,
output_size=hparams.hidden_size,
first_kernel_size=(3, 1),
second_kernel_size=(5, 1),
padding="SAME",
nonpadding_mask=nonpadding_mask,
dropout=hparams.relu_dropout)
x = common_layers.layer_postprocess(x, y, hparams)
return x
|
[
"def",
"transformer_encoder_ffn_unit",
"(",
"x",
",",
"hparams",
",",
"nonpadding_mask",
"=",
"None",
",",
"pad_remover",
"=",
"None",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"ffn\"",
")",
":",
"if",
"hparams",
".",
"transformer_ffn_type",
"==",
"\"fc\"",
":",
"y",
"=",
"transformer",
".",
"transformer_ffn_layer",
"(",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"hparams",
",",
"pad_remover",
",",
"conv_padding",
"=",
"\"SAME\"",
",",
"nonpadding_mask",
"=",
"nonpadding_mask",
")",
"if",
"hparams",
".",
"transformer_ffn_type",
"==",
"\"sepconv\"",
":",
"assert",
"nonpadding_mask",
"is",
"not",
"None",
",",
"(",
"\"The nonpadding_mask should be provided, otherwise the model uses \"",
"\"the leaked padding information to estimate the length!\"",
")",
"y",
"=",
"common_layers",
".",
"sepconv_relu_sepconv",
"(",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"filter_size",
"=",
"hparams",
".",
"filter_size",
",",
"output_size",
"=",
"hparams",
".",
"hidden_size",
",",
"first_kernel_size",
"=",
"(",
"3",
",",
"1",
")",
",",
"second_kernel_size",
"=",
"(",
"5",
",",
"1",
")",
",",
"padding",
"=",
"\"SAME\"",
",",
"nonpadding_mask",
"=",
"nonpadding_mask",
",",
"dropout",
"=",
"hparams",
".",
"relu_dropout",
")",
"x",
"=",
"common_layers",
".",
"layer_postprocess",
"(",
"x",
",",
"y",
",",
"hparams",
")",
"return",
"x"
] |
Applies a feed-forward function which is parametrised for encoding.
Args:
x: input
hparams: model hyper-parameters
nonpadding_mask: optional Tensor with shape [batch_size, encoder_length]
indicating what positions are not padding. This is used
to mask out padding in convoltutional layers. We generally only
need this mask for "packed" datasets, because for ordinary datasets,
no padding is ever followed by nonpadding.
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
the output tensor
|
[
"Applies",
"a",
"feed",
"-",
"forward",
"function",
"which",
"is",
"parametrised",
"for",
"encoding",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L357-L402
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
transformer_encoder_attention_unit
|
def transformer_encoder_attention_unit(x,
hparams,
encoder_self_attention_bias,
attention_dropout_broadcast_dims,
save_weights_to=None,
make_image_summary=True):
"""Applies multihead attention function which is parametrised for encoding.
Args:
x: input
hparams: model hyper-parameters
encoder_self_attention_bias: a bias tensor for use in encoder self-attention
attention_dropout_broadcast_dims: Fpr noise broadcasting in the dropout
layers to save memory during training
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).
make_image_summary: Whether to make an attention image summary.
Returns:
the output tensor
"""
with tf.variable_scope("self_attention"):
y = common_attention.multihead_attention(
common_layers.layer_preprocess(x, hparams),
None,
encoder_self_attention_bias,
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,
attention_type=hparams.self_attention_type,
save_weights_to=save_weights_to,
max_relative_position=hparams.max_relative_position,
make_image_summary=make_image_summary,
dropout_broadcast_dims=attention_dropout_broadcast_dims,
hard_attention_k=hparams.hard_attention_k)
x = common_layers.layer_postprocess(x, y, hparams)
return x
|
python
|
def transformer_encoder_attention_unit(x,
hparams,
encoder_self_attention_bias,
attention_dropout_broadcast_dims,
save_weights_to=None,
make_image_summary=True):
"""Applies multihead attention function which is parametrised for encoding.
Args:
x: input
hparams: model hyper-parameters
encoder_self_attention_bias: a bias tensor for use in encoder self-attention
attention_dropout_broadcast_dims: Fpr noise broadcasting in the dropout
layers to save memory during training
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).
make_image_summary: Whether to make an attention image summary.
Returns:
the output tensor
"""
with tf.variable_scope("self_attention"):
y = common_attention.multihead_attention(
common_layers.layer_preprocess(x, hparams),
None,
encoder_self_attention_bias,
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,
attention_type=hparams.self_attention_type,
save_weights_to=save_weights_to,
max_relative_position=hparams.max_relative_position,
make_image_summary=make_image_summary,
dropout_broadcast_dims=attention_dropout_broadcast_dims,
hard_attention_k=hparams.hard_attention_k)
x = common_layers.layer_postprocess(x, y, hparams)
return x
|
[
"def",
"transformer_encoder_attention_unit",
"(",
"x",
",",
"hparams",
",",
"encoder_self_attention_bias",
",",
"attention_dropout_broadcast_dims",
",",
"save_weights_to",
"=",
"None",
",",
"make_image_summary",
"=",
"True",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"self_attention\"",
")",
":",
"y",
"=",
"common_attention",
".",
"multihead_attention",
"(",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"None",
",",
"encoder_self_attention_bias",
",",
"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",
",",
"attention_type",
"=",
"hparams",
".",
"self_attention_type",
",",
"save_weights_to",
"=",
"save_weights_to",
",",
"max_relative_position",
"=",
"hparams",
".",
"max_relative_position",
",",
"make_image_summary",
"=",
"make_image_summary",
",",
"dropout_broadcast_dims",
"=",
"attention_dropout_broadcast_dims",
",",
"hard_attention_k",
"=",
"hparams",
".",
"hard_attention_k",
")",
"x",
"=",
"common_layers",
".",
"layer_postprocess",
"(",
"x",
",",
"y",
",",
"hparams",
")",
"return",
"x"
] |
Applies multihead attention function which is parametrised for encoding.
Args:
x: input
hparams: model hyper-parameters
encoder_self_attention_bias: a bias tensor for use in encoder self-attention
attention_dropout_broadcast_dims: Fpr noise broadcasting in the dropout
layers to save memory during training
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).
make_image_summary: Whether to make an attention image summary.
Returns:
the output tensor
|
[
"Applies",
"multihead",
"attention",
"function",
"which",
"is",
"parametrised",
"for",
"encoding",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L405-L446
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
transformer_decoder_attention_unit
|
def transformer_decoder_attention_unit(x,
hparams,
encoder_output,
decoder_self_attention_bias,
encoder_decoder_attention_bias,
attention_dropout_broadcast_dims,
save_weights_to=None,
make_image_summary=True):
"""Applies multihead attention function which is parametrised for decoding.
Args:
x: input (decoder input)
hparams: model hyper-parameters
encoder_output: Encoder representation. [batch_size, input_length,
hidden_dim]
decoder_self_attention_bias: Bias and mask weights for decoder
self-attention. [batch_size, decoder_length]
encoder_decoder_attention_bias: Bias and mask weights for encoder-decoder
attention. [batch_size, input_length]
attention_dropout_broadcast_dims: Fpr noise broadcasting in the dropout
layers to save memory during training
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).
make_image_summary: Whether to make an attention image summary.
Returns:
The output tensor
"""
with tf.variable_scope("self_attention"):
y = common_attention.multihead_attention(
common_layers.layer_preprocess(x, hparams),
None,
decoder_self_attention_bias,
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,
attention_type=hparams.self_attention_type,
save_weights_to=save_weights_to,
max_relative_position=hparams.max_relative_position,
cache=None,
make_image_summary=make_image_summary,
dropout_broadcast_dims=attention_dropout_broadcast_dims,
hard_attention_k=hparams.hard_attention_k)
x = common_layers.layer_postprocess(x, y, hparams)
if encoder_output is not None:
with tf.variable_scope("encdec_attention"):
y = common_attention.multihead_attention(
common_layers.layer_preprocess(x, hparams),
encoder_output,
encoder_decoder_attention_bias,
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,
save_weights_to=save_weights_to,
make_image_summary=make_image_summary,
dropout_broadcast_dims=attention_dropout_broadcast_dims,
hard_attention_k=hparams.hard_attention_k)
x = common_layers.layer_postprocess(x, y, hparams)
return x
|
python
|
def transformer_decoder_attention_unit(x,
hparams,
encoder_output,
decoder_self_attention_bias,
encoder_decoder_attention_bias,
attention_dropout_broadcast_dims,
save_weights_to=None,
make_image_summary=True):
"""Applies multihead attention function which is parametrised for decoding.
Args:
x: input (decoder input)
hparams: model hyper-parameters
encoder_output: Encoder representation. [batch_size, input_length,
hidden_dim]
decoder_self_attention_bias: Bias and mask weights for decoder
self-attention. [batch_size, decoder_length]
encoder_decoder_attention_bias: Bias and mask weights for encoder-decoder
attention. [batch_size, input_length]
attention_dropout_broadcast_dims: Fpr noise broadcasting in the dropout
layers to save memory during training
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).
make_image_summary: Whether to make an attention image summary.
Returns:
The output tensor
"""
with tf.variable_scope("self_attention"):
y = common_attention.multihead_attention(
common_layers.layer_preprocess(x, hparams),
None,
decoder_self_attention_bias,
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,
attention_type=hparams.self_attention_type,
save_weights_to=save_weights_to,
max_relative_position=hparams.max_relative_position,
cache=None,
make_image_summary=make_image_summary,
dropout_broadcast_dims=attention_dropout_broadcast_dims,
hard_attention_k=hparams.hard_attention_k)
x = common_layers.layer_postprocess(x, y, hparams)
if encoder_output is not None:
with tf.variable_scope("encdec_attention"):
y = common_attention.multihead_attention(
common_layers.layer_preprocess(x, hparams),
encoder_output,
encoder_decoder_attention_bias,
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,
save_weights_to=save_weights_to,
make_image_summary=make_image_summary,
dropout_broadcast_dims=attention_dropout_broadcast_dims,
hard_attention_k=hparams.hard_attention_k)
x = common_layers.layer_postprocess(x, y, hparams)
return x
|
[
"def",
"transformer_decoder_attention_unit",
"(",
"x",
",",
"hparams",
",",
"encoder_output",
",",
"decoder_self_attention_bias",
",",
"encoder_decoder_attention_bias",
",",
"attention_dropout_broadcast_dims",
",",
"save_weights_to",
"=",
"None",
",",
"make_image_summary",
"=",
"True",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"self_attention\"",
")",
":",
"y",
"=",
"common_attention",
".",
"multihead_attention",
"(",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"None",
",",
"decoder_self_attention_bias",
",",
"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",
",",
"attention_type",
"=",
"hparams",
".",
"self_attention_type",
",",
"save_weights_to",
"=",
"save_weights_to",
",",
"max_relative_position",
"=",
"hparams",
".",
"max_relative_position",
",",
"cache",
"=",
"None",
",",
"make_image_summary",
"=",
"make_image_summary",
",",
"dropout_broadcast_dims",
"=",
"attention_dropout_broadcast_dims",
",",
"hard_attention_k",
"=",
"hparams",
".",
"hard_attention_k",
")",
"x",
"=",
"common_layers",
".",
"layer_postprocess",
"(",
"x",
",",
"y",
",",
"hparams",
")",
"if",
"encoder_output",
"is",
"not",
"None",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"encdec_attention\"",
")",
":",
"y",
"=",
"common_attention",
".",
"multihead_attention",
"(",
"common_layers",
".",
"layer_preprocess",
"(",
"x",
",",
"hparams",
")",
",",
"encoder_output",
",",
"encoder_decoder_attention_bias",
",",
"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",
",",
"save_weights_to",
"=",
"save_weights_to",
",",
"make_image_summary",
"=",
"make_image_summary",
",",
"dropout_broadcast_dims",
"=",
"attention_dropout_broadcast_dims",
",",
"hard_attention_k",
"=",
"hparams",
".",
"hard_attention_k",
")",
"x",
"=",
"common_layers",
".",
"layer_postprocess",
"(",
"x",
",",
"y",
",",
"hparams",
")",
"return",
"x"
] |
Applies multihead attention function which is parametrised for decoding.
Args:
x: input (decoder input)
hparams: model hyper-parameters
encoder_output: Encoder representation. [batch_size, input_length,
hidden_dim]
decoder_self_attention_bias: Bias and mask weights for decoder
self-attention. [batch_size, decoder_length]
encoder_decoder_attention_bias: Bias and mask weights for encoder-decoder
attention. [batch_size, input_length]
attention_dropout_broadcast_dims: Fpr noise broadcasting in the dropout
layers to save memory during training
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).
make_image_summary: Whether to make an attention image summary.
Returns:
The output tensor
|
[
"Applies",
"multihead",
"attention",
"function",
"which",
"is",
"parametrised",
"for",
"decoding",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L492-L556
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_basic
|
def universal_transformer_basic(layer_inputs,
step, hparams,
ffn_unit,
attention_unit):
"""Basic Universal Transformer.
This model is pretty similar to the vanilla transformer in which weights are
shared between layers. For some tasks, this simple idea brings a
generalization that is not achievable by playing with the size of the model
or drop_out parameters in the vanilla transformer.
Args:
layer_inputs:
- state: state
step: indicates number of steps taken so far
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
layer_output:
new_state: new state
"""
state, inputs, memory = tf.unstack(layer_inputs, num=None, axis=0,
name="unstack")
new_state = step_preprocess(state, step, hparams)
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
new_state = ffn_unit(attention_unit(new_state))
return new_state, inputs, memory
|
python
|
def universal_transformer_basic(layer_inputs,
step, hparams,
ffn_unit,
attention_unit):
"""Basic Universal Transformer.
This model is pretty similar to the vanilla transformer in which weights are
shared between layers. For some tasks, this simple idea brings a
generalization that is not achievable by playing with the size of the model
or drop_out parameters in the vanilla transformer.
Args:
layer_inputs:
- state: state
step: indicates number of steps taken so far
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
layer_output:
new_state: new state
"""
state, inputs, memory = tf.unstack(layer_inputs, num=None, axis=0,
name="unstack")
new_state = step_preprocess(state, step, hparams)
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
new_state = ffn_unit(attention_unit(new_state))
return new_state, inputs, memory
|
[
"def",
"universal_transformer_basic",
"(",
"layer_inputs",
",",
"step",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
")",
":",
"state",
",",
"inputs",
",",
"memory",
"=",
"tf",
".",
"unstack",
"(",
"layer_inputs",
",",
"num",
"=",
"None",
",",
"axis",
"=",
"0",
",",
"name",
"=",
"\"unstack\"",
")",
"new_state",
"=",
"step_preprocess",
"(",
"state",
",",
"step",
",",
"hparams",
")",
"for",
"i",
"in",
"range",
"(",
"hparams",
".",
"num_inrecurrence_layers",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"rec_layer_%d\"",
"%",
"i",
")",
":",
"new_state",
"=",
"ffn_unit",
"(",
"attention_unit",
"(",
"new_state",
")",
")",
"return",
"new_state",
",",
"inputs",
",",
"memory"
] |
Basic Universal Transformer.
This model is pretty similar to the vanilla transformer in which weights are
shared between layers. For some tasks, this simple idea brings a
generalization that is not achievable by playing with the size of the model
or drop_out parameters in the vanilla transformer.
Args:
layer_inputs:
- state: state
step: indicates number of steps taken so far
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
layer_output:
new_state: new state
|
[
"Basic",
"Universal",
"Transformer",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L559-L590
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_highway
|
def universal_transformer_highway(layer_inputs,
step, hparams,
ffn_unit,
attention_unit,
pad_remover=None):
"""Universal Transformer with highway connection.
It transforms the state using a block contaaining sel-attention and transition
function and wrap the whole block with a highway connection.
(the new state is a combination of the state and the transformed-state
based on cary/transform gates.)
Interesting observation:
Controlling the cary/transform gate with the original inputs works usually
better (i.e. hparams.gates_inputs="i")
Args:
layer_inputs:
- state: state
- inputs: the original embedded inputs (= inputs to the first step)
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: the original embedded inputs (= inputs to the first step)
"""
state, inputs, memory = layer_inputs
new_state = step_preprocess(state, step, hparams)
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
new_state = ffn_unit(attention_unit(new_state))
transformed_state = new_state
gate_inputs = []
if "s" in hparams.gates_inputs:
gate_inputs.append(state)
if "t" in hparams.gates_inputs:
gate_inputs.append(transformed_state)
if "i" in hparams.gates_inputs:
gate_inputs.append(inputs)
gate_ffn_layer = hparams.gate_ffn_layer
transform_gate = _ffn_layer_multi_inputs(
gate_inputs,
hparams,
ffn_layer_type=gate_ffn_layer,
name="transform",
bias_initializer=tf.constant_initializer(hparams.transform_bias_init),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=True,
postprocess=True)
if hparams.couple_carry_transform_gates:
carry_gate = tf.subtract(1.0, transform_gate, name="carry")
else:
carry_gate = _ffn_layer_multi_inputs(
gate_inputs,
hparams,
ffn_layer_type=gate_ffn_layer,
name="carry",
bias_initializer=tf.constant_initializer(-hparams.transform_bias_init),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=True,
postprocess=True)
new_state = state * carry_gate + transformed_state * transform_gate
tf.contrib.summary.scalar("highway_transform_gate_layer",
tf.reduce_mean(transform_gate))
tf.contrib.summary.scalar("highway_carry_gate_layer",
tf.reduce_mean(carry_gate))
return new_state, inputs, memory
|
python
|
def universal_transformer_highway(layer_inputs,
step, hparams,
ffn_unit,
attention_unit,
pad_remover=None):
"""Universal Transformer with highway connection.
It transforms the state using a block contaaining sel-attention and transition
function and wrap the whole block with a highway connection.
(the new state is a combination of the state and the transformed-state
based on cary/transform gates.)
Interesting observation:
Controlling the cary/transform gate with the original inputs works usually
better (i.e. hparams.gates_inputs="i")
Args:
layer_inputs:
- state: state
- inputs: the original embedded inputs (= inputs to the first step)
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: the original embedded inputs (= inputs to the first step)
"""
state, inputs, memory = layer_inputs
new_state = step_preprocess(state, step, hparams)
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
new_state = ffn_unit(attention_unit(new_state))
transformed_state = new_state
gate_inputs = []
if "s" in hparams.gates_inputs:
gate_inputs.append(state)
if "t" in hparams.gates_inputs:
gate_inputs.append(transformed_state)
if "i" in hparams.gates_inputs:
gate_inputs.append(inputs)
gate_ffn_layer = hparams.gate_ffn_layer
transform_gate = _ffn_layer_multi_inputs(
gate_inputs,
hparams,
ffn_layer_type=gate_ffn_layer,
name="transform",
bias_initializer=tf.constant_initializer(hparams.transform_bias_init),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=True,
postprocess=True)
if hparams.couple_carry_transform_gates:
carry_gate = tf.subtract(1.0, transform_gate, name="carry")
else:
carry_gate = _ffn_layer_multi_inputs(
gate_inputs,
hparams,
ffn_layer_type=gate_ffn_layer,
name="carry",
bias_initializer=tf.constant_initializer(-hparams.transform_bias_init),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=True,
postprocess=True)
new_state = state * carry_gate + transformed_state * transform_gate
tf.contrib.summary.scalar("highway_transform_gate_layer",
tf.reduce_mean(transform_gate))
tf.contrib.summary.scalar("highway_carry_gate_layer",
tf.reduce_mean(carry_gate))
return new_state, inputs, memory
|
[
"def",
"universal_transformer_highway",
"(",
"layer_inputs",
",",
"step",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
"=",
"None",
")",
":",
"state",
",",
"inputs",
",",
"memory",
"=",
"layer_inputs",
"new_state",
"=",
"step_preprocess",
"(",
"state",
",",
"step",
",",
"hparams",
")",
"for",
"i",
"in",
"range",
"(",
"hparams",
".",
"num_inrecurrence_layers",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"rec_layer_%d\"",
"%",
"i",
")",
":",
"new_state",
"=",
"ffn_unit",
"(",
"attention_unit",
"(",
"new_state",
")",
")",
"transformed_state",
"=",
"new_state",
"gate_inputs",
"=",
"[",
"]",
"if",
"\"s\"",
"in",
"hparams",
".",
"gates_inputs",
":",
"gate_inputs",
".",
"append",
"(",
"state",
")",
"if",
"\"t\"",
"in",
"hparams",
".",
"gates_inputs",
":",
"gate_inputs",
".",
"append",
"(",
"transformed_state",
")",
"if",
"\"i\"",
"in",
"hparams",
".",
"gates_inputs",
":",
"gate_inputs",
".",
"append",
"(",
"inputs",
")",
"gate_ffn_layer",
"=",
"hparams",
".",
"gate_ffn_layer",
"transform_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"gate_inputs",
",",
"hparams",
",",
"ffn_layer_type",
"=",
"gate_ffn_layer",
",",
"name",
"=",
"\"transform\"",
",",
"bias_initializer",
"=",
"tf",
".",
"constant_initializer",
"(",
"hparams",
".",
"transform_bias_init",
")",
",",
"activation",
"=",
"tf",
".",
"sigmoid",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"True",
",",
"postprocess",
"=",
"True",
")",
"if",
"hparams",
".",
"couple_carry_transform_gates",
":",
"carry_gate",
"=",
"tf",
".",
"subtract",
"(",
"1.0",
",",
"transform_gate",
",",
"name",
"=",
"\"carry\"",
")",
"else",
":",
"carry_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"gate_inputs",
",",
"hparams",
",",
"ffn_layer_type",
"=",
"gate_ffn_layer",
",",
"name",
"=",
"\"carry\"",
",",
"bias_initializer",
"=",
"tf",
".",
"constant_initializer",
"(",
"-",
"hparams",
".",
"transform_bias_init",
")",
",",
"activation",
"=",
"tf",
".",
"sigmoid",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"True",
",",
"postprocess",
"=",
"True",
")",
"new_state",
"=",
"state",
"*",
"carry_gate",
"+",
"transformed_state",
"*",
"transform_gate",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"highway_transform_gate_layer\"",
",",
"tf",
".",
"reduce_mean",
"(",
"transform_gate",
")",
")",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"highway_carry_gate_layer\"",
",",
"tf",
".",
"reduce_mean",
"(",
"carry_gate",
")",
")",
"return",
"new_state",
",",
"inputs",
",",
"memory"
] |
Universal Transformer with highway connection.
It transforms the state using a block contaaining sel-attention and transition
function and wrap the whole block with a highway connection.
(the new state is a combination of the state and the transformed-state
based on cary/transform gates.)
Interesting observation:
Controlling the cary/transform gate with the original inputs works usually
better (i.e. hparams.gates_inputs="i")
Args:
layer_inputs:
- state: state
- inputs: the original embedded inputs (= inputs to the first step)
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: the original embedded inputs (= inputs to the first step)
|
[
"Universal",
"Transformer",
"with",
"highway",
"connection",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L593-L682
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_depthwise_attention
|
def universal_transformer_depthwise_attention(layer_inputs,
step, hparams,
ffn_unit,
attention_unit):
"""universal_transformer with depth-wise attention.
It uses an attention mechanism-flipped vertically-
over all the states from previous steps to generate the new_state.
Args:
layer_inputs:
- state: state
- memory: contains states from all the previous steps.
step: indicating number of steps take so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
layer_output:
new_state: new state
memory: contains states from all the previous steps.
"""
_, inputs, memory = layer_inputs
all_states = memory
# add depth signal
if hparams.depth_embedding:
all_states = add_depth_embedding(all_states)
# get the states up to the current step (non-zero part of the memory)
states_so_far = all_states[:step, :, :, :]
states_so_far_weights = tf.nn.softmax(
common_layers.dense(
states_so_far, (hparams.hidden_size if hparams.dwa_elements else 1),
activation=None,
use_bias=True),
axis=-1)
# prepare the state tensor that will be transformed
state_to_be_transformed = tf.reduce_sum(
(states_so_far * states_so_far_weights), axis=0)
new_state = step_preprocess(state_to_be_transformed, step, hparams)
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
new_state = ffn_unit(attention_unit(new_state))
# add the new state to the memory
memory = fill_memory_slot(memory, new_state, step + 1)
return new_state, inputs, memory
|
python
|
def universal_transformer_depthwise_attention(layer_inputs,
step, hparams,
ffn_unit,
attention_unit):
"""universal_transformer with depth-wise attention.
It uses an attention mechanism-flipped vertically-
over all the states from previous steps to generate the new_state.
Args:
layer_inputs:
- state: state
- memory: contains states from all the previous steps.
step: indicating number of steps take so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
layer_output:
new_state: new state
memory: contains states from all the previous steps.
"""
_, inputs, memory = layer_inputs
all_states = memory
# add depth signal
if hparams.depth_embedding:
all_states = add_depth_embedding(all_states)
# get the states up to the current step (non-zero part of the memory)
states_so_far = all_states[:step, :, :, :]
states_so_far_weights = tf.nn.softmax(
common_layers.dense(
states_so_far, (hparams.hidden_size if hparams.dwa_elements else 1),
activation=None,
use_bias=True),
axis=-1)
# prepare the state tensor that will be transformed
state_to_be_transformed = tf.reduce_sum(
(states_so_far * states_so_far_weights), axis=0)
new_state = step_preprocess(state_to_be_transformed, step, hparams)
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
new_state = ffn_unit(attention_unit(new_state))
# add the new state to the memory
memory = fill_memory_slot(memory, new_state, step + 1)
return new_state, inputs, memory
|
[
"def",
"universal_transformer_depthwise_attention",
"(",
"layer_inputs",
",",
"step",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
")",
":",
"_",
",",
"inputs",
",",
"memory",
"=",
"layer_inputs",
"all_states",
"=",
"memory",
"# add depth signal",
"if",
"hparams",
".",
"depth_embedding",
":",
"all_states",
"=",
"add_depth_embedding",
"(",
"all_states",
")",
"# get the states up to the current step (non-zero part of the memory)",
"states_so_far",
"=",
"all_states",
"[",
":",
"step",
",",
":",
",",
":",
",",
":",
"]",
"states_so_far_weights",
"=",
"tf",
".",
"nn",
".",
"softmax",
"(",
"common_layers",
".",
"dense",
"(",
"states_so_far",
",",
"(",
"hparams",
".",
"hidden_size",
"if",
"hparams",
".",
"dwa_elements",
"else",
"1",
")",
",",
"activation",
"=",
"None",
",",
"use_bias",
"=",
"True",
")",
",",
"axis",
"=",
"-",
"1",
")",
"# prepare the state tensor that will be transformed",
"state_to_be_transformed",
"=",
"tf",
".",
"reduce_sum",
"(",
"(",
"states_so_far",
"*",
"states_so_far_weights",
")",
",",
"axis",
"=",
"0",
")",
"new_state",
"=",
"step_preprocess",
"(",
"state_to_be_transformed",
",",
"step",
",",
"hparams",
")",
"for",
"i",
"in",
"range",
"(",
"hparams",
".",
"num_inrecurrence_layers",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"rec_layer_%d\"",
"%",
"i",
")",
":",
"new_state",
"=",
"ffn_unit",
"(",
"attention_unit",
"(",
"new_state",
")",
")",
"# add the new state to the memory",
"memory",
"=",
"fill_memory_slot",
"(",
"memory",
",",
"new_state",
",",
"step",
"+",
"1",
")",
"return",
"new_state",
",",
"inputs",
",",
"memory"
] |
universal_transformer with depth-wise attention.
It uses an attention mechanism-flipped vertically-
over all the states from previous steps to generate the new_state.
Args:
layer_inputs:
- state: state
- memory: contains states from all the previous steps.
step: indicating number of steps take so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
layer_output:
new_state: new state
memory: contains states from all the previous steps.
|
[
"universal_transformer",
"with",
"depth",
"-",
"wise",
"attention",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L777-L832
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_with_gru_as_transition_function
|
def universal_transformer_with_gru_as_transition_function(
layer_inputs, step, hparams, ffn_unit, attention_unit, pad_remover=None):
"""Universal Transformer which uses a gru as transition function.
It's kind of like having a gru, filliped vertically next to the Universal
Transformer that controls the flow of the information in depth,
over different steps of the Universal Transformer.
Args:
layer_inputs:
- state: state
- inputs: not used here
- memory: not used here
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: not uesed
memory: not used
"""
state, unused_inputs, unused_memory = tf.unstack(
layer_inputs, num=None, axis=0, name="unstack")
# state (ut_state): output of the gru in the previous step
# Multi_head_attention:
assert not hparams.add_step_timing_signal # Let gru count for us!
mh_attention_input = step_preprocess(state, step, hparams)
transition_function_input = attention_unit(mh_attention_input)
# Transition Function:
if hparams.add_ffn_unit_to_the_transition_function:
transition_function_input = ffn_unit(transition_function_input)
transition_function_input = common_layers.layer_preprocess(
transition_function_input, hparams)
with tf.variable_scope("gru"):
# gru update gate: z_t = sigmoid(W_z.x_t + U_z.h_{t-1})
transition_function_update_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="update",
bias_initializer=tf.constant_initializer(1.0),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("gru_update_gate",
tf.reduce_mean(transition_function_update_gate))
# gru reset gate: r_t = sigmoid(W_r.x_t + U_r.h_{t-1})
transition_function_reset_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="reset",
bias_initializer=tf.constant_initializer(1.0),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("gru_reset_gate",
tf.reduce_mean(transition_function_reset_gate))
reset_state = transition_function_reset_gate * state
# gru_candidate_activation: h' = tanh(W_{x_t} + U (r_t h_{t-1})
transition_function_candidate = _ffn_layer_multi_inputs(
[transition_function_input, reset_state],
hparams,
name="candidate",
bias_initializer=tf.zeros_initializer(),
activation=tf.tanh,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
transition_function_output = (
(1 - transition_function_update_gate) * transition_function_input +
transition_function_update_gate * transition_function_candidate)
transition_function_output = common_layers.layer_preprocess(
transition_function_output, hparams)
return transition_function_output, unused_inputs, unused_memory
|
python
|
def universal_transformer_with_gru_as_transition_function(
layer_inputs, step, hparams, ffn_unit, attention_unit, pad_remover=None):
"""Universal Transformer which uses a gru as transition function.
It's kind of like having a gru, filliped vertically next to the Universal
Transformer that controls the flow of the information in depth,
over different steps of the Universal Transformer.
Args:
layer_inputs:
- state: state
- inputs: not used here
- memory: not used here
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: not uesed
memory: not used
"""
state, unused_inputs, unused_memory = tf.unstack(
layer_inputs, num=None, axis=0, name="unstack")
# state (ut_state): output of the gru in the previous step
# Multi_head_attention:
assert not hparams.add_step_timing_signal # Let gru count for us!
mh_attention_input = step_preprocess(state, step, hparams)
transition_function_input = attention_unit(mh_attention_input)
# Transition Function:
if hparams.add_ffn_unit_to_the_transition_function:
transition_function_input = ffn_unit(transition_function_input)
transition_function_input = common_layers.layer_preprocess(
transition_function_input, hparams)
with tf.variable_scope("gru"):
# gru update gate: z_t = sigmoid(W_z.x_t + U_z.h_{t-1})
transition_function_update_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="update",
bias_initializer=tf.constant_initializer(1.0),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("gru_update_gate",
tf.reduce_mean(transition_function_update_gate))
# gru reset gate: r_t = sigmoid(W_r.x_t + U_r.h_{t-1})
transition_function_reset_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="reset",
bias_initializer=tf.constant_initializer(1.0),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("gru_reset_gate",
tf.reduce_mean(transition_function_reset_gate))
reset_state = transition_function_reset_gate * state
# gru_candidate_activation: h' = tanh(W_{x_t} + U (r_t h_{t-1})
transition_function_candidate = _ffn_layer_multi_inputs(
[transition_function_input, reset_state],
hparams,
name="candidate",
bias_initializer=tf.zeros_initializer(),
activation=tf.tanh,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
transition_function_output = (
(1 - transition_function_update_gate) * transition_function_input +
transition_function_update_gate * transition_function_candidate)
transition_function_output = common_layers.layer_preprocess(
transition_function_output, hparams)
return transition_function_output, unused_inputs, unused_memory
|
[
"def",
"universal_transformer_with_gru_as_transition_function",
"(",
"layer_inputs",
",",
"step",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
"=",
"None",
")",
":",
"state",
",",
"unused_inputs",
",",
"unused_memory",
"=",
"tf",
".",
"unstack",
"(",
"layer_inputs",
",",
"num",
"=",
"None",
",",
"axis",
"=",
"0",
",",
"name",
"=",
"\"unstack\"",
")",
"# state (ut_state): output of the gru in the previous step",
"# Multi_head_attention:",
"assert",
"not",
"hparams",
".",
"add_step_timing_signal",
"# Let gru count for us!",
"mh_attention_input",
"=",
"step_preprocess",
"(",
"state",
",",
"step",
",",
"hparams",
")",
"transition_function_input",
"=",
"attention_unit",
"(",
"mh_attention_input",
")",
"# Transition Function:",
"if",
"hparams",
".",
"add_ffn_unit_to_the_transition_function",
":",
"transition_function_input",
"=",
"ffn_unit",
"(",
"transition_function_input",
")",
"transition_function_input",
"=",
"common_layers",
".",
"layer_preprocess",
"(",
"transition_function_input",
",",
"hparams",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"\"gru\"",
")",
":",
"# gru update gate: z_t = sigmoid(W_z.x_t + U_z.h_{t-1})",
"transition_function_update_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"update\"",
",",
"bias_initializer",
"=",
"tf",
".",
"constant_initializer",
"(",
"1.0",
")",
",",
"activation",
"=",
"tf",
".",
"sigmoid",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"gru_update_gate\"",
",",
"tf",
".",
"reduce_mean",
"(",
"transition_function_update_gate",
")",
")",
"# gru reset gate: r_t = sigmoid(W_r.x_t + U_r.h_{t-1})",
"transition_function_reset_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"reset\"",
",",
"bias_initializer",
"=",
"tf",
".",
"constant_initializer",
"(",
"1.0",
")",
",",
"activation",
"=",
"tf",
".",
"sigmoid",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"gru_reset_gate\"",
",",
"tf",
".",
"reduce_mean",
"(",
"transition_function_reset_gate",
")",
")",
"reset_state",
"=",
"transition_function_reset_gate",
"*",
"state",
"# gru_candidate_activation: h' = tanh(W_{x_t} + U (r_t h_{t-1})",
"transition_function_candidate",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"reset_state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"candidate\"",
",",
"bias_initializer",
"=",
"tf",
".",
"zeros_initializer",
"(",
")",
",",
"activation",
"=",
"tf",
".",
"tanh",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"transition_function_output",
"=",
"(",
"(",
"1",
"-",
"transition_function_update_gate",
")",
"*",
"transition_function_input",
"+",
"transition_function_update_gate",
"*",
"transition_function_candidate",
")",
"transition_function_output",
"=",
"common_layers",
".",
"layer_preprocess",
"(",
"transition_function_output",
",",
"hparams",
")",
"return",
"transition_function_output",
",",
"unused_inputs",
",",
"unused_memory"
] |
Universal Transformer which uses a gru as transition function.
It's kind of like having a gru, filliped vertically next to the Universal
Transformer that controls the flow of the information in depth,
over different steps of the Universal Transformer.
Args:
layer_inputs:
- state: state
- inputs: not used here
- memory: not used here
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: not uesed
memory: not used
|
[
"Universal",
"Transformer",
"which",
"uses",
"a",
"gru",
"as",
"transition",
"function",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L835-L924
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_with_lstm_as_transition_function
|
def universal_transformer_with_lstm_as_transition_function(
layer_inputs, step, hparams, ffn_unit, attention_unit, pad_remover=None):
"""Universal Transformer which uses a lstm as transition function.
It's kind of like having a lstm, filliped vertically next to the Universal
Transformer that controls the flow of the information in depth,
over different steps of the Universal Transformer.
Args:
layer_inputs:
- state: state
- inputs: the original embedded inputs (= inputs to the first step)
- memory: memory used in lstm.
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: the original embedded inputs (= inputs to the first step)
memory: contains information of state from all the previous steps.
"""
state, unused_inputs, memory = tf.unstack(
layer_inputs, num=None, axis=0, name="unstack")
# NOTE:
# state (ut_state): output of the lstm in the previous step
# inputs (ut_input): original input --> we don't use it here
# memory: lstm memory
# Multi_head_attention:
assert not hparams.add_step_timing_signal # Let lstm count for us!
mh_attention_input = step_preprocess(state, step, hparams)
transition_function_input = attention_unit(mh_attention_input)
# Transition Function:
if hparams.add_ffn_unit_to_the_transition_function:
transition_function_input = ffn_unit(transition_function_input)
transition_function_input = common_layers.layer_preprocess(
transition_function_input, hparams)
with tf.variable_scope("lstm"):
# lstm input gate: i_t = sigmoid(W_i.x_t + U_i.h_{t-1})
transition_function_input_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="input",
bias_initializer=tf.zeros_initializer(),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("lstm_input_gate",
tf.reduce_mean(transition_function_input_gate))
# lstm forget gate: f_t = sigmoid(W_f.x_t + U_f.h_{t-1})
transition_function_forget_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="forget",
bias_initializer=tf.zeros_initializer(),
activation=None,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
forget_bias_tensor = tf.constant(hparams.lstm_forget_bias)
transition_function_forget_gate = tf.sigmoid(
transition_function_forget_gate + forget_bias_tensor)
tf.contrib.summary.scalar("lstm_forget_gate",
tf.reduce_mean(transition_function_forget_gate))
# lstm output gate: o_t = sigmoid(W_o.x_t + U_o.h_{t-1})
transition_function_output_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="output",
bias_initializer=tf.zeros_initializer(),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("lstm_output_gate",
tf.reduce_mean(transition_function_output_gate))
# lstm input modulation
transition_function_input_modulation = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="input_modulation",
bias_initializer=tf.zeros_initializer(),
activation=tf.tanh,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
transition_function_memory = (
memory * transition_function_forget_gate +
transition_function_input_gate * transition_function_input_modulation)
transition_function_output = (
tf.tanh(transition_function_memory) * transition_function_output_gate)
transition_function_output = common_layers.layer_preprocess(
transition_function_output, hparams)
return transition_function_output, unused_inputs, transition_function_memory
|
python
|
def universal_transformer_with_lstm_as_transition_function(
layer_inputs, step, hparams, ffn_unit, attention_unit, pad_remover=None):
"""Universal Transformer which uses a lstm as transition function.
It's kind of like having a lstm, filliped vertically next to the Universal
Transformer that controls the flow of the information in depth,
over different steps of the Universal Transformer.
Args:
layer_inputs:
- state: state
- inputs: the original embedded inputs (= inputs to the first step)
- memory: memory used in lstm.
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: the original embedded inputs (= inputs to the first step)
memory: contains information of state from all the previous steps.
"""
state, unused_inputs, memory = tf.unstack(
layer_inputs, num=None, axis=0, name="unstack")
# NOTE:
# state (ut_state): output of the lstm in the previous step
# inputs (ut_input): original input --> we don't use it here
# memory: lstm memory
# Multi_head_attention:
assert not hparams.add_step_timing_signal # Let lstm count for us!
mh_attention_input = step_preprocess(state, step, hparams)
transition_function_input = attention_unit(mh_attention_input)
# Transition Function:
if hparams.add_ffn_unit_to_the_transition_function:
transition_function_input = ffn_unit(transition_function_input)
transition_function_input = common_layers.layer_preprocess(
transition_function_input, hparams)
with tf.variable_scope("lstm"):
# lstm input gate: i_t = sigmoid(W_i.x_t + U_i.h_{t-1})
transition_function_input_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="input",
bias_initializer=tf.zeros_initializer(),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("lstm_input_gate",
tf.reduce_mean(transition_function_input_gate))
# lstm forget gate: f_t = sigmoid(W_f.x_t + U_f.h_{t-1})
transition_function_forget_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="forget",
bias_initializer=tf.zeros_initializer(),
activation=None,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
forget_bias_tensor = tf.constant(hparams.lstm_forget_bias)
transition_function_forget_gate = tf.sigmoid(
transition_function_forget_gate + forget_bias_tensor)
tf.contrib.summary.scalar("lstm_forget_gate",
tf.reduce_mean(transition_function_forget_gate))
# lstm output gate: o_t = sigmoid(W_o.x_t + U_o.h_{t-1})
transition_function_output_gate = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="output",
bias_initializer=tf.zeros_initializer(),
activation=tf.sigmoid,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
tf.contrib.summary.scalar("lstm_output_gate",
tf.reduce_mean(transition_function_output_gate))
# lstm input modulation
transition_function_input_modulation = _ffn_layer_multi_inputs(
[transition_function_input, state],
hparams,
name="input_modulation",
bias_initializer=tf.zeros_initializer(),
activation=tf.tanh,
pad_remover=pad_remover,
preprocess=False,
postprocess=False)
transition_function_memory = (
memory * transition_function_forget_gate +
transition_function_input_gate * transition_function_input_modulation)
transition_function_output = (
tf.tanh(transition_function_memory) * transition_function_output_gate)
transition_function_output = common_layers.layer_preprocess(
transition_function_output, hparams)
return transition_function_output, unused_inputs, transition_function_memory
|
[
"def",
"universal_transformer_with_lstm_as_transition_function",
"(",
"layer_inputs",
",",
"step",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
",",
"pad_remover",
"=",
"None",
")",
":",
"state",
",",
"unused_inputs",
",",
"memory",
"=",
"tf",
".",
"unstack",
"(",
"layer_inputs",
",",
"num",
"=",
"None",
",",
"axis",
"=",
"0",
",",
"name",
"=",
"\"unstack\"",
")",
"# NOTE:",
"# state (ut_state): output of the lstm in the previous step",
"# inputs (ut_input): original input --> we don't use it here",
"# memory: lstm memory",
"# Multi_head_attention:",
"assert",
"not",
"hparams",
".",
"add_step_timing_signal",
"# Let lstm count for us!",
"mh_attention_input",
"=",
"step_preprocess",
"(",
"state",
",",
"step",
",",
"hparams",
")",
"transition_function_input",
"=",
"attention_unit",
"(",
"mh_attention_input",
")",
"# Transition Function:",
"if",
"hparams",
".",
"add_ffn_unit_to_the_transition_function",
":",
"transition_function_input",
"=",
"ffn_unit",
"(",
"transition_function_input",
")",
"transition_function_input",
"=",
"common_layers",
".",
"layer_preprocess",
"(",
"transition_function_input",
",",
"hparams",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"\"lstm\"",
")",
":",
"# lstm input gate: i_t = sigmoid(W_i.x_t + U_i.h_{t-1})",
"transition_function_input_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"input\"",
",",
"bias_initializer",
"=",
"tf",
".",
"zeros_initializer",
"(",
")",
",",
"activation",
"=",
"tf",
".",
"sigmoid",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"lstm_input_gate\"",
",",
"tf",
".",
"reduce_mean",
"(",
"transition_function_input_gate",
")",
")",
"# lstm forget gate: f_t = sigmoid(W_f.x_t + U_f.h_{t-1})",
"transition_function_forget_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"forget\"",
",",
"bias_initializer",
"=",
"tf",
".",
"zeros_initializer",
"(",
")",
",",
"activation",
"=",
"None",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"forget_bias_tensor",
"=",
"tf",
".",
"constant",
"(",
"hparams",
".",
"lstm_forget_bias",
")",
"transition_function_forget_gate",
"=",
"tf",
".",
"sigmoid",
"(",
"transition_function_forget_gate",
"+",
"forget_bias_tensor",
")",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"lstm_forget_gate\"",
",",
"tf",
".",
"reduce_mean",
"(",
"transition_function_forget_gate",
")",
")",
"# lstm output gate: o_t = sigmoid(W_o.x_t + U_o.h_{t-1})",
"transition_function_output_gate",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"output\"",
",",
"bias_initializer",
"=",
"tf",
".",
"zeros_initializer",
"(",
")",
",",
"activation",
"=",
"tf",
".",
"sigmoid",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"lstm_output_gate\"",
",",
"tf",
".",
"reduce_mean",
"(",
"transition_function_output_gate",
")",
")",
"# lstm input modulation",
"transition_function_input_modulation",
"=",
"_ffn_layer_multi_inputs",
"(",
"[",
"transition_function_input",
",",
"state",
"]",
",",
"hparams",
",",
"name",
"=",
"\"input_modulation\"",
",",
"bias_initializer",
"=",
"tf",
".",
"zeros_initializer",
"(",
")",
",",
"activation",
"=",
"tf",
".",
"tanh",
",",
"pad_remover",
"=",
"pad_remover",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
"transition_function_memory",
"=",
"(",
"memory",
"*",
"transition_function_forget_gate",
"+",
"transition_function_input_gate",
"*",
"transition_function_input_modulation",
")",
"transition_function_output",
"=",
"(",
"tf",
".",
"tanh",
"(",
"transition_function_memory",
")",
"*",
"transition_function_output_gate",
")",
"transition_function_output",
"=",
"common_layers",
".",
"layer_preprocess",
"(",
"transition_function_output",
",",
"hparams",
")",
"return",
"transition_function_output",
",",
"unused_inputs",
",",
"transition_function_memory"
] |
Universal Transformer which uses a lstm as transition function.
It's kind of like having a lstm, filliped vertically next to the Universal
Transformer that controls the flow of the information in depth,
over different steps of the Universal Transformer.
Args:
layer_inputs:
- state: state
- inputs: the original embedded inputs (= inputs to the first step)
- memory: memory used in lstm.
step: indicates number of steps taken so far
hparams: model hyper-parameters.
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
pad_remover: to mask out padding in convolutional layers (efficiency).
Returns:
layer_output:
new_state: new state
inputs: the original embedded inputs (= inputs to the first step)
memory: contains information of state from all the previous steps.
|
[
"Universal",
"Transformer",
"which",
"uses",
"a",
"lstm",
"as",
"transition",
"function",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L927-L1037
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
universal_transformer_act
|
def universal_transformer_act(x, hparams, ffn_unit, attention_unit):
"""ACT based models.
Implementations of all act models are based on craffel@'s cl/160711592.
(1) Basic AUT based on remainder-distribution ACT (position-wise).
(2) AUT with global halting probability (not position-wise).
(3) AUT with random halting probability (not position-wise).
(4) AUT with final state as accumulation of all states.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
the output tensor, (ponder_times, remainders)
Raises:
ValueError: Unknown act type
"""
if hparams.act_type not in ["basic", "global", "random", "accumulated"]:
raise ValueError("Unknown act type: %s" % hparams.act_type)
state = x
act_max_steps = hparams.act_max_steps
threshold = 1.0 - hparams.act_epsilon
state_shape_static = state.get_shape()
state_slice = slice(0, 2)
if hparams.act_type == "global":
state_slice = slice(0, 1)
# Dynamic shape for update tensors below
update_shape = tf.shape(state)[state_slice]
# Halting probabilities (p_t^n in the paper)
halting_probability = tf.zeros(update_shape, name="halting_probability")
# Remainders (R(t) in the paper)
remainders = tf.zeros(update_shape, name="remainder")
# Number of updates performed (N(t) in the paper)
n_updates = tf.zeros(update_shape, name="n_updates")
# Previous cell states (s_t in the paper)
previous_state = tf.zeros_like(state, name="previous_state")
step = tf.constant(0, dtype=tf.int32)
def ut_function(state, step, halting_probability, remainders, n_updates,
previous_state):
"""implements act (position-wise halting).
Args:
state: 3-D Tensor: [batch_size, length, channel]
step: indicates number of steps taken so far
halting_probability: halting probability
remainders: act remainders
n_updates: act n_updates
previous_state: previous state
Returns:
transformed_state: transformed state
step: step+1
halting_probability: halting probability
remainders: act remainders
n_updates: act n_updates
new_state: new state
"""
state = step_preprocess(state, step, hparams)
if hparams.act_type == "random":
# random as halting probability
p = tf.random_uniform(
shape=common_layers.shape_list(halting_probability))
else:
with tf.variable_scope("sigmoid_activation_for_pondering"):
p = common_layers.dense(
state,
1,
activation=tf.nn.sigmoid,
use_bias=True,
bias_initializer=tf.constant_initializer(
hparams.act_halting_bias_init))
if hparams.act_type == "global":
# average over all positions (as a global halting prob)
p = tf.reduce_mean(p, axis=1)
p = tf.squeeze(p)
else:
# maintain position-wise probabilities
p = tf.squeeze(p, axis=-1)
# Mask for inputs which have not halted yet
still_running = tf.cast(tf.less(halting_probability, 1.0), tf.float32)
# Mask of inputs which halted at this step
new_halted = tf.cast(
tf.greater(halting_probability + p * still_running, threshold),
tf.float32) * still_running
# Mask of inputs which haven't halted, and didn't halt this step
still_running = tf.cast(
tf.less_equal(halting_probability + p * still_running, threshold),
tf.float32) * still_running
# Add the halting probability for this step to the halting
# probabilities for those input which haven't halted yet
halting_probability += p * still_running
# Compute remainders for the inputs which halted at this step
remainders += new_halted * (1 - halting_probability)
# Add the remainders to those inputs which halted at this step
halting_probability += new_halted * remainders
# Increment n_updates for all inputs which are still running
n_updates += still_running + new_halted
# Compute the weight to be applied to the new state and output
# 0 when the input has already halted
# p when the input hasn't halted yet
# the remainders when it halted this step
update_weights = tf.expand_dims(
p * still_running + new_halted * remainders, -1)
if hparams.act_type == "global":
update_weights = tf.expand_dims(update_weights, -1)
# apply transformation on the state
transformed_state = state
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
transformed_state = ffn_unit(attention_unit(transformed_state))
# update running part in the weighted state and keep the rest
new_state = ((transformed_state * update_weights) +
(previous_state * (1 - update_weights)))
if hparams.act_type == "accumulated":
# Add in the weighted state
new_state = (transformed_state * update_weights) + previous_state
# remind TensorFlow of everything's shape
transformed_state.set_shape(state_shape_static)
for x in [halting_probability, remainders, n_updates]:
x.set_shape(state_shape_static[state_slice])
new_state.set_shape(state_shape_static)
step += 1
return (transformed_state, step, halting_probability, remainders, n_updates,
new_state)
# While loop stops when this predicate is FALSE.
# Ie all (probability < 1-eps AND counter < N) are false.
def should_continue(u0, u1, halting_probability, u2, n_updates, u3):
del u0, u1, u2, u3
return tf.reduce_any(
tf.logical_and(
tf.less(halting_probability, threshold),
tf.less(n_updates, act_max_steps)))
# Do while loop iterations until predicate above is false.
(_, _, _, remainder, n_updates, new_state) = tf.while_loop(
should_continue, ut_function,
(state, step, halting_probability, remainders, n_updates, previous_state),
maximum_iterations=act_max_steps + 1)
ponder_times = n_updates
remainders = remainder
tf.contrib.summary.scalar("ponder_times", tf.reduce_mean(ponder_times))
return new_state, (ponder_times, remainders)
|
python
|
def universal_transformer_act(x, hparams, ffn_unit, attention_unit):
"""ACT based models.
Implementations of all act models are based on craffel@'s cl/160711592.
(1) Basic AUT based on remainder-distribution ACT (position-wise).
(2) AUT with global halting probability (not position-wise).
(3) AUT with random halting probability (not position-wise).
(4) AUT with final state as accumulation of all states.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
the output tensor, (ponder_times, remainders)
Raises:
ValueError: Unknown act type
"""
if hparams.act_type not in ["basic", "global", "random", "accumulated"]:
raise ValueError("Unknown act type: %s" % hparams.act_type)
state = x
act_max_steps = hparams.act_max_steps
threshold = 1.0 - hparams.act_epsilon
state_shape_static = state.get_shape()
state_slice = slice(0, 2)
if hparams.act_type == "global":
state_slice = slice(0, 1)
# Dynamic shape for update tensors below
update_shape = tf.shape(state)[state_slice]
# Halting probabilities (p_t^n in the paper)
halting_probability = tf.zeros(update_shape, name="halting_probability")
# Remainders (R(t) in the paper)
remainders = tf.zeros(update_shape, name="remainder")
# Number of updates performed (N(t) in the paper)
n_updates = tf.zeros(update_shape, name="n_updates")
# Previous cell states (s_t in the paper)
previous_state = tf.zeros_like(state, name="previous_state")
step = tf.constant(0, dtype=tf.int32)
def ut_function(state, step, halting_probability, remainders, n_updates,
previous_state):
"""implements act (position-wise halting).
Args:
state: 3-D Tensor: [batch_size, length, channel]
step: indicates number of steps taken so far
halting_probability: halting probability
remainders: act remainders
n_updates: act n_updates
previous_state: previous state
Returns:
transformed_state: transformed state
step: step+1
halting_probability: halting probability
remainders: act remainders
n_updates: act n_updates
new_state: new state
"""
state = step_preprocess(state, step, hparams)
if hparams.act_type == "random":
# random as halting probability
p = tf.random_uniform(
shape=common_layers.shape_list(halting_probability))
else:
with tf.variable_scope("sigmoid_activation_for_pondering"):
p = common_layers.dense(
state,
1,
activation=tf.nn.sigmoid,
use_bias=True,
bias_initializer=tf.constant_initializer(
hparams.act_halting_bias_init))
if hparams.act_type == "global":
# average over all positions (as a global halting prob)
p = tf.reduce_mean(p, axis=1)
p = tf.squeeze(p)
else:
# maintain position-wise probabilities
p = tf.squeeze(p, axis=-1)
# Mask for inputs which have not halted yet
still_running = tf.cast(tf.less(halting_probability, 1.0), tf.float32)
# Mask of inputs which halted at this step
new_halted = tf.cast(
tf.greater(halting_probability + p * still_running, threshold),
tf.float32) * still_running
# Mask of inputs which haven't halted, and didn't halt this step
still_running = tf.cast(
tf.less_equal(halting_probability + p * still_running, threshold),
tf.float32) * still_running
# Add the halting probability for this step to the halting
# probabilities for those input which haven't halted yet
halting_probability += p * still_running
# Compute remainders for the inputs which halted at this step
remainders += new_halted * (1 - halting_probability)
# Add the remainders to those inputs which halted at this step
halting_probability += new_halted * remainders
# Increment n_updates for all inputs which are still running
n_updates += still_running + new_halted
# Compute the weight to be applied to the new state and output
# 0 when the input has already halted
# p when the input hasn't halted yet
# the remainders when it halted this step
update_weights = tf.expand_dims(
p * still_running + new_halted * remainders, -1)
if hparams.act_type == "global":
update_weights = tf.expand_dims(update_weights, -1)
# apply transformation on the state
transformed_state = state
for i in range(hparams.num_inrecurrence_layers):
with tf.variable_scope("rec_layer_%d" % i):
transformed_state = ffn_unit(attention_unit(transformed_state))
# update running part in the weighted state and keep the rest
new_state = ((transformed_state * update_weights) +
(previous_state * (1 - update_weights)))
if hparams.act_type == "accumulated":
# Add in the weighted state
new_state = (transformed_state * update_weights) + previous_state
# remind TensorFlow of everything's shape
transformed_state.set_shape(state_shape_static)
for x in [halting_probability, remainders, n_updates]:
x.set_shape(state_shape_static[state_slice])
new_state.set_shape(state_shape_static)
step += 1
return (transformed_state, step, halting_probability, remainders, n_updates,
new_state)
# While loop stops when this predicate is FALSE.
# Ie all (probability < 1-eps AND counter < N) are false.
def should_continue(u0, u1, halting_probability, u2, n_updates, u3):
del u0, u1, u2, u3
return tf.reduce_any(
tf.logical_and(
tf.less(halting_probability, threshold),
tf.less(n_updates, act_max_steps)))
# Do while loop iterations until predicate above is false.
(_, _, _, remainder, n_updates, new_state) = tf.while_loop(
should_continue, ut_function,
(state, step, halting_probability, remainders, n_updates, previous_state),
maximum_iterations=act_max_steps + 1)
ponder_times = n_updates
remainders = remainder
tf.contrib.summary.scalar("ponder_times", tf.reduce_mean(ponder_times))
return new_state, (ponder_times, remainders)
|
[
"def",
"universal_transformer_act",
"(",
"x",
",",
"hparams",
",",
"ffn_unit",
",",
"attention_unit",
")",
":",
"if",
"hparams",
".",
"act_type",
"not",
"in",
"[",
"\"basic\"",
",",
"\"global\"",
",",
"\"random\"",
",",
"\"accumulated\"",
"]",
":",
"raise",
"ValueError",
"(",
"\"Unknown act type: %s\"",
"%",
"hparams",
".",
"act_type",
")",
"state",
"=",
"x",
"act_max_steps",
"=",
"hparams",
".",
"act_max_steps",
"threshold",
"=",
"1.0",
"-",
"hparams",
".",
"act_epsilon",
"state_shape_static",
"=",
"state",
".",
"get_shape",
"(",
")",
"state_slice",
"=",
"slice",
"(",
"0",
",",
"2",
")",
"if",
"hparams",
".",
"act_type",
"==",
"\"global\"",
":",
"state_slice",
"=",
"slice",
"(",
"0",
",",
"1",
")",
"# Dynamic shape for update tensors below",
"update_shape",
"=",
"tf",
".",
"shape",
"(",
"state",
")",
"[",
"state_slice",
"]",
"# Halting probabilities (p_t^n in the paper)",
"halting_probability",
"=",
"tf",
".",
"zeros",
"(",
"update_shape",
",",
"name",
"=",
"\"halting_probability\"",
")",
"# Remainders (R(t) in the paper)",
"remainders",
"=",
"tf",
".",
"zeros",
"(",
"update_shape",
",",
"name",
"=",
"\"remainder\"",
")",
"# Number of updates performed (N(t) in the paper)",
"n_updates",
"=",
"tf",
".",
"zeros",
"(",
"update_shape",
",",
"name",
"=",
"\"n_updates\"",
")",
"# Previous cell states (s_t in the paper)",
"previous_state",
"=",
"tf",
".",
"zeros_like",
"(",
"state",
",",
"name",
"=",
"\"previous_state\"",
")",
"step",
"=",
"tf",
".",
"constant",
"(",
"0",
",",
"dtype",
"=",
"tf",
".",
"int32",
")",
"def",
"ut_function",
"(",
"state",
",",
"step",
",",
"halting_probability",
",",
"remainders",
",",
"n_updates",
",",
"previous_state",
")",
":",
"\"\"\"implements act (position-wise halting).\n\n Args:\n state: 3-D Tensor: [batch_size, length, channel]\n step: indicates number of steps taken so far\n halting_probability: halting probability\n remainders: act remainders\n n_updates: act n_updates\n previous_state: previous state\n\n Returns:\n transformed_state: transformed state\n step: step+1\n halting_probability: halting probability\n remainders: act remainders\n n_updates: act n_updates\n new_state: new state\n \"\"\"",
"state",
"=",
"step_preprocess",
"(",
"state",
",",
"step",
",",
"hparams",
")",
"if",
"hparams",
".",
"act_type",
"==",
"\"random\"",
":",
"# random as halting probability",
"p",
"=",
"tf",
".",
"random_uniform",
"(",
"shape",
"=",
"common_layers",
".",
"shape_list",
"(",
"halting_probability",
")",
")",
"else",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"sigmoid_activation_for_pondering\"",
")",
":",
"p",
"=",
"common_layers",
".",
"dense",
"(",
"state",
",",
"1",
",",
"activation",
"=",
"tf",
".",
"nn",
".",
"sigmoid",
",",
"use_bias",
"=",
"True",
",",
"bias_initializer",
"=",
"tf",
".",
"constant_initializer",
"(",
"hparams",
".",
"act_halting_bias_init",
")",
")",
"if",
"hparams",
".",
"act_type",
"==",
"\"global\"",
":",
"# average over all positions (as a global halting prob)",
"p",
"=",
"tf",
".",
"reduce_mean",
"(",
"p",
",",
"axis",
"=",
"1",
")",
"p",
"=",
"tf",
".",
"squeeze",
"(",
"p",
")",
"else",
":",
"# maintain position-wise probabilities",
"p",
"=",
"tf",
".",
"squeeze",
"(",
"p",
",",
"axis",
"=",
"-",
"1",
")",
"# Mask for inputs which have not halted yet",
"still_running",
"=",
"tf",
".",
"cast",
"(",
"tf",
".",
"less",
"(",
"halting_probability",
",",
"1.0",
")",
",",
"tf",
".",
"float32",
")",
"# Mask of inputs which halted at this step",
"new_halted",
"=",
"tf",
".",
"cast",
"(",
"tf",
".",
"greater",
"(",
"halting_probability",
"+",
"p",
"*",
"still_running",
",",
"threshold",
")",
",",
"tf",
".",
"float32",
")",
"*",
"still_running",
"# Mask of inputs which haven't halted, and didn't halt this step",
"still_running",
"=",
"tf",
".",
"cast",
"(",
"tf",
".",
"less_equal",
"(",
"halting_probability",
"+",
"p",
"*",
"still_running",
",",
"threshold",
")",
",",
"tf",
".",
"float32",
")",
"*",
"still_running",
"# Add the halting probability for this step to the halting",
"# probabilities for those input which haven't halted yet",
"halting_probability",
"+=",
"p",
"*",
"still_running",
"# Compute remainders for the inputs which halted at this step",
"remainders",
"+=",
"new_halted",
"*",
"(",
"1",
"-",
"halting_probability",
")",
"# Add the remainders to those inputs which halted at this step",
"halting_probability",
"+=",
"new_halted",
"*",
"remainders",
"# Increment n_updates for all inputs which are still running",
"n_updates",
"+=",
"still_running",
"+",
"new_halted",
"# Compute the weight to be applied to the new state and output",
"# 0 when the input has already halted",
"# p when the input hasn't halted yet",
"# the remainders when it halted this step",
"update_weights",
"=",
"tf",
".",
"expand_dims",
"(",
"p",
"*",
"still_running",
"+",
"new_halted",
"*",
"remainders",
",",
"-",
"1",
")",
"if",
"hparams",
".",
"act_type",
"==",
"\"global\"",
":",
"update_weights",
"=",
"tf",
".",
"expand_dims",
"(",
"update_weights",
",",
"-",
"1",
")",
"# apply transformation on the state",
"transformed_state",
"=",
"state",
"for",
"i",
"in",
"range",
"(",
"hparams",
".",
"num_inrecurrence_layers",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"\"rec_layer_%d\"",
"%",
"i",
")",
":",
"transformed_state",
"=",
"ffn_unit",
"(",
"attention_unit",
"(",
"transformed_state",
")",
")",
"# update running part in the weighted state and keep the rest",
"new_state",
"=",
"(",
"(",
"transformed_state",
"*",
"update_weights",
")",
"+",
"(",
"previous_state",
"*",
"(",
"1",
"-",
"update_weights",
")",
")",
")",
"if",
"hparams",
".",
"act_type",
"==",
"\"accumulated\"",
":",
"# Add in the weighted state",
"new_state",
"=",
"(",
"transformed_state",
"*",
"update_weights",
")",
"+",
"previous_state",
"# remind TensorFlow of everything's shape",
"transformed_state",
".",
"set_shape",
"(",
"state_shape_static",
")",
"for",
"x",
"in",
"[",
"halting_probability",
",",
"remainders",
",",
"n_updates",
"]",
":",
"x",
".",
"set_shape",
"(",
"state_shape_static",
"[",
"state_slice",
"]",
")",
"new_state",
".",
"set_shape",
"(",
"state_shape_static",
")",
"step",
"+=",
"1",
"return",
"(",
"transformed_state",
",",
"step",
",",
"halting_probability",
",",
"remainders",
",",
"n_updates",
",",
"new_state",
")",
"# While loop stops when this predicate is FALSE.",
"# Ie all (probability < 1-eps AND counter < N) are false.",
"def",
"should_continue",
"(",
"u0",
",",
"u1",
",",
"halting_probability",
",",
"u2",
",",
"n_updates",
",",
"u3",
")",
":",
"del",
"u0",
",",
"u1",
",",
"u2",
",",
"u3",
"return",
"tf",
".",
"reduce_any",
"(",
"tf",
".",
"logical_and",
"(",
"tf",
".",
"less",
"(",
"halting_probability",
",",
"threshold",
")",
",",
"tf",
".",
"less",
"(",
"n_updates",
",",
"act_max_steps",
")",
")",
")",
"# Do while loop iterations until predicate above is false.",
"(",
"_",
",",
"_",
",",
"_",
",",
"remainder",
",",
"n_updates",
",",
"new_state",
")",
"=",
"tf",
".",
"while_loop",
"(",
"should_continue",
",",
"ut_function",
",",
"(",
"state",
",",
"step",
",",
"halting_probability",
",",
"remainders",
",",
"n_updates",
",",
"previous_state",
")",
",",
"maximum_iterations",
"=",
"act_max_steps",
"+",
"1",
")",
"ponder_times",
"=",
"n_updates",
"remainders",
"=",
"remainder",
"tf",
".",
"contrib",
".",
"summary",
".",
"scalar",
"(",
"\"ponder_times\"",
",",
"tf",
".",
"reduce_mean",
"(",
"ponder_times",
")",
")",
"return",
"new_state",
",",
"(",
"ponder_times",
",",
"remainders",
")"
] |
ACT based models.
Implementations of all act models are based on craffel@'s cl/160711592.
(1) Basic AUT based on remainder-distribution ACT (position-wise).
(2) AUT with global halting probability (not position-wise).
(3) AUT with random halting probability (not position-wise).
(4) AUT with final state as accumulation of all states.
Args:
x: input
hparams: model hyper-parameters
ffn_unit: feed-forward unit
attention_unit: multi-head attention unit
Returns:
the output tensor, (ponder_times, remainders)
Raises:
ValueError: Unknown act type
|
[
"ACT",
"based",
"models",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1040-L1212
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
_ffn_layer_multi_inputs
|
def _ffn_layer_multi_inputs(inputs_list,
hparams,
ffn_layer_type="dense",
name="ffn",
kernel_initializer=None,
bias_initializer=None,
activation=None,
pad_remover=None,
preprocess=False,
postprocess=False):
"""Implements a Feed-forward layer with multiple inputs, pad-removing, etc.
Args:
inputs_list: list of input tensors
hparams: hyper-parameters
ffn_layer_type: dense / dense_dropconnect/ dense_relu_dense
name: name
kernel_initializer: kernel initializer
bias_initializer: bias initializer
activation: activation function
pad_remover: pad remover
preprocess: if preprocess the input
postprocess: if postprocess the output
Returns:
a tensor
Raises:
ValueError: Unknown ffn_layer type.
"""
# need at least one inputs
num_inputs = len(inputs_list)
assert num_inputs > 0
if preprocess and num_inputs == 1:
inputs_list[0] = common_layers.layer_preprocess(inputs_list[0], hparams)
if postprocess:
original_inputs = inputs_list[0]
# the output size is the hidden size of the main inputs
main_input = inputs_list[0]
original_shape = common_layers.shape_list(main_input)
assert hparams.hidden_size == common_layers.shape_list(main_input)[-1]
# all the inputs are in the same shape with main inputs
for inputs in inputs_list:
main_input.get_shape().assert_is_compatible_with(inputs.get_shape())
def remove_pads(x):
original_shape = common_layers.shape_list(x)
# Collapse `x` across examples, and remove padding positions.
x = tf.reshape(x, tf.concat([[-1], original_shape[2:]], axis=0))
x = tf.expand_dims(pad_remover.remove(x), axis=0)
return x
if pad_remover:
for i, inputs in enumerate(inputs_list):
inputs_list[i] = remove_pads(inputs)
ffn_inputs = inputs_list[0]
if len(inputs_list) != 1:
ffn_inputs = tf.concat(inputs_list, axis=-1)
if ffn_layer_type == "dense":
output = common_layers.dense(
ffn_inputs,
hparams.hidden_size,
name=name,
activation=activation,
use_bias=True,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer)
elif ffn_layer_type == "dense_dropconnect":
output = common_layers.dense_dropconnect(
ffn_inputs,
hparams.hidden_size,
name=name,
dropconnect_dropout=hparams.dropconnect_dropout,
output_activation=activation)
postprocess = False # no dropout on the output unit
elif ffn_layer_type == "dense_relu_dense":
output = common_layers.dense_relu_dense(
ffn_inputs,
hparams.filter_size,
hparams.hidden_size,
name=name,
dropout=hparams.relu_dropout,
output_activation=activation,
)
else:
raise ValueError("Unknown ffn_layer type: %s" % ffn_layer_type)
if pad_remover:
# Restore `output` to the original shape of `x`, including padding.
output = tf.reshape(
pad_remover.restore(tf.squeeze(output, axis=0)), original_shape)
if postprocess:
if num_inputs == 1:
output = common_layers.layer_postprocess(original_inputs, output, hparams)
else: # only dropout (no residual)x
hp = copy.copy(hparams)
hp.layer_postprocess_sequence = hp.layer_postprocess_sequence.replace(
"a", "")
output = common_layers.layer_postprocess(original_inputs, output, hp)
return output
|
python
|
def _ffn_layer_multi_inputs(inputs_list,
hparams,
ffn_layer_type="dense",
name="ffn",
kernel_initializer=None,
bias_initializer=None,
activation=None,
pad_remover=None,
preprocess=False,
postprocess=False):
"""Implements a Feed-forward layer with multiple inputs, pad-removing, etc.
Args:
inputs_list: list of input tensors
hparams: hyper-parameters
ffn_layer_type: dense / dense_dropconnect/ dense_relu_dense
name: name
kernel_initializer: kernel initializer
bias_initializer: bias initializer
activation: activation function
pad_remover: pad remover
preprocess: if preprocess the input
postprocess: if postprocess the output
Returns:
a tensor
Raises:
ValueError: Unknown ffn_layer type.
"""
# need at least one inputs
num_inputs = len(inputs_list)
assert num_inputs > 0
if preprocess and num_inputs == 1:
inputs_list[0] = common_layers.layer_preprocess(inputs_list[0], hparams)
if postprocess:
original_inputs = inputs_list[0]
# the output size is the hidden size of the main inputs
main_input = inputs_list[0]
original_shape = common_layers.shape_list(main_input)
assert hparams.hidden_size == common_layers.shape_list(main_input)[-1]
# all the inputs are in the same shape with main inputs
for inputs in inputs_list:
main_input.get_shape().assert_is_compatible_with(inputs.get_shape())
def remove_pads(x):
original_shape = common_layers.shape_list(x)
# Collapse `x` across examples, and remove padding positions.
x = tf.reshape(x, tf.concat([[-1], original_shape[2:]], axis=0))
x = tf.expand_dims(pad_remover.remove(x), axis=0)
return x
if pad_remover:
for i, inputs in enumerate(inputs_list):
inputs_list[i] = remove_pads(inputs)
ffn_inputs = inputs_list[0]
if len(inputs_list) != 1:
ffn_inputs = tf.concat(inputs_list, axis=-1)
if ffn_layer_type == "dense":
output = common_layers.dense(
ffn_inputs,
hparams.hidden_size,
name=name,
activation=activation,
use_bias=True,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer)
elif ffn_layer_type == "dense_dropconnect":
output = common_layers.dense_dropconnect(
ffn_inputs,
hparams.hidden_size,
name=name,
dropconnect_dropout=hparams.dropconnect_dropout,
output_activation=activation)
postprocess = False # no dropout on the output unit
elif ffn_layer_type == "dense_relu_dense":
output = common_layers.dense_relu_dense(
ffn_inputs,
hparams.filter_size,
hparams.hidden_size,
name=name,
dropout=hparams.relu_dropout,
output_activation=activation,
)
else:
raise ValueError("Unknown ffn_layer type: %s" % ffn_layer_type)
if pad_remover:
# Restore `output` to the original shape of `x`, including padding.
output = tf.reshape(
pad_remover.restore(tf.squeeze(output, axis=0)), original_shape)
if postprocess:
if num_inputs == 1:
output = common_layers.layer_postprocess(original_inputs, output, hparams)
else: # only dropout (no residual)x
hp = copy.copy(hparams)
hp.layer_postprocess_sequence = hp.layer_postprocess_sequence.replace(
"a", "")
output = common_layers.layer_postprocess(original_inputs, output, hp)
return output
|
[
"def",
"_ffn_layer_multi_inputs",
"(",
"inputs_list",
",",
"hparams",
",",
"ffn_layer_type",
"=",
"\"dense\"",
",",
"name",
"=",
"\"ffn\"",
",",
"kernel_initializer",
"=",
"None",
",",
"bias_initializer",
"=",
"None",
",",
"activation",
"=",
"None",
",",
"pad_remover",
"=",
"None",
",",
"preprocess",
"=",
"False",
",",
"postprocess",
"=",
"False",
")",
":",
"# need at least one inputs",
"num_inputs",
"=",
"len",
"(",
"inputs_list",
")",
"assert",
"num_inputs",
">",
"0",
"if",
"preprocess",
"and",
"num_inputs",
"==",
"1",
":",
"inputs_list",
"[",
"0",
"]",
"=",
"common_layers",
".",
"layer_preprocess",
"(",
"inputs_list",
"[",
"0",
"]",
",",
"hparams",
")",
"if",
"postprocess",
":",
"original_inputs",
"=",
"inputs_list",
"[",
"0",
"]",
"# the output size is the hidden size of the main inputs",
"main_input",
"=",
"inputs_list",
"[",
"0",
"]",
"original_shape",
"=",
"common_layers",
".",
"shape_list",
"(",
"main_input",
")",
"assert",
"hparams",
".",
"hidden_size",
"==",
"common_layers",
".",
"shape_list",
"(",
"main_input",
")",
"[",
"-",
"1",
"]",
"# all the inputs are in the same shape with main inputs",
"for",
"inputs",
"in",
"inputs_list",
":",
"main_input",
".",
"get_shape",
"(",
")",
".",
"assert_is_compatible_with",
"(",
"inputs",
".",
"get_shape",
"(",
")",
")",
"def",
"remove_pads",
"(",
"x",
")",
":",
"original_shape",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"# Collapse `x` across examples, and remove padding positions.",
"x",
"=",
"tf",
".",
"reshape",
"(",
"x",
",",
"tf",
".",
"concat",
"(",
"[",
"[",
"-",
"1",
"]",
",",
"original_shape",
"[",
"2",
":",
"]",
"]",
",",
"axis",
"=",
"0",
")",
")",
"x",
"=",
"tf",
".",
"expand_dims",
"(",
"pad_remover",
".",
"remove",
"(",
"x",
")",
",",
"axis",
"=",
"0",
")",
"return",
"x",
"if",
"pad_remover",
":",
"for",
"i",
",",
"inputs",
"in",
"enumerate",
"(",
"inputs_list",
")",
":",
"inputs_list",
"[",
"i",
"]",
"=",
"remove_pads",
"(",
"inputs",
")",
"ffn_inputs",
"=",
"inputs_list",
"[",
"0",
"]",
"if",
"len",
"(",
"inputs_list",
")",
"!=",
"1",
":",
"ffn_inputs",
"=",
"tf",
".",
"concat",
"(",
"inputs_list",
",",
"axis",
"=",
"-",
"1",
")",
"if",
"ffn_layer_type",
"==",
"\"dense\"",
":",
"output",
"=",
"common_layers",
".",
"dense",
"(",
"ffn_inputs",
",",
"hparams",
".",
"hidden_size",
",",
"name",
"=",
"name",
",",
"activation",
"=",
"activation",
",",
"use_bias",
"=",
"True",
",",
"kernel_initializer",
"=",
"kernel_initializer",
",",
"bias_initializer",
"=",
"bias_initializer",
")",
"elif",
"ffn_layer_type",
"==",
"\"dense_dropconnect\"",
":",
"output",
"=",
"common_layers",
".",
"dense_dropconnect",
"(",
"ffn_inputs",
",",
"hparams",
".",
"hidden_size",
",",
"name",
"=",
"name",
",",
"dropconnect_dropout",
"=",
"hparams",
".",
"dropconnect_dropout",
",",
"output_activation",
"=",
"activation",
")",
"postprocess",
"=",
"False",
"# no dropout on the output unit",
"elif",
"ffn_layer_type",
"==",
"\"dense_relu_dense\"",
":",
"output",
"=",
"common_layers",
".",
"dense_relu_dense",
"(",
"ffn_inputs",
",",
"hparams",
".",
"filter_size",
",",
"hparams",
".",
"hidden_size",
",",
"name",
"=",
"name",
",",
"dropout",
"=",
"hparams",
".",
"relu_dropout",
",",
"output_activation",
"=",
"activation",
",",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"Unknown ffn_layer type: %s\"",
"%",
"ffn_layer_type",
")",
"if",
"pad_remover",
":",
"# Restore `output` to the original shape of `x`, including padding.",
"output",
"=",
"tf",
".",
"reshape",
"(",
"pad_remover",
".",
"restore",
"(",
"tf",
".",
"squeeze",
"(",
"output",
",",
"axis",
"=",
"0",
")",
")",
",",
"original_shape",
")",
"if",
"postprocess",
":",
"if",
"num_inputs",
"==",
"1",
":",
"output",
"=",
"common_layers",
".",
"layer_postprocess",
"(",
"original_inputs",
",",
"output",
",",
"hparams",
")",
"else",
":",
"# only dropout (no residual)x",
"hp",
"=",
"copy",
".",
"copy",
"(",
"hparams",
")",
"hp",
".",
"layer_postprocess_sequence",
"=",
"hp",
".",
"layer_postprocess_sequence",
".",
"replace",
"(",
"\"a\"",
",",
"\"\"",
")",
"output",
"=",
"common_layers",
".",
"layer_postprocess",
"(",
"original_inputs",
",",
"output",
",",
"hp",
")",
"return",
"output"
] |
Implements a Feed-forward layer with multiple inputs, pad-removing, etc.
Args:
inputs_list: list of input tensors
hparams: hyper-parameters
ffn_layer_type: dense / dense_dropconnect/ dense_relu_dense
name: name
kernel_initializer: kernel initializer
bias_initializer: bias initializer
activation: activation function
pad_remover: pad remover
preprocess: if preprocess the input
postprocess: if postprocess the output
Returns:
a tensor
Raises:
ValueError: Unknown ffn_layer type.
|
[
"Implements",
"a",
"Feed",
"-",
"forward",
"layer",
"with",
"multiple",
"inputs",
"pad",
"-",
"removing",
"etc",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1215-L1326
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
fill_memory_slot
|
def fill_memory_slot(memory, value, index):
"""Fills the memory slot at a particular index with the given value.
Args:
memory: a 4-d tensor [memory_size, batch, length, channel] containing
the state of all steps
value: a 3-d tensor [batch, length, channel] as the sate
index: integer in [0, memory_size)
Returns:
filled memory
"""
mask = tf.to_float(
tf.one_hot(index,
tf.shape(memory)[0])[:, None, None, None])
fill_memory = (1 - mask) * memory + mask * value[None, ...]
return fill_memory
|
python
|
def fill_memory_slot(memory, value, index):
"""Fills the memory slot at a particular index with the given value.
Args:
memory: a 4-d tensor [memory_size, batch, length, channel] containing
the state of all steps
value: a 3-d tensor [batch, length, channel] as the sate
index: integer in [0, memory_size)
Returns:
filled memory
"""
mask = tf.to_float(
tf.one_hot(index,
tf.shape(memory)[0])[:, None, None, None])
fill_memory = (1 - mask) * memory + mask * value[None, ...]
return fill_memory
|
[
"def",
"fill_memory_slot",
"(",
"memory",
",",
"value",
",",
"index",
")",
":",
"mask",
"=",
"tf",
".",
"to_float",
"(",
"tf",
".",
"one_hot",
"(",
"index",
",",
"tf",
".",
"shape",
"(",
"memory",
")",
"[",
"0",
"]",
")",
"[",
":",
",",
"None",
",",
"None",
",",
"None",
"]",
")",
"fill_memory",
"=",
"(",
"1",
"-",
"mask",
")",
"*",
"memory",
"+",
"mask",
"*",
"value",
"[",
"None",
",",
"...",
"]",
"return",
"fill_memory"
] |
Fills the memory slot at a particular index with the given value.
Args:
memory: a 4-d tensor [memory_size, batch, length, channel] containing
the state of all steps
value: a 3-d tensor [batch, length, channel] as the sate
index: integer in [0, memory_size)
Returns:
filled memory
|
[
"Fills",
"the",
"memory",
"slot",
"at",
"a",
"particular",
"index",
"with",
"the",
"given",
"value",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1329-L1346
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
add_depth_embedding
|
def add_depth_embedding(x):
"""Add n-dimensional embedding as the depth embedding (timing signal).
Adds embeddings to represent the position of the step in the recurrent
tower.
Args:
x: a tensor with shape [max_step, batch, length, depth]
Returns:
a Tensor the same shape as x.
"""
x_shape = common_layers.shape_list(x)
depth = x_shape[-1]
num_steps = x_shape[0]
shape = [num_steps, 1, 1, depth]
depth_embedding = (
tf.get_variable(
"depth_embedding",
shape,
initializer=tf.random_normal_initializer(0, depth**-0.5)) * (depth**
0.5))
x += depth_embedding
return x
|
python
|
def add_depth_embedding(x):
"""Add n-dimensional embedding as the depth embedding (timing signal).
Adds embeddings to represent the position of the step in the recurrent
tower.
Args:
x: a tensor with shape [max_step, batch, length, depth]
Returns:
a Tensor the same shape as x.
"""
x_shape = common_layers.shape_list(x)
depth = x_shape[-1]
num_steps = x_shape[0]
shape = [num_steps, 1, 1, depth]
depth_embedding = (
tf.get_variable(
"depth_embedding",
shape,
initializer=tf.random_normal_initializer(0, depth**-0.5)) * (depth**
0.5))
x += depth_embedding
return x
|
[
"def",
"add_depth_embedding",
"(",
"x",
")",
":",
"x_shape",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"depth",
"=",
"x_shape",
"[",
"-",
"1",
"]",
"num_steps",
"=",
"x_shape",
"[",
"0",
"]",
"shape",
"=",
"[",
"num_steps",
",",
"1",
",",
"1",
",",
"depth",
"]",
"depth_embedding",
"=",
"(",
"tf",
".",
"get_variable",
"(",
"\"depth_embedding\"",
",",
"shape",
",",
"initializer",
"=",
"tf",
".",
"random_normal_initializer",
"(",
"0",
",",
"depth",
"**",
"-",
"0.5",
")",
")",
"*",
"(",
"depth",
"**",
"0.5",
")",
")",
"x",
"+=",
"depth_embedding",
"return",
"x"
] |
Add n-dimensional embedding as the depth embedding (timing signal).
Adds embeddings to represent the position of the step in the recurrent
tower.
Args:
x: a tensor with shape [max_step, batch, length, depth]
Returns:
a Tensor the same shape as x.
|
[
"Add",
"n",
"-",
"dimensional",
"embedding",
"as",
"the",
"depth",
"embedding",
"(",
"timing",
"signal",
")",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1349-L1373
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
step_preprocess
|
def step_preprocess(x, step, hparams):
"""Preprocess the input at the beginning of each step.
Args:
x: input tensor
step: step
hparams: model hyper-parameters
Returns:
preprocessed input.
"""
original_channel_size = common_layers.shape_list(x)[-1]
if hparams.add_position_timing_signal:
x = add_position_timing_signal(x, step, hparams)
if hparams.add_step_timing_signal:
x = add_step_timing_signal(x, step, hparams)
if ((hparams.add_position_timing_signal or hparams.add_position_timing_signal)
and hparams.add_or_concat_timing_signal == "concat"):
# linear projection to the original dimension of x
x = common_layers.dense(
x, original_channel_size, activation=None, use_bias=False)
if hparams.add_sru:
x = common_layers.sru(x)
return x
|
python
|
def step_preprocess(x, step, hparams):
"""Preprocess the input at the beginning of each step.
Args:
x: input tensor
step: step
hparams: model hyper-parameters
Returns:
preprocessed input.
"""
original_channel_size = common_layers.shape_list(x)[-1]
if hparams.add_position_timing_signal:
x = add_position_timing_signal(x, step, hparams)
if hparams.add_step_timing_signal:
x = add_step_timing_signal(x, step, hparams)
if ((hparams.add_position_timing_signal or hparams.add_position_timing_signal)
and hparams.add_or_concat_timing_signal == "concat"):
# linear projection to the original dimension of x
x = common_layers.dense(
x, original_channel_size, activation=None, use_bias=False)
if hparams.add_sru:
x = common_layers.sru(x)
return x
|
[
"def",
"step_preprocess",
"(",
"x",
",",
"step",
",",
"hparams",
")",
":",
"original_channel_size",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"-",
"1",
"]",
"if",
"hparams",
".",
"add_position_timing_signal",
":",
"x",
"=",
"add_position_timing_signal",
"(",
"x",
",",
"step",
",",
"hparams",
")",
"if",
"hparams",
".",
"add_step_timing_signal",
":",
"x",
"=",
"add_step_timing_signal",
"(",
"x",
",",
"step",
",",
"hparams",
")",
"if",
"(",
"(",
"hparams",
".",
"add_position_timing_signal",
"or",
"hparams",
".",
"add_position_timing_signal",
")",
"and",
"hparams",
".",
"add_or_concat_timing_signal",
"==",
"\"concat\"",
")",
":",
"# linear projection to the original dimension of x",
"x",
"=",
"common_layers",
".",
"dense",
"(",
"x",
",",
"original_channel_size",
",",
"activation",
"=",
"None",
",",
"use_bias",
"=",
"False",
")",
"if",
"hparams",
".",
"add_sru",
":",
"x",
"=",
"common_layers",
".",
"sru",
"(",
"x",
")",
"return",
"x"
] |
Preprocess the input at the beginning of each step.
Args:
x: input tensor
step: step
hparams: model hyper-parameters
Returns:
preprocessed input.
|
[
"Preprocess",
"the",
"input",
"at",
"the",
"beginning",
"of",
"each",
"step",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1376-L1405
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
add_position_timing_signal
|
def add_position_timing_signal(x, step, hparams):
"""Add n-dimensional embedding as the position (horizontal) timing signal.
Args:
x: a tensor with shape [batch, length, depth]
step: step
hparams: model hyper parameters
Returns:
a Tensor with the same shape as x.
"""
if not hparams.position_start_index:
index = 0
elif hparams.position_start_index == "random":
# Shift all positions randomly
# TODO(dehghani): What would be reasonable for max number of shift?
index = tf.random_uniform(
[], maxval=common_layers.shape_list(x)[1], dtype=tf.int32)
elif hparams.position_start_index == "step":
# Shift positions based on the step
if hparams.recurrence_type == "act":
num_steps = hparams.act_max_steps
else:
num_steps = hparams.num_rec_steps
index = tf.cast(
common_layers.shape_list(x)[1] * step / num_steps, dtype=tf.int32)
# No need for the timing signal in the encoder/decoder input preparation
assert hparams.pos is None
length = common_layers.shape_list(x)[1]
channels = common_layers.shape_list(x)[2]
signal = common_attention.get_timing_signal_1d(
length, channels, start_index=index)
if hparams.add_or_concat_timing_signal == "add":
x_with_timing = x + common_layers.cast_like(signal, x)
elif hparams.add_or_concat_timing_signal == "concat":
batch_size = common_layers.shape_list(x)[0]
signal_tiled = tf.tile(signal, [batch_size, 1, 1])
x_with_timing = tf.concat((x, signal_tiled), axis=-1)
return x_with_timing
|
python
|
def add_position_timing_signal(x, step, hparams):
"""Add n-dimensional embedding as the position (horizontal) timing signal.
Args:
x: a tensor with shape [batch, length, depth]
step: step
hparams: model hyper parameters
Returns:
a Tensor with the same shape as x.
"""
if not hparams.position_start_index:
index = 0
elif hparams.position_start_index == "random":
# Shift all positions randomly
# TODO(dehghani): What would be reasonable for max number of shift?
index = tf.random_uniform(
[], maxval=common_layers.shape_list(x)[1], dtype=tf.int32)
elif hparams.position_start_index == "step":
# Shift positions based on the step
if hparams.recurrence_type == "act":
num_steps = hparams.act_max_steps
else:
num_steps = hparams.num_rec_steps
index = tf.cast(
common_layers.shape_list(x)[1] * step / num_steps, dtype=tf.int32)
# No need for the timing signal in the encoder/decoder input preparation
assert hparams.pos is None
length = common_layers.shape_list(x)[1]
channels = common_layers.shape_list(x)[2]
signal = common_attention.get_timing_signal_1d(
length, channels, start_index=index)
if hparams.add_or_concat_timing_signal == "add":
x_with_timing = x + common_layers.cast_like(signal, x)
elif hparams.add_or_concat_timing_signal == "concat":
batch_size = common_layers.shape_list(x)[0]
signal_tiled = tf.tile(signal, [batch_size, 1, 1])
x_with_timing = tf.concat((x, signal_tiled), axis=-1)
return x_with_timing
|
[
"def",
"add_position_timing_signal",
"(",
"x",
",",
"step",
",",
"hparams",
")",
":",
"if",
"not",
"hparams",
".",
"position_start_index",
":",
"index",
"=",
"0",
"elif",
"hparams",
".",
"position_start_index",
"==",
"\"random\"",
":",
"# Shift all positions randomly",
"# TODO(dehghani): What would be reasonable for max number of shift?",
"index",
"=",
"tf",
".",
"random_uniform",
"(",
"[",
"]",
",",
"maxval",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"1",
"]",
",",
"dtype",
"=",
"tf",
".",
"int32",
")",
"elif",
"hparams",
".",
"position_start_index",
"==",
"\"step\"",
":",
"# Shift positions based on the step",
"if",
"hparams",
".",
"recurrence_type",
"==",
"\"act\"",
":",
"num_steps",
"=",
"hparams",
".",
"act_max_steps",
"else",
":",
"num_steps",
"=",
"hparams",
".",
"num_rec_steps",
"index",
"=",
"tf",
".",
"cast",
"(",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"1",
"]",
"*",
"step",
"/",
"num_steps",
",",
"dtype",
"=",
"tf",
".",
"int32",
")",
"# No need for the timing signal in the encoder/decoder input preparation",
"assert",
"hparams",
".",
"pos",
"is",
"None",
"length",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"1",
"]",
"channels",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"2",
"]",
"signal",
"=",
"common_attention",
".",
"get_timing_signal_1d",
"(",
"length",
",",
"channels",
",",
"start_index",
"=",
"index",
")",
"if",
"hparams",
".",
"add_or_concat_timing_signal",
"==",
"\"add\"",
":",
"x_with_timing",
"=",
"x",
"+",
"common_layers",
".",
"cast_like",
"(",
"signal",
",",
"x",
")",
"elif",
"hparams",
".",
"add_or_concat_timing_signal",
"==",
"\"concat\"",
":",
"batch_size",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"0",
"]",
"signal_tiled",
"=",
"tf",
".",
"tile",
"(",
"signal",
",",
"[",
"batch_size",
",",
"1",
",",
"1",
"]",
")",
"x_with_timing",
"=",
"tf",
".",
"concat",
"(",
"(",
"x",
",",
"signal_tiled",
")",
",",
"axis",
"=",
"-",
"1",
")",
"return",
"x_with_timing"
] |
Add n-dimensional embedding as the position (horizontal) timing signal.
Args:
x: a tensor with shape [batch, length, depth]
step: step
hparams: model hyper parameters
Returns:
a Tensor with the same shape as x.
|
[
"Add",
"n",
"-",
"dimensional",
"embedding",
"as",
"the",
"position",
"(",
"horizontal",
")",
"timing",
"signal",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1408-L1455
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/models/research/universal_transformer_util.py
|
add_step_timing_signal
|
def add_step_timing_signal(x, step, hparams):
"""Add n-dimensional embedding as the step (vertical) timing signal.
Args:
x: a tensor with shape [batch, length, depth]
step: step
hparams: model hyper parameters
Returns:
a Tensor with the same shape as x.
"""
if hparams.recurrence_type == "act":
num_steps = hparams.act_max_steps
else:
num_steps = hparams.num_rec_steps
channels = common_layers.shape_list(x)[-1]
if hparams.step_timing_signal_type == "learned":
signal = common_attention.get_layer_timing_signal_learned_1d(
channels, step, num_steps)
elif hparams.step_timing_signal_type == "sinusoid":
signal = common_attention.get_layer_timing_signal_sinusoid_1d(
channels, step, num_steps)
if hparams.add_or_concat_timing_signal == "add":
x_with_timing = x + common_layers.cast_like(signal, x)
elif hparams.add_or_concat_timing_signal == "concat":
batch_size = common_layers.shape_list(x)[0]
length = common_layers.shape_list(x)[1]
signal_tiled = tf.tile(signal, [batch_size, length, 1])
x_with_timing = tf.concat((x, signal_tiled), axis=-1)
return x_with_timing
|
python
|
def add_step_timing_signal(x, step, hparams):
"""Add n-dimensional embedding as the step (vertical) timing signal.
Args:
x: a tensor with shape [batch, length, depth]
step: step
hparams: model hyper parameters
Returns:
a Tensor with the same shape as x.
"""
if hparams.recurrence_type == "act":
num_steps = hparams.act_max_steps
else:
num_steps = hparams.num_rec_steps
channels = common_layers.shape_list(x)[-1]
if hparams.step_timing_signal_type == "learned":
signal = common_attention.get_layer_timing_signal_learned_1d(
channels, step, num_steps)
elif hparams.step_timing_signal_type == "sinusoid":
signal = common_attention.get_layer_timing_signal_sinusoid_1d(
channels, step, num_steps)
if hparams.add_or_concat_timing_signal == "add":
x_with_timing = x + common_layers.cast_like(signal, x)
elif hparams.add_or_concat_timing_signal == "concat":
batch_size = common_layers.shape_list(x)[0]
length = common_layers.shape_list(x)[1]
signal_tiled = tf.tile(signal, [batch_size, length, 1])
x_with_timing = tf.concat((x, signal_tiled), axis=-1)
return x_with_timing
|
[
"def",
"add_step_timing_signal",
"(",
"x",
",",
"step",
",",
"hparams",
")",
":",
"if",
"hparams",
".",
"recurrence_type",
"==",
"\"act\"",
":",
"num_steps",
"=",
"hparams",
".",
"act_max_steps",
"else",
":",
"num_steps",
"=",
"hparams",
".",
"num_rec_steps",
"channels",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"-",
"1",
"]",
"if",
"hparams",
".",
"step_timing_signal_type",
"==",
"\"learned\"",
":",
"signal",
"=",
"common_attention",
".",
"get_layer_timing_signal_learned_1d",
"(",
"channels",
",",
"step",
",",
"num_steps",
")",
"elif",
"hparams",
".",
"step_timing_signal_type",
"==",
"\"sinusoid\"",
":",
"signal",
"=",
"common_attention",
".",
"get_layer_timing_signal_sinusoid_1d",
"(",
"channels",
",",
"step",
",",
"num_steps",
")",
"if",
"hparams",
".",
"add_or_concat_timing_signal",
"==",
"\"add\"",
":",
"x_with_timing",
"=",
"x",
"+",
"common_layers",
".",
"cast_like",
"(",
"signal",
",",
"x",
")",
"elif",
"hparams",
".",
"add_or_concat_timing_signal",
"==",
"\"concat\"",
":",
"batch_size",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"0",
"]",
"length",
"=",
"common_layers",
".",
"shape_list",
"(",
"x",
")",
"[",
"1",
"]",
"signal_tiled",
"=",
"tf",
".",
"tile",
"(",
"signal",
",",
"[",
"batch_size",
",",
"length",
",",
"1",
"]",
")",
"x_with_timing",
"=",
"tf",
".",
"concat",
"(",
"(",
"x",
",",
"signal_tiled",
")",
",",
"axis",
"=",
"-",
"1",
")",
"return",
"x_with_timing"
] |
Add n-dimensional embedding as the step (vertical) timing signal.
Args:
x: a tensor with shape [batch, length, depth]
step: step
hparams: model hyper parameters
Returns:
a Tensor with the same shape as x.
|
[
"Add",
"n",
"-",
"dimensional",
"embedding",
"as",
"the",
"step",
"(",
"vertical",
")",
"timing",
"signal",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/universal_transformer_util.py#L1458-L1493
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/wikisum/utils.py
|
wet_records_from_file_obj
|
def wet_records_from_file_obj(f, take_ownership=False):
"""Iterate through records in WET file object."""
while True:
record = WETRecord.read(f)
if record is None:
break
if not record.url:
continue
yield record
if take_ownership:
f.close()
|
python
|
def wet_records_from_file_obj(f, take_ownership=False):
"""Iterate through records in WET file object."""
while True:
record = WETRecord.read(f)
if record is None:
break
if not record.url:
continue
yield record
if take_ownership:
f.close()
|
[
"def",
"wet_records_from_file_obj",
"(",
"f",
",",
"take_ownership",
"=",
"False",
")",
":",
"while",
"True",
":",
"record",
"=",
"WETRecord",
".",
"read",
"(",
"f",
")",
"if",
"record",
"is",
"None",
":",
"break",
"if",
"not",
"record",
".",
"url",
":",
"continue",
"yield",
"record",
"if",
"take_ownership",
":",
"f",
".",
"close",
"(",
")"
] |
Iterate through records in WET file object.
|
[
"Iterate",
"through",
"records",
"in",
"WET",
"file",
"object",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikisum/utils.py#L101-L115
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/wikisum/utils.py
|
wet_records
|
def wet_records(wet_filepath):
"""Generate WETRecords from filepath."""
if wet_filepath.endswith('.gz'):
fopen = gzip.open
else:
fopen = tf.gfile.GFile
with fopen(wet_filepath) as f:
for record in wet_records_from_file_obj(f):
yield record
|
python
|
def wet_records(wet_filepath):
"""Generate WETRecords from filepath."""
if wet_filepath.endswith('.gz'):
fopen = gzip.open
else:
fopen = tf.gfile.GFile
with fopen(wet_filepath) as f:
for record in wet_records_from_file_obj(f):
yield record
|
[
"def",
"wet_records",
"(",
"wet_filepath",
")",
":",
"if",
"wet_filepath",
".",
"endswith",
"(",
"'.gz'",
")",
":",
"fopen",
"=",
"gzip",
".",
"open",
"else",
":",
"fopen",
"=",
"tf",
".",
"gfile",
".",
"GFile",
"with",
"fopen",
"(",
"wet_filepath",
")",
"as",
"f",
":",
"for",
"record",
"in",
"wet_records_from_file_obj",
"(",
"f",
")",
":",
"yield",
"record"
] |
Generate WETRecords from filepath.
|
[
"Generate",
"WETRecords",
"from",
"filepath",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikisum/utils.py#L118-L127
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/wikisum/utils.py
|
filter_paragraph
|
def filter_paragraph(p):
"""Simple filter to remove obviously bad paragraphs (bad text extraction).
Note this needs to run very quickly as it is applied to every paragraph
in the corpus, so nothing fancy! This whole method should be linear
expected time in len(p).
Args:
p: string, paragraph
Returns:
True if we should remove the paragraph.
"""
# Expect a minimum number of words.
tokens = p.split()
if len(tokens) < 6:
return True
# Require some letters.
if not re.search(_SOME_ALPHA_RE, p):
return True
# Keep this one at the end, probably the most complicated logic.
# We try to detect sentences, which should have a minimum of 3 tokens
# with only alphabetic characters.
last = 0
found_sentence = False
num_alpha = 0
for i, x in enumerate(tokens):
if x == '.':
if i - last > 3 and num_alpha >= 3:
found_sentence = True
break
last = i
num_alpha = 0
if re.match(_ONLY_ALPHA_RE, x):
num_alpha += 1
if not found_sentence:
return True
return False
|
python
|
def filter_paragraph(p):
"""Simple filter to remove obviously bad paragraphs (bad text extraction).
Note this needs to run very quickly as it is applied to every paragraph
in the corpus, so nothing fancy! This whole method should be linear
expected time in len(p).
Args:
p: string, paragraph
Returns:
True if we should remove the paragraph.
"""
# Expect a minimum number of words.
tokens = p.split()
if len(tokens) < 6:
return True
# Require some letters.
if not re.search(_SOME_ALPHA_RE, p):
return True
# Keep this one at the end, probably the most complicated logic.
# We try to detect sentences, which should have a minimum of 3 tokens
# with only alphabetic characters.
last = 0
found_sentence = False
num_alpha = 0
for i, x in enumerate(tokens):
if x == '.':
if i - last > 3 and num_alpha >= 3:
found_sentence = True
break
last = i
num_alpha = 0
if re.match(_ONLY_ALPHA_RE, x):
num_alpha += 1
if not found_sentence:
return True
return False
|
[
"def",
"filter_paragraph",
"(",
"p",
")",
":",
"# Expect a minimum number of words.",
"tokens",
"=",
"p",
".",
"split",
"(",
")",
"if",
"len",
"(",
"tokens",
")",
"<",
"6",
":",
"return",
"True",
"# Require some letters.",
"if",
"not",
"re",
".",
"search",
"(",
"_SOME_ALPHA_RE",
",",
"p",
")",
":",
"return",
"True",
"# Keep this one at the end, probably the most complicated logic.",
"# We try to detect sentences, which should have a minimum of 3 tokens",
"# with only alphabetic characters.",
"last",
"=",
"0",
"found_sentence",
"=",
"False",
"num_alpha",
"=",
"0",
"for",
"i",
",",
"x",
"in",
"enumerate",
"(",
"tokens",
")",
":",
"if",
"x",
"==",
"'.'",
":",
"if",
"i",
"-",
"last",
">",
"3",
"and",
"num_alpha",
">=",
"3",
":",
"found_sentence",
"=",
"True",
"break",
"last",
"=",
"i",
"num_alpha",
"=",
"0",
"if",
"re",
".",
"match",
"(",
"_ONLY_ALPHA_RE",
",",
"x",
")",
":",
"num_alpha",
"+=",
"1",
"if",
"not",
"found_sentence",
":",
"return",
"True",
"return",
"False"
] |
Simple filter to remove obviously bad paragraphs (bad text extraction).
Note this needs to run very quickly as it is applied to every paragraph
in the corpus, so nothing fancy! This whole method should be linear
expected time in len(p).
Args:
p: string, paragraph
Returns:
True if we should remove the paragraph.
|
[
"Simple",
"filter",
"to",
"remove",
"obviously",
"bad",
"paragraphs",
"(",
"bad",
"text",
"extraction",
")",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikisum/utils.py#L214-L254
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/wikisum/utils.py
|
timing
|
def timing(name=''):
"""Log start, end, and duration."""
start = datetime.datetime.now()
timestamp = start.strftime('%H:%M')
tf.logging.info('Starting job [%s] at %s', name, timestamp)
yield
end = datetime.datetime.now()
timestamp = end.strftime('%H:%M')
tf.logging.info('Finished job [%s] at %s', name, timestamp)
duration = end - start
duration_mins = duration.total_seconds() / 60
tf.logging.info('Total time [%s] (m): %d', name, int(duration_mins))
|
python
|
def timing(name=''):
"""Log start, end, and duration."""
start = datetime.datetime.now()
timestamp = start.strftime('%H:%M')
tf.logging.info('Starting job [%s] at %s', name, timestamp)
yield
end = datetime.datetime.now()
timestamp = end.strftime('%H:%M')
tf.logging.info('Finished job [%s] at %s', name, timestamp)
duration = end - start
duration_mins = duration.total_seconds() / 60
tf.logging.info('Total time [%s] (m): %d', name, int(duration_mins))
|
[
"def",
"timing",
"(",
"name",
"=",
"''",
")",
":",
"start",
"=",
"datetime",
".",
"datetime",
".",
"now",
"(",
")",
"timestamp",
"=",
"start",
".",
"strftime",
"(",
"'%H:%M'",
")",
"tf",
".",
"logging",
".",
"info",
"(",
"'Starting job [%s] at %s'",
",",
"name",
",",
"timestamp",
")",
"yield",
"end",
"=",
"datetime",
".",
"datetime",
".",
"now",
"(",
")",
"timestamp",
"=",
"end",
".",
"strftime",
"(",
"'%H:%M'",
")",
"tf",
".",
"logging",
".",
"info",
"(",
"'Finished job [%s] at %s'",
",",
"name",
",",
"timestamp",
")",
"duration",
"=",
"end",
"-",
"start",
"duration_mins",
"=",
"duration",
".",
"total_seconds",
"(",
")",
"/",
"60",
"tf",
".",
"logging",
".",
"info",
"(",
"'Total time [%s] (m): %d'",
",",
"name",
",",
"int",
"(",
"duration_mins",
")",
")"
] |
Log start, end, and duration.
|
[
"Log",
"start",
"end",
"and",
"duration",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikisum/utils.py#L258-L269
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/wikisum/utils.py
|
WETHeader.read
|
def read(cls, f):
"""Read header from file. Headers end with length and then 1 blank line."""
url = None
line = f.readline()
if not line:
# EOF
return None
while not line.startswith(cls.LENGTH_HEADER):
if line.startswith(cls.URI_HEADER):
url = line[len(cls.URI_HEADER):].strip()
line = f.readline()
# Consume empty separator
f.readline()
# Read content
length = int(line.split(':')[1])
return cls(url, length)
|
python
|
def read(cls, f):
"""Read header from file. Headers end with length and then 1 blank line."""
url = None
line = f.readline()
if not line:
# EOF
return None
while not line.startswith(cls.LENGTH_HEADER):
if line.startswith(cls.URI_HEADER):
url = line[len(cls.URI_HEADER):].strip()
line = f.readline()
# Consume empty separator
f.readline()
# Read content
length = int(line.split(':')[1])
return cls(url, length)
|
[
"def",
"read",
"(",
"cls",
",",
"f",
")",
":",
"url",
"=",
"None",
"line",
"=",
"f",
".",
"readline",
"(",
")",
"if",
"not",
"line",
":",
"# EOF",
"return",
"None",
"while",
"not",
"line",
".",
"startswith",
"(",
"cls",
".",
"LENGTH_HEADER",
")",
":",
"if",
"line",
".",
"startswith",
"(",
"cls",
".",
"URI_HEADER",
")",
":",
"url",
"=",
"line",
"[",
"len",
"(",
"cls",
".",
"URI_HEADER",
")",
":",
"]",
".",
"strip",
"(",
")",
"line",
"=",
"f",
".",
"readline",
"(",
")",
"# Consume empty separator",
"f",
".",
"readline",
"(",
")",
"# Read content",
"length",
"=",
"int",
"(",
"line",
".",
"split",
"(",
"':'",
")",
"[",
"1",
"]",
")",
"return",
"cls",
"(",
"url",
",",
"length",
")"
] |
Read header from file. Headers end with length and then 1 blank line.
|
[
"Read",
"header",
"from",
"file",
".",
"Headers",
"end",
"with",
"length",
"and",
"then",
"1",
"blank",
"line",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikisum/utils.py#L61-L80
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/data_generators/wikisum/utils.py
|
WETRecord.read
|
def read(cls, f):
"""Read WETRecord from file. Records end with 2 blank lines."""
header = WETHeader.read(f)
if header is None:
# EOF
return None
content = f.read(header.length)
# Consume empty separators
f.readline()
f.readline()
return cls(header.url, content)
|
python
|
def read(cls, f):
"""Read WETRecord from file. Records end with 2 blank lines."""
header = WETHeader.read(f)
if header is None:
# EOF
return None
content = f.read(header.length)
# Consume empty separators
f.readline()
f.readline()
return cls(header.url, content)
|
[
"def",
"read",
"(",
"cls",
",",
"f",
")",
":",
"header",
"=",
"WETHeader",
".",
"read",
"(",
"f",
")",
"if",
"header",
"is",
"None",
":",
"# EOF",
"return",
"None",
"content",
"=",
"f",
".",
"read",
"(",
"header",
".",
"length",
")",
"# Consume empty separators",
"f",
".",
"readline",
"(",
")",
"f",
".",
"readline",
"(",
")",
"return",
"cls",
"(",
"header",
".",
"url",
",",
"content",
")"
] |
Read WETRecord from file. Records end with 2 blank lines.
|
[
"Read",
"WETRecord",
"from",
"file",
".",
"Records",
"end",
"with",
"2",
"blank",
"lines",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/wikisum/utils.py#L86-L98
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/trax/models/mlp.py
|
MLP
|
def MLP(num_hidden_layers=2,
hidden_size=512,
activation_fn=layers.Relu,
num_output_classes=10,
mode="train"):
"""Multi-layer feed-forward neural network with non-linear activations."""
del mode
cur_layers = [layers.Flatten()]
for _ in range(num_hidden_layers):
cur_layers += [layers.Dense(hidden_size), activation_fn()]
cur_layers += [layers.Dense(num_output_classes), layers.LogSoftmax()]
return layers.Serial(*cur_layers)
|
python
|
def MLP(num_hidden_layers=2,
hidden_size=512,
activation_fn=layers.Relu,
num_output_classes=10,
mode="train"):
"""Multi-layer feed-forward neural network with non-linear activations."""
del mode
cur_layers = [layers.Flatten()]
for _ in range(num_hidden_layers):
cur_layers += [layers.Dense(hidden_size), activation_fn()]
cur_layers += [layers.Dense(num_output_classes), layers.LogSoftmax()]
return layers.Serial(*cur_layers)
|
[
"def",
"MLP",
"(",
"num_hidden_layers",
"=",
"2",
",",
"hidden_size",
"=",
"512",
",",
"activation_fn",
"=",
"layers",
".",
"Relu",
",",
"num_output_classes",
"=",
"10",
",",
"mode",
"=",
"\"train\"",
")",
":",
"del",
"mode",
"cur_layers",
"=",
"[",
"layers",
".",
"Flatten",
"(",
")",
"]",
"for",
"_",
"in",
"range",
"(",
"num_hidden_layers",
")",
":",
"cur_layers",
"+=",
"[",
"layers",
".",
"Dense",
"(",
"hidden_size",
")",
",",
"activation_fn",
"(",
")",
"]",
"cur_layers",
"+=",
"[",
"layers",
".",
"Dense",
"(",
"num_output_classes",
")",
",",
"layers",
".",
"LogSoftmax",
"(",
")",
"]",
"return",
"layers",
".",
"Serial",
"(",
"*",
"cur_layers",
")"
] |
Multi-layer feed-forward neural network with non-linear activations.
|
[
"Multi",
"-",
"layer",
"feed",
"-",
"forward",
"neural",
"network",
"with",
"non",
"-",
"linear",
"activations",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/trax/models/mlp.py#L25-L36
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/env_problem.py
|
EnvProblem._verify_same_spaces
|
def _verify_same_spaces(self):
"""Verifies that all the envs have the same observation and action space."""
# Pre-conditions: self._envs is initialized.
if self._envs is None:
raise ValueError("Environments not initialized.")
if not isinstance(self._envs, list):
tf.logging.warning("Not checking observation and action space "
"compatibility across envs, since there is just one.")
return
# NOTE: We compare string representations of observation_space and
# action_space because compositional classes like space.Tuple don't return
# true on object comparison.
if not all(
str(env.observation_space) == str(self.observation_space)
for env in self._envs):
err_str = ("All environments should have the same observation space, but "
"don't.")
tf.logging.error(err_str)
# Log all observation spaces.
for i, env in enumerate(self._envs):
tf.logging.error("Env[%d] has observation space [%s]", i,
env.observation_space)
raise ValueError(err_str)
if not all(
str(env.action_space) == str(self.action_space) for env in self._envs):
err_str = "All environments should have the same action space, but don't."
tf.logging.error(err_str)
# Log all action spaces.
for i, env in enumerate(self._envs):
tf.logging.error("Env[%d] has action space [%s]", i, env.action_space)
raise ValueError(err_str)
|
python
|
def _verify_same_spaces(self):
"""Verifies that all the envs have the same observation and action space."""
# Pre-conditions: self._envs is initialized.
if self._envs is None:
raise ValueError("Environments not initialized.")
if not isinstance(self._envs, list):
tf.logging.warning("Not checking observation and action space "
"compatibility across envs, since there is just one.")
return
# NOTE: We compare string representations of observation_space and
# action_space because compositional classes like space.Tuple don't return
# true on object comparison.
if not all(
str(env.observation_space) == str(self.observation_space)
for env in self._envs):
err_str = ("All environments should have the same observation space, but "
"don't.")
tf.logging.error(err_str)
# Log all observation spaces.
for i, env in enumerate(self._envs):
tf.logging.error("Env[%d] has observation space [%s]", i,
env.observation_space)
raise ValueError(err_str)
if not all(
str(env.action_space) == str(self.action_space) for env in self._envs):
err_str = "All environments should have the same action space, but don't."
tf.logging.error(err_str)
# Log all action spaces.
for i, env in enumerate(self._envs):
tf.logging.error("Env[%d] has action space [%s]", i, env.action_space)
raise ValueError(err_str)
|
[
"def",
"_verify_same_spaces",
"(",
"self",
")",
":",
"# Pre-conditions: self._envs is initialized.",
"if",
"self",
".",
"_envs",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"Environments not initialized.\"",
")",
"if",
"not",
"isinstance",
"(",
"self",
".",
"_envs",
",",
"list",
")",
":",
"tf",
".",
"logging",
".",
"warning",
"(",
"\"Not checking observation and action space \"",
"\"compatibility across envs, since there is just one.\"",
")",
"return",
"# NOTE: We compare string representations of observation_space and",
"# action_space because compositional classes like space.Tuple don't return",
"# true on object comparison.",
"if",
"not",
"all",
"(",
"str",
"(",
"env",
".",
"observation_space",
")",
"==",
"str",
"(",
"self",
".",
"observation_space",
")",
"for",
"env",
"in",
"self",
".",
"_envs",
")",
":",
"err_str",
"=",
"(",
"\"All environments should have the same observation space, but \"",
"\"don't.\"",
")",
"tf",
".",
"logging",
".",
"error",
"(",
"err_str",
")",
"# Log all observation spaces.",
"for",
"i",
",",
"env",
"in",
"enumerate",
"(",
"self",
".",
"_envs",
")",
":",
"tf",
".",
"logging",
".",
"error",
"(",
"\"Env[%d] has observation space [%s]\"",
",",
"i",
",",
"env",
".",
"observation_space",
")",
"raise",
"ValueError",
"(",
"err_str",
")",
"if",
"not",
"all",
"(",
"str",
"(",
"env",
".",
"action_space",
")",
"==",
"str",
"(",
"self",
".",
"action_space",
")",
"for",
"env",
"in",
"self",
".",
"_envs",
")",
":",
"err_str",
"=",
"\"All environments should have the same action space, but don't.\"",
"tf",
".",
"logging",
".",
"error",
"(",
"err_str",
")",
"# Log all action spaces.",
"for",
"i",
",",
"env",
"in",
"enumerate",
"(",
"self",
".",
"_envs",
")",
":",
"tf",
".",
"logging",
".",
"error",
"(",
"\"Env[%d] has action space [%s]\"",
",",
"i",
",",
"env",
".",
"action_space",
")",
"raise",
"ValueError",
"(",
"err_str",
")"
] |
Verifies that all the envs have the same observation and action space.
|
[
"Verifies",
"that",
"all",
"the",
"envs",
"have",
"the",
"same",
"observation",
"and",
"action",
"space",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/env_problem.py#L199-L235
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/env_problem.py
|
EnvProblem.initialize_environments
|
def initialize_environments(self, batch_size=1):
"""Initializes the environments and trajectories.
Subclasses can override this if they don't want a default implementation
which initializes `batch_size` environments, but must take care to
initialize self._trajectories (this is checked in __init__ anyways).
Args:
batch_size: (int) Number of `self.base_env_name` envs to initialize.
"""
assert batch_size >= 1
self._batch_size = batch_size
self._envs = [gym.make(self.base_env_name) for _ in range(batch_size)]
if self._env_wrapper_fn is not None:
self._envs = list(map(self._env_wrapper_fn, self._envs))
# If self.observation_space and self.action_space aren't None, then it means
# that this is a re-initialization of this class, in that case make sure
# that this matches our previous behaviour.
if self._observation_space:
assert str(self._observation_space) == str(
self._envs[0].observation_space)
else:
# This means that we are initializing this class for the first time.
#
# We set this equal to the first env's observation space, later on we'll
# verify that all envs have the same observation space.
self._observation_space = self._envs[0].observation_space
# Similarly for action_space
if self._action_space:
assert str(self._action_space) == str(self._envs[0].action_space)
else:
self._action_space = self._envs[0].action_space
self._verify_same_spaces()
# If self.reward_range is None, i.e. this means that we should take the
# reward range of the env.
if self.reward_range is None:
self._reward_range = self._envs[0].reward_range
# This data structure stores the history of each env.
#
# NOTE: Even if the env is a NN and can step in all batches concurrently, it
# is still valuable to store the trajectories separately.
self._trajectories = trajectory.BatchTrajectory(batch_size=batch_size)
|
python
|
def initialize_environments(self, batch_size=1):
"""Initializes the environments and trajectories.
Subclasses can override this if they don't want a default implementation
which initializes `batch_size` environments, but must take care to
initialize self._trajectories (this is checked in __init__ anyways).
Args:
batch_size: (int) Number of `self.base_env_name` envs to initialize.
"""
assert batch_size >= 1
self._batch_size = batch_size
self._envs = [gym.make(self.base_env_name) for _ in range(batch_size)]
if self._env_wrapper_fn is not None:
self._envs = list(map(self._env_wrapper_fn, self._envs))
# If self.observation_space and self.action_space aren't None, then it means
# that this is a re-initialization of this class, in that case make sure
# that this matches our previous behaviour.
if self._observation_space:
assert str(self._observation_space) == str(
self._envs[0].observation_space)
else:
# This means that we are initializing this class for the first time.
#
# We set this equal to the first env's observation space, later on we'll
# verify that all envs have the same observation space.
self._observation_space = self._envs[0].observation_space
# Similarly for action_space
if self._action_space:
assert str(self._action_space) == str(self._envs[0].action_space)
else:
self._action_space = self._envs[0].action_space
self._verify_same_spaces()
# If self.reward_range is None, i.e. this means that we should take the
# reward range of the env.
if self.reward_range is None:
self._reward_range = self._envs[0].reward_range
# This data structure stores the history of each env.
#
# NOTE: Even if the env is a NN and can step in all batches concurrently, it
# is still valuable to store the trajectories separately.
self._trajectories = trajectory.BatchTrajectory(batch_size=batch_size)
|
[
"def",
"initialize_environments",
"(",
"self",
",",
"batch_size",
"=",
"1",
")",
":",
"assert",
"batch_size",
">=",
"1",
"self",
".",
"_batch_size",
"=",
"batch_size",
"self",
".",
"_envs",
"=",
"[",
"gym",
".",
"make",
"(",
"self",
".",
"base_env_name",
")",
"for",
"_",
"in",
"range",
"(",
"batch_size",
")",
"]",
"if",
"self",
".",
"_env_wrapper_fn",
"is",
"not",
"None",
":",
"self",
".",
"_envs",
"=",
"list",
"(",
"map",
"(",
"self",
".",
"_env_wrapper_fn",
",",
"self",
".",
"_envs",
")",
")",
"# If self.observation_space and self.action_space aren't None, then it means",
"# that this is a re-initialization of this class, in that case make sure",
"# that this matches our previous behaviour.",
"if",
"self",
".",
"_observation_space",
":",
"assert",
"str",
"(",
"self",
".",
"_observation_space",
")",
"==",
"str",
"(",
"self",
".",
"_envs",
"[",
"0",
"]",
".",
"observation_space",
")",
"else",
":",
"# This means that we are initializing this class for the first time.",
"#",
"# We set this equal to the first env's observation space, later on we'll",
"# verify that all envs have the same observation space.",
"self",
".",
"_observation_space",
"=",
"self",
".",
"_envs",
"[",
"0",
"]",
".",
"observation_space",
"# Similarly for action_space",
"if",
"self",
".",
"_action_space",
":",
"assert",
"str",
"(",
"self",
".",
"_action_space",
")",
"==",
"str",
"(",
"self",
".",
"_envs",
"[",
"0",
"]",
".",
"action_space",
")",
"else",
":",
"self",
".",
"_action_space",
"=",
"self",
".",
"_envs",
"[",
"0",
"]",
".",
"action_space",
"self",
".",
"_verify_same_spaces",
"(",
")",
"# If self.reward_range is None, i.e. this means that we should take the",
"# reward range of the env.",
"if",
"self",
".",
"reward_range",
"is",
"None",
":",
"self",
".",
"_reward_range",
"=",
"self",
".",
"_envs",
"[",
"0",
"]",
".",
"reward_range",
"# This data structure stores the history of each env.",
"#",
"# NOTE: Even if the env is a NN and can step in all batches concurrently, it",
"# is still valuable to store the trajectories separately.",
"self",
".",
"_trajectories",
"=",
"trajectory",
".",
"BatchTrajectory",
"(",
"batch_size",
"=",
"batch_size",
")"
] |
Initializes the environments and trajectories.
Subclasses can override this if they don't want a default implementation
which initializes `batch_size` environments, but must take care to
initialize self._trajectories (this is checked in __init__ anyways).
Args:
batch_size: (int) Number of `self.base_env_name` envs to initialize.
|
[
"Initializes",
"the",
"environments",
"and",
"trajectories",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/env_problem.py#L248-L295
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/env_problem.py
|
EnvProblem.process_rewards
|
def process_rewards(self, rewards):
"""Clips, rounds, and changes to integer type.
Args:
rewards: numpy array of raw (float) rewards.
Returns:
processed_rewards: numpy array of np.int64
"""
min_reward, max_reward = self.reward_range
# Clips at min and max reward.
rewards = np.clip(rewards, min_reward, max_reward)
# Round to (nearest) int and convert to integral type.
rewards = np.around(rewards, decimals=0).astype(np.int64)
return rewards
|
python
|
def process_rewards(self, rewards):
"""Clips, rounds, and changes to integer type.
Args:
rewards: numpy array of raw (float) rewards.
Returns:
processed_rewards: numpy array of np.int64
"""
min_reward, max_reward = self.reward_range
# Clips at min and max reward.
rewards = np.clip(rewards, min_reward, max_reward)
# Round to (nearest) int and convert to integral type.
rewards = np.around(rewards, decimals=0).astype(np.int64)
return rewards
|
[
"def",
"process_rewards",
"(",
"self",
",",
"rewards",
")",
":",
"min_reward",
",",
"max_reward",
"=",
"self",
".",
"reward_range",
"# Clips at min and max reward.",
"rewards",
"=",
"np",
".",
"clip",
"(",
"rewards",
",",
"min_reward",
",",
"max_reward",
")",
"# Round to (nearest) int and convert to integral type.",
"rewards",
"=",
"np",
".",
"around",
"(",
"rewards",
",",
"decimals",
"=",
"0",
")",
".",
"astype",
"(",
"np",
".",
"int64",
")",
"return",
"rewards"
] |
Clips, rounds, and changes to integer type.
Args:
rewards: numpy array of raw (float) rewards.
Returns:
processed_rewards: numpy array of np.int64
|
[
"Clips",
"rounds",
"and",
"changes",
"to",
"integer",
"type",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/env_problem.py#L352-L368
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/env_problem.py
|
EnvProblem.num_rewards
|
def num_rewards(self):
"""Returns the number of distinct rewards.
Returns:
Returns None if the reward range is infinite or the processed rewards
aren't discrete, otherwise returns the number of distinct rewards.
"""
# Pre-conditions: reward range is finite.
# : processed rewards are discrete.
if not self.is_reward_range_finite:
tf.logging.error("Infinite reward range, `num_rewards returning None`")
return None
if not self.is_processed_rewards_discrete:
tf.logging.error(
"Processed rewards are not discrete, `num_rewards` returning None")
return None
min_reward, max_reward = self.reward_range
return max_reward - min_reward + 1
|
python
|
def num_rewards(self):
"""Returns the number of distinct rewards.
Returns:
Returns None if the reward range is infinite or the processed rewards
aren't discrete, otherwise returns the number of distinct rewards.
"""
# Pre-conditions: reward range is finite.
# : processed rewards are discrete.
if not self.is_reward_range_finite:
tf.logging.error("Infinite reward range, `num_rewards returning None`")
return None
if not self.is_processed_rewards_discrete:
tf.logging.error(
"Processed rewards are not discrete, `num_rewards` returning None")
return None
min_reward, max_reward = self.reward_range
return max_reward - min_reward + 1
|
[
"def",
"num_rewards",
"(",
"self",
")",
":",
"# Pre-conditions: reward range is finite.",
"# : processed rewards are discrete.",
"if",
"not",
"self",
".",
"is_reward_range_finite",
":",
"tf",
".",
"logging",
".",
"error",
"(",
"\"Infinite reward range, `num_rewards returning None`\"",
")",
"return",
"None",
"if",
"not",
"self",
".",
"is_processed_rewards_discrete",
":",
"tf",
".",
"logging",
".",
"error",
"(",
"\"Processed rewards are not discrete, `num_rewards` returning None\"",
")",
"return",
"None",
"min_reward",
",",
"max_reward",
"=",
"self",
".",
"reward_range",
"return",
"max_reward",
"-",
"min_reward",
"+",
"1"
] |
Returns the number of distinct rewards.
Returns:
Returns None if the reward range is infinite or the processed rewards
aren't discrete, otherwise returns the number of distinct rewards.
|
[
"Returns",
"the",
"number",
"of",
"distinct",
"rewards",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/env_problem.py#L380-L399
|
train
|
tensorflow/tensor2tensor
|
tensor2tensor/envs/env_problem.py
|
EnvProblem._reset
|
def _reset(self, indices):
"""Resets environments at indices shouldn't pre-process or record.
Subclasses should override this to do the actual reset if something other
than the default implementation is desired.
Args:
indices: list of indices of underlying envs to call reset on.
Returns:
np.ndarray of stacked observations from the reset-ed envs.
"""
# Pre-conditions: common_preconditions, see `assert_common_preconditions`.
self.assert_common_preconditions()
# This returns a numpy array with first dimension `len(indices)` and the
# rest being the dimensionality of the observation.
return np.stack([self._envs[index].reset() for index in indices])
|
python
|
def _reset(self, indices):
"""Resets environments at indices shouldn't pre-process or record.
Subclasses should override this to do the actual reset if something other
than the default implementation is desired.
Args:
indices: list of indices of underlying envs to call reset on.
Returns:
np.ndarray of stacked observations from the reset-ed envs.
"""
# Pre-conditions: common_preconditions, see `assert_common_preconditions`.
self.assert_common_preconditions()
# This returns a numpy array with first dimension `len(indices)` and the
# rest being the dimensionality of the observation.
return np.stack([self._envs[index].reset() for index in indices])
|
[
"def",
"_reset",
"(",
"self",
",",
"indices",
")",
":",
"# Pre-conditions: common_preconditions, see `assert_common_preconditions`.",
"self",
".",
"assert_common_preconditions",
"(",
")",
"# This returns a numpy array with first dimension `len(indices)` and the",
"# rest being the dimensionality of the observation.",
"return",
"np",
".",
"stack",
"(",
"[",
"self",
".",
"_envs",
"[",
"index",
"]",
".",
"reset",
"(",
")",
"for",
"index",
"in",
"indices",
"]",
")"
] |
Resets environments at indices shouldn't pre-process or record.
Subclasses should override this to do the actual reset if something other
than the default implementation is desired.
Args:
indices: list of indices of underlying envs to call reset on.
Returns:
np.ndarray of stacked observations from the reset-ed envs.
|
[
"Resets",
"environments",
"at",
"indices",
"shouldn",
"t",
"pre",
"-",
"process",
"or",
"record",
"."
] |
272500b6efe353aeb638d2745ed56e519462ca31
|
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/envs/env_problem.py#L454-L472
|
train
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.