layer_text_vectorization {keras3} | R Documentation |
A preprocessing layer which maps text features to integer sequences.
Description
This layer has basic options for managing text in a Keras model. It
transforms a batch of strings (one example = one string) into either a list
of token indices (one example = 1D tensor of integer token indices) or a
dense representation (one example = 1D tensor of float values representing
data about the example's tokens). This layer is meant to handle natural
language inputs. To handle simple string inputs (categorical strings or
pre-tokenized strings) see layer_string_lookup()
.
The vocabulary for the layer must be either supplied on construction or
learned via adapt()
. When this layer is adapted, it will analyze the
dataset, determine the frequency of individual string values, and create a
vocabulary from them. This vocabulary can have unlimited size or be capped,
depending on the configuration options for this layer; if there are more
unique values in the input than the maximum vocabulary size, the most
frequent terms will be used to create the vocabulary.
The processing of each example contains the following steps:
Standardize each example (usually lowercasing + punctuation stripping)
Split each example into substrings (usually words)
Recombine substrings into tokens (usually ngrams)
Index tokens (associate a unique int value with each token)
Transform each example using this index, either into a vector of ints or a dense float vector.
Some notes on passing callables to customize splitting and normalization for this layer:
Any callable can be passed to this Layer, but if you want to serialize this object you should only pass functions that are registered Keras serializables (see
register_keras_serializable()
for more details).When using a custom callable for
standardize
, the data received by the callable will be exactly as passed to this layer. The callable should return a tensor of the same shape as the input.When using a custom callable for
split
, the data received by the callable will have the 1st dimension squeezed out - instead oflist("string to split", "another string to split")
, the Callable will seec("string to split", "another string to split")
. The callable should return atf.Tensor
of dtypestring
with the first dimension containing the split tokens - in this example, we should see something likelist(c("string", "to", "split"), c("another", "string", "to", "split"))
.
Note: This layer uses TensorFlow internally. It cannot be used as part of the compiled computation graph of a model with any backend other than TensorFlow. It can however be used with any backend when running eagerly. It can also always be used as part of an input preprocessing pipeline with any backend (outside the model itself), which is how we recommend to use this layer.
Note: This layer is safe to use inside a tf.data
pipeline
(independently of which backend you're using).
Usage
layer_text_vectorization(
object,
max_tokens = NULL,
standardize = "lower_and_strip_punctuation",
split = "whitespace",
ngrams = NULL,
output_mode = "int",
output_sequence_length = NULL,
pad_to_max_tokens = FALSE,
vocabulary = NULL,
idf_weights = NULL,
sparse = FALSE,
ragged = FALSE,
encoding = "utf-8",
name = NULL,
...
)
get_vocabulary(object, include_special_tokens = TRUE)
set_vocabulary(object, vocabulary, idf_weights = NULL, ...)
Arguments
object |
Object to compose the layer with. A tensor, array, or sequential model. |
max_tokens |
Maximum size of the vocabulary for this layer. This should
only be specified when adapting a vocabulary or when setting
|
standardize |
Optional specification for standardization to apply to the input text. Values can be:
|
split |
Optional specification for splitting the input text. Values can be:
|
ngrams |
Optional specification for ngrams to create from the
possibly-split input text. Values can be |
output_mode |
Optional specification for the output of the layer.
Values can be
|
output_sequence_length |
Only valid in INT mode. If set, the output will
have its time dimension padded or truncated to exactly
|
pad_to_max_tokens |
Only valid in |
vocabulary |
Optional. Either an array of strings or a string path to a
text file. If passing an array, can pass a list, list,
1D NumPy array, or 1D tensor containing the string vocabulary terms.
If passing a file path, the file should contain one line per term
in the vocabulary. If this argument is set,
there is no need to |
idf_weights |
An R vector, 1D numpy array, or 1D tensor of inverse document frequency weights with equal length to vocabulary. Must be set if output_mode is "tf_idf". Should not be set otherwise. |
sparse |
Boolean. Only applicable to |
ragged |
Boolean. Only applicable to |
encoding |
Optional. The text encoding to use to interpret the input
strings. Defaults to |
name |
String, name for the object |
... |
For forward/backward compatability. |
include_special_tokens |
If TRUE, the returned vocabulary will include the padding and OOV tokens, and a term's index in the vocabulary will equal the term's index when calling the layer. If FALSE, the returned vocabulary will not include any padding or OOV tokens. |
Value
The return value depends on the value provided for the first argument.
If object
is:
a
keras_model_sequential()
, then the layer is added to the sequential model (which is modified in place). To enable piping, the sequential model is also returned, invisibly.a
keras_input()
, then the output tensor from callinglayer(input)
is returned.-
NULL
or missing, then aLayer
instance is returned.
Examples
This example instantiates a TextVectorization
layer that lowercases text,
splits on whitespace, strips punctuation, and outputs integer vocab indices.
max_tokens <- 5000 # Maximum vocab size. max_len <- 4 # Sequence length to pad the outputs to. # Create the layer. vectorize_layer <- layer_text_vectorization( max_tokens = max_tokens, output_mode = 'int', output_sequence_length = max_len)
# Now that the vocab layer has been created, call `adapt` on the # list of strings to create the vocabulary. vectorize_layer %>% adapt(c("foo bar", "bar baz", "baz bada boom"))
# Now, the layer can map strings to integers -- you can use an # embedding layer to map these integers to learned embeddings. input_data <- rbind("foo qux bar", "qux baz") vectorize_layer(input_data)
## tf.Tensor( ## [[4 1 3 0] ## [1 2 0 0]], shape=(2, 4), dtype=int64)
This example instantiates a TextVectorization
layer by passing a list
of vocabulary terms to the layer's initialize()
method.
vocab_data <- c("earth", "wind", "and", "fire") max_len <- 4 # Sequence length to pad the outputs to. # Create the layer, passing the vocab directly. You can also pass the # vocabulary arg a path to a file containing one vocabulary word per # line. vectorize_layer <- layer_text_vectorization( max_tokens = max_tokens, output_mode = 'int', output_sequence_length = max_len, vocabulary = vocab_data)
# Because we've passed the vocabulary directly, we don't need to adapt # the layer - the vocabulary is already set. The vocabulary contains the # padding token ('') and OOV token ('[UNK]') # as well as the passed tokens. vectorize_layer %>% get_vocabulary()
## [1] "" "[UNK]" "earth" "wind" "and" "fire"
# ['', '[UNK]', 'earth', 'wind', 'and', 'fire']
See Also
Other preprocessing layers:
layer_category_encoding()
layer_center_crop()
layer_discretization()
layer_feature_space()
layer_hashed_crossing()
layer_hashing()
layer_integer_lookup()
layer_mel_spectrogram()
layer_normalization()
layer_random_brightness()
layer_random_contrast()
layer_random_crop()
layer_random_flip()
layer_random_rotation()
layer_random_translation()
layer_random_zoom()
layer_rescaling()
layer_resizing()
layer_string_lookup()
Other layers:
Layer()
layer_activation()
layer_activation_elu()
layer_activation_leaky_relu()
layer_activation_parametric_relu()
layer_activation_relu()
layer_activation_softmax()
layer_activity_regularization()
layer_add()
layer_additive_attention()
layer_alpha_dropout()
layer_attention()
layer_average()
layer_average_pooling_1d()
layer_average_pooling_2d()
layer_average_pooling_3d()
layer_batch_normalization()
layer_bidirectional()
layer_category_encoding()
layer_center_crop()
layer_concatenate()
layer_conv_1d()
layer_conv_1d_transpose()
layer_conv_2d()
layer_conv_2d_transpose()
layer_conv_3d()
layer_conv_3d_transpose()
layer_conv_lstm_1d()
layer_conv_lstm_2d()
layer_conv_lstm_3d()
layer_cropping_1d()
layer_cropping_2d()
layer_cropping_3d()
layer_dense()
layer_depthwise_conv_1d()
layer_depthwise_conv_2d()
layer_discretization()
layer_dot()
layer_dropout()
layer_einsum_dense()
layer_embedding()
layer_feature_space()
layer_flatten()
layer_flax_module_wrapper()
layer_gaussian_dropout()
layer_gaussian_noise()
layer_global_average_pooling_1d()
layer_global_average_pooling_2d()
layer_global_average_pooling_3d()
layer_global_max_pooling_1d()
layer_global_max_pooling_2d()
layer_global_max_pooling_3d()
layer_group_normalization()
layer_group_query_attention()
layer_gru()
layer_hashed_crossing()
layer_hashing()
layer_identity()
layer_integer_lookup()
layer_jax_model_wrapper()
layer_lambda()
layer_layer_normalization()
layer_lstm()
layer_masking()
layer_max_pooling_1d()
layer_max_pooling_2d()
layer_max_pooling_3d()
layer_maximum()
layer_mel_spectrogram()
layer_minimum()
layer_multi_head_attention()
layer_multiply()
layer_normalization()
layer_permute()
layer_random_brightness()
layer_random_contrast()
layer_random_crop()
layer_random_flip()
layer_random_rotation()
layer_random_translation()
layer_random_zoom()
layer_repeat_vector()
layer_rescaling()
layer_reshape()
layer_resizing()
layer_rnn()
layer_separable_conv_1d()
layer_separable_conv_2d()
layer_simple_rnn()
layer_spatial_dropout_1d()
layer_spatial_dropout_2d()
layer_spatial_dropout_3d()
layer_spectral_normalization()
layer_string_lookup()
layer_subtract()
layer_tfsm()
layer_time_distributed()
layer_torch_module_wrapper()
layer_unit_normalization()
layer_upsampling_1d()
layer_upsampling_2d()
layer_upsampling_3d()
layer_zero_padding_1d()
layer_zero_padding_2d()
layer_zero_padding_3d()
rnn_cell_gru()
rnn_cell_lstm()
rnn_cell_simple()
rnn_cells_stack()