Constructs a RaggedTensorValue from a nested Python list. Warning: This function returns a `RaggedTensorValue`, not a `RaggedTensor`. If you wish to construct a constant `RaggedTensor`, use [`ragged.constant(...)`](constant.md) instead. Example: ```python >>> ragged.constant_value([[1
(pylist, dtype=None, ragged_rank=None, inner_shape=None,
row_splits_dtype="int64")
| 89 | |
| 90 | @tf_export(v1=["ragged.constant_value"]) |
| 91 | def constant_value(pylist, dtype=None, ragged_rank=None, inner_shape=None, |
| 92 | row_splits_dtype="int64"): |
| 93 | """Constructs a RaggedTensorValue from a nested Python list. |
| 94 | |
| 95 | Warning: This function returns a `RaggedTensorValue`, not a `RaggedTensor`. |
| 96 | If you wish to construct a constant `RaggedTensor`, use |
| 97 | [`ragged.constant(...)`](constant.md) instead. |
| 98 | |
| 99 | Example: |
| 100 | |
| 101 | ```python |
| 102 | >>> ragged.constant_value([[1, 2], [3], [4, 5, 6]]) |
| 103 | RaggedTensorValue(values=[1, 2, 3, 4, 5, 6], splits=[0, 2, 3, 6]) |
| 104 | ``` |
| 105 | |
| 106 | All scalar values in `pylist` must have the same nesting depth `K`, and the |
| 107 | returned `RaggedTensorValue` will have rank `K`. If `pylist` contains no |
| 108 | scalar values, then `K` is one greater than the maximum depth of empty lists |
| 109 | in `pylist`. All scalar values in `pylist` must be compatible with `dtype`. |
| 110 | |
| 111 | Args: |
| 112 | pylist: A nested `list`, `tuple` or `np.ndarray`. Any nested element that |
| 113 | is not a `list` or `tuple` must be a scalar value compatible with `dtype`. |
| 114 | dtype: `numpy.dtype`. The type of elements for the returned `RaggedTensor`. |
| 115 | If not specified, then a default is chosen based on the scalar values in |
| 116 | `pylist`. |
| 117 | ragged_rank: An integer specifying the ragged rank of the returned |
| 118 | `RaggedTensorValue`. Must be nonnegative and less than `K`. Defaults to |
| 119 | `max(0, K - 1)` if `inner_shape` is not specified. Defaults to `max(0, K |
| 120 | - 1 - len(inner_shape))` if `inner_shape` is specified. |
| 121 | inner_shape: A tuple of integers specifying the shape for individual inner |
| 122 | values in the returned `RaggedTensorValue`. Defaults to `()` if |
| 123 | `ragged_rank` is not specified. If `ragged_rank` is specified, then a |
| 124 | default is chosen based on the contents of `pylist`. |
| 125 | row_splits_dtype: data type for the constructed `RaggedTensorValue`'s |
| 126 | row_splits. One of `numpy.int32` or `numpy.int64`. |
| 127 | |
| 128 | Returns: |
| 129 | A `tf.RaggedTensorValue` or `numpy.array` with rank `K` and the specified |
| 130 | `ragged_rank`, containing the values from `pylist`. |
| 131 | |
| 132 | Raises: |
| 133 | ValueError: If the scalar values in `pylist` have inconsistent nesting |
| 134 | depth; or if ragged_rank or inner_shape are incompatible with `pylist`. |
| 135 | """ |
| 136 | if dtype is not None and isinstance(dtype, dtypes.DType): |
| 137 | dtype = dtype.as_numpy_dtype |
| 138 | row_splits_dtype = dtypes.as_dtype(row_splits_dtype).as_numpy_dtype |
| 139 | def _ragged_factory(values, row_splits): |
| 140 | row_splits = np.array(row_splits, dtype=row_splits_dtype) |
| 141 | return ragged_tensor_value.RaggedTensorValue(values, row_splits) |
| 142 | |
| 143 | def _inner_factory(pylist, dtype, shape, name=None): # pylint: disable=unused-argument |
| 144 | return np.reshape(np.array(pylist, dtype=dtype), shape) |
| 145 | |
| 146 | return _constant_value(_ragged_factory, _inner_factory, pylist, dtype, |
| 147 | ragged_rank, inner_shape) |
| 148 |
nothing calls this directly
no test coverage detected