tf.sparse.reshape
Stay organized with collections
Save and categorize content based on your preferences.
Reshapes a SparseTensor
to represent values in a new dense shape.
tf.sparse.reshape(
sp_input, shape, name=None
)
This operation has the same semantics as reshape
on the represented dense
tensor. The indices of non-empty values in sp_input
are recomputed based
on the new dense shape, and a new SparseTensor
is returned containing the
new indices and new shape. The order of non-empty values in sp_input
is
unchanged.
If one component of shape
is the special value -1, the size of that
dimension is computed so that the total dense size remains constant. At
most one component of shape
can be -1. The number of dense elements
implied by shape
must be the same as the number of dense elements
originally represented by sp_input
.
For example, if sp_input
has shape [2, 3, 6]
and indices
/ values
:
[0, 0, 0]: a
[0, 0, 1]: b
[0, 1, 0]: c
[1, 0, 0]: d
[1, 2, 3]: e
and shape
is [9, -1]
, then the output will be a SparseTensor
of
shape [9, 4]
and indices
/ values
:
[0, 0]: a
[0, 1]: b
[1, 2]: c
[4, 2]: d
[8, 1]: e
Args |
sp_input
|
The input SparseTensor .
|
shape
|
A 1-D (vector) int64 Tensor specifying the new dense shape of the
represented SparseTensor .
|
name
|
A name prefix for the returned tensors (optional)
|
Returns |
A SparseTensor with the same non-empty values but with indices calculated
by the new dense shape.
|
Raises |
TypeError
|
If sp_input is not a SparseTensor .
|
ValueError
|
If argument shape requests a SparseTensor with a different
number of elements than sp_input .
|
ValueError
|
If shape has more than one inferred (== -1) dimension.
|
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.sparse.reshape\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/sparse_ops.py#L876-L980) |\n\nReshapes a `SparseTensor` to represent values in a new dense shape.\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.sparse.reshape`](https://www.tensorflow.org/api_docs/python/tf/sparse/reshape), [`tf.compat.v1.sparse_reshape`](https://www.tensorflow.org/api_docs/python/tf/sparse/reshape)\n\n\u003cbr /\u003e\n\n tf.sparse.reshape(\n sp_input, shape, name=None\n )\n\nThis operation has the same semantics as `reshape` on the represented dense\ntensor. The indices of non-empty values in `sp_input` are recomputed based\non the new dense shape, and a new `SparseTensor` is returned containing the\nnew indices and new shape. The order of non-empty values in `sp_input` is\nunchanged.\n\nIf one component of `shape` is the special value -1, the size of that\ndimension is computed so that the total dense size remains constant. At\nmost one component of `shape` can be -1. The number of dense elements\nimplied by `shape` must be the same as the number of dense elements\noriginally represented by `sp_input`.\n\nFor example, if `sp_input` has shape `[2, 3, 6]` and `indices` / `values`: \n\n [0, 0, 0]: a\n [0, 0, 1]: b\n [0, 1, 0]: c\n [1, 0, 0]: d\n [1, 2, 3]: e\n\nand `shape` is `[9, -1]`, then the output will be a `SparseTensor` of\nshape `[9, 4]` and `indices` / `values`: \n\n [0, 0]: a\n [0, 1]: b\n [1, 2]: c\n [4, 2]: d\n [8, 1]: e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|-------------------------------------------------------------------------------------------------|\n| `sp_input` | The input `SparseTensor`. |\n| `shape` | A 1-D (vector) int64 `Tensor` specifying the new dense shape of the represented `SparseTensor`. |\n| `name` | A name prefix for the returned tensors (optional) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `SparseTensor` with the same non-empty values but with indices calculated by the new dense shape. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|----------------------------------------------------------------------------------------------------|\n| `TypeError` | If `sp_input` is not a `SparseTensor`. |\n| `ValueError` | If argument `shape` requests a `SparseTensor` with a different number of elements than `sp_input`. |\n| `ValueError` | If `shape` has more than one inferred (== -1) dimension. |\n\n\u003cbr /\u003e"]]