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

Function from_numpy

python/paddle/tensor/creation.py:1186–1216  ·  view source on GitHub ↗

Creates a ``paddle.Tensor`` from a ``numpy.ndarray``. The returned Tensor and the input ``ndarray`` share the same underlying memory. Changes to the Tensor will be reflected in the ``ndarray`` and vice versa. Args: ndarray(numpy.ndarray): The numpy ndarray to be converted

(ndarray: NDArray[Any])

Source from the content-addressed store, hash-verified

1184
1185
1186def from_numpy(ndarray: NDArray[Any]) -> paddle.Tensor:
1187 """
1188 Creates a ``paddle.Tensor`` from a ``numpy.ndarray``.
1189
1190 The returned Tensor and the input ``ndarray`` share the same underlying memory.
1191 Changes to the Tensor will be reflected in the ``ndarray`` and vice versa.
1192
1193 Args:
1194 ndarray(numpy.ndarray): The numpy ndarray to be converted to a Tensor.
1195
1196 Returns:
1197 Tensor: A Tensor that shares the same memory with the input ``ndarray``.
1198
1199 Examples:
1200 .. code-block:: pycon
1201
1202 >>> import paddle
1203 >>> import numpy as np
1204
1205 >>> np_data = np.array([1, 2, 3]).astype('int64')
1206 >>> tensor = paddle.from_numpy(np_data)
1207 >>> print(tensor)
1208 Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
1209 [1, 2, 3])
1210 """
1211 if not isinstance(ndarray, np.ndarray):
1212 raise TypeError(
1213 f"The input type of from_numpy() must be numpy.ndarray, but received {type(ndarray)}. "
1214 "To convert other types to tensor, please use paddle.tensor() instead."
1215 )
1216 return tensor(ndarray)
1217
1218
1219def asarray(

Callers

nothing calls this directly

Calls 3

TypeErrorClass · 0.85
tensorFunction · 0.85
typeFunction · 0.50

Tested by

no test coverage detected