MCPcopy
hub / github.com/tinygrad/tinygrad / rand_like

Method rand_like

tinygrad/tensor.py:619–632  ·  view source on GitHub ↗

Creates a tensor with the same shape and sharding as `self`, filled with random values from a uniform distribution over the interval `[0, 1)`. You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor. Additionally, all other keyword argum

(self, **kwargs)

Source from the content-addressed store, hash-verified

617 return Tensor.full(self.shape, fill_value, dtype=dtype or self.dtype, device=device)
618
619 def rand_like(self, **kwargs) -> Tensor:
620 """
621 Creates a tensor with the same shape and sharding as `self`, filled with random values from a uniform distribution over the interval `[0, 1)`.
622
623 You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
624 Additionally, all other keyword arguments are passed to the constructor of the tensor.
625
626 ```python exec="true" source="above" session="tensor" result="python"
627 t = Tensor.ones(2, 3)
628 print(Tensor.rand_like(t).numpy())
629 ```
630 """
631 if isinstance(self.device, tuple): return self._multi_like(Tensor.rand, **kwargs)
632 return Tensor.rand(*self.shape, device=kwargs.pop("device", self.device), dtype=kwargs.pop("dtype", self.dtype), **kwargs)
633
634 # ***** random functions *****
635

Callers 15

randn_likeMethod · 0.80
multinomialMethod · 0.80
dropoutMethod · 0.80
forwardMethod · 0.80
dropout_7Function · 0.80
test_rand_likeMethod · 0.80
test_rand_like_dtypeMethod · 0.80
test_rand_like_deviceMethod · 0.80

Calls 2

_multi_likeMethod · 0.95
randMethod · 0.80

Tested by 10

test_rand_likeMethod · 0.64
test_rand_like_dtypeMethod · 0.64
test_rand_like_deviceMethod · 0.64