tf.feature_column.embedding_column
Stay organized with collections
Save and categorize content based on your preferences.
DenseColumn
that converts from sparse, categorical input.
tf.feature_column.embedding_column(
categorical_column, dimension, combiner='mean', initializer=None,
ckpt_to_load_from=None, tensor_name_in_ckpt=None, max_norm=None, trainable=True
)
Use this when your inputs are sparse, but you want to convert them to a dense
representation (e.g., to feed to a DNN).
Inputs must be a CategoricalColumn
created by any of the
categorical_column_*
function. Here is an example of using
embedding_column
with DNNClassifier
:
video_id = categorical_column_with_identity(
key='video_id', num_buckets=1000000, default_value=0)
columns = [embedding_column(video_id, 9),...]
estimator = tf.estimator.DNNClassifier(feature_columns=columns, ...)
label_column = ...
def input_fn():
features = tf.io.parse_example(
..., features=make_parse_example_spec(columns + [label_column]))
labels = features.pop(label_column.name)
return features, labels
estimator.train(input_fn=input_fn, steps=100)
Here is an example using embedding_column
with model_fn:
def model_fn(features, ...):
video_id = categorical_column_with_identity(
key='video_id', num_buckets=1000000, default_value=0)
columns = [embedding_column(video_id, 9),...]
dense_tensor = input_layer(features, columns)
# Form DNN layers, calculate loss, and return EstimatorSpec.
...
Args |
categorical_column
|
A CategoricalColumn created by a
categorical_column_with_* function. This column produces the sparse IDs
that are inputs to the embedding lookup.
|
dimension
|
An integer specifying dimension of the embedding, must be > 0.
|
combiner
|
A string specifying how to reduce if there are multiple entries in
a single row. Currently 'mean', 'sqrtn' and 'sum' are supported, with
'mean' the default. 'sqrtn' often achieves good accuracy, in particular
with bag-of-words columns. Each of this can be thought as example level
normalizations on the column. For more information, see
tf.embedding_lookup_sparse .
|
initializer
|
A variable initializer function to be used in embedding
variable initialization. If not specified, defaults to
truncated_normal_initializer with mean 0.0 and
standard deviation 1/sqrt(dimension) .
|
ckpt_to_load_from
|
String representing checkpoint name/pattern from which to
restore column weights. Required if tensor_name_in_ckpt is not None .
|
tensor_name_in_ckpt
|
Name of the Tensor in ckpt_to_load_from from which
to restore the column weights. Required if ckpt_to_load_from is not
None .
|
max_norm
|
If not None , embedding values are l2-normalized to this value.
|
trainable
|
Whether or not the embedding is trainable. Default is True.
|
Returns |
DenseColumn that converts from sparse input.
|
Raises |
ValueError
|
if dimension not > 0.
|
ValueError
|
if exactly one of ckpt_to_load_from and tensor_name_in_ckpt
is specified.
|
ValueError
|
if initializer is specified and is not callable.
|
RuntimeError
|
If eager execution is enabled.
|
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.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.feature_column.embedding_column\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/feature_column/embedding_column) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/feature_column/feature_column_v2.py#L820-L922) |\n\n`DenseColumn` that converts from sparse, categorical input.\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.embedding_column`](/api_docs/python/tf/feature_column/embedding_column)\n\n\u003cbr /\u003e\n\n tf.feature_column.embedding_column(\n categorical_column, dimension, combiner='mean', initializer=None,\n ckpt_to_load_from=None, tensor_name_in_ckpt=None, max_norm=None, trainable=True\n )\n\nUse this when your inputs are sparse, but you want to convert them to a dense\nrepresentation (e.g., to feed to a DNN).\n\nInputs must be a `CategoricalColumn` created by any of the\n`categorical_column_*` function. Here is an example of using\n`embedding_column` with `DNNClassifier`: \n\n video_id = categorical_column_with_identity(\n key='video_id', num_buckets=1000000, default_value=0)\n columns = [embedding_column(video_id, 9),...]\n\n estimator = tf.estimator.DNNClassifier(feature_columns=columns, ...)\n\n label_column = ...\n def input_fn():\n features = tf.io.parse_example(\n ..., features=make_parse_example_spec(columns + [label_column]))\n labels = features.pop(label_column.name)\n return features, labels\n\n estimator.train(input_fn=input_fn, steps=100)\n\nHere is an example using `embedding_column` with model_fn: \n\n def model_fn(features, ...):\n video_id = categorical_column_with_identity(\n key='video_id', num_buckets=1000000, default_value=0)\n columns = [embedding_column(video_id, 9),...]\n dense_tensor = input_layer(features, columns)\n # Form DNN layers, calculate loss, and return EstimatorSpec.\n ...\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `categorical_column` | A `CategoricalColumn` created by a `categorical_column_with_*` function. This column produces the sparse IDs that are inputs to the embedding lookup. |\n| `dimension` | An integer specifying dimension of the embedding, must be \\\u003e 0. |\n| `combiner` | A string specifying how to reduce if there are multiple entries in a single row. Currently 'mean', 'sqrtn' and 'sum' are supported, with 'mean' the default. 'sqrtn' often achieves good accuracy, in particular with bag-of-words columns. Each of this can be thought as example level normalizations on the column. For more information, see `tf.embedding_lookup_sparse`. |\n| `initializer` | A variable initializer function to be used in embedding variable initialization. If not specified, defaults to `truncated_normal_initializer` with mean `0.0` and standard deviation `1/sqrt(dimension)`. |\n| `ckpt_to_load_from` | String representing checkpoint name/pattern from which to restore column weights. Required if `tensor_name_in_ckpt` is not `None`. |\n| `tensor_name_in_ckpt` | Name of the `Tensor` in `ckpt_to_load_from` from which to restore the column weights. Required if `ckpt_to_load_from` is not `None`. |\n| `max_norm` | If not `None`, embedding values are l2-normalized to this value. |\n| `trainable` | Whether or not the embedding is trainable. Default is True. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| `DenseColumn` that converts from sparse input. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|-------------------------------------------------------------------------------|\n| `ValueError` | if `dimension` not \\\u003e 0. |\n| `ValueError` | if exactly one of `ckpt_to_load_from` and `tensor_name_in_ckpt` is specified. |\n| `ValueError` | if `initializer` is specified and is not callable. |\n| `RuntimeError` | If eager execution is enabled. |\n\n\u003cbr /\u003e"]]