tf.keras.layers.experimental.preprocessing.CategoryCrossing

Category crossing layer.

Inherits From: PreprocessingLayer, Layer, Module

This layer concatenates multiple categorical inputs into a single categorical output (similar to Cartesian product). The output dtype is string.

Usage:

inp_1 = ['a', 'b', 'c']
inp_2 = ['d', 'e', 'f']
layer = tf.keras.layers.experimental.preprocessing.CategoryCrossing()
layer([inp_1, inp_2])
<tf.Tensor: shape=(3, 1), dtype=string, numpy=
  array([[b'a_X_d'],
         [b'b_X_e'],
         [b'c_X_f']], dtype=object)>
inp_1 = ['a', 'b', 'c']
inp_2 = ['d', 'e', 'f']
layer = tf.keras.layers.experimental.preprocessing.CategoryCrossing(
   separator='-')
layer([inp_1, inp_2])
<tf.Tensor: shape=(3, 1), dtype=string, numpy=
  array([[b'a-d'],
         [b'b-e'],
         [b'c-f']], dtype=object)>

depth depth of input crossing. By default None, all inputs are crossed into one output. It can also be an int or tuple/list of ints. Passing an integer will create combinations of crossed outputs with depth up to that integer, i.e., [1, 2, ..., depth), and passing a tuple of integers will create crossed outputs with depth for the specified values in the tuple, i.e., depth=(N1, N2) will create all possible crossed outputs with depth equal to N1 or N2. Passing None means a single crossed output with all inputs. For example, with inputs a, b and c, depth=2 means the output will be [a;b;c;cross(a, b);cross(bc);cross(ca)].
separator A string added between each input being joined. Defaults to 'X'.
name Name to give to the layer.
**kwargs Keyword arguments to construct a layer.

Input shape: a list of string or int tensors or sparse tensors of shape [batch_size, d1, ..., dm]

Output shape: a single string or int tensor or sparse tensor of shape [batch_size, d1, ..., dm]

If any input is RaggedTensor, the output is RaggedTensor. Else, if any input is SparseTensor, the output is SparseTensor. Otherwise, the output is Tensor.

Example: (depth=None) If the layer receives three inputs: a=[[1], [4]], b=[[2], [5]], c=[[3], [6]] the output will be a string tensor: [[b'1_X_2_X_3'], [b'4_X_5_X_6']]

Example: (depth is an integer) With the same input above, and if depth=2, the output will be a list of 6 string tensors: [[b'1'], [b'4']] [[b'2'], [b'5']] [[b'3'], [b'6']] [[b'1_X_2'], [b'4_X_5']], [[b'2_X_3'], [b'5_X_6']], [[b'3_X_1'], [b'6_X_4']]

Example: (depth is a tuple/list of integers) With the same input above, and if depth=(2, 3) the output will be a list of 4 string tensors: [[b'1_X_2'], [b'4_X_5']], [[b'2_X_3'], [b'5_X_6']], [[b'3_X_1'], [b'6_X_4']], [[b'1_X_2_X_3'], [b'4_X_5_X_6']]

is_adapted Whether the layer has been fit to data already.
streaming Whether adapt can be called twice without resetting the state.

Methods

adapt

View source

Fits the state of the preprocessing layer to the data being passed.

Arguments
data The data to train on. It can be passed either as a tf.data Dataset, or as a numpy array.
batch_size Integer or None. Number of samples per state update. If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).
steps Integer or None. Total number of steps (batches of samples) When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined. If x is a tf.data dataset, and 'steps' is None, the epoch will run until the input dataset is exhausted. When passing an infinitely repeating dataset, you must specify the steps argument. This argument is not supported with array inputs.
reset_state Optional argument specifying whether to clear the state of the layer at the start of the call to adapt, or whether to start from the existing state. This argument may not be relevant to all preprocessing layers: a subclass of PreprocessingLayer may choose to throw if 'reset_state' is set to False.

compile

View source

Configures the layer for adapt.

Arguments
run_eagerly Bool. Defaults to False. If True, this Model's logic will not be wrapped in a tf.function. Recommended to leave this as None unless your Model cannot be run inside a tf.function. steps_per_execution: Int. Defaults to 1. The number of batches to run during each tf.function call. Running multiple batches inside a single tf.function call can greatly improve performance on TPUs or small models with a large Python overhead.

finalize_state

View source

Finalize the statistics for the preprocessing layer.

This method is called at the end of adapt. This method handles any one-time operations that should occur after all data has been seen.

make_adapt_function

View source

Creates a function to execute one step of adapt.

This method can be overridden to support custom adapt logic. This method is called by PreprocessingLayer.adapt.

Typically, this method directly controls tf.function settings, and delegates the actual state update logic to PreprocessingLayer.update_state.

This function is cached the first time PreprocessingLayer.adapt is called. The cache is cleared whenever PreprocessingLayer.compile is called.

Returns
Function. The function created by this method should accept a tf.data.Iterator, retrieve a batch, and update the state of the layer.

merge_state

View source

Merge the statistics of multiple preprocessing layers.

This layer will contain the merged state.

Arguments
layers Layers whose statistics should be merge with the statistics of this layer.

partial_crossing

View source

Gets the crossed output from a partial list/tuple of inputs.

reset_state

View source

Resets the statistics of the preprocessing layer.

update_state

View source

Accumulates statistics for the preprocessing layer.

Arguments
data A mini-batch of inputs to the layer.