tf.feature_column.weighted_categorical_column
Stay organized with collections
Save and categorize content based on your preferences.
Applies weight values to a CategoricalColumn
.
tf.feature_column.weighted_categorical_column(
categorical_column,
weight_feature_key,
dtype=tf.dtypes.float32
)
Use this when each of your sparse inputs has both an ID and a value. For
example, if you're representing text documents as a collection of word
frequencies, you can provide 2 parallel sparse input features ('terms' and
'frequencies' below).
Example:
Input tf.Example
objects:
[
features {
feature {
key: "terms"
value {bytes_list {value: "very" value: "model"} }
}
feature {
key: "frequencies"
value {float_list {value: 0.3 value: 0.1} }
}
},
features {
feature {
key: "terms"
value {bytes_list {value: "when" value: "course" value: "human"} }
}
feature {
key: "frequencies"
value {float_list {value: 0.4 value: 0.1 value: 0.2} }
}
}
]
categorical_column = categorical_column_with_hash_bucket(
column_name='terms', hash_bucket_size=1000)
weighted_column = weighted_categorical_column(
categorical_column=categorical_column, weight_feature_key='frequencies')
columns = [weighted_column, ...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
linear_prediction, _, _ = linear_model(features, columns)
This assumes the input dictionary contains a SparseTensor
for key
'terms', and a SparseTensor
for key 'frequencies'. These 2 tensors must have
the same indices and dense shape.
Args |
categorical_column
|
A CategoricalColumn created by
categorical_column_with_* functions.
|
weight_feature_key
|
String key for weight values.
|
dtype
|
Type of weights, such as tf.float32 . Only float and integer weights
are supported.
|
Returns |
A CategoricalColumn composed of two sparse features: one represents id,
the other represents weight (value) of the id feature in that example.
|
Raises |
ValueError
|
if dtype is not convertible to float.
|
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-03-17 UTC.
[null,null,["Last updated 2023-03-17 UTC."],[],[],null,["# tf.feature_column.weighted_categorical_column\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.10.1/tensorflow/python/feature_column/feature_column_v2.py#L1718-L1791) |\n\nApplies weight values to a `CategoricalColumn`.\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.weighted_categorical_column`](https://www.tensorflow.org/api_docs/python/tf/feature_column/weighted_categorical_column)\n\n\u003cbr /\u003e\n\n tf.feature_column.weighted_categorical_column(\n categorical_column,\n weight_feature_key,\n dtype=../../tf/dtypes#float32\n )\n\nUse this when each of your sparse inputs has both an ID and a value. For\nexample, if you're representing text documents as a collection of word\nfrequencies, you can provide 2 parallel sparse input features ('terms' and\n'frequencies' below).\n\n#### Example:\n\nInput `tf.Example` objects: \n\n [\n features {\n feature {\n key: \"terms\"\n value {bytes_list {value: \"very\" value: \"model\"} }\n }\n feature {\n key: \"frequencies\"\n value {float_list {value: 0.3 value: 0.1} }\n }\n },\n features {\n feature {\n key: \"terms\"\n value {bytes_list {value: \"when\" value: \"course\" value: \"human\"} }\n }\n feature {\n key: \"frequencies\"\n value {float_list {value: 0.4 value: 0.1 value: 0.2} }\n }\n }\n ]\n\n categorical_column = categorical_column_with_hash_bucket(\n column_name='terms', hash_bucket_size=1000)\n weighted_column = weighted_categorical_column(\n categorical_column=categorical_column, weight_feature_key='frequencies')\n columns = [weighted_column, ...]\n features = tf.io.parse_example(..., features=make_parse_example_spec(columns))\n linear_prediction, _, _ = linear_model(features, columns)\n\nThis assumes the input dictionary contains a `SparseTensor` for key\n'terms', and a `SparseTensor` for key 'frequencies'. These 2 tensors must have\nthe same indices and dense shape.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------|----------------------------------------------------------------------------------------------------------|\n| `categorical_column` | A `CategoricalColumn` created by `categorical_column_with_*` functions. |\n| `weight_feature_key` | String key for weight values. |\n| `dtype` | Type of weights, such as [`tf.float32`](../../tf#float32). Only float and integer weights are supported. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `CategoricalColumn` composed of two sparse features: one represents id, the other represents weight (value) of the id feature in that example. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-----------------------------------------|\n| `ValueError` | if `dtype` is not convertible to float. |\n\n\u003cbr /\u003e"]]