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, name=None)
| 268 | @tf_export("expand_dims", v1=[]) |
| 269 | @dispatch.add_dispatch_support |
| 270 | def expand_dims_v2(input, axis, name=None): |
| 271 | """Inserts a dimension of 1 into a tensor's shape. |
| 272 | |
| 273 | Given a tensor `input`, this operation inserts a dimension of 1 at the |
| 274 | dimension index `axis` of `input`'s shape. The dimension index `axis` starts |
| 275 | at zero; if you specify a negative number for `axis` it is counted backward |
| 276 | from the end. |
| 277 | |
| 278 | This operation is useful if you want to add a batch dimension to a single |
| 279 | element. For example, if you have a single image of shape `[height, width, |
| 280 | channels]`, you can make it a batch of 1 image with `expand_dims(image, 0)`, |
| 281 | which will make the shape `[1, height, width, channels]`. |
| 282 | |
| 283 | Other examples: |
| 284 | |
| 285 | ```python |
| 286 | # 't' is a tensor of shape [2] |
| 287 | tf.shape(tf.expand_dims(t, 0)) # [1, 2] |
| 288 | tf.shape(tf.expand_dims(t, 1)) # [2, 1] |
| 289 | tf.shape(tf.expand_dims(t, -1)) # [2, 1] |
| 290 | |
| 291 | # 't2' is a tensor of shape [2, 3, 5] |
| 292 | tf.shape(tf.expand_dims(t2, 0)) # [1, 2, 3, 5] |
| 293 | tf.shape(tf.expand_dims(t2, 2)) # [2, 3, 1, 5] |
| 294 | tf.shape(tf.expand_dims(t2, 3)) # [2, 3, 5, 1] |
| 295 | ``` |
| 296 | |
| 297 | This operation requires that: |
| 298 | |
| 299 | `-1-input.dims() <= dim <= input.dims()` |
| 300 | |
| 301 | This operation is related to `squeeze()`, which removes dimensions of |
| 302 | size 1. |
| 303 | |
| 304 | Args: |
| 305 | input: A `Tensor`. |
| 306 | axis: 0-D (scalar). Specifies the dimension index at which to expand the |
| 307 | shape of `input`. Must be in the range `[-rank(input) - 1, rank(input)]`. |
| 308 | name: The name of the output `Tensor` (optional). |
| 309 | |
| 310 | Returns: |
| 311 | A `Tensor` with the same data as `input`, but its shape has an additional |
| 312 | dimension of size 1 added. |
| 313 | """ |
| 314 | return gen_array_ops.expand_dims(input, axis, name) |
| 315 | |
| 316 | |
| 317 | # pylint: enable=redefined-builtin,protected-access |
no test coverage detected