tf.keras.layers.Hashing
Stay organized with collections
Save and categorize content based on your preferences.
A preprocessing layer which hashes and bins categorical features.
Inherits From: Layer
, Module
tf.keras.layers.Hashing(
num_bins, mask_value=None, salt=None, **kwargs
)
This layer transforms categorical inputs to hashed output. It element-wise
converts a ints or strings to ints in a fixed range. The stable hash
function uses 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 salt
argument in the constructor. In that case, the layer will use the
SipHash64 hash function, with
the salt
value serving as additional input to the hash function.
For an overview and full list of preprocessing layers, see the preprocessing
guide.
Example (FarmHash64)
layer = tf.keras.layers.Hashing(num_bins=3)
inp = [['A'], ['B'], ['C'], ['D'], ['E']]
layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
array([[1],
[0],
[1],
[1],
[2]])>
Example (FarmHash64) with a mask value
layer = tf.keras.layers.Hashing(num_bins=3, mask_value='')
inp = [['A'], ['B'], [''], ['C'], ['D']]
layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
array([[1],
[1],
[0],
[2],
[2]])>
Example (SipHash64)
layer = tf.keras.layers.Hashing(num_bins=3, salt=[133, 137])
inp = [['A'], ['B'], ['C'], ['D'], ['E']]
layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
array([[1],
[2],
[1],
[0],
[2]])>
Example (Siphash64 with a single integer, same as salt=[133, 133]
)
layer = tf.keras.layers.Hashing(num_bins=3, salt=133)
inp = [['A'], ['B'], ['C'], ['D'], ['E']]
layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
array([[0],
[0],
[2],
[1],
[0]])>
Args |
num_bins
|
Number of hash bins. Note that this includes the mask_value bin,
so the effective number of bins is (num_bins - 1) if mask_value is
set.
|
mask_value
|
A value that represents masked inputs, which are mapped to
index 0. Defaults to None, meaning no mask term will be added and the
hashing will start at index 0.
|
salt
|
A single unsigned integer or None.
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 None (in that
case, the FarmHash64 hash function is used). It also supports
tuple/list of 2 unsigned integer numbers, see reference paper for details.
|
**kwargs
|
Keyword arguments to construct a layer.
|
|
A single or list of string, int32 or int64 Tensor ,
SparseTensor or RaggedTensor of shape (batch_size, ...,)
|
Output shape |
An int64 Tensor , SparseTensor or RaggedTensor of shape
(batch_size, ...) . If any input is RaggedTensor then output is
RaggedTensor , otherwise if any input is SparseTensor then output is
SparseTensor , otherwise the output is Tensor .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2022-09-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-09-07 UTC."],[],[],null,["# tf.keras.layers.Hashing\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.7.0/keras/layers/preprocessing/hashing.py#L27-L206) |\n\nA preprocessing layer which hashes and bins categorical features.\n\nInherits From: [`Layer`](../../../tf/keras/layers/Layer), [`Module`](../../../tf/Module)\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.layers.experimental.preprocessing.Hashing`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Hashing)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.layers.Hashing`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Hashing), [`tf.compat.v1.keras.layers.experimental.preprocessing.Hashing`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Hashing)\n\n\u003cbr /\u003e\n\n tf.keras.layers.Hashing(\n num_bins, mask_value=None, salt=None, **kwargs\n )\n\nThis layer transforms categorical inputs to hashed output. It element-wise\nconverts a ints or strings to ints in a fixed range. The stable hash\nfunction uses `tensorflow::ops::Fingerprint` to produce the same output\nconsistently across all platforms.\n\nThis layer uses [FarmHash64](https://github.com/google/farmhash) by default,\nwhich provides a consistent hashed output across different platforms and is\nstable across invocations, regardless of device and context, by mixing the\ninput bits thoroughly.\n\nIf you want to obfuscate the hashed output, you can also pass a random `salt`\nargument in the constructor. In that case, the layer will use the\n[SipHash64](https://github.com/google/highwayhash) hash function, with\nthe `salt` value serving as additional input to the hash function.\n\nFor an overview and full list of preprocessing layers, see the preprocessing\n[guide](https://www.tensorflow.org/guide/keras/preprocessing_layers).\n\n**Example (FarmHash64)** \n\n layer = tf.keras.layers.Hashing(num_bins=3)\n inp = [['A'], ['B'], ['C'], ['D'], ['E']]\n layer(inp)\n \u003ctf.Tensor: shape=(5, 1), dtype=int64, numpy=\n array([[1],\n [0],\n [1],\n [1],\n [2]])\u003e\n\n**Example (FarmHash64) with a mask value** \n\n layer = tf.keras.layers.Hashing(num_bins=3, mask_value='')\n inp = [['A'], ['B'], [''], ['C'], ['D']]\n layer(inp)\n \u003ctf.Tensor: shape=(5, 1), dtype=int64, numpy=\n array([[1],\n [1],\n [0],\n [2],\n [2]])\u003e\n\n**Example (SipHash64)** \n\n layer = tf.keras.layers.Hashing(num_bins=3, salt=[133, 137])\n inp = [['A'], ['B'], ['C'], ['D'], ['E']]\n layer(inp)\n \u003ctf.Tensor: shape=(5, 1), dtype=int64, numpy=\n array([[1],\n [2],\n [1],\n [0],\n [2]])\u003e\n\n**Example (Siphash64 with a single integer, same as `salt=[133, 133]`)** \n\n layer = tf.keras.layers.Hashing(num_bins=3, salt=133)\n inp = [['A'], ['B'], ['C'], ['D'], ['E']]\n layer(inp)\n \u003ctf.Tensor: shape=(5, 1), dtype=int64, numpy=\n array([[0],\n [0],\n [2],\n [1],\n [0]])\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `num_bins` | Number of hash bins. Note that this includes the `mask_value` bin, so the effective number of bins is `(num_bins - 1)` if `mask_value` is set. |\n| `mask_value` | A value that represents masked inputs, which are mapped to index 0. Defaults to None, meaning no mask term will be added and the hashing will start at index 0. |\n| `salt` | A single unsigned integer or None. 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 `None` (in that case, the FarmHash64 hash function is used). It also supports tuple/list of 2 unsigned integer numbers, see reference paper for details. |\n| `**kwargs` | Keyword arguments to construct a layer. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Input shape ----------- ||\n|---|---|\n| A single or list of string, int32 or int64 `Tensor`, `SparseTensor` or `RaggedTensor` of shape `(batch_size, ...,)` ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Output shape ------------ ||\n|---|---|\n| An int64 `Tensor`, `SparseTensor` or `RaggedTensor` of shape `(batch_size, ...)`. If any input is `RaggedTensor` then output is `RaggedTensor`, otherwise if any input is `SparseTensor` then output is `SparseTensor`, otherwise the output is `Tensor`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Reference --------- ||\n|---|---|\n| \u003cbr /\u003e - [SipHash with salt](https://www.131002.net/siphash/siphash.pdf) ||\n\n\u003cbr /\u003e"]]