tf.feature_column.categorical_column_with_identity
Stay organized with collections
Save and categorize content based on your preferences.
A CategoricalColumn
that returns identity values.
tf.feature_column.categorical_column_with_identity(
key, num_buckets, default_value=None
)
Use this when your inputs are integers in the range [0, num_buckets)
, and
you want to use the input value itself as the categorical ID. Values outside
this range will result in default_value
if specified, otherwise it will
fail.
Typically, this is used for contiguous ranges of integer indexes, but
it doesn't have to be. This might be inefficient, however, if many of IDs
are unused. Consider categorical_column_with_hash_bucket
in that case.
For input dictionary features
, features[key]
is either Tensor
or
SparseTensor
. If Tensor
, missing values can be represented by -1
for int
and ''
for string, which will be dropped by this feature column.
In the following examples, each input in the range [0, 1000000)
is assigned
the same value. All other inputs are assigned default_value
0. Note that a
literal 0 in inputs will result in the same default ID.
Linear model:
import tensorflow as tf
video_id = tf.feature_column.categorical_column_with_identity(
key='video_id', num_buckets=1000000, default_value=0)
columns = [video_id]
features = {'video_id': tf.sparse.from_dense([[2, 85, 0, 0, 0],
[33,78, 2, 73, 1]])}
linear_prediction = tf.compat.v1.feature_column.linear_model(features,
columns)
Embedding for a DNN model:
import tensorflow as tf
video_id = tf.feature_column.categorical_column_with_identity(
key='video_id', num_buckets=1000000, default_value=0)
columns = [tf.feature_column.embedding_column(video_id, 9)]
features = {'video_id': tf.sparse.from_dense([[2, 85, 0, 0, 0],
[33,78, 2, 73, 1]])}
input_layer = tf.keras.layers.DenseFeatures(columns)
dense_tensor = input_layer(features)
Args |
key
|
A unique string identifying the input feature. It is used as the
column name and the dictionary key for feature parsing configs, feature
Tensor objects, and feature columns.
|
num_buckets
|
Range of inputs and outputs is [0, num_buckets) .
|
default_value
|
If set, values outside of range [0, num_buckets) will
be replaced with this value. If not set, values >= num_buckets will
cause a failure while values < 0 will be dropped.
|
Returns |
A CategoricalColumn that returns identity values.
|
Raises |
ValueError
|
if num_buckets is less than one.
|
ValueError
|
if default_value is not in range [0, num_buckets) .
|
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 2021-08-16 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 2021-08-16 UTC."],[],[],null,["# tf.feature_column.categorical_column_with_identity\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/feature_column/categorical_column_with_identity) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.6.0/tensorflow/python/feature_column/feature_column_v2.py#L1603-L1676) |\n\nA `CategoricalColumn` that returns identity values.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.feature_column.categorical_column_with_identity`](https://www.tensorflow.org/api_docs/python/tf/feature_column/categorical_column_with_identity)\n\n\u003cbr /\u003e\n\n tf.feature_column.categorical_column_with_identity(\n key, num_buckets, default_value=None\n )\n\nUse this when your inputs are integers in the range `[0, num_buckets)`, and\nyou want to use the input value itself as the categorical ID. Values outside\nthis range will result in `default_value` if specified, otherwise it will\nfail.\n\nTypically, this is used for contiguous ranges of integer indexes, but\nit doesn't have to be. This might be inefficient, however, if many of IDs\nare unused. Consider `categorical_column_with_hash_bucket` in that case.\n\nFor input dictionary `features`, `features[key]` is either `Tensor` or\n`SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int\nand `''` for string, which will be dropped by this feature column.\n\nIn the following examples, each input in the range `[0, 1000000)` is assigned\nthe same value. All other inputs are assigned `default_value` 0. Note that a\nliteral 0 in inputs will result in the same default ID.\n\n#### Linear model:\n\n import tensorflow as tf\n video_id = tf.feature_column.categorical_column_with_identity(\n key='video_id', num_buckets=1000000, default_value=0)\n columns = [video_id]\n features = {'video_id': tf.sparse.from_dense([[2, 85, 0, 0, 0],\n [33,78, 2, 73, 1]])}\n linear_prediction = tf.compat.v1.feature_column.linear_model(features,\n columns)\n\nEmbedding for a DNN model: \n\n import tensorflow as tf\n video_id = tf.feature_column.categorical_column_with_identity(\n key='video_id', num_buckets=1000000, default_value=0)\n columns = [tf.feature_column.embedding_column(video_id, 9)]\n features = {'video_id': tf.sparse.from_dense([[2, 85, 0, 0, 0],\n [33,78, 2, 73, 1]])}\n input_layer = tf.keras.layers.DenseFeatures(columns)\n dense_tensor = input_layer(features)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `key` | A unique string identifying the input feature. It is used as the column name and the dictionary key for feature parsing configs, feature `Tensor` objects, and feature columns. |\n| `num_buckets` | Range of inputs and outputs is `[0, num_buckets)`. |\n| `default_value` | If set, values outside of range `[0, num_buckets)` will be replaced with this value. If not set, values \\\u003e= num_buckets will cause a failure while values \\\u003c 0 will be dropped. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `CategoricalColumn` that returns identity values. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------------------------------------|\n| `ValueError` | if `num_buckets` is less than one. |\n| `ValueError` | if `default_value` is not in range `[0, num_buckets)`. |\n\n\u003cbr /\u003e"]]