How to get multiple translations for a sentence (beam search?)

After training, I would like to get multiple translations for a sentence.
I am using opennmt-tf, and the config file is given at the end of this post.

The command I used to get translation is:
onmt-main infer --config config.yml data.yml --features_file test.txt --predictions_file translations.out

In the documentation :,
-beam_size and -n_best are mentioned, but I am confused where to put these two arguments in the config file. There is already beam_width: 5 in the section params.


Default OpenNMT parameters from the Lua version.

optimizer: AdamOptimizer
beta1: 0.9
beta2: 0.998
learning_rate: 0.0001
param_init: 0.1
clip_gradients: 5.0
decay_type: exponential_decay
decay_rate: 0.85
decay_steps: 10000
start_decay_steps: 10000
beam_width: 5
maximum_iterations: 2500

batch_size: 100
bucket_width: 1
save_checkpoints_steps: 5000
save_summary_steps: 100
train_steps: 1000000
maximum_features_length: 30
maximum_labels_length: 30

Consider setting this to -1 to match the number of training examples.

sample_buffer_size: -1

eval_delay: 3600 # Every 1 hours.

batch_size: 30

You can add the n_best option under the infer section. See:

1 Like

Thank you. By the way, does beam_width: 5 under the param section has relation with n_best under infer section? For example, if beam_width = 1, does n_best = 10 is meaningful?

An error is thown if n_best is greater than beam_width.

1 Like

I set beam_width: 50 under section params
During training, everything is fine.
However, during inference, I got error as shown at the end.
I then make the file in test.txt in --features_file test.txt to contain one line.
The error is still there. Then I change beam_width to 13 and it works.
But for different sentences, the maximal values for beam_width to work during inference are different. This cause me problems because I want test.txt to contain several sentences and just launch the inference once.

What is the right way to get several (a fixed number) translations for each sentence in one inference command. (If it can only find fewer translations than specified for a particular sentence, just output something like None).

Error message:

INFO:tensorflow:Done running local_init_op.
2018-06-08 05:00:49.064659: W tensorflow/core/framework/] OP_REQUIRES failed at : Invalid argument: flat indices[233, :] = [12, 0, 19] does not index into param (shape: [14,1,18,7]).
Traceback (most recent call last):
File “/home/shieh/Applications/miniconda3/envs/deep-learning-base/lib/python3.6/site-packages/tensorflow/python/client/”, line 1322, in _do_call
return fn(*args)
File “/home/shieh/Applications/miniconda3/envs/deep-learning-base/lib/python3.6/site-packages/tensorflow/python/client/”, line 1307, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File “/home/shieh/Applications/miniconda3/envs/deep-learning-base/lib/python3.6/site-packages/tensorflow/python/client/”, line 1409, in _call_tf_sessionrun
tensorflow.python.framework.errors_impl.InvalidArgumentError: flat indices[233, :] = [12, 0, 19] does not index into param (shape: [14,1,18,7]).
[[Node: seq2seq/decoder_1/decoder_1/GatherNd = GatherNd[Tindices=DT_INT32, Tparams=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](seq2seq/decoder_1/decoder_1/Reshape_2, seq2seq/decoder_1/decoder_1/stack)]]

During handling of the above exception, another exception occurred:

See this issue: