Join the newly launched Discord
community for real-time discussions, peer support, and direct interaction with the Meridian team!
meridian.backend.stack
Stay organized with collections
Save and categorize content based on your preferences.
Stacks a list of rank- R
tensors into one rank- (R+1)
tensor.
meridian
.
backend
.
stack
(
values
,
axis
=
0
,
name
=
'stack'
)
See also tf.concat
, tf.tile
, tf.repeat
.
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:
>>>
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
>>> np.array_equal(np.stack([x, y, z]), tf.stack([x, y, z]))
True
A list of Tensor
objects with the same shape and type.
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)
.
A name for this operation (optional).
A stacked Tensor
with the same type as values
.
If axis
is out of the range [-(R+1), R+1).
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 2025-08-05 UTC.
[[["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 2025-08-05 UTC."],[],[],null,["# meridian.backend.stack\n\n\u003cbr /\u003e\n\nStacks a list of rank-`R` tensors into one rank-`(R+1)` tensor. \n\n meridian.backend.stack(\n values, axis=0, name='stack'\n )\n\nSee also `tf.concat`, `tf.tile`, `tf.repeat`.\n\nPacks the list of tensors in `values` into a tensor with rank one higher than\neach tensor in `values`, by packing them along the `axis` dimension.\nGiven a list of length `N` of tensors of shape `(A, B, C)`;\n\nif `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`.\nif `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`.\nEtc.\n\n#### For example:\n\n \u003e\u003e\u003e x = tf.constant([1, 4])\n \u003e\u003e\u003e y = tf.constant([2, 5])\n \u003e\u003e\u003e z = tf.constant([3, 6])\n \u003e\u003e\u003e tf.stack([x, y, z])\n \u003ctf.Tensor: shape=(3, 2), dtype=int32, numpy=\n array([[1, 4],\n [2, 5],\n [3, 6]], dtype=int32)\u003e\n \u003e\u003e\u003e tf.stack([x, y, z], axis=1)\n \u003ctf.Tensor: shape=(2, 3), dtype=int32, numpy=\n array([[1, 2, 3],\n [4, 5, 6]], dtype=int32)\u003e\n\nThis is the opposite of unstack. The numpy equivalent is `np.stack` \n\n \u003e\u003e\u003e np.array_equal(np.stack([x, y, z]), tf.stack([x, y, z]))\n True\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-----------------------------------------------------------------------------------------------------------------------------------------|\n| `values` | A list of `Tensor` objects with the same shape and type. |\n| `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)`. |\n| `name` | A name for this operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|----------|----------------------------------------------------|\n| `output` | A stacked `Tensor` with the same type as `values`. |\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 out of the range \\[-(R+1), R+1). |\n\n\u003cbr /\u003e"]]