tf.io.SparseFeature
Stay organized with collections
Save and categorize content based on your preferences.
Configuration for parsing a sparse input feature from an Example
.
tf.io.SparseFeature(
index_key, value_key, dtype, size, already_sorted=False
)
Note, preferably use VarLenFeature
(possibly in combination with a
SequenceExample
) in order to parse out SparseTensor
s instead of
SparseFeature
due to its simplicity.
Closely mimicking the SparseTensor
that will be obtained by parsing an
Example
with a SparseFeature
config, a SparseFeature
contains a
value_key
: The name of key for a Feature
in the Example
whose parsed
Tensor
will be the resulting SparseTensor.values
.
index_key
: A list of names - one for each dimension in the resulting
SparseTensor
whose indices[i][dim]
indicating the position of
the i
-th value in the dim
dimension will be equal to the i
-th value in
the Feature with key named index_key[dim]
in the Example
.
size
: A list of ints for the resulting SparseTensor.dense_shape
.
For example, we can represent the following 2D SparseTensor
SparseTensor(indices=[[3, 1], [20, 0]],
values=[0.5, -1.0]
dense_shape=[100, 3])
with an Example
input proto
features {
feature { key: "val" value { float_list { value: [ 0.5, -1.0 ] } } }
feature { key: "ix0" value { int64_list { value: [ 3, 20 ] } } }
feature { key: "ix1" value { int64_list { value: [ 1, 0 ] } } }
}
and SparseFeature
config with 2 index_key
s
SparseFeature(index_key=["ix0", "ix1"],
value_key="val",
dtype=tf.float32,
size=[100, 3])
Fields |
index_key
|
A single string name or a list of string names of index features.
For each key the underlying feature's type must be int64 and its length
must always match that of the value_key feature.
To represent SparseTensor s with a dense_shape of rank higher than 1
a list of length rank should be used.
|
value_key
|
Name of value feature. The underlying feature's type must
be dtype and its length must always match that of all the index_key s'
features.
|
dtype
|
Data type of the value_key feature.
|
size
|
A Python int or list thereof specifying the dense shape. Should be a
list if and only if index_key is a list. In that case the list must be
equal to the length of index_key . Each for each entry i all values in
the index_key [i] feature must be in [0, size[i]) .
|
already_sorted
|
A Python boolean to specify whether the values in
value_key are already sorted by their index position. If so skip
sorting. False by default (optional).
|
Attributes |
index_key
|
A namedtuple alias for field number 0
|
value_key
|
A namedtuple alias for field number 1
|
dtype
|
A namedtuple alias for field number 2
|
size
|
A namedtuple alias for field number 3
|
already_sorted
|
A namedtuple alias for field number 4
|
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 2024-04-26 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 2024-04-26 UTC."],[],[],null,["# tf.io.SparseFeature\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/parsing_config.py#L223-L295) |\n\nConfiguration for parsing a sparse input feature from an `Example`.\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.SparseFeature`](https://www.tensorflow.org/api_docs/python/tf/io/SparseFeature), [`tf.compat.v1.io.SparseFeature`](https://www.tensorflow.org/api_docs/python/tf/io/SparseFeature)\n\n\u003cbr /\u003e\n\n tf.io.SparseFeature(\n index_key, value_key, dtype, size, already_sorted=False\n )\n\nNote, preferably use `VarLenFeature` (possibly in combination with a\n`SequenceExample`) in order to parse out `SparseTensor`s instead of\n`SparseFeature` due to its simplicity.\n\nClosely mimicking the `SparseTensor` that will be obtained by parsing an\n`Example` with a `SparseFeature` config, a `SparseFeature` contains a\n\n- `value_key`: The name of key for a `Feature` in the `Example` whose parsed\n `Tensor` will be the resulting [`SparseTensor.values`](../../tf/sparse/SparseTensor#values).\n\n- `index_key`: A list of names - one for each dimension in the resulting\n `SparseTensor` whose `indices[i][dim]` indicating the position of\n the `i`-th value in the `dim` dimension will be equal to the `i`-th value in\n the Feature with key named `index_key[dim]` in the `Example`.\n\n- `size`: A list of ints for the resulting [`SparseTensor.dense_shape`](../../tf/sparse/SparseTensor#dense_shape).\n\nFor example, we can represent the following 2D `SparseTensor` \n\n SparseTensor(indices=[[3, 1], [20, 0]],\n values=[0.5, -1.0]\n dense_shape=[100, 3])\n\nwith an `Example` input proto \n\n features {\n feature { key: \"val\" value { float_list { value: [ 0.5, -1.0 ] } } }\n feature { key: \"ix0\" value { int64_list { value: [ 3, 20 ] } } }\n feature { key: \"ix1\" value { int64_list { value: [ 1, 0 ] } } }\n }\n\nand `SparseFeature` config with 2 `index_key`s \n\n SparseFeature(index_key=[\"ix0\", \"ix1\"],\n value_key=\"val\",\n dtype=tf.float32,\n size=[100, 3])\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Fields ------ ||\n|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `index_key` | A single string name or a list of string names of index features. For each key the underlying feature's type must be `int64` and its length must always match that of the `value_key` feature. To represent `SparseTensor`s with a `dense_shape` of `rank` higher than 1 a list of length `rank` should be used. |\n| `value_key` | Name of value feature. The underlying feature's type must be `dtype` and its length must always match that of all the `index_key`s' features. |\n| `dtype` | Data type of the `value_key` feature. |\n| `size` | A Python int or list thereof specifying the dense shape. Should be a list if and only if `index_key` is a list. In that case the list must be equal to the length of `index_key`. Each for each entry `i` all values in the `index_key`\\[i\\] feature must be in `[0, size[i])`. |\n| `already_sorted` | A Python boolean to specify whether the values in `value_key` are already sorted by their index position. If so skip sorting. False by default (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|------------------|-----------------------------------------|\n| `index_key` | A `namedtuple` alias for field number 0 |\n| `value_key` | A `namedtuple` alias for field number 1 |\n| `dtype` | A `namedtuple` alias for field number 2 |\n| `size` | A `namedtuple` alias for field number 3 |\n| `already_sorted` | A `namedtuple` alias for field number 4 |\n\n\u003cbr /\u003e"]]