Given a tensor input, this operation inserts a dimension of length 1 at the
dimension index axis of input's shape. The dimension index follows python
indexing rules: It's zero-based, a negative index it is counted backward
from the end.
This operation is useful to:
Add an outer "batch" dimension to a single element.
Align axes for broadcasting.
To add an inner vector length axis to a tensor of scalars.
For example:
If you have a sparse tensor with shape [height, width, depth]:
This operation requires that axis is a valid index for input.shape,
following python indexing rules:
-1-tf.rank(input) <=axis <=tf.rank(input)
This operation is related to:
tf.expand_dims, which provides this functionality for dense tensors.
tf.squeeze, which removes dimensions of size 1, from dense tensors.
tf.sparse.reshape, which provides more flexible reshaping capability.
Args
sp_input
A SparseTensor.
axis
0-D (scalar). Specifies the dimension index at which to expand the
shape of input. Must be in the range [-rank(sp_input) - 1,
rank(sp_input)]. Defaults to -1.
name
The name of the output SparseTensor.
Returns
A SparseTensor with the same data as sp_input, but its shape has an
additional dimension of size 1 added.
[[["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 2021-08-16 UTC."],[],[],null,["# tf.sparse.expand_dims\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/sparse/expand_dims) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.6.0/tensorflow/python/ops/sparse_ops.py#L138-L241) |\n\nReturns a tensor with an length 1 axis inserted at index `axis`.\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.expand_dims`](https://www.tensorflow.org/api_docs/python/tf/sparse/expand_dims)\n\n\u003cbr /\u003e\n\n tf.sparse.expand_dims(\n sp_input, axis=None, name=None\n )\n\nGiven a tensor `input`, this operation inserts a dimension of length 1 at the\ndimension index `axis` of `input`'s shape. The dimension index follows python\nindexing rules: It's zero-based, a negative index it is counted backward\nfrom the end.\n\nThis operation is useful to:\n\n- Add an outer \"batch\" dimension to a single element.\n- Align axes for broadcasting.\n- To add an inner vector length axis to a tensor of scalars.\n\n#### For example:\n\nIf you have a sparse tensor with shape `[height, width, depth]`: \n\n sp = tf.sparse.SparseTensor(indices=[[3,4,1]], values=[7,],\n dense_shape=[10,10,3])\n\nYou can add an outer `batch` axis by passing `axis=0`: \n\n tf.sparse.expand_dims(sp, axis=0).shape.as_list()\n [1, 10, 10, 3]\n\nThe new axis location matches Python `list.insert(axis, 1)`: \n\n tf.sparse.expand_dims(sp, axis=1).shape.as_list()\n [10, 1, 10, 3]\n\nFollowing standard python indexing rules, a negative `axis` counts from the\nend so `axis=-1` adds an inner most dimension: \n\n tf.sparse.expand_dims(sp, axis=-1).shape.as_list()\n [10, 10, 3, 1]\n\n**Note:** Unlike [`tf.expand_dims`](../../tf/expand_dims) this function includes a default value for the `axis`: `-1`. So if \\`axis is not specified, an inner dimension is added. \n\n sp.shape.as_list()\n [10, 10, 3]\n tf.sparse.expand_dims(sp).shape.as_list()\n [10, 10, 3, 1]\n\nThis operation requires that `axis` is a valid index for `input.shape`,\nfollowing python indexing rules: \n\n -1-tf.rank(input) \u003c= axis \u003c= tf.rank(input)\n\nThis operation is related to:\n\n- [`tf.expand_dims`](../../tf/expand_dims), which provides this functionality for dense tensors.\n- [`tf.squeeze`](../../tf/squeeze), which removes dimensions of size 1, from dense tensors.\n- [`tf.sparse.reshape`](../../tf/sparse/reshape), which provides more flexible reshaping capability.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `sp_input` | A `SparseTensor`. |\n| `axis` | 0-D (scalar). Specifies the dimension index at which to expand the shape of `input`. Must be in the range `[-rank(sp_input) - 1, rank(sp_input)]`. Defaults to `-1`. |\n| `name` | The name of the output `SparseTensor`. |\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 data as `sp_input`, but its shape has an additional dimension of size 1 added. ||\n\n\u003cbr /\u003e"]]