Converts a `tf.Tensor` into a `RaggedTensor`. The set of absent/default values may be specified using a vector of lengths or a padding value (but not both). If `lengths` is specified, then the output tensor will satisfy `output[row] = tensor[row][:lengths[row]]`. If 'lengths' is a
(cls,
tensor,
lengths=None,
padding=None,
ragged_rank=1,
name=None,
row_splits_dtype=dtypes.int64)
| 1319 | |
| 1320 | @classmethod |
| 1321 | def from_tensor(cls, |
| 1322 | tensor, |
| 1323 | lengths=None, |
| 1324 | padding=None, |
| 1325 | ragged_rank=1, |
| 1326 | name=None, |
| 1327 | row_splits_dtype=dtypes.int64): |
| 1328 | """Converts a `tf.Tensor` into a `RaggedTensor`. |
| 1329 | |
| 1330 | The set of absent/default values may be specified using a vector of lengths |
| 1331 | or a padding value (but not both). If `lengths` is specified, then the |
| 1332 | output tensor will satisfy `output[row] = tensor[row][:lengths[row]]`. If |
| 1333 | 'lengths' is a list of lists or tuple of lists, those lists will be used |
| 1334 | as nested row lengths. If `padding` is specified, then any row *suffix* |
| 1335 | consisting entirely of `padding` will be excluded from the returned |
| 1336 | `RaggedTensor`. If neither `lengths` nor `padding` is specified, then the |
| 1337 | returned `RaggedTensor` will have no absent/default values. |
| 1338 | |
| 1339 | Examples: |
| 1340 | |
| 1341 | ```python |
| 1342 | >>> dt = tf.constant([[5, 7, 0], [0, 3, 0], [6, 0, 0]]) |
| 1343 | >>> tf.RaggedTensor.from_tensor(dt) |
| 1344 | <tf.RaggedTensor [[5, 7, 0], [0, 3, 0], [6, 0, 0]]> |
| 1345 | >>> tf.RaggedTensor.from_tensor(dt, lengths=[1, 0, 3]) |
| 1346 | <tf.RaggedTensor [[5], [], [6, 0, 0]]> |
| 1347 | |
| 1348 | >>> tf.RaggedTensor.from_tensor(dt, padding=0) |
| 1349 | <tf.RaggedTensor [[5, 7], [0, 3], [6]]> |
| 1350 | |
| 1351 | >>> dt = tf.constant([[[5, 0], [7, 0], [0, 0]], |
| 1352 | [[0, 0], [3, 0], [0, 0]], |
| 1353 | [[6, 0], [0, 0], [0, 0]]]) |
| 1354 | >>> tf.RaggedTensor.from_tensor(dt, lengths=([2, 0, 3], [1, 1, 2, 0, 1])) |
| 1355 | <tf.RaggedTensor [[[5], [7]], [], [[6, 0], [], [0]]]> |
| 1356 | ``` |
| 1357 | |
| 1358 | Args: |
| 1359 | tensor: The `Tensor` to convert. Must have rank `ragged_rank + 1` or |
| 1360 | higher. |
| 1361 | lengths: An optional set of row lengths, specified using a 1-D integer |
| 1362 | `Tensor` whose length is equal to `tensor.shape[0]` (the number of rows |
| 1363 | in `tensor`). If specified, then `output[row]` will contain |
| 1364 | `tensor[row][:lengths[row]]`. Negative lengths are treated as zero. You |
| 1365 | may optionally pass a list or tuple of lengths to this argument, which |
| 1366 | will be used as nested row lengths to construct a ragged tensor with |
| 1367 | multiple ragged dimensions. |
| 1368 | padding: An optional padding value. If specified, then any row suffix |
| 1369 | consisting entirely of `padding` will be excluded from the returned |
| 1370 | RaggedTensor. `padding` is a `Tensor` with the same dtype as `tensor` |
| 1371 | and with `shape=tensor.shape[ragged_rank + 1:]`. |
| 1372 | ragged_rank: Integer specifying the ragged rank for the returned |
| 1373 | `RaggedTensor`. Must be greater than zero. |
| 1374 | name: A name prefix for the returned tensors (optional). |
| 1375 | row_splits_dtype: `dtype` for the returned `RaggedTensor`'s `row_splits` |
| 1376 | tensor. One of `tf.int32` or `tf.int64`. |
| 1377 | |
| 1378 | Returns: |