Returns the constant value of the given tensor, if efficiently calculable. This function attempts to partially evaluate the given tensor, and returns its value as a numpy ndarray if this succeeds. Compatibility(V1): If `constant_value(tensor)` returns a non-`None` result, it will no longer
(tensor, partial=False)
| 766 | |
| 767 | @tf_export("get_static_value") |
| 768 | def constant_value(tensor, partial=False): # pylint: disable=invalid-name |
| 769 | """Returns the constant value of the given tensor, if efficiently calculable. |
| 770 | |
| 771 | This function attempts to partially evaluate the given tensor, and |
| 772 | returns its value as a numpy ndarray if this succeeds. |
| 773 | |
| 774 | Compatibility(V1): If `constant_value(tensor)` returns a non-`None` result, it |
| 775 | will no longer be possible to feed a different value for `tensor`. This allows |
| 776 | the result of this function to influence the graph that is constructed, and |
| 777 | permits static shape optimizations. |
| 778 | |
| 779 | Args: |
| 780 | tensor: The Tensor to be evaluated. |
| 781 | partial: If True, the returned numpy array is allowed to have partially |
| 782 | evaluated values. Values that can't be evaluated will be None. |
| 783 | |
| 784 | Returns: |
| 785 | A numpy ndarray containing the constant value of the given `tensor`, |
| 786 | or None if it cannot be calculated. |
| 787 | |
| 788 | Raises: |
| 789 | TypeError: if tensor is not an ops.Tensor. |
| 790 | """ |
| 791 | if isinstance(tensor, ops.EagerTensor): |
| 792 | return tensor.numpy() |
| 793 | if not is_tensor(tensor): |
| 794 | return tensor |
| 795 | if not isinstance(tensor, ops.Tensor): |
| 796 | return None |
| 797 | ret = _ConstantValue(tensor, partial) |
| 798 | if ret is not None: |
| 799 | # The caller may now depend on the constant value of `tensor`, so we |
| 800 | # conservatively prevent it from being fed. |
| 801 | tensor.graph.prevent_feeding(tensor) |
| 802 | return ret |
| 803 | |
| 804 | |
| 805 | def constant_value_as_shape(tensor): # pylint: disable=invalid-name |
no test coverage detected