MCPcopy
hub / github.com/tinygrad/tinygrad / item

Method item

tinygrad/tensor.py:310–320  ·  view source on GitHub ↗

Returns the value of this tensor as a standard Python number. ```python exec="true" source="above" session="tensor" result="python" t = Tensor(42) print(t.item()) ```

(self)

Source from the content-addressed store, hash-verified

308 return self._buffer().as_memoryview().cast(self.dtype.base.fmt, self.shape)
309
310 def item(self) -> PyConst:
311 """
312 Returns the value of this tensor as a standard Python number.
313
314 ```python exec="true" source="above" session="tensor" result="python"
315 t = Tensor(42)
316 print(t.item())
317 ```
318 """
319 assert self.numel() == 1, "must have one element for item"
320 return self.data()[(0,) * len(self.shape)]
321
322 # NOTE: list[Any] because return type is recursive (list[list[...]] for higher dimensions)
323 def tolist(self) -> PyConst|list[Any]:

Callers 15

test_constMethod · 0.95
test_implicit_outputMethod · 0.95
test_implicit_ioMethod · 0.95
test_uop_sum_dtypeMethod · 0.95
test_assign_add_jitMethod · 0.95
test_assign_other_jitMethod · 0.95
test_zero_dim_indexMethod · 0.95
train_bertFunction · 0.95
__init__Method · 0.80

Calls 2

dataMethod · 0.95
numelMethod · 0.80

Tested by 15

test_constMethod · 0.76
test_implicit_outputMethod · 0.76
test_implicit_ioMethod · 0.76
test_uop_sum_dtypeMethod · 0.76
test_assign_add_jitMethod · 0.76
test_assign_other_jitMethod · 0.76
test_zero_dim_indexMethod · 0.76
test_matmulFunction · 0.64
test_2d_slice_setitemMethod · 0.64