Converts this `RaggedTensor` into a `tf.Tensor`. Example: ```python >>> rt = ragged.constant([[9, 8, 7], [], [6, 5], [4]]) >>> print rt.to_tensor() [[9 8 7] [0 0 0] [6 5 0] [4 0 0]] ``` Args: default_value: Value to set for indices not specified in
(self, default_value=None, name=None)
| 1503 | return cls.from_row_splits(values, splits, validate=False) |
| 1504 | |
| 1505 | def to_tensor(self, default_value=None, name=None): |
| 1506 | """Converts this `RaggedTensor` into a `tf.Tensor`. |
| 1507 | |
| 1508 | Example: |
| 1509 | |
| 1510 | ```python |
| 1511 | >>> rt = ragged.constant([[9, 8, 7], [], [6, 5], [4]]) |
| 1512 | >>> print rt.to_tensor() |
| 1513 | [[9 8 7] |
| 1514 | [0 0 0] |
| 1515 | [6 5 0] |
| 1516 | [4 0 0]] |
| 1517 | ``` |
| 1518 | |
| 1519 | Args: |
| 1520 | default_value: Value to set for indices not specified in `self`. Defaults |
| 1521 | to zero. `default_value` must be broadcastable to |
| 1522 | `self.shape[self.ragged_rank + 1:]`. |
| 1523 | name: A name prefix for the returned tensors (optional). |
| 1524 | |
| 1525 | Returns: |
| 1526 | A `Tensor` with shape `ragged.bounding_shape(self)` and the |
| 1527 | values specified by the non-empty values in `self`. Empty values are |
| 1528 | assigned `default_value`. |
| 1529 | """ |
| 1530 | with ops.name_scope(name, "RaggedToTensor", [self, default_value]): |
| 1531 | if default_value is not None: |
| 1532 | default_value = ops.convert_to_tensor( |
| 1533 | default_value, name="default_value", dtype=self.dtype) |
| 1534 | |
| 1535 | # If ragged_rank > 1, then recursively convert the ragged values into a |
| 1536 | # `Tensor` before we proceed. |
| 1537 | values = self.values |
| 1538 | if is_ragged(values): |
| 1539 | values = values.to_tensor(default_value) |
| 1540 | |
| 1541 | # Tile the default value, if necessary. |
| 1542 | if default_value is not None: |
| 1543 | if values.shape.ndims is not None: |
| 1544 | default_value.shape.with_rank_at_most(values.shape.ndims - 1) |
| 1545 | if (values.shape.ndims is None or default_value.shape.ndims is None or |
| 1546 | values.shape.ndims != default_value.shape.ndims + 1): |
| 1547 | value_shape = array_ops.shape(values)[1:] |
| 1548 | default_value = array_ops.broadcast_to(default_value, value_shape) |
| 1549 | default_value.shape.assert_is_compatible_with(values.shape[1:]) |
| 1550 | |
| 1551 | # Get the expected dense shape ([nrows, ncols] + value_shape). |
| 1552 | rt_row_lengths = [self.row_splits[1:] - self.row_splits[:-1]] |
| 1553 | nrows = array_ops.shape(self.row_splits, |
| 1554 | out_type=self._row_splits.dtype)[0] - 1 |
| 1555 | ncols = math_ops.maximum(math_ops.reduce_max(rt_row_lengths), 0) |
| 1556 | values_shape = array_ops.shape(values, out_type=self._row_splits.dtype) |
| 1557 | value_shape = values_shape[1:] |
| 1558 | nvals = values_shape[0] |
| 1559 | |
| 1560 | # Build a default value if none was supplied. |
| 1561 | if default_value is None: |
| 1562 | default_value = array_ops.zeros(value_shape, dtype=values.dtype) |