Generates a InputSpec based on the description of input tensor. Args: tensor(Tensor): the source tensor to generate a InputSpec instance Returns: A InputSpec instance generated from Tensor. Examples: .. code-block:: pycon
(cls, tensor: Tensor, name: str | None = None)
| 286 | |
| 287 | @classmethod |
| 288 | def from_tensor(cls, tensor: Tensor, name: str | None = None) -> Self: |
| 289 | """ |
| 290 | Generates a InputSpec based on the description of input tensor. |
| 291 | |
| 292 | Args: |
| 293 | tensor(Tensor): the source tensor to generate a InputSpec instance |
| 294 | |
| 295 | Returns: |
| 296 | A InputSpec instance generated from Tensor. |
| 297 | |
| 298 | Examples: |
| 299 | .. code-block:: pycon |
| 300 | |
| 301 | >>> import paddle |
| 302 | >>> from paddle.static import InputSpec |
| 303 | |
| 304 | >>> paddle.disable_static() |
| 305 | |
| 306 | >>> x = paddle.ones([2, 2], dtype="float32") |
| 307 | >>> x_spec = InputSpec.from_tensor(x, name='x') |
| 308 | >>> print(x_spec) |
| 309 | InputSpec(shape=(2, 2), dtype=paddle.float32, name=x, stop_gradient=False) |
| 310 | |
| 311 | """ |
| 312 | if isinstance(tensor, (Variable, core.eager.Tensor, paddle.pir.Value)): |
| 313 | return cls(tensor.shape, tensor.dtype, name or tensor.name) |
| 314 | else: |
| 315 | raise ValueError( |
| 316 | f"Input `tensor` should be a Tensor, but received {type(tensor).__name__}." |
| 317 | ) |
| 318 | |
| 319 | @classmethod |
| 320 | def from_numpy( |