MCPcopy
hub / github.com/tinygrad/tinygrad / randn_like

Method randn_like

tinygrad/tensor.py:636–650  ·  view source on GitHub ↗

Creates a tensor with the same shape and sharding as `self`, filled with random values from a normal distribution with mean 0 and variance 1. You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor. Additionally, all other keyword argume

(self, dtype:DTypeLike|None=None, **kwargs)

Source from the content-addressed store, hash-verified

634 # ***** random functions *****
635
636 def randn_like(self, dtype:DTypeLike|None=None, **kwargs) -> Tensor:
637 """
638 Creates a tensor with the same shape and sharding as `self`, filled with random values from a normal distribution with mean 0 and variance 1.
639
640 You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
641 Additionally, all other keyword arguments are passed to the constructor of the tensor.
642
643 ```python exec="true" source="above" session="tensor" result="python"
644 t = Tensor.ones(2, 3)
645 print(Tensor.randn_like(t).numpy())
646 ```
647 """
648 src = self.stack(self).rand_like(**{**kwargs, "dtype": dtypes.float32})
649 # https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
650 return src[0].mul(2*math.pi).cos().mul((1 - src[1]).log().mul(-2).sqrt()).cast(dtype or self.dtype)
651
652 @staticmethod
653 def randn(*shape, dtype:DTypeLike|None=None, **kwargs) -> Tensor:

Callers 5

randnMethod · 0.80
test_biased_conv2dMethod · 0.80
test_randn_likeMethod · 0.80
test_randn_like_dtypeMethod · 0.80
rfMethod · 0.80

Calls 7

rand_likeMethod · 0.80
stackMethod · 0.80
mulMethod · 0.80
cosMethod · 0.80
sqrtMethod · 0.80
logMethod · 0.80
castMethod · 0.45

Tested by 3

test_biased_conv2dMethod · 0.64
test_randn_likeMethod · 0.64
test_randn_like_dtypeMethod · 0.64