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

Function tensor

python/paddle/tensor/creation.py:980–1099  ·  view source on GitHub ↗

r""" Constructs a ``paddle.Tensor`` from ``data`` , which can be scalar, tuple, list, numpy\.ndarray, paddle\.Tensor. If the ``data`` is already a Tensor, copy will be performed and return a new tensor. If you only want to change stop_gradient property, please call ``Tensor.stop_gra

(
    data: TensorLike | NestedNumericSequence,
    dtype: DTypeLike | None = None,
    device: PlaceLike | None = None,
    requires_grad: bool = False,
    pin_memory: bool = False,
)

Source from the content-addressed store, hash-verified

978
979
980def tensor(
981 data: TensorLike | NestedNumericSequence,
982 dtype: DTypeLike | None = None,
983 device: PlaceLike | None = None,
984 requires_grad: bool = False,
985 pin_memory: bool = False,
986) -> paddle.Tensor:
987 r"""
988 Constructs a ``paddle.Tensor`` from ``data`` ,
989 which can be scalar, tuple, list, numpy\.ndarray, paddle\.Tensor.
990
991 If the ``data`` is already a Tensor, copy will be performed and return a new tensor.
992 If you only want to change stop_gradient property, please call ``Tensor.stop_gradient = stop_gradient`` directly.
993
994 .. code-block:: text
995
996 We use the dtype conversion rules following this:
997 Keep dtype
998 np.number ───────────► paddle.Tensor
999 (0-D Tensor)
1000 default_dtype
1001 Python Number ───────────────► paddle.Tensor
1002 (0-D Tensor)
1003 Keep dtype
1004 np.ndarray ───────────► paddle.Tensor
1005
1006 Args:
1007 data(scalar|tuple|list|ndarray|Tensor): Initial data for the tensor.
1008 Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor.
1009 dtype(str|np.dtype, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
1010 'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8',
1011 'complex64' , 'complex128'. Default: None, infers dtype from ``data``
1012 except for python float number which gets dtype from ``get_default_type`` .
1013 device(CPUPlace|CUDAPinnedPlace|CUDAPlace|str, optional): The place to allocate Tensor. Can be
1014 CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If ``device`` is
1015 string, It can be ``cpu``, ``gpu:x`` and ``gpu_pinned``, where ``x`` is the index of the GPUs.
1016 requires_grad(bool, optional): Whether to block the gradient propagation of Autograd. Default: False.
1017 pin_memory(bool, optional): If set, return tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False
1018
1019 Returns:
1020 Tensor: A Tensor constructed from ``data`` .
1021
1022 Examples:
1023 .. code-block:: pycon
1024
1025 >>> # type: ignore
1026 >>> import paddle
1027
1028 >>> type(paddle.tensor(1))
1029 <class 'paddle.Tensor'>
1030
1031 >>> paddle.tensor(1)
1032 Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
1033 1)
1034
1035 >>> x = paddle.tensor(1, requires_grad=True)
1036 >>> print(x)
1037 Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=False,

Callers 15

to_tensorFunction · 0.85
from_numpyFunction · 0.85
asarrayFunction · 0.85
DenseOperandInfoMethod · 0.85
WrapReduceOperationFunction · 0.85
RepeatFunction · 0.85
PadFunction · 0.85
DropoutInferFunction · 0.85
DoReduceFunction · 0.85
MakeCacheTensorFunction · 0.85
ConvertReduceBodyFunction · 0.85
TESTFunction · 0.85

Calls 11

_get_paddle_placeFunction · 0.85
_current_expected_place_Function · 0.85
RuntimeErrorClass · 0.85
_to_tensor_non_staticFunction · 0.85
strFunction · 0.85
_to_tensor_staticFunction · 0.85
XPUPinnedPlaceMethod · 0.80
pin_memoryMethod · 0.80
is_tensorMethod · 0.45
warnMethod · 0.45
compileMethod · 0.45

Tested by 12

TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68