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 single image of shape [height, width, channels]:
image=tf.zeros([10,10,3])
You can add an outer batch axis by passing axis=0:
[[["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 2023-10-06 UTC."],[],[],null,["# tf.compat.v1.expand_dims\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/array_ops.py#L312-L382) |\n\nReturns a tensor with a length 1 axis inserted at index `axis`. (deprecated arguments) \n\n tf.compat.v1.expand_dims(\n input, axis=None, name=None, dim=None\n )\n\n| **Deprecated:** SOME ARGUMENTS ARE DEPRECATED: `(dim)`. They will be removed in a future version. Instructions for updating: Use the `axis` argument instead\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 single image of shape `[height, width, channels]`: \n\n image = tf.zeros([10,10,3])\n\nYou can add an outer `batch` axis by passing `axis=0`: \n\n tf.expand_dims(image, axis=0).shape.as_list()\n [1, 10, 10, 3]\n\nThe new axis location matches Python `list.insert(axis, 1)`: \n\n tf.expand_dims(image, 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.expand_dims(image, -1).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.squeeze`](../../../tf/squeeze), which removes dimensions of size 1.\n- [`tf.reshape`](../../../tf/reshape), which provides more flexible reshaping capability.\n- [`tf.sparse.expand_dims`](../../../tf/sparse/expand_dims), which provides this functionality for [`tf.SparseTensor`](../../../tf/sparse/SparseTensor)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|----------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A `Tensor`. |\n| `axis` | 0-D (scalar). Specifies the dimension index at which to expand the shape of `input`. Must be in the range `[-rank(input) - 1, rank(input)]`. |\n| `name` | The name of the output `Tensor` (optional). |\n| `dim` | 0-D (scalar). Equivalent to `axis`, to be deprecated. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` with the same data as `input`, but its shape has an additional dimension of size 1 added. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------------------------------------------|\n| `ValueError` | if either both or neither of `dim` and `axis` are specified. |\n\n\u003cbr /\u003e"]]