layer_text_vectorization {keras} | R Documentation |
A preprocessing layer which maps text features to integer sequences.
Description
A preprocessing layer which maps text features to integer sequences.
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,
...
)
get_vocabulary(object, include_special_tokens = TRUE)
set_vocabulary(object, vocabulary, idf_weights = NULL, ...)
Arguments
object |
What to compose the new
|
max_tokens |
The maximum size of the vocabulary for this layer. If NULL,
there is no cap on the size of the vocabulary. Note that this vocabulary
contains 1 OOV token, so the effective number of tokens is |
standardize |
Optional specification for standardization to apply to the
input text. Values can be NULL (no standardization),
|
split |
Optional specification for splitting the input text. Values can be
NULL (no splitting), |
ngrams |
Optional specification for ngrams to create from the possibly-split input text. Values can be NULL, an integer or list of integers; passing an integer will create ngrams up to that integer, and passing a list of integers will create ngrams for the specified values in the list. Passing NULL means that no ngrams will be created. |
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 for |
... |
standard layer arguments. |
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. |
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. |
Details
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).
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
tf$keras$utils$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 ofmatrix(c("string to split", "another string to split"))
, the Callable will seec("string to split", "another string to split")
. The callable should return a Tensor 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"))
. This makes the callable site natively compatible withtf$strings$split()
.
See Also
-
https://www.tensorflow.org/api_docs/python/tf/keras/layers/TextVectorization
-
https://keras.io/api/layers/preprocessing_layers/text/text_vectorization
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()