Inserts a dimension of 1 into a tensor's shape. Given a tensor `input`, this operation inserts a dimension of 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. Th
(input, axis=None, name=None, dim=None)
| 212 | @dispatch.add_dispatch_support |
| 213 | @deprecation.deprecated_args(None, "Use the `axis` argument instead", "dim") |
| 214 | def expand_dims(input, axis=None, name=None, dim=None): |
| 215 | """Inserts a dimension of 1 into a tensor's shape. |
| 216 | |
| 217 | Given a tensor `input`, this operation inserts a dimension of 1 at the |
| 218 | dimension index `axis` of `input`'s shape. The dimension index `axis` starts |
| 219 | at zero; if you specify a negative number for `axis` it is counted backward |
| 220 | from the end. |
| 221 | |
| 222 | This operation is useful if you want to add a batch dimension to a single |
| 223 | element. For example, if you have a single image of shape `[height, width, |
| 224 | channels]`, you can make it a batch of 1 image with `expand_dims(image, 0)`, |
| 225 | which will make the shape `[1, height, width, channels]`. |
| 226 | |
| 227 | Other examples: |
| 228 | |
| 229 | ```python |
| 230 | # 't' is a tensor of shape [2] |
| 231 | tf.shape(tf.expand_dims(t, 0)) # [1, 2] |
| 232 | tf.shape(tf.expand_dims(t, 1)) # [2, 1] |
| 233 | tf.shape(tf.expand_dims(t, -1)) # [2, 1] |
| 234 | |
| 235 | # 't2' is a tensor of shape [2, 3, 5] |
| 236 | tf.shape(tf.expand_dims(t2, 0)) # [1, 2, 3, 5] |
| 237 | tf.shape(tf.expand_dims(t2, 2)) # [2, 3, 1, 5] |
| 238 | tf.shape(tf.expand_dims(t2, 3)) # [2, 3, 5, 1] |
| 239 | ``` |
| 240 | |
| 241 | This operation requires that: |
| 242 | |
| 243 | `-1-input.dims() <= dim <= input.dims()` |
| 244 | |
| 245 | This operation is related to `squeeze()`, which removes dimensions of |
| 246 | size 1. |
| 247 | |
| 248 | Args: |
| 249 | input: A `Tensor`. |
| 250 | axis: 0-D (scalar). Specifies the dimension index at which to expand the |
| 251 | shape of `input`. Must be in the range `[-rank(input) - 1, rank(input)]`. |
| 252 | name: The name of the output `Tensor` (optional). |
| 253 | dim: 0-D (scalar). Equivalent to `axis`, to be deprecated. |
| 254 | |
| 255 | Returns: |
| 256 | A `Tensor` with the same data as `input`, but its shape has an additional |
| 257 | dimension of size 1 added. |
| 258 | |
| 259 | Raises: |
| 260 | ValueError: if either both or neither of `dim` and `axis` are specified. |
| 261 | """ |
| 262 | axis = deprecation.deprecated_argument_lookup("axis", axis, "dim", dim) |
| 263 | if axis is None: |
| 264 | raise ValueError("Must specify an axis argument to tf.expand_dims()") |
| 265 | return expand_dims_v2(input, axis, name) |
| 266 | |
| 267 | |
| 268 | @tf_export("expand_dims", v1=[]) |
no test coverage detected