tf.expand_dims
Stay organized with collections
Save and categorize content based on your preferences.
Returns a tensor with an additional dimension inserted at index axis
.
tf.expand_dims(
input, axis, name=None
)
Given a tensor input
, this operation inserts a dimension of size 1 at the
dimension index axis
of input
's shape. The dimension index axis
starts
at zero; if you specify a negative number for axis
it is counted backward
from the end.
This operation is useful if you want to add a batch dimension to a single
element. For example, if you have a single image of shape [height, width,
channels]
, you can make it a batch of one image with expand_dims(image, 0)
,
which will make the shape [1, height, width, channels]
.
Examples:
t = [[1, 2, 3],[4, 5, 6]] # shape [2, 3]
tf.expand_dims(t, 0)
<tf.Tensor: shape=(1, 2, 3), dtype=int32, numpy=
array([[[1, 2, 3],
[4, 5, 6]]], dtype=int32)>
tf.expand_dims(t, 1)
<tf.Tensor: shape=(2, 1, 3), dtype=int32, numpy=
array([[[1, 2, 3]],
[[4, 5, 6]]], dtype=int32)>
tf.expand_dims(t, 2)
<tf.Tensor: shape=(2, 3, 1), dtype=int32, numpy=
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]], dtype=int32)>
tf.expand_dims(t, -1) # Last dimension index. In this case, same as 2.
<tf.Tensor: shape=(2, 3, 1), dtype=int32, numpy=
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]], dtype=int32)>
This operation is related to:
tf.squeeze
, which removes dimensions of size 1.
tf.reshape
, which provides more flexible reshaping capability
Args |
input
|
A Tensor .
|
axis
|
Integer specifying the dimension index at which to expand the
shape of input . Given an input of D dimensions, axis must be in range
[-(D+1), D] (inclusive).
|
name
|
Optional string. The name of the output Tensor .
|
Returns |
A tensor with the same data as input , with an additional dimension
inserted at the index specified by axis .
|
Raises |
ValueError
|
If axis is not specified.
|
InvalidArgumentError
|
If axis is out of range [-(D+1), D] .
|
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.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.expand_dims\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/expand_dims) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/ops/array_ops.py#L332-L399) |\n\nReturns a tensor with an additional dimension inserted at index `axis`. \n\n tf.expand_dims(\n input, axis, name=None\n )\n\nGiven a tensor `input`, this operation inserts a dimension of size 1 at the\ndimension index `axis` of `input`'s shape. The dimension index `axis` starts\nat zero; if you specify a negative number for `axis` it is counted backward\nfrom the end.\n\nThis operation is useful if you want to add a batch dimension to a single\nelement. For example, if you have a single image of shape `[height, width,\nchannels]`, you can make it a batch of one image with `expand_dims(image, 0)`,\nwhich will make the shape `[1, height, width, channels]`.\n\n#### Examples:\n\n t = [[1, 2, 3],[4, 5, 6]] # shape [2, 3]\n\n tf.expand_dims(t, 0)\n \u003ctf.Tensor: shape=(1, 2, 3), dtype=int32, numpy=\n array([[[1, 2, 3],\n [4, 5, 6]]], dtype=int32)\u003e\n\n tf.expand_dims(t, 1)\n \u003ctf.Tensor: shape=(2, 1, 3), dtype=int32, numpy=\n array([[[1, 2, 3]],\n [[4, 5, 6]]], dtype=int32)\u003e\n\n tf.expand_dims(t, 2)\n \u003ctf.Tensor: shape=(2, 3, 1), dtype=int32, numpy=\n array([[[1],\n [2],\n [3]],\n [[4],\n [5],\n [6]]], dtype=int32)\u003e\n\n tf.expand_dims(t, -1) # Last dimension index. In this case, same as 2.\n \u003ctf.Tensor: shape=(2, 3, 1), dtype=int32, numpy=\n array([[[1],\n [2],\n [3]],\n [[4],\n [5],\n [6]]], dtype=int32)\u003e\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\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A `Tensor`. |\n| `axis` | Integer specifying the dimension index at which to expand the shape of `input`. Given an input of D dimensions, `axis` must be in range `[-(D+1), D]` (inclusive). |\n| `name` | Optional string. The name of the output `Tensor`. |\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`, with an additional dimension inserted at the index specified by `axis`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|------------------------|------------------------------------------|\n| `ValueError` | If `axis` is not specified. |\n| `InvalidArgumentError` | If `axis` is out of range `[-(D+1), D]`. |\n\n\u003cbr /\u003e"]]