Skip to content

sub_string_delim_at_index

SubStringDelimAtIndexLayer ¤

SubStringDelimAtIndexLayer(
    name=None,
    input_dtype=None,
    output_dtype=None,
    delimiter="_",
    index=0,
    default_value="",
    **kwargs
)

Bases: BaseLayer

Layer which splits a string tensor by a delimiter and returns the substring at the specified index. If the delimiter is the empty string, the string is split into bytes/characters. If the index is negative, start counting from the end of the string. If the index is out of bounds, the default value is returned.

Initialise the SubStringDelimAtIndexLayer layer.

Parameters:

Name Type Description Default
name Optional[str]

The name of the layer. Defaults to None.

None
input_dtype Optional[str]

The dtype to cast the input to. Defaults to None.

None
output_dtype Optional[str]

The dtype to cast the output to. Defaults to None.

None
delimiter str

String to split on. Defaults to "_".

'_'
index int

Index of the substring to return. Defaults to 0. If the index is negative, start counting from the end of the string.

0
default_value str

Value to return if index is out of bounds. Defaults to "". Defaults to "".

''
Source code in src/kamae/keras/tensorflow/layers/sub_string_delim_at_index.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def __init__(
    self,
    name: Optional[str] = None,
    input_dtype: Optional[str] = None,
    output_dtype: Optional[str] = None,
    delimiter: str = "_",
    index: int = 0,
    default_value: str = "",
    **kwargs: Any,
) -> None:
    """
    Initialise the SubStringDelimAtIndexLayer layer.

    :param name: The name of the layer. Defaults to `None`.
    :param input_dtype: The dtype to cast the input to. Defaults to `None`.
    :param output_dtype: The dtype to cast the output to. Defaults to `None`.
    :param delimiter: String to split on. Defaults to `"_"`.
    :param index: Index of the substring to return. Defaults to `0`.
    If the index is negative, start counting from the end of the string.
    :param default_value: Value to return if index is out of bounds.
    Defaults to `""`.
    Defaults to `""`.
    """
    super().__init__(
        name=name, input_dtype=input_dtype, output_dtype=output_dtype, **kwargs
    )
    self.delimiter = delimiter
    self.index = index
    self.default_value = default_value

compatible_dtypes property ¤

compatible_dtypes

Returns the compatible dtypes of the layer.

Returns:

Type Description
Optional[List[str]]

The compatible dtypes of the layer.

get_config ¤

get_config()

Returns the config of the SubStringDelimAtIndex layer. Used for saving and loading from a model.

Specifically adds the delimiter, index and default_value to the config.

Returns:

Type Description
Dict[str, Any]

Dictionary of the config of the layer.

Source code in src/kamae/keras/tensorflow/layers/sub_string_delim_at_index.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
def get_config(self) -> Dict[str, Any]:
    """
    Returns the config of the SubStringDelimAtIndex layer.
    Used for saving and loading from a model.

    Specifically adds the `delimiter`, `index` and `default_value` to the config.

    :returns: Dictionary of the config of the layer.
    """
    config = super().get_config()
    config.update(
        {
            "delimiter": self.delimiter,
            "index": self.index,
            "default_value": self.default_value,
        }
    )
    return config

resolve_negative_indices staticmethod ¤

resolve_negative_indices(ragged_tensor, index)

Resolves negative indices to positive indices.

Parameters:

Name Type Description Default
ragged_tensor RaggedTensor

Ragged tensor

required
index int

The index to resolve.

required

Returns:

Type Description
Tensor

The resolved index.

Source code in src/kamae/keras/tensorflow/layers/sub_string_delim_at_index.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@staticmethod
def resolve_negative_indices(
    ragged_tensor: tf.RaggedTensor, index: int
) -> tf.Tensor:
    """
    Resolves negative indices to positive indices.

    :param ragged_tensor: Ragged tensor
    :param index: The index to resolve.
    :returns: The resolved index.
    """
    if index >= 0:
        raise ValueError("Index should be negative to resolve. Got positive index.")
    ragged_row_lengths = ragged_tensor.row_lengths(axis=-1)
    # Positive index is the length of the row + index. So that index = -1
    # resolves to the last dimension
    return tf.math.add(ragged_row_lengths, index)