Returns the value of this tensor as a `numpy.ndarray`. ```python exec="true" source="above" session="tensor" result="python" t = Tensor([1, 2, 3, 4]) print(repr(t.numpy())) ```
(self)
| 339 | return self.data().tolist() |
| 340 | |
| 341 | def numpy(self) -> 'numpy.ndarray': |
| 342 | """ |
| 343 | Returns the value of this tensor as a `numpy.ndarray`. |
| 344 | |
| 345 | ```python exec="true" source="above" session="tensor" result="python" |
| 346 | t = Tensor([1, 2, 3, 4]) |
| 347 | print(repr(t.numpy())) |
| 348 | ``` |
| 349 | """ |
| 350 | assert all_int(self.shape), f"no data if shape is symbolic, {self.shape=}" |
| 351 | import numpy as np |
| 352 | if self.dtype.base in { dtypes.bfloat16, *dtypes.fp8s }: return self.float().numpy() |
| 353 | if 0 in self.shape: return np.empty(self.shape, dtype=_to_np_dtype(self.dtype.base)) |
| 354 | return self._buffer().numpy().reshape(self.shape) |
| 355 | |
| 356 | def clone(self, device:str|tuple[str, ...]|None=None) -> Tensor: |
| 357 | """ |