Constructs a constant RaggedTensor or RaggedTensorValue. Args: ragged_factory: A factory function with the signature: `ragged_factory(values, row_splits)` inner_factory: A factory function with the signature: `inner_factory(pylist, dtype, shape, name)` pylist: A nested `li
(ragged_factory, inner_factory, pylist, dtype, ragged_rank,
inner_shape)
| 148 | |
| 149 | |
| 150 | def _constant_value(ragged_factory, inner_factory, pylist, dtype, ragged_rank, |
| 151 | inner_shape): |
| 152 | """Constructs a constant RaggedTensor or RaggedTensorValue. |
| 153 | |
| 154 | Args: |
| 155 | ragged_factory: A factory function with the signature: |
| 156 | `ragged_factory(values, row_splits)` |
| 157 | inner_factory: A factory function with the signature: `inner_factory(pylist, |
| 158 | dtype, shape, name)` |
| 159 | pylist: A nested `list`, `tuple` or `np.ndarray`. |
| 160 | dtype: Data type for returned value. |
| 161 | ragged_rank: Ragged rank for returned value. |
| 162 | inner_shape: Inner value shape for returned value. |
| 163 | |
| 164 | Returns: |
| 165 | A value returned by `ragged_factory` or `inner_factory`. |
| 166 | |
| 167 | Raises: |
| 168 | ValueError: If the scalar values in `pylist` have inconsistent nesting |
| 169 | depth; or if ragged_rank or inner_shape are incompatible with `pylist`. |
| 170 | """ |
| 171 | if ragged_tensor.is_ragged(pylist): |
| 172 | raise TypeError("pylist may not be a RaggedTensor or RaggedTensorValue.") |
| 173 | # np.ndim builds an array, so we short-circuit lists and tuples. |
| 174 | if not isinstance(pylist, (list, tuple)) and np.ndim(pylist) == 0: |
| 175 | # Scalar value |
| 176 | if ragged_rank is not None and ragged_rank != 0: |
| 177 | raise ValueError("Invalid pylist=%r: incompatible with ragged_rank=%d" % |
| 178 | (pylist, ragged_rank)) |
| 179 | if inner_shape is not None and inner_shape: |
| 180 | raise ValueError( |
| 181 | "Invalid pylist=%r: incompatible with dim(inner_shape)=%d" % |
| 182 | (pylist, len(inner_shape))) |
| 183 | return inner_factory(pylist, dtype, ()) |
| 184 | |
| 185 | if ragged_rank is not None and ragged_rank < 0: |
| 186 | raise ValueError( |
| 187 | "Invalid ragged_rank=%r: must be nonnegative" % ragged_rank) |
| 188 | |
| 189 | # Find the depth of scalar values in `pylist`. |
| 190 | scalar_depth, max_depth = _find_scalar_and_max_depth(pylist) |
| 191 | if scalar_depth is not None: |
| 192 | if max_depth > scalar_depth: |
| 193 | raise ValueError("Invalid pylist=%r: empty list nesting is greater " |
| 194 | "than scalar value nesting" % pylist) |
| 195 | |
| 196 | # If both inner_shape and ragged_rank were specified, then check that |
| 197 | # they are compatible with pylist. |
| 198 | if inner_shape is not None and ragged_rank is not None: |
| 199 | expected_depth = ragged_rank + len(inner_shape) + 1 |
| 200 | if ((scalar_depth is not None and expected_depth != scalar_depth) or |
| 201 | (scalar_depth is None and expected_depth < max_depth)): |
| 202 | raise ValueError( |
| 203 | "Invalid pylist=%r: incompatible with ragged_rank=%d " |
| 204 | "and dim(inner_shape)=%d" % (pylist, ragged_rank, len(inner_shape))) |
| 205 | |
| 206 | # Check if the result is a `Tensor`. |
| 207 | if (ragged_rank == 0 or |
no test coverage detected