Returns the rank of a tensor. Args: input: A `Tensor` or `SparseTensor`. name: A name for the operation (optional). optimize: if true, encode the rank as a constant when possible. Returns: A `Tensor` of type `int32`.
(input, name=None, optimize=True)
| 607 | |
| 608 | |
| 609 | def rank_internal(input, name=None, optimize=True): |
| 610 | # pylint: disable=redefined-builtin |
| 611 | """Returns the rank of a tensor. |
| 612 | |
| 613 | Args: |
| 614 | input: A `Tensor` or `SparseTensor`. |
| 615 | name: A name for the operation (optional). |
| 616 | optimize: if true, encode the rank as a constant when possible. |
| 617 | |
| 618 | Returns: |
| 619 | A `Tensor` of type `int32`. |
| 620 | """ |
| 621 | with ops.name_scope(name, "Rank", [input]) as name: |
| 622 | if isinstance( |
| 623 | input, (sparse_tensor.SparseTensor, sparse_tensor.SparseTensorValue)): |
| 624 | return gen_array_ops.size(input.dense_shape, name=name) |
| 625 | else: |
| 626 | input_tensor = ops.convert_to_tensor(input) |
| 627 | input_shape = input_tensor.get_shape() |
| 628 | if optimize and input_shape.ndims is not None: |
| 629 | return constant(input_shape.ndims, dtypes.int32, name=name) |
| 630 | return gen_array_ops.rank(input, name=name) |
| 631 | |
| 632 | |
| 633 | _SLICE_TYPE_ERROR = ( |