A preprocessing layer which hashes and bins categorical features.
layer_hashing(object, num_bins, mask_value = NULL, salt = NULL, ...)
What to compose the new
Layer instance with. Typically a
Sequential model or a Tensor (e.g., as returned by
The return value depends on
Layer instance is returned.
Sequential model, the model with an additional layer is returned.
a Tensor, the output tensor from
layer_instance(object) is returned.
Number of hash bins. Note that this includes the
so the effective number of bins is
(num_bins - 1) if
A value that represents masked inputs, which are mapped to index 0. Defaults to NULL, meaning no mask term will be added and the hashing will start at index 0.
A single unsigned integer or NULL.
If passed, the hash function used will be SipHash64, with these values
used as an additional input (known as a "salt" in cryptography).
These should be non-zero. Defaults to
NULL (in that
case, the FarmHash64 hash function is used). It also supports
list of 2 unsigned integer numbers, see reference paper for details.
standard layer arguments.
This layer transforms single or multiple categorical inputs to hashed output.
It converts a sequence of int or string to a sequence of int. The stable hash
tensorflow::ops::Fingerprint to produce the same output
consistently across all platforms.
This layer uses FarmHash64 by default, which provides a consistent hashed output across different platforms and is stable across invocations, regardless of device and context, by mixing the input bits thoroughly.
If you want to obfuscate the hashed output, you can also pass a random
argument in the constructor. In that case, the layer will use the
SipHash64 hash function, with
salt value serving as additional input to the hash function.
layer <- layer_hashing(num_bins=3) inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([, # , # , # , # ])>
Example (FarmHash64) with a mask value
layer <- layer_hashing(num_bins=3, mask_value='') inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([, # , # , # , # ])>
layer <- layer_hashing(num_bins=3, salt=c(133, 137)) inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([, # , # , # , # ])>
Example (Siphash64 with a single integer, same as
Other preprocessing layers: