Issue when trying to decode a file that was not part of the training
When I try decode a file that was not part of the training/testing set, the following error occurs:
INFO:tensorflow:Performing Decoding from a file.
INFO:tensorflow:Getting sorted inputs
INFO:tensorflow: batch 94
INFO:tensorflow:Deocding batch 0
Traceback (most recent call last):
File "/usr/local/bin/t2t-trainer", line 83, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/usr/local/bin/t2t-trainer", line 79, in main
schedule=FLAGS.schedule)
File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/trainer_utils.py", line 240, in run
run_locally(exp_fn(output_dir))
File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/trainer_utils.py", line 543, in run_locally
decode_from_file(estimator, FLAGS.decode_from_file)
File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/trainer_utils.py", line 645, in decode_from_file
result_iter = estimator.predict(input_fn=input_fn.next, as_iterable=True)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 289, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 590, in predict
as_iterable=as_iterable)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 883, in _infer_model
features = self._get_features_from_input_fn(input_fn)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 863, in _get_features_from_input_fn
result = input_fn()
File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/trainer_utils.py", line 725, in _decode_batch_input_fn
input_ids = vocabulary.encode(inputs)
File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/data_generators/text_encoder.py", line 132, in encode
ret = [self._token_to_id[tok] for tok in sentence.strip().split()]
KeyError: '@-@'
The decoding command I use is as follows:
PROBLEM=wmt_ende_bpe32k
MODEL=transformer
HPARAMS=transformer_base
DATA_DIR=$HOME/t2t_data
TMP_DIR=/tmp/t2t_datagen
TRAIN_DIR=$HOME/t2t_train/$PROBLEM/$MODEL-$HPARAMS
BEAM_SIZE=4
ALPHA=0.6
t2t-trainer \
--data_dir=$DATA_DIR \
--problems=$PROBLEM \
--model=$MODEL \
--hparams_set=$HPARAMS \
--output_dir=$TRAIN_DIR \
--train_steps=0 \
--eval_steps=0 \
--decode_beam_size=$BEAM_SIZE \
--decode_alpha=$ALPHA \
--decode_from_file /tmp/t2t_datagen/newsdev2016.bpe.en
Now, I got it. The problem was that the new file to be translated had not been preprocessed with the .bpe file used for training. After running the .bpe over it, it's working!
Thanks for finding the fix!
Hi @lukaszkaiser , @mehmedes I found this "issue", too. I understand that this is not a bug, because you might be able to guarantee the processed training data doesn't contain any OOV word. But I think it would be more flexible if (As I said in this issue):
- More detailed README to make users easier to run experiments without triggering such exception.
- Or, rewriting the codes a little bit, like,
def encode(self, sentence):
"""Converts a space-separated string of tokens to a list of ids."""
ret = [self._token_to_id[tok] if tok in self._token_to_id \
else self._token_to_id['UNK'] for tok in sentence.strip().split()]
return ret[::-1] if self._reverse else ret
to avoid this exception.
Thank you so much
Is this BPE-only or does it happen with tokens_32k too? We would prefer not to build any special support for external BPE as it's hard to maintain (needs perl scripts) and isn't invertible.
Hi @lukaszkaiser
Thank you.
If I ran the bulit-in problems such as tokens_32k or bpe_32k, it works well without corruption.
What I want to say is that, if a user want to use his/her own vocabulary, and the training data contains OOVs, it might raise this exception.
I haven't tried to use my own vocabularies to run the token_32k experiment, but I think there might be such problems, too. I'll try it and let you know if any progress.
We try to include every character in the vocabulary in "tokens_XX" -- so hopefully it should handle at least all words composed of characters that appear in the training data. Please reopen if you see the problem again!