View source on GitHub
|
A preprocessing layer which encodes integer features.
Inherits From: Layer, Operation
tf.keras.layers.CategoryEncoding(
num_tokens=None, output_mode='multi_hot', sparse=False, **kwargs
)
Used in the notebooks
| Used in the guide | Used in the tutorials |
|---|---|
This layer provides options for condensing data into a categorical encoding
when the total number of tokens are known in advance. It accepts integer
values as inputs, and it outputs a dense or sparse representation of those
inputs. For integer inputs where the total number of tokens is not known,
use keras.layers.IntegerLookup instead.
Examples:
One-hot encoding data
layer = keras.layers.CategoryEncoding(num_tokens=4, output_mode="one_hot")layer([3, 2, 0, 1])array([[0., 0., 0., 1.],[0., 0., 1., 0.],[1., 0., 0., 0.],[0., 1., 0., 0.]]>
Multi-hot encoding data
layer = keras.layers.CategoryEncoding(num_tokens=4, output_mode="multi_hot")layer([[0, 1], [0, 0], [1, 2], [3, 1]])array([[1., 1., 0., 0.],[1., 0., 0., 0.],[0., 1., 1., 0.],[0., 1., 0., 1.]]>
Using weighted inputs in "count" mode
layer = keras.layers.CategoryEncoding(num_tokens=4, output_mode="count")count_weights = np.array([[.1, .2], [.1, .1], [.2, .3], [.4, .2]])layer([[0, 1], [0, 0], [1, 2], [3, 1]], count_weights=count_weights)array([[0.1, 0.2, 0. , 0. ],[0.2, 0. , 0. , 0. ],[0. , 0.2, 0.3, 0. ],[0. , 0.2, 0. , 0.4]]>
Methods
convert_weight
convert_weight(
weight
)
Convert the weight if it is from the a different backend.
from_config
@classmethodfrom_config( config )
Creates an operation from its config.
This method is the reverse of get_config, capable of instantiating the
same operation from the config dictionary.
if "dtype" in config and isinstance(config["dtype"], dict):
policy = dtype_policies.deserialize(config["dtype"])
| Args | |
|---|---|
config
|
A Python dictionary, typically the output of get_config.
|
| Returns | |
|---|---|
| An operation instance. |
View source on GitHub