|
65 | 65 | ShapeLike, |
66 | 66 | TensorOrTensors, |
67 | 67 | ) |
68 | | - |
| 68 | +from paddle._C_ops import expand_as # noqa: F401 |
69 | 69 | from paddle.utils.decorator_utils import ForbidKeywordsDecorator |
70 | 70 |
|
71 | 71 | __all__ = [] |
@@ -4832,90 +4832,6 @@ def repeat( |
4832 | 4832 | return tile(input, repeat_times=repeats) |
4833 | 4833 |
|
4834 | 4834 |
|
4835 | | -def expand_as(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: |
4836 | | - """ |
4837 | | -
|
4838 | | - Expand the input tensor ``x`` to the same shape as the input tensor ``y``. |
4839 | | -
|
4840 | | - Both the number of dimensions of ``x`` and ``y`` must be less than or equal to 6, and the number of dimensions of ``y`` must be greater than or equal to that of ``x``. The dimension to expand must have a value of 0. |
4841 | | -
|
4842 | | - The following diagram illustrates how a one-dimensional tensor is transformed into a tensor with a shape of [2,3] through the expand_as operation. The target tensor has a shape of [2,3], and through expand_as, the one-dimensional tensor is expanded into a tensor with a shape of [2,3]. |
4843 | | -
|
4844 | | - .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/expand_as.png |
4845 | | - :width: 800 |
4846 | | - :alt: expand_as API |
4847 | | - :align: center |
4848 | | -
|
4849 | | - Args: |
4850 | | - x (Tensor): The input tensor, its data type is bool, float32, float64, int32 or int64. |
4851 | | - y (Tensor): The input tensor that gives the shape to expand to. |
4852 | | - name (str|None, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. |
4853 | | -
|
4854 | | - Returns: |
4855 | | - N-D Tensor, A Tensor with the same shape as ``y``. The data type is the same as ``x``. |
4856 | | -
|
4857 | | - Examples: |
4858 | | - .. code-block:: python |
4859 | | -
|
4860 | | - >>> import paddle |
4861 | | -
|
4862 | | - >>> data_x = paddle.to_tensor([1, 2, 3], 'int32') |
4863 | | - >>> data_y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], 'int32') |
4864 | | - >>> out = paddle.expand_as(data_x, data_y) |
4865 | | - >>> print(out) |
4866 | | - Tensor(shape=[2, 3], dtype=int32, place=Place(cpu), stop_gradient=True, |
4867 | | - [[1, 2, 3], |
4868 | | - [1, 2, 3]]) |
4869 | | - """ |
4870 | | - if in_dynamic_mode(): |
4871 | | - return _C_ops.expand_as(x, None, y.shape) |
4872 | | - elif in_pir_mode(): |
4873 | | - if convert_dtype(x.dtype) == 'bool' and not x.stop_gradient: |
4874 | | - raise ValueError( |
4875 | | - "When the data type of input 'x' for expand_as is bool, " |
4876 | | - "you must set its stop_gradient to be False by " |
4877 | | - "some_var.stop_gradient = True, supporting " |
4878 | | - "some_var as the input 'x'." |
4879 | | - ) |
4880 | | - return _C_ops.expand_as(x, y, y.shape) |
4881 | | - else: |
4882 | | - check_variable_and_dtype( |
4883 | | - x, |
4884 | | - 'x', |
4885 | | - [ |
4886 | | - 'bool', |
4887 | | - 'float32', |
4888 | | - 'float64', |
4889 | | - 'int32', |
4890 | | - 'int64', |
4891 | | - 'float16', |
4892 | | - 'uint16', |
4893 | | - ], |
4894 | | - 'expand_as', |
4895 | | - ) |
4896 | | - check_type(y, 'y', Variable, 'expand_as') |
4897 | | - |
4898 | | - if convert_dtype(x.dtype) == 'bool' and not x.stop_gradient: |
4899 | | - raise ValueError( |
4900 | | - "When the data type of input 'x' for expand_as is bool, " |
4901 | | - "you must set its stop_gradient to be False by " |
4902 | | - "some_var.stop_gradient = True, supporting " |
4903 | | - "some_var as the input 'x'." |
4904 | | - ) |
4905 | | - inputs = {"X": [x], "Y": [y]} |
4906 | | - |
4907 | | - helper = LayerHelper('expand_as', **locals()) |
4908 | | - dtype = helper.input_dtype(input_param_name='x') |
4909 | | - out = helper.create_variable_for_type_inference(dtype) |
4910 | | - helper.append_op( |
4911 | | - type='expand_as_v2', |
4912 | | - inputs=inputs, |
4913 | | - attrs={'target_shape': y.shape}, |
4914 | | - outputs={'Out': out}, |
4915 | | - ) |
4916 | | - return out |
4917 | | - |
4918 | | - |
4919 | 4835 | @ParamAliasDecorator({"x": ["input"], "shape": ["size"]}) |
4920 | 4836 | def broadcast_to( |
4921 | 4837 | x: Tensor, |
|
0 commit comments