Skip to content

Commit

Permalink
Explicit reshape dimension in case the alignment vector is empty (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumekln authored Mar 11, 2019
1 parent 1020e97 commit 760eaa6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ OpenNMT-tf follows [semantic versioning 2.0.0](https://semver.org/). The API cov

* Fix compatibility issue with legacy TensorFlow 1.4
* Fix inference of language models
* Fix inference error when using `replace_unknown_target` and the alignment vector was empty

## [1.21.4](https://github.com/OpenNMT/OpenNMT-tf/releases/tag/v1.21.4) (2019-03-07)

Expand Down
6 changes: 4 additions & 2 deletions opennmt/models/sequence_to_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from opennmt.models.model import Model
from opennmt.utils import compat
from opennmt.utils.losses import cross_entropy_sequence_loss
from opennmt.utils.misc import print_bytes, format_translation_output, merge_dict
from opennmt.utils.misc import print_bytes, format_translation_output, merge_dict, shape_list
from opennmt.decoders.decoder import get_sampling_probability


Expand Down Expand Up @@ -277,7 +277,9 @@ def _call(self, features, labels, params, mode):
# Merge batch and beam dimensions.
original_shape = tf.shape(target_tokens)
target_tokens = tf.reshape(target_tokens, [-1, original_shape[-1]])
attention = tf.reshape(alignment, [-1, tf.shape(alignment)[2], tf.shape(alignment)[3]])
align_shape = shape_list(alignment)
attention = tf.reshape(
alignment, [align_shape[0] * align_shape[1], align_shape[2], align_shape[3]])
# We don't have attention for </s> but ensure that the attention time dimension matches
# the tokens time dimension.
attention = reducer.align_in_time(attention, tf.shape(target_tokens)[1])
Expand Down

0 comments on commit 760eaa6

Please sign in to comment.