tf.keras.layers.HashedCrossing
Stay organized with collections
Save and categorize content based on your preferences.
A preprocessing layer which crosses features using the "hashing trick".
Inherits From: Layer
, Module
tf.keras.layers.HashedCrossing(
num_bins, output_mode='int', sparse=False, **kwargs
)
This layer performs crosses of categorical features using the "hasing
trick". Conceptually, the transformation can be thought of as:
hash(concatenation of features) % num_bins
.
This layer currently only performs crosses of scalar inputs and batches of
scalar inputs. Valid input shapes are (batch_size, 1)
, (batch_size,)
and
()
.
For an overview and full list of preprocessing layers, see the preprocessing
guide.
Args |
num_bins
|
Number of hash bins.
|
output_mode
|
Specification for the output of the layer. Values can be
"int" , or "one_hot" configuring the layer as follows:
"int" : Return the integer bin indices directly.
"one_hot" : Encodes each individual element in the input into an
array the same size as num_bins , containing a 1 at the input's bin
index.
Defaults to "int" .
|
sparse
|
Boolean. Only applicable to "one_hot" mode. If True, returns a
SparseTensor instead of a dense Tensor . Defaults to False .
|
**kwargs
|
Keyword arguments to construct a layer.
|
Examples:
Crossing two scalar features.
layer = tf.keras.layers.HashedCrossing(
num_bins=5)
feat1 = tf.constant(['A', 'B', 'A', 'B', 'A'])
feat2 = tf.constant([101, 101, 101, 102, 102])
layer((feat1, feat2))
<tf.Tensor: shape=(5,), dtype=int64, numpy=array([1, 4, 1, 1, 3])>
Crossing and one-hotting two scalar features.
layer = tf.keras.layers.HashedCrossing(
num_bins=5, output_mode='one_hot')
feat1 = tf.constant(['A', 'B', 'A', 'B', 'A'])
feat2 = tf.constant([101, 101, 101, 102, 102])
layer((feat1, feat2))
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 1.],
[0., 1., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0.]], dtype=float32)>
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 2023-10-06 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 2023-10-06 UTC."],[],[],null,["# tf.keras.layers.HashedCrossing\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.13.1/keras/layers/preprocessing/hashed_crossing.py#L33-L227) |\n\nA preprocessing layer which crosses features using the \"hashing trick\".\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.HashedCrossing`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/HashedCrossing)\n\n\u003cbr /\u003e\n\n tf.keras.layers.HashedCrossing(\n num_bins, output_mode='int', sparse=False, **kwargs\n )\n\nThis layer performs crosses of categorical features using the \"hasing\ntrick\". Conceptually, the transformation can be thought of as:\nhash(concatenation of features) % `num_bins`.\n\nThis layer currently only performs crosses of scalar inputs and batches of\nscalar inputs. Valid input shapes are `(batch_size, 1)`, `(batch_size,)` and\n`()`.\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\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `num_bins` | Number of hash bins. |\n| `output_mode` | Specification for the output of the layer. Values can be `\"int\"`, or `\"one_hot\"` configuring the layer as follows: \u003cbr /\u003e - `\"int\"`: Return the integer bin indices directly. - `\"one_hot\"`: Encodes each individual element in the input into an array the same size as `num_bins`, containing a 1 at the input's bin index. Defaults to `\"int\"`. |\n| `sparse` | Boolean. Only applicable to `\"one_hot\"` mode. If True, returns a `SparseTensor` instead of a dense `Tensor`. Defaults to `False`. |\n| `**kwargs` | Keyword arguments to construct a layer. |\n\n#### Examples:\n\n**Crossing two scalar features.** \n\n layer = tf.keras.layers.HashedCrossing(\n num_bins=5)\n feat1 = tf.constant(['A', 'B', 'A', 'B', 'A'])\n feat2 = tf.constant([101, 101, 101, 102, 102])\n layer((feat1, feat2))\n \u003ctf.Tensor: shape=(5,), dtype=int64, numpy=array([1, 4, 1, 1, 3])\u003e\n\n**Crossing and one-hotting two scalar features.** \n\n layer = tf.keras.layers.HashedCrossing(\n num_bins=5, output_mode='one_hot')\n feat1 = tf.constant(['A', 'B', 'A', 'B', 'A'])\n feat2 = tf.constant([101, 101, 101, 102, 102])\n layer((feat1, feat2))\n \u003ctf.Tensor: shape=(5, 5), dtype=float32, numpy=\n array([[0., 1., 0., 0., 0.],\n [0., 0., 0., 0., 1.],\n [0., 1., 0., 0., 0.],\n [0., 1., 0., 0., 0.],\n [0., 0., 0., 1., 0.]], dtype=float32)\u003e"]]