tf.compat.v1.estimator.regressor_parse_example_spec
bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
Generates parsing spec for tf.parse_example to be used with regressors.
tf.compat.v1.estimator.regressor_parse_example_spec(
feature_columns, label_key, label_dtype=tf.dtypes.float32, label_default=None,
label_dimension=1, weight_column=None
)
If users keep data in tf.Example format, they need to call tf.parse_example
with a proper feature spec. There are two main things that this utility helps:
- Users need to combine parsing spec of features with labels and weights
(if any) since they are all parsed from same tf.Example instance. This
utility combines these specs.
- It is difficult to map expected label by a regressor such as
DNNRegressor
to corresponding tf.parse_example spec. This utility encodes it by getting
related information from users (key, dtype).
Example output of parsing spec:
# Define features and transformations
feature_b = tf.feature_column.numeric_column(...)
feature_c_bucketized = tf.feature_column.bucketized_column(
tf.feature_column.numeric_column("feature_c"), ...)
feature_a_x_feature_c = tf.feature_column.crossed_column(
columns=["feature_a", feature_c_bucketized], ...)
feature_columns = [feature_b, feature_c_bucketized, feature_a_x_feature_c]
parsing_spec = tf.estimator.regressor_parse_example_spec(
feature_columns, label_key='my-label')
# For the above example, regressor_parse_example_spec would return the dict:
assert parsing_spec == {
"feature_a": parsing_ops.VarLenFeature(tf.string),
"feature_b": parsing_ops.FixedLenFeature([1], dtype=tf.float32),
"feature_c": parsing_ops.FixedLenFeature([1], dtype=tf.float32)
"my-label" : parsing_ops.FixedLenFeature([1], dtype=tf.float32)
}
Example usage with a regressor:
feature_columns = # define features via tf.feature_column
estimator = DNNRegressor(
hidden_units=[256, 64, 16],
feature_columns=feature_columns,
weight_column='example-weight',
label_dimension=3)
# This label configuration tells the regressor the following:
# * weights are retrieved with key 'example-weight'
# * label is a 3 dimension tensor with float32 dtype.
# Input builders
def input_fn_train(): # Returns a tuple of features and labels.
features = tf.contrib.learn.read_keyed_batch_features(
file_pattern=train_files,
batch_size=batch_size,
# creates parsing configuration for tf.parse_example
features=tf.estimator.classifier_parse_example_spec(
feature_columns,
label_key='my-label',
label_dimension=3,
weight_column='example-weight'),
reader=tf.RecordIOReader)
labels = features.pop('my-label')
return features, labels
estimator.train(input_fn=input_fn_train)
Args |
feature_columns
|
An iterable containing all feature columns. All items
should be instances of classes derived from _FeatureColumn .
|
label_key
|
A string identifying the label. It means tf.Example stores labels
with this key.
|
label_dtype
|
A tf.dtype identifies the type of labels. By default it is
tf.float32 .
|
label_default
|
used as label if label_key does not exist in given
tf.Example. By default default_value is none, which means
tf.parse_example will error out if there is any missing label.
|
label_dimension
|
Number of regression targets per example. This is the
size of the last dimension of the labels and logits Tensor objects
(typically, these have shape [batch_size, label_dimension] ).
|
weight_column
|
A string or a NumericColumn created by
tf.feature_column.numeric_column defining feature column representing
weights. It is used to down weight or boost examples during training. It
will be multiplied by the loss of the example. If it is a string, it is
used as a key to fetch weight tensor from the features . If it is a
NumericColumn , raw tensor is fetched by key weight_column.key ,
then weight_column.normalizer_fn is applied on it to get weight tensor.
|
Returns |
A dict mapping each feature key to a FixedLenFeature or VarLenFeature
value.
|
Raises |
ValueError
|
If label is used in feature_columns .
|
ValueError
|
If weight_column is used in feature_columns .
|
ValueError
|
If any of the given feature_columns is not a _FeatureColumn
instance.
|
ValueError
|
If weight_column is not a NumericColumn instance.
|
ValueError
|
if label_key is None.
|
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.
[[["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 2020-10-01 UTC."],[],[],null,["# tf.compat.v1.estimator.regressor_parse_example_spec\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/estimator/tree/master/tensorflow_estimator/python/estimator/canned/parsing_utils.py) |\n\nGenerates parsing spec for tf.parse_example to be used with regressors. \n\n tf.compat.v1.estimator.regressor_parse_example_spec(\n feature_columns, label_key, label_dtype=tf.dtypes.float32, label_default=None,\n label_dimension=1, weight_column=None\n )\n\nIf users keep data in tf.Example format, they need to call tf.parse_example\nwith a proper feature spec. There are two main things that this utility helps:\n\n- Users need to combine parsing spec of features with labels and weights (if any) since they are all parsed from same tf.Example instance. This utility combines these specs.\n- It is difficult to map expected label by a regressor such as `DNNRegressor` to corresponding tf.parse_example spec. This utility encodes it by getting related information from users (key, dtype).\n\nExample output of parsing spec: \n\n # Define features and transformations\n feature_b = tf.feature_column.numeric_column(...)\n feature_c_bucketized = tf.feature_column.bucketized_column(\n tf.feature_column.numeric_column(\"feature_c\"), ...)\n feature_a_x_feature_c = tf.feature_column.crossed_column(\n columns=[\"feature_a\", feature_c_bucketized], ...)\n\n feature_columns = [feature_b, feature_c_bucketized, feature_a_x_feature_c]\n parsing_spec = tf.estimator.regressor_parse_example_spec(\n feature_columns, label_key='my-label')\n\n # For the above example, regressor_parse_example_spec would return the dict:\n assert parsing_spec == {\n \"feature_a\": parsing_ops.VarLenFeature(tf.string),\n \"feature_b\": parsing_ops.FixedLenFeature([1], dtype=tf.float32),\n \"feature_c\": parsing_ops.FixedLenFeature([1], dtype=tf.float32)\n \"my-label\" : parsing_ops.FixedLenFeature([1], dtype=tf.float32)\n }\n\nExample usage with a regressor: \n\n feature_columns = # define features via tf.feature_column\n estimator = DNNRegressor(\n hidden_units=[256, 64, 16],\n feature_columns=feature_columns,\n weight_column='example-weight',\n label_dimension=3)\n # This label configuration tells the regressor the following:\n # * weights are retrieved with key 'example-weight'\n # * label is a 3 dimension tensor with float32 dtype.\n\n\n # Input builders\n def input_fn_train(): # Returns a tuple of features and labels.\n features = tf.contrib.learn.read_keyed_batch_features(\n file_pattern=train_files,\n batch_size=batch_size,\n # creates parsing configuration for tf.parse_example\n features=tf.estimator.classifier_parse_example_spec(\n feature_columns,\n label_key='my-label',\n label_dimension=3,\n weight_column='example-weight'),\n reader=tf.RecordIOReader)\n labels = features.pop('my-label')\n return features, labels\n\n estimator.train(input_fn=input_fn_train)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `feature_columns` | An iterable containing all feature columns. All items should be instances of classes derived from `_FeatureColumn`. |\n| `label_key` | A string identifying the label. It means tf.Example stores labels with this key. |\n| `label_dtype` | A `tf.dtype` identifies the type of labels. By default it is [`tf.float32`](../../../../tf#float32). |\n| `label_default` | used as label if label_key does not exist in given tf.Example. By default default_value is none, which means `tf.parse_example` will error out if there is any missing label. |\n| `label_dimension` | Number of regression targets per example. This is the size of the last dimension of the labels and logits `Tensor` objects (typically, these have shape `[batch_size, label_dimension]`). |\n| `weight_column` | A string or a `NumericColumn` created by [`tf.feature_column.numeric_column`](../../../../tf/feature_column/numeric_column) defining feature column representing weights. It is used to down weight or boost examples during training. It will be multiplied by the loss of the example. If it is a string, it is used as a key to fetch weight tensor from the `features`. If it is a `NumericColumn`, raw tensor is fetched by key `weight_column.key`, then weight_column.normalizer_fn is applied on it to get weight tensor. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A dict mapping each feature key to a `FixedLenFeature` or `VarLenFeature` value. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------------------------------------------|\n| `ValueError` | If label is used in `feature_columns`. |\n| `ValueError` | If weight_column is used in `feature_columns`. |\n| `ValueError` | If any of the given `feature_columns` is not a `_FeatureColumn` instance. |\n| `ValueError` | If `weight_column` is not a `NumericColumn` instance. |\n| `ValueError` | if label_key is None. |\n\n\u003cbr /\u003e"]]