<br />

<button value="stable" default="">See Stable</button> <button value="nightly">See Nightly</button>

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/array_ops_stack.py#L24-L85) |

Stacks a list of rank-`R` tensors into one rank-`(R+1)` tensor.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.stack`](https://www.tensorflow.org/api_docs/python/tf/stack.md.txt)

<br />

```python
tf.stack(
    values, axis=0, name='stack'
)
```

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [Extension types](https://www.tensorflow.org/guide/extension_type.md.txt) - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data.md.txt) - [TensorFlow basics](https://www.tensorflow.org/guide/basics.md.txt) - [Random number generation](https://www.tensorflow.org/guide/random_numbers.md.txt) | - [Time series forecasting](https://www.tensorflow.org/tutorials/structured_data/time_series.md.txt) - [Custom training: walkthrough](https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough.md.txt) - [pix2pix: Image-to-image translation with a conditional GAN](https://www.tensorflow.org/tutorials/generative/pix2pix.md.txt) - [Overfit and underfit](https://www.tensorflow.org/tutorials/keras/overfit_and_underfit.md.txt) - [Load CSV data](https://www.tensorflow.org/tutorials/load_data/csv.md.txt) |

See also [`tf.concat`](https://www.tensorflow.org/api_docs/python/tf/concat.md.txt), [`tf.tile`](https://www.tensorflow.org/api_docs/python/tf/tile.md.txt), [`tf.repeat`](https://www.tensorflow.org/api_docs/python/tf/repeat.md.txt).

Packs the list of tensors in `values` into a tensor with rank one higher than
each tensor in `values`, by packing them along the `axis` dimension.
Given a list of length `N` of tensors of shape `(A, B, C)`;

if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`.
if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`.
Etc.

#### For example:

```python
x = tf.constant([1, 4])
y = tf.constant([2, 5])
z = tf.constant([3, 6])
tf.stack([x, y, z])
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 4],
       [2, 5],
       [3, 6]], dtype=int32)>
tf.stack([x, y, z], axis=1)
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>
```

This is the opposite of unstack. The numpy equivalent is `np.stack`

```python
np.array_equal(np.stack([x, y, z]), tf.stack([x, y, z]))
True
```

<br />

<br />

<br />

| ## Args ||
|---|---|
| `values` | A list of `Tensor` objects with the same shape and type. |
| `axis` | An `int`. The axis to stack along. Defaults to the first dimension. Negative values wrap around, so the valid range is `[-(R+1), R+1)`. |
| `name` | A name for this operation (optional). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `output` | A stacked `Tensor` with the same type as `values`. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If `axis` is out of the range \[-(R+1), R+1). |

<br />