tensor2tensor icon indicating copy to clipboard operation
tensor2tensor copied to clipboard

KeyError: '@@' while translating with BPE

Open mehmedes opened this issue 9 years ago • 3 comments

When translating with the BPE model, T2T produces the following error message:

Traceback (most recent call last):
  File "/usr/local/bin/t2t-trainer", line 82, 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 78, in main
    schedule=FLAGS.schedule)
  File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/trainer_utils.py", line 285, in run
    decode(exp.estimator)
  File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/trainer_utils.py", line 338, in decode
    decoding.decode_from_file(estimator, FLAGS.decode_from_file)
  File "/usr/local/lib/python2.7/dist-packages/tensor2tensor/utils/decoding.py", line 117, in decode_from_file
    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/decoding.py", line 205, 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 174, in encode
    ret = [self._token_to_id[tok] for tok in sentence.strip().split()]
KeyError: '@@'

Do you have any hints? The document to be translated has been tokenized and preprocessed with the bpe file that has been used for training.

The model had been trained on T2T V 1.0.10. After upgrading to the latest release this error occurs. Moreover, continuing training of this model on the latest release also fails...

mehmedes avatar Aug 10 '17 13:08 mehmedes

I think it shouldn't contain the @@ symbol in a BPE setting. Maybe the easiest way is to remove this symbol from the vocabulary? It might encounter such problems in some specific cases. I instead fix this by something like:

ret = [self._token_to_id[tok] if tok in self._token_to_id else 'UNK'
       for tok in sentence.strip().split()]

But it's just my solution (not optimal)

cshanbo avatar Aug 11 '17 06:08 cshanbo

@cshanbo Thank you, I'll try that!

There seems to be an issue with unicode. Chars like ', /, _ , : , ' produce the KeyError. I'm wondering why sometimes translation succeeds but sometimes fails, while those chars appear every time...

mehmedes avatar Aug 11 '17 16:08 mehmedes

The issue was due to how I ran the tokenizer. The following preprocessing for translation works for me:



MOSES=/home/mehmedes/tensor2tensor/processing/moses-scripts

SUB=/home/mehmedes/tensor2tensor/processing/subword-nmt


$MOSES/scripts/tokenizer/tokenizer.perl -q -l $SRC -threads 8 | \
$SUB/apply_bpe.py -c bpe.32000 

mehmedes avatar Aug 12 '17 19:08 mehmedes