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])` * *
| 491 | |
| 492 | # @tf_export("TensorShape") |
| 493 | class TensorShape: |
| 494 | """Represents the shape of a `Tensor`. |
| 495 | |
| 496 | A `TensorShape` represents a possibly-partial shape specification for a |
| 497 | `Tensor`. It may be one of the following: |
| 498 | |
| 499 | * *Fully-known shape:* has a known number of dimensions and a known size |
| 500 | for each dimension. e.g. `TensorShape([16, 256])` |
| 501 | * *Partially-known shape:* has a known number of dimensions, and an unknown |
| 502 | size for one or more dimension. e.g. `TensorShape([None, 256])` |
| 503 | * *Unknown shape:* has an unknown number of dimensions, and an unknown |
| 504 | size in all dimensions. e.g. `TensorShape(None)` |
| 505 | |
| 506 | If a tensor is produced by an operation of type `"Foo"`, its shape |
| 507 | may be inferred if there is a registered shape function for |
| 508 | `"Foo"`. See @{$adding_an_op#shape-functions-in-c$`Shape functions in C++`} |
| 509 | for details of shape functions and how to register them. Alternatively, |
| 510 | the shape may be set explicitly using @{tf.Tensor.set_shape}. |
| 511 | """ |
| 512 | |
| 513 | def __init__(self, dims): |
| 514 | """Creates a new TensorShape with the given dimensions. |
| 515 | |
| 516 | Args: |
| 517 | dims: A list of Dimensions, or None if the shape is unspecified. |
| 518 | DEPRECATED: A single integer is treated as a singleton list. |
| 519 | |
| 520 | Raises: |
| 521 | TypeError: If dims cannot be converted to a list of dimensions. |
| 522 | """ |
| 523 | # TODO(irving): Eliminate the single integer special case. |
| 524 | if dims is None: |
| 525 | self._dims = None |
| 526 | elif isinstance(dims, compat.bytes_or_text_types): |
| 527 | raise TypeError( |
| 528 | "A string has ambiguous TensorShape, please wrap in a " |
| 529 | "list or convert to an int: %s" % dims |
| 530 | ) |
| 531 | elif isinstance(dims, tensor_shape_pb2.TensorShapeProto): |
| 532 | if dims.unknown_rank: |
| 533 | self._dims = None |
| 534 | else: |
| 535 | self._dims = [ |
| 536 | # Protos store variable-size dimensions as -1 |
| 537 | as_dimension(dim.size if dim.size != -1 else None) |
| 538 | for dim in dims.dim |
| 539 | ] |
| 540 | elif isinstance(dims, TensorShape): |
| 541 | self._dims = dims.dims |
| 542 | else: |
| 543 | try: |
| 544 | dims_iter = iter(dims) |
| 545 | except TypeError: |
| 546 | # Treat as a singleton dimension |
| 547 | self._dims = [as_dimension(dims)] |
| 548 | else: |
| 549 | # Got a list of dimensions |
| 550 | self._dims = [as_dimension(d) for d in dims_iter] |
no outgoing calls
no test coverage detected
searching dependent graphs…