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

Commit 9053259

Browse files
authored
Merge pull request #686 from rsepassi/push
v1.5.6
2 parents 7d1c9ea + 160bed3 commit 9053259

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2413
-533
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ env:
1111
- TF_VERSION="1.4.*"
1212
- TF_VERSION="1.5.*"
1313
- TF_VERSION="1.6.*"
14+
- TF_VERSION="1.7.*"
1415
matrix:
1516
exclude:
1617
- python: "3.6"
1718
env: TF_VERSION="1.4.*"
1819
- python: "3.6"
1920
env: TF_VERSION="1.5.*"
21+
- python: "3.6"
22+
env: TF_VERSION="1.6.*"
2023
before_install:
2124
- echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list
2225
- curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ README](https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/da
355355

356356
## Papers
357357

358+
When referencing Tensor2Tensor, please cite [this
359+
paper](https://arxiv.org/abs/1803.07416).
360+
361+
```
362+
@article{tensor2tensor,
363+
author = {Ashish Vaswani and Samy Bengio and Eugene Brevdo and
364+
Francois Chollet and Aidan N. Gomez and Stephan Gouws and Llion Jones and
365+
\L{}ukasz Kaiser and Nal Kalchbrenner and Niki Parmar and Ryan Sepassi and
366+
Noam Shazeer and Jakob Uszkoreit},
367+
title = {Tensor2Tensor for Neural Machine Translation},
368+
journal = {CoRR},
369+
volume = {abs/1803.07416},
370+
year = {2018},
371+
url = {http://arxiv.org/abs/1803.07416},
372+
}
373+
```
374+
358375
Tensor2Tensor was used to develop a number of state-of-the-art models
359376
and deep learning methods. Here we list some papers that were based on T2T
360377
from the start and benefited from its features and architecture in ways
@@ -370,5 +387,7 @@ T2T](https://research.googleblog.com/2017/06/accelerating-deep-learning-research
370387
Sequences](https://arxiv.org/abs/1801.10198)
371388
* [Image Transformer](https://arxiv.org/abs/1802.05751)
372389
* [Training Tips for the Transformer Model](https://arxiv.org/abs/1804.00247)
390+
* [Self-Attention with Relative Position Representations](https://arxiv.org/abs/1803.02155)
391+
* [Fast Decoding in Sequence Models using Discrete Latent Variables](https://arxiv.org/abs/1803.03382)
373392

374393
*Note: This is not an official Google product.*

docs/cloud_mlengine.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ machines with 4 or 8 GPUs.
2828
You can additionally pass the `--cloud_mlengine_master_type` to select another
2929
kind of machine (see the [docs for
3030
`masterType`](https://cloud.google.com/ml-engine/reference/rest/v1/projects.jobs#traininginput)
31-
for options, including
32-
[ML Engine machine types](https://cloud.google.com/ml-engine/docs/training-overview)
31+
for options, including
32+
[ML Engine machine
33+
types](https://cloud.google.com/ml-engine/docs/training-overview)
3334
and their
3435
[specs](https://cloud.google.com/compute/docs/machine-types)).
3536
If you provide this flag yourself, make sure you pass the
36-
correct value for `--worker_gpu` (for non-GPU machines, you must explicitly pass `--worker_gpu=0`).
37+
correct value for `--worker_gpu` (for non-GPU machines, you should pass
38+
`--worker_gpu=0`).
3739

3840
**Note**: `t2t-trainer` only currently supports launching with single machines,
3941
possibly with multiple GPUs. Multi-machine setups are not yet supported out of

docs/walkthrough.md

+19
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ README](https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/da
355355

356356
## Papers
357357

358+
When referencing Tensor2Tensor, please cite [this
359+
paper](https://arxiv.org/abs/1803.07416).
360+
361+
```
362+
@article{tensor2tensor,
363+
author = {Ashish Vaswani and Samy Bengio and Eugene Brevdo and
364+
Francois Chollet and Aidan N. Gomez and Stephan Gouws and Llion Jones and
365+
\L{}ukasz Kaiser and Nal Kalchbrenner and Niki Parmar and Ryan Sepassi and
366+
Noam Shazeer and Jakob Uszkoreit},
367+
title = {Tensor2Tensor for Neural Machine Translation},
368+
journal = {CoRR},
369+
volume = {abs/1803.07416},
370+
year = {2018},
371+
url = {http://arxiv.org/abs/1803.07416},
372+
}
373+
```
374+
358375
Tensor2Tensor was used to develop a number of state-of-the-art models
359376
and deep learning methods. Here we list some papers that were based on T2T
360377
from the start and benefited from its features and architecture in ways
@@ -370,5 +387,7 @@ T2T](https://research.googleblog.com/2017/06/accelerating-deep-learning-research
370387
Sequences](https://arxiv.org/abs/1801.10198)
371388
* [Image Transformer](https://arxiv.org/abs/1802.05751)
372389
* [Training Tips for the Transformer Model](https://arxiv.org/abs/1804.00247)
390+
* [Self-Attention with Relative Position Representations](https://arxiv.org/abs/1803.02155)
391+
* [Fast Decoding in Sequence Models using Discrete Latent Variables](https://arxiv.org/abs/1803.03382)
373392

374393
*Note: This is not an official Google product.*

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='tensor2tensor',
8-
version='1.5.5',
8+
version='1.5.6',
99
description='Tensor2Tensor',
1010
author='Google Inc.',
1111
author_email='[email protected]',

tensor2tensor/bin/t2t_avg_all.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from __future__ import print_function
2020

2121
from collections import deque
22-
import logging
2322
import os
2423
import shutil
2524

@@ -45,8 +44,6 @@
4544

4645

4746
def main(_):
48-
tf.logging._handler.setFormatter( # pylint: disable=protected-access
49-
logging.Formatter("%(asctime)s:" + logging.BASIC_FORMAT, None))
5047
tf.logging.set_verbosity(tf.logging.INFO)
5148

5249
model_dir = os.path.expanduser(FLAGS.model_dir)
@@ -56,7 +53,8 @@ def main(_):
5653
# Copy flags.txt with the original time, so t2t-bleu can report correct
5754
# relative time.
5855
tf.gfile.MakeDirs(FLAGS.output_dir)
59-
if not os.path.exists(os.path.join(output_dir, "flags.txt")):
56+
if (not os.path.exists(os.path.join(output_dir, "flags.txt")) and
57+
os.path.exists(os.path.join(model_dir, "flags.txt"))):
6058
shutil.copy2(os.path.join(model_dir, "flags.txt"),
6159
os.path.join(output_dir, "flags.txt"))
6260

tensor2tensor/bin/t2t_bleu.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def main(_):
113113
transl_dir = os.path.expanduser(FLAGS.translations_dir)
114114
if not os.path.exists(transl_dir):
115115
exit_time = time.time() + FLAGS.wait_minutes * 60
116-
tf.logging.info("Translation dir %s does not exist, waiting till %s."
117-
% (transl_dir, time.asctime(time.localtime(exit_time))))
116+
tf.logging.info("Translation dir %s does not exist, waiting till %s.",
117+
transl_dir, time.asctime(time.localtime(exit_time)))
118118
while not os.path.exists(transl_dir):
119119
time.sleep(10)
120120
if time.time() > exit_time:

tensor2tensor/bin/t2t_decoder.py

+73
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
# Dependency imports
3838

3939
from tensor2tensor.bin import t2t_trainer
40+
from tensor2tensor.data_generators import text_encoder
4041
from tensor2tensor.utils import decoding
42+
from tensor2tensor.utils import registry
4143
from tensor2tensor.utils import trainer_lib
4244
from tensor2tensor.utils import usr_dir
4345

@@ -59,6 +61,8 @@
5961
flags.DEFINE_bool("decode_interactive", False,
6062
"Interactive local inference mode.")
6163
flags.DEFINE_integer("decode_shards", 1, "Number of decoding replicas.")
64+
flags.DEFINE_string("score_file", "", "File to score. Each line in the file "
65+
"must be in the format input \t target.")
6266

6367

6468
def create_hparams():
@@ -96,11 +100,80 @@ def decode(estimator, hparams, decode_hp):
96100
dataset_split="test" if FLAGS.eval_use_test_set else None)
97101

98102

103+
def score_file(filename):
104+
"""Score each line in a file and return the scores."""
105+
# Prepare model.
106+
hparams = create_hparams()
107+
encoders = registry.problem(FLAGS.problems).feature_encoders(FLAGS.data_dir)
108+
has_inputs = "inputs" in encoders
109+
110+
# Prepare features for feeding into the model.
111+
if has_inputs:
112+
inputs_ph = tf.placeholder(dtype=tf.int32) # Just length dimension.
113+
batch_inputs = tf.reshape(inputs_ph, [1, -1, 1, 1]) # Make it 4D.
114+
targets_ph = tf.placeholder(dtype=tf.int32) # Just length dimension.
115+
batch_targets = tf.reshape(targets_ph, [1, -1, 1, 1]) # Make it 4D.
116+
features = {
117+
"inputs": batch_inputs,
118+
"targets": batch_targets,
119+
} if has_inputs else {"targets": batch_targets}
120+
121+
# Prepare the model and the graph when model runs on features.
122+
model = registry.model(FLAGS.model)(hparams, tf.estimator.ModeKeys.EVAL)
123+
_, losses = model(features)
124+
saver = tf.train.Saver()
125+
126+
with tf.Session() as sess:
127+
# Load weights from checkpoint.
128+
ckpts = tf.train.get_checkpoint_state(FLAGS.output_dir)
129+
ckpt = ckpts.model_checkpoint_path
130+
saver.restore(sess, ckpt)
131+
# Run on each line.
132+
results = []
133+
for line in open(filename):
134+
tab_split = line.split("\t")
135+
if len(tab_split) > 2:
136+
raise ValueError("Each line must have at most one tab separator.")
137+
if len(tab_split) == 1:
138+
targets = tab_split[0].strip()
139+
else:
140+
targets = tab_split[1].strip()
141+
inputs = tab_split[0].strip()
142+
# Run encoders and append EOS symbol.
143+
targets_numpy = encoders["targets"].encode(
144+
targets) + [text_encoder.EOS_ID]
145+
if has_inputs:
146+
inputs_numpy = encoders["inputs"].encode(inputs) + [text_encoder.EOS_ID]
147+
# Prepare the feed.
148+
feed = {
149+
inputs_ph: inputs_numpy,
150+
targets_ph: targets_numpy
151+
} if has_inputs else {targets_ph: targets_numpy}
152+
# Get the score.
153+
np_loss = sess.run(losses["training"], feed)
154+
results.append(np_loss)
155+
return results
156+
157+
99158
def main(_):
100159
tf.logging.set_verbosity(tf.logging.INFO)
160+
trainer_lib.set_random_seed(FLAGS.random_seed)
101161
usr_dir.import_usr_dir(FLAGS.t2t_usr_dir)
102162
FLAGS.use_tpu = False # decoding not supported on TPU
103163

164+
if FLAGS.score_file:
165+
filename = os.path.expanduser(FLAGS.score_file)
166+
if not tf.gfile.Exists(filename):
167+
raise ValueError("The file to score doesn't exist: %s" % filename)
168+
results = score_file(filename)
169+
if not FLAGS.decode_to_file:
170+
raise ValueError("To score a file, specify --decode_to_file for results.")
171+
write_file = open(os.path.expanduser(FLAGS.decode_to_file), "w")
172+
for score in results:
173+
write_file.write("%.6f\n" % score)
174+
write_file.close()
175+
return
176+
104177
hp = create_hparams()
105178
decode_hp = create_decode_hparams()
106179

tensor2tensor/bin/t2t_trainer.py

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def create_run_config(hp):
178178
save_ckpt_secs = FLAGS.save_checkpoints_secs or None
179179
if save_ckpt_secs:
180180
save_ckpt_steps = None
181+
assert FLAGS.output_dir
181182
return trainer_lib.create_run_config(
182183
model_dir=os.path.expanduser(FLAGS.output_dir),
183184
master=FLAGS.master,

tensor2tensor/data_generators/all_problems.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from tensor2tensor.data_generators import problem_hparams
4242
from tensor2tensor.data_generators import ptb
4343
from tensor2tensor.data_generators import snli
44+
from tensor2tensor.data_generators import squad
4445
from tensor2tensor.data_generators import translate_encs
4546
from tensor2tensor.data_generators import translate_ende
4647
from tensor2tensor.data_generators import translate_enfr

tensor2tensor/data_generators/celeba.py

+37
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,43 @@ def generate_data(self, data_dir, tmp_dir, task_id=-1):
149149
self.dev_filepaths(data_dir, self.dev_shards, shuffled=False))
150150

151151

152+
@registry.register_problem
153+
class ImageCelebaMultiResolution(ImageCeleba):
154+
"""CelebA at multiple resolutions.
155+
156+
The resolutions are specified as a hyperparameter during preprocessing.
157+
"""
158+
159+
def dataset_filename(self):
160+
return "image_celeba"
161+
162+
def preprocess_example(self, example, mode, hparams):
163+
image = example["inputs"]
164+
if hasattr(hparams, "resize_method"):
165+
method = getattr(tf.image.ResizeMethod, hparams.resize_method)
166+
else: # default
167+
method = tf.image.ResizeMethod.BICUBIC
168+
169+
# Remove boundaries in CelebA images. Remove 40 pixels each side
170+
# vertically and 20 pixels each side horizontally.
171+
image = tf.image.crop_to_bounding_box(image, 40, 20, 218 - 80, 178 - 40)
172+
173+
scaled_images = image_utils.make_multiscale(
174+
image, hparams.resolutions,
175+
resize_method=method, num_channels=self.num_channels)
176+
177+
# Pack tuple of scaled images into one tensor. We do this by enforcing the
178+
# columns to match for every resolution.
179+
highest_res = hparams.resolutions[-1]
180+
example["inputs"] = image
181+
example["targets"] = tf.concat([
182+
tf.reshape(scaled_image,
183+
[res**2 // highest_res, highest_res, self.num_channels])
184+
for scaled_image, res in zip(scaled_images, hparams.resolutions)],
185+
axis=0)
186+
return example
187+
188+
152189
@registry.register_problem
153190
class Img2imgCeleba(ImageCeleba):
154191
"""8px to 32px problem."""

tensor2tensor/data_generators/generator_utils.py

+19-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from __future__ import division
2020
from __future__ import print_function
2121

22-
from collections import defaultdict
2322
import gzip
2423
import os
2524
import random
@@ -34,7 +33,6 @@
3433
import six.moves.urllib_request as urllib # Imports urllib on Python2, urllib.request on Python3
3534

3635
from tensor2tensor.data_generators import text_encoder
37-
from tensor2tensor.data_generators import tokenizer
3836

3937
import tensorflow as tf
4038

@@ -299,40 +297,41 @@ def gunzip_file(gz_path, new_path):
299297

300298

301299
def get_or_generate_vocab_inner(data_dir, vocab_filename, vocab_size,
302-
generator):
300+
generator, max_subtoken_length=None,
301+
reserved_tokens=None):
303302
"""Inner implementation for vocab generators.
304303
305304
Args:
306305
data_dir: The base directory where data and vocab files are stored. If None,
307-
then do not save the vocab even if it doesn't exist.
306+
then do not save the vocab even if it doesn't exist.
308307
vocab_filename: relative filename where vocab file is stored
309308
vocab_size: target size of the vocabulary constructed by SubwordTextEncoder
310309
generator: a generator that produces tokens from the vocabulary
310+
max_subtoken_length: an optional integer. Set this to a finite value to
311+
avoid quadratic costs during vocab building.
312+
reserved_tokens: List of reserved tokens. `text_encoder.RESERVED_TOKENS`
313+
should be a prefix of `reserved_tokens`. If `None`, defaults to
314+
`RESERVED_TOKENS`.
311315
312316
Returns:
313317
A SubwordTextEncoder vocabulary object.
314318
"""
315-
if data_dir is None:
316-
vocab_filepath = None
317-
else:
319+
if data_dir and vocab_filename:
318320
vocab_filepath = os.path.join(data_dir, vocab_filename)
319-
320-
if vocab_filepath is not None and tf.gfile.Exists(vocab_filepath):
321-
tf.logging.info("Found vocab file: %s", vocab_filepath)
322-
vocab = text_encoder.SubwordTextEncoder(vocab_filepath)
323-
return vocab
321+
if tf.gfile.Exists(vocab_filepath):
322+
tf.logging.info("Found vocab file: %s", vocab_filepath)
323+
return text_encoder.SubwordTextEncoder(vocab_filepath)
324+
else:
325+
vocab_filepath = None
324326

325327
tf.logging.info("Generating vocab file: %s", vocab_filepath)
326-
token_counts = defaultdict(int)
327-
for item in generator:
328-
for tok in tokenizer.encode(text_encoder.native_to_unicode(item)):
329-
token_counts[tok] += 1
330-
331-
vocab = text_encoder.SubwordTextEncoder.build_to_target_size(
332-
vocab_size, token_counts, 1, 1e3)
328+
vocab = text_encoder.SubwordTextEncoder.build_from_generator(
329+
generator, vocab_size, max_subtoken_length=max_subtoken_length,
330+
reserved_tokens=reserved_tokens)
333331

334-
if vocab_filepath is not None:
332+
if vocab_filepath:
335333
vocab.store_to_file(vocab_filepath)
334+
336335
return vocab
337336

338337

@@ -368,7 +367,6 @@ def generate():
368367
gunzip_file(filepath, new_filepath)
369368
filepath = new_filepath
370369

371-
# Use Tokenizer to count the word occurrences.
372370
with tf.gfile.GFile(filepath, mode="r") as source_file:
373371
file_byte_budget_ = file_byte_budget
374372
counter = 0

0 commit comments

Comments
 (0)