Updates the shape of this tensor. This method can be called multiple times, and will merge the given `shape` with the current shape of this tensor. It can be used to provide additional information about the shape of this tensor that cannot be inferred from the graph alone. For examp
(self, shape)
| 582 | return self.shape |
| 583 | |
| 584 | def set_shape(self, shape): |
| 585 | """Updates the shape of this tensor. |
| 586 | |
| 587 | This method can be called multiple times, and will merge the given |
| 588 | `shape` with the current shape of this tensor. It can be used to |
| 589 | provide additional information about the shape of this tensor that |
| 590 | cannot be inferred from the graph alone. For example, this can be used |
| 591 | to provide additional information about the shapes of images: |
| 592 | |
| 593 | ```python |
| 594 | _, image_data = tf.compat.v1.TFRecordReader(...).read(...) |
| 595 | image = tf.image.decode_png(image_data, channels=3) |
| 596 | |
| 597 | # The height and width dimensions of `image` are data dependent, and |
| 598 | # cannot be computed without executing the op. |
| 599 | print(image.shape) |
| 600 | ==> TensorShape([Dimension(None), Dimension(None), Dimension(3)]) |
| 601 | |
| 602 | # We know that each image in this dataset is 28 x 28 pixels. |
| 603 | image.set_shape([28, 28, 3]) |
| 604 | print(image.shape) |
| 605 | ==> TensorShape([Dimension(28), Dimension(28), Dimension(3)]) |
| 606 | ``` |
| 607 | |
| 608 | NOTE: This shape is not enforced at runtime. Setting incorrect shapes can |
| 609 | result in inconsistencies between the statically-known graph and the runtime |
| 610 | value of tensors. For runtime validation of the shape, use `tf.ensure_shape` |
| 611 | instead. |
| 612 | |
| 613 | Args: |
| 614 | shape: A `TensorShape` representing the shape of this tensor, a |
| 615 | `TensorShapeProto`, a list, a tuple, or None. |
| 616 | |
| 617 | Raises: |
| 618 | ValueError: If `shape` is not compatible with the current shape of |
| 619 | this tensor. |
| 620 | """ |
| 621 | # Reset cached shape. |
| 622 | self._shape_val = None |
| 623 | |
| 624 | # We want set_shape to be reflected in the C API graph for when we run it. |
| 625 | if not isinstance(shape, tensor_shape.TensorShape): |
| 626 | shape = tensor_shape.TensorShape(shape) |
| 627 | dim_list = [] |
| 628 | if shape.dims is None: |
| 629 | unknown_shape = True |
| 630 | else: |
| 631 | unknown_shape = False |
| 632 | for dim in shape.dims: |
| 633 | if dim.value is None: |
| 634 | dim_list.append(-1) |
| 635 | else: |
| 636 | dim_list.append(dim.value) |
| 637 | try: |
| 638 | c_api.TF_GraphSetTensorShape_wrapper( |
| 639 | self._op._graph._c_graph, # pylint: disable=protected-access |
| 640 | self._as_tf_output(), |
| 641 | dim_list, |