Represents the shape of a `Tensor`. A `TensorShape` represents a possibly-partial shape specification for a `Tensor`. It may be one of the following: * *Fully-known shape:* has a known number of dimensions and a known size for each dimension. e.g. `TensorShape([16, 256])` * *Partially-
| 720 | |
| 721 | @tf_export("TensorShape") |
| 722 | class TensorShape(object): |
| 723 | """Represents the shape of a `Tensor`. |
| 724 | |
| 725 | A `TensorShape` represents a possibly-partial shape specification for a |
| 726 | `Tensor`. It may be one of the following: |
| 727 | |
| 728 | * *Fully-known shape:* has a known number of dimensions and a known size |
| 729 | for each dimension. e.g. `TensorShape([16, 256])` |
| 730 | * *Partially-known shape:* has a known number of dimensions, and an unknown |
| 731 | size for one or more dimension. e.g. `TensorShape([None, 256])` |
| 732 | * *Unknown shape:* has an unknown number of dimensions, and an unknown |
| 733 | size in all dimensions. e.g. `TensorShape(None)` |
| 734 | |
| 735 | If a tensor is produced by an operation of type `"Foo"`, its shape |
| 736 | may be inferred if there is a registered shape function for |
| 737 | `"Foo"`. See [Shape |
| 738 | functions](https://tensorflow.org/extend/adding_an_op#shape_functions_in_c) |
| 739 | for details of shape functions and how to register them. Alternatively, |
| 740 | the shape may be set explicitly using `tf.Tensor.set_shape`. |
| 741 | """ |
| 742 | |
| 743 | def __init__(self, dims): |
| 744 | """Creates a new TensorShape with the given dimensions. |
| 745 | |
| 746 | Args: |
| 747 | dims: A list of Dimensions, or None if the shape is unspecified. |
| 748 | |
| 749 | Raises: |
| 750 | TypeError: If dims cannot be converted to a list of dimensions. |
| 751 | """ |
| 752 | if dims is None: |
| 753 | self._dims = None |
| 754 | elif isinstance(dims, compat.bytes_or_text_types): |
| 755 | raise TypeError("A string has ambiguous TensorShape, please wrap in a " |
| 756 | "list or convert to an int: %s" % dims) |
| 757 | elif isinstance(dims, tensor_shape_pb2.TensorShapeProto): |
| 758 | if dims.unknown_rank: |
| 759 | self._dims = None |
| 760 | else: |
| 761 | self._dims = [ |
| 762 | # Protos store variable-size dimensions as -1 |
| 763 | as_dimension(dim.size if dim.size != -1 else None) |
| 764 | for dim in dims.dim |
| 765 | ] |
| 766 | elif isinstance(dims, TensorShape): |
| 767 | self._dims = dims.dims |
| 768 | else: |
| 769 | try: |
| 770 | dims_iter = iter(dims) |
| 771 | except TypeError: |
| 772 | # Treat as a singleton dimension |
| 773 | self._dims = [as_dimension(dims)] |
| 774 | else: |
| 775 | # Got a list of dimensions |
| 776 | self._dims = [as_dimension(d) for d in dims_iter] |
| 777 | |
| 778 | @property |
| 779 | def _v2_behavior(self): |
no outgoing calls