tf.ragged.constant
Stay organized with collections
Save and categorize content based on your preferences.
Constructs a constant RaggedTensor from a nested Python list.
tf.ragged.constant(
pylist,
dtype=None,
ragged_rank=None,
inner_shape=None,
name=None,
row_splits_dtype=tf.dtypes.int64
) -> Union[tf.RaggedTensor
, ops._EagerTensorBase, tf.Operation
]
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
Example:
tf.ragged.constant([[1, 2], [3], [4, 5, 6]])
<tf.RaggedTensor [[1, 2], [3], [4, 5, 6]]>
All scalar values in pylist
must have the same nesting depth K
, and the
returned RaggedTensor
will have rank K
. If pylist
contains no scalar
values, then K
is one greater than the maximum depth of empty lists in
pylist
. All scalar values in pylist
must be compatible with dtype
.
Args |
pylist
|
A nested list , tuple or np.ndarray . Any nested element that
is not a list , tuple or np.ndarray must be a scalar value
compatible with dtype .
|
dtype
|
The type of elements for the returned RaggedTensor . If not
specified, then a default is chosen based on the scalar values in
pylist .
|
ragged_rank
|
An integer specifying the ragged rank of the returned
RaggedTensor . Must be nonnegative and less than K . Defaults to
max(0, K - 1) if inner_shape is not specified. Defaults to
max(0, K - 1 - len(inner_shape)) if inner_shape is specified.
|
inner_shape
|
A tuple of integers specifying the shape for individual inner
values in the returned RaggedTensor . Defaults to () if ragged_rank
is not specified. If ragged_rank is specified, then a default is chosen
based on the contents of pylist .
|
name
|
A name prefix for the returned tensor (optional).
|
row_splits_dtype
|
data type for the constructed RaggedTensor 's row_splits.
One of tf.int32 or tf.int64 .
|
Returns |
A potentially ragged tensor with rank K and the specified ragged_rank ,
containing the values from pylist .
|
Raises |
ValueError
|
If the scalar values in pylist have inconsistent nesting
depth; or if ragged_rank or inner_shape are incompatible with pylist .
|
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.constant\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_factory_ops.py#L35-L91) |\n\nConstructs a constant RaggedTensor from a nested Python list.\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.constant`](https://www.tensorflow.org/api_docs/python/tf/ragged/constant)\n\n\u003cbr /\u003e\n\n tf.ragged.constant(\n pylist,\n dtype=None,\n ragged_rank=None,\n inner_shape=None,\n name=None,\n row_splits_dtype=../../tf/dtypes#int64\n ) -\u003e Union[../../tf/RaggedTensor, ops._EagerTensorBase, ../../tf/Operation]\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) - [Introduction to Tensors](https://www.tensorflow.org/guide/tensor) | - [TF Lattice Aggregate Function Models](https://www.tensorflow.org/lattice/tutorials/aggregate_function_models) - [TF.Text Metrics](https://www.tensorflow.org/text/tutorials/text_similarity) |\n\n#### Example:\n\n tf.ragged.constant([[1, 2], [3], [4, 5, 6]])\n \u003ctf.RaggedTensor [[1, 2], [3], [4, 5, 6]]\u003e\n\nAll scalar values in `pylist` must have the same nesting depth `K`, and the\nreturned `RaggedTensor` will have rank `K`. If `pylist` contains no scalar\nvalues, then `K` is one greater than the maximum depth of empty lists in\n`pylist`. All scalar values in `pylist` must be compatible with `dtype`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `pylist` | A nested `list`, `tuple` or `np.ndarray`. Any nested element that is not a `list`, `tuple` or `np.ndarray` must be a scalar value compatible with `dtype`. |\n| `dtype` | The type of elements for the returned `RaggedTensor`. If not specified, then a default is chosen based on the scalar values in `pylist`. |\n| `ragged_rank` | An integer specifying the ragged rank of the returned `RaggedTensor`. Must be nonnegative and less than `K`. Defaults to `max(0, K - 1)` if `inner_shape` is not specified. Defaults to `max(0, K - 1 - len(inner_shape))` if `inner_shape` is specified. |\n| `inner_shape` | A tuple of integers specifying the shape for individual inner values in the returned `RaggedTensor`. Defaults to `()` if `ragged_rank` is not specified. If `ragged_rank` is specified, then a default is chosen based on the contents of `pylist`. |\n| `name` | A name prefix for the returned tensor (optional). |\n| `row_splits_dtype` | data type for the constructed `RaggedTensor`'s row_splits. One of [`tf.int32`](../../tf#int32) or [`tf.int64`](../../tf#int64). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A potentially ragged tensor with rank `K` and the specified `ragged_rank`, containing the values from `pylist`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If the scalar values in `pylist` have inconsistent nesting depth; or if ragged_rank or inner_shape are incompatible with `pylist`. |\n\n\u003cbr /\u003e"]]