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

Function asarray

python/paddle/tensor/creation.py:1219–1300  ·  view source on GitHub ↗

r""" Constructs a ``paddle.Tensor`` from ``obj`` , which can be scalar, tuple, list, numpy\.ndarray, paddle\.Tensor. If the ``obj`` is already a tensor, copy will be performed and return a new tensor. .. note:: The parameter ``copy`` will not affect this api's behavior. Copy wi

(
    obj: TensorLike | NestedNumericSequence,
    *,
    dtype: DTypeLike | None = None,
    device: PlaceLike | None = None,
    copy: bool | None = None,
    requires_grad: bool = False,
)

Source from the content-addressed store, hash-verified

1217
1218
1219def asarray(
1220 obj: TensorLike | NestedNumericSequence,
1221 *,
1222 dtype: DTypeLike | None = None,
1223 device: PlaceLike | None = None,
1224 copy: bool | None = None,
1225 requires_grad: bool = False,
1226):
1227 r"""
1228 Constructs a ``paddle.Tensor`` from ``obj`` ,
1229 which can be scalar, tuple, list, numpy\.ndarray, paddle\.Tensor.
1230
1231 If the ``obj`` is already a tensor, copy will be performed and return a new tensor.
1232
1233 .. note::
1234 The parameter ``copy`` will not affect this api's behavior. Copy will always be performed if ``obj`` is a tensor.
1235
1236 .. code-block:: text
1237
1238 We use the dtype conversion rules following this:
1239 Keep dtype
1240 np.number ───────────► paddle.Tensor
1241 (0-D Tensor)
1242 default_dtype
1243 Python Number ───────────────► paddle.Tensor
1244 (0-D Tensor)
1245 Keep dtype
1246 np.ndarray ───────────► paddle.Tensor
1247
1248 Args:
1249 obj(scalar|tuple|list|ndarray|Tensor): Initial data for the tensor.
1250 Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor.
1251 dtype(str|np.dtype, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
1252 'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8',
1253 'complex64' , 'complex128'. Default: None, infers dtype from ``data``
1254 except for python float number which gets dtype from ``get_default_type`` .
1255 device(CPUPlace|CUDAPinnedPlace|CUDAPlace|str, optional): The place to allocate Tensor. Can be
1256 CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If ``place`` is
1257 string, It can be ``cpu``, ``gpu:x`` and ``gpu_pinned``, where ``x`` is the index of the GPUs.
1258 copy(bool, optional): This param is ignored and has no effect.
1259 requires_grad(bool, optional): Whether to block the gradient propagation of autograd. Default: False.
1260
1261 Returns:
1262 Tensor: A Tensor constructed from ``data`` .
1263
1264 Examples:
1265 .. code-block:: pycon
1266
1267 >>> import paddle
1268
1269 >>> type(paddle.asarray(1))
1270 <class 'paddle.Tensor'>
1271
1272 >>> paddle.asarray(1)
1273 Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
1274 1)
1275
1276 >>> x = paddle.asarray(1, requires_grad=True)

Callers 5

_fftc2cFunction · 0.85
_fftr2cFunction · 0.85
_fftc2rFunction · 0.85
_fft_r2c_ndFunction · 0.85
_fft_c2r_ndFunction · 0.85

Calls 1

tensorFunction · 0.85

Tested by

no test coverage detected