MCPcopy
hub / github.com/tinygrad/tinygrad / numpy

Method numpy

tinygrad/tensor.py:341–354  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 """

Calls 6

_bufferMethod · 0.95
all_intFunction · 0.90
_to_np_dtypeFunction · 0.90
floatMethod · 0.80
reshapeMethod · 0.80
emptyMethod · 0.45