tf.ragged.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)
RaggedTensor
.
tf.ragged.stack(
values: typing.List[ragged_tensor.RaggedOrDense], axis=0, name=None
)
Given a list of tensors or ragged tensors with the same rank R
(R >= axis
), returns a rank-R+1
RaggedTensor
result
such that
result[i0...iaxis]
is [value[i0...iaxis] for value in values]
.
Examples:
# Stacking two ragged tensors.
t1 = tf.ragged.constant([[1, 2], [3, 4, 5]])
t2 = tf.ragged.constant([[6], [7, 8, 9]])
tf.ragged.stack([t1, t2], axis=0)
<tf.RaggedTensor [[[1, 2], [3, 4, 5]], [[6], [7, 8, 9]]]>
tf.ragged.stack([t1, t2], axis=1)
<tf.RaggedTensor [[[1, 2], [6]], [[3, 4, 5], [7, 8, 9]]]>
# Stacking two dense tensors with different sizes.
t3 = tf.constant([[1, 2, 3], [4, 5, 6]])
t4 = tf.constant([[5], [6], [7]])
tf.ragged.stack([t3, t4], axis=0)
<tf.RaggedTensor [[[1, 2, 3], [4, 5, 6]], [[5], [6], [7]]]>
Args |
values
|
A list of tf.Tensor or tf.RaggedTensor . May not be empty. All
values must have the same rank and the same dtype; but unlike
tf.stack , they can have arbitrary dimension sizes.
|
axis
|
A python integer, indicating the dimension along which to stack.
(Note: Unlike tf.stack , the axis parameter must be statically known.)
Negative values are supported only if the rank of at least one
values value is statically known.
|
name
|
A name prefix for the returned tensor (optional).
|
Returns |
A RaggedTensor with rank R+1 (if R>0 ).
If R==0 , then the result will be returned as a 1D Tensor , since
RaggedTensor can only be used when rank>1 .
result.ragged_rank=1+max(axis, max(rt.ragged_rank for rt in values])) .
|
Raises |
ValueError
|
If values is empty, if axis is out of bounds or if
the input tensors have different ranks.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.ragged.stack\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/ragged/ragged_concat_ops.py#L73-L124) |\n\nStacks a list of rank-`R` tensors into one rank-`(R+1)` `RaggedTensor`.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.ragged.stack`](https://www.tensorflow.org/api_docs/python/tf/ragged/stack)\n\n\u003cbr /\u003e\n\n tf.ragged.stack(\n values: typing.List[ragged_tensor.RaggedOrDense], axis=0, name=None\n )\n\nGiven a list of tensors or ragged tensors with the same rank `R`\n(`R \u003e= axis`), returns a rank-`R+1` `RaggedTensor` `result` such that\n`result[i0...iaxis]` is `[value[i0...iaxis] for value in values]`.\n\n#### Examples:\n\n # Stacking two ragged tensors.\n t1 = tf.ragged.constant([[1, 2], [3, 4, 5]])\n t2 = tf.ragged.constant([[6], [7, 8, 9]])\n tf.ragged.stack([t1, t2], axis=0)\n \u003ctf.RaggedTensor [[[1, 2], [3, 4, 5]], [[6], [7, 8, 9]]]\u003e\n tf.ragged.stack([t1, t2], axis=1)\n \u003ctf.RaggedTensor [[[1, 2], [6]], [[3, 4, 5], [7, 8, 9]]]\u003e\n\n # Stacking two dense tensors with different sizes.\n t3 = tf.constant([[1, 2, 3], [4, 5, 6]])\n t4 = tf.constant([[5], [6], [7]])\n tf.ragged.stack([t3, t4], axis=0)\n \u003ctf.RaggedTensor [[[1, 2, 3], [4, 5, 6]], [[5], [6], [7]]]\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `values` | A list of [`tf.Tensor`](../../tf/Tensor) or [`tf.RaggedTensor`](../../tf/RaggedTensor). May not be empty. All `values` must have the same rank and the same dtype; but unlike [`tf.stack`](../../tf/stack), they can have arbitrary dimension sizes. |\n| `axis` | A python integer, indicating the dimension along which to stack. (Note: Unlike [`tf.stack`](../../tf/stack), the `axis` parameter must be statically known.) Negative values are supported only if the rank of at least one `values` value is statically known. |\n| `name` | A name prefix for the returned tensor (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `RaggedTensor` with rank `R+1` (if `R\u003e0`). If `R==0`, then the result will be returned as a 1D `Tensor`, since `RaggedTensor` can only be used when `rank\u003e1`. `result.ragged_rank=1+max(axis, max(rt.ragged_rank for rt in values]))`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------------------------------------------|\n| `ValueError` | If `values` is empty, if `axis` is out of bounds or if the input tensors have different ranks. |\n\n\u003cbr /\u003e"]]