KerasRS / API documentation / Embedding Layers / EmbedReduce layer

EmbedReduce layer

[source]

EmbedReduce class

keras_rs.layers.EmbedReduce(
    input_dim: int,
    output_dim: int,
    embeddings_initializer: Union[
        str,
        keras.src.initializers.initializer.Initializer,
        Type[keras.src.initializers.initializer.Initializer],
        Callable[[Sequence[Optional[int]], str], Any],
        Any,
    ] = "uniform",
    embeddings_regularizer: Union[
        str,
        keras.src.regularizers.regularizers.Regularizer,
        Type[keras.src.regularizers.regularizers.Regularizer],
        Callable[[Any], Any],
        NoneType,
    ] = None,
    embeddings_constraint: Union[
        str,
        keras.src.constraints.constraints.Constraint,
        Type[keras.src.constraints.constraints.Constraint],
        Callable[[Any], Any],
        NoneType,
    ] = None,
    mask_zero: bool = False,
    weights: Any = None,
    combiner: str = "mean",
    **kwargs: Any
)

An embedding layer that reduces with a combiner.

This layer embeds inputs and then applies a reduction to combine a set of embeddings into a single embedding. This is typically used to embed a sequence of items as a single embedding.

If the inputs passed to __call__ are 1D, no reduction is applied. If the inputs are 2D, dimension 1 is reduced using the combiner so that the result is of shape (batch_size, output_dim). Inputs of rank 3 and higher are not allowed. Weights can optionally be passed to the __call__ method to apply weights to different samples before reduction.

This layer supports sparse inputs and ragged inputs with backends that support them. The output after reduction is dense. For ragged inputs, the ragged dimension must be 1 as it is the dimension that is reduced.

Arguments

  • input_dim: Integer. Size of the vocabulary, maximum integer index + 1.
  • output_dim: Integer. Dimension of the dense embedding.
  • embeddings_initializer: Initializer for the embeddings matrix (see keras.initializers).
  • embeddings_regularizer: Regularizer function applied to the embeddings matrix (see keras.regularizers).
  • embeddings_constraint: Constraint function applied to the embeddings matrix (see keras.constraints).
  • mask_zero: Boolean, whether or not the input value 0 is a special "padding" value that should be masked out. This is useful when using recurrent layers which may take variable length input. If this is True, then all subsequent layers in the model need to support masking or an exception will be raised. If mask_zero is set to True, as a consequence, index 0 cannot be used in the vocabulary (input_dim should equal size of vocabulary + 1).
  • weights: Optional floating-point matrix of size (input_dim, output_dim). The initial embeddings values to use.
  • combiner: Specifies how to reduce if there are multiple entries in a single row. Currently mean, sqrtn and sum are supported. mean is the default. sqrtn often achieves good accuracy, in particular with bag-of-words columns.
  • **kwargs: Additional keyword arguments passed to Embedding.

[source]

call method

EmbedReduce.call(inputs: Any, weights: Optional[Any] = None)

Apply embedding and reduction.

Arguments

  • inputs: 1D tensor to embed or 2D tensor to embed and reduce.
  • weights: Optional tensor of weights to apply before reduction, which can be 1D or 2D and must match for the first dimension of inputs (1D case) or match the shape of inputs (2D case).

Returns

A dense 2D tensor of shape (batch_size, output_dim).