Pads a tensor with identity values up to :obj:`max_sequence_length`. Args: x: A ``tf.Tensor`` of shape ``[batch_size, time, depth]``. sequence_length: The true sequence length of :obj:`x`. max_sequence_length: The sequence length up to which the tensor must contain :obj
(x, sequence_length, max_sequence_length, identity_values=0, maxlen=None)
| 30 | |
| 31 | |
| 32 | def pad_with_identity(x, sequence_length, max_sequence_length, identity_values=0, maxlen=None): |
| 33 | """Pads a tensor with identity values up to :obj:`max_sequence_length`. |
| 34 | Args: |
| 35 | x: A ``tf.Tensor`` of shape ``[batch_size, time, depth]``. |
| 36 | sequence_length: The true sequence length of :obj:`x`. |
| 37 | max_sequence_length: The sequence length up to which the tensor must contain |
| 38 | :obj:`identity values`. |
| 39 | identity_values: The identity value. |
| 40 | maxlen: Size of the output time dimension. Default is the maximum value in |
| 41 | obj:`max_sequence_length`. |
| 42 | Returns: |
| 43 | A ``tf.Tensor`` of shape ``[batch_size, maxlen, depth]``. |
| 44 | """ |
| 45 | if maxlen is None: |
| 46 | maxlen = tf.reduce_max(max_sequence_length) |
| 47 | |
| 48 | mask = tf.sequence_mask(sequence_length, maxlen=maxlen, dtype=x.dtype) |
| 49 | mask = tf.expand_dims(mask, axis=-1) |
| 50 | mask_combined = tf.sequence_mask( |
| 51 | max_sequence_length, maxlen=maxlen, dtype=x.dtype) |
| 52 | mask_combined = tf.expand_dims(mask_combined, axis=-1) |
| 53 | |
| 54 | identity_mask = mask_combined * (1.0 - mask) |
| 55 | |
| 56 | x = pad_in_time(x, maxlen - tf.shape(x)[1]) |
| 57 | x = x * mask + (identity_mask * identity_values) |
| 58 | |
| 59 | return x |
| 60 | |
| 61 | |
| 62 | def pad_n_with_identity(inputs, sequence_lengths, identity_values=0): |
no test coverage detected