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])
| 1184 | |
| 1185 | |
| 1186 | def 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 | |
| 1219 | def asarray( |