MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / from_tensor

Method from_tensor

python/paddle/static/input.py:288–317  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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(

Callers 7

shard_tensorFunction · 0.45
unshard_dtensorFunction · 0.45
_infer_item_specMethod · 0.45
get_d2s_specFunction · 0.45
_convert_Function · 0.45
test_from_tensorMethod · 0.45

Calls 2

ValueErrorClass · 0.85
typeFunction · 0.50

Tested by 1

test_from_tensorMethod · 0.36