Exemple #1
0
def _ragged_as_leaf_node(
        ragged_tensor: tf.RaggedTensor, is_repeated: bool,
        reference_ragged_tensor: tf.RaggedTensor,
        options: calculate_options.Options) -> prensor.LeafNodeTensor:
    """Creates a ragged tensor as a leaf node."""
    assertions = []
    size_dim = tf.compat.dimension_at_index(ragged_tensor.shape, 0).value
    reference_size_dim = tf.compat.dimension_at_index(
        reference_ragged_tensor.shape, 0).value
    if (size_dim is not None and reference_size_dim is not None):
        if size_dim != reference_size_dim:
            raise ValueError("Returned ragged tensor is not the right size.")
    elif options.ragged_checks:
        assertions.append(
            tf.assert_equal(ragged_tensor.nrows(),
                            reference_ragged_tensor.nrows()))

    if not is_repeated:
        rowids = ragged_tensor.value_rowids()
        if options.ragged_checks:
            assertions.append(
                tf.compat.v1.assert_positive(rowids[1:] - rowids[:-1]))
    if assertions:
        with tf.control_dependencies(assertions):
            parent_index = ragged_tensor.value_rowids()
            return prensor.LeafNodeTensor(parent_index, ragged_tensor.values,
                                          is_repeated)
    else:
        parent_index = ragged_tensor.value_rowids()
        return prensor.LeafNodeTensor(parent_index, ragged_tensor.values,
                                      is_repeated)
Exemple #2
0
def pad_sequence_left(sequences_batch: tf.RaggedTensor, mask: bool):
    """ Pad sequences with zeros on left side """

    # Truncate rows to have at most `settings.SEQUENCE_LENGTH` items
    sequences_batch = sequences_batch[:,-settings.settings.sequence_length:]

    if mask:
        # Add one to indices, to reserve 0 index for padding
        sequences_batch += 1

    pad_row_lengths = settings.settings.sequence_length - sequences_batch.row_lengths()
    pad_values = tf.zeros( [(settings.settings.sequence_length * sequences_batch.nrows()) - tf.size(sequences_batch, tf.int64)], 
        sequences_batch.dtype)
    padding = tf.RaggedTensor.from_row_lengths(pad_values, pad_row_lengths)
    return tf.concat([padding, sequences_batch], axis=1).to_tensor()