MCPcopy
hub / github.com/tinygrad/tinygrad / normalize

Method normalize

tinygrad/mixin/__init__.py:523–542  ·  view source on GitHub ↗

Performs Lp normalization of the tensor along the specified dimension. See: https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html ```python exec="true" source="above" session="tensor" result="python" Tensor.manual_seed(42) t = Tensor.randn(2, 3) prin

(self, p:float=2.0, dim:int=1, eps:float=1e-12)

Source from the content-addressed store, hash-verified

521 return self.std(axis, keepdim, correction), self.mean(axis, keepdim)
522
523 def normalize(self, p:float=2.0, dim:int=1, eps:float=1e-12) -> Self:
524 """
525 Performs Lp normalization of the tensor along the specified dimension.
526
527 See: https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html
528
529 ```python exec="true" source="above" session="tensor" result="python"
530 Tensor.manual_seed(42)
531 t = Tensor.randn(2, 3)
532 print(t.numpy())
533 ```
534 ```python exec="true" source="above" session="tensor" result="python"
535 print(t.normalize().numpy())
536 ```
537 ```python exec="true" source="above" session="tensor" result="python"
538 print(t.normalize(p=1, dim=0).numpy())
539 ```
540 """
541 if p == 0: return self / self.ne(0).sum(dim, keepdim=True).maximum(eps)
542 return self / self.abs().pow(p).sum(dim, keepdim=True).pow(1/p).maximum(eps)
543
544 def logsumexp(self, axis=None, keepdim=False) -> Self:
545 """

Callers 3

_attentionMethod · 0.45
_run_strip_accentsFunction · 0.45
_run_strip_accentsFunction · 0.45

Calls 5

maximumMethod · 0.80
sumMethod · 0.80
neMethod · 0.80
powMethod · 0.80
absMethod · 0.80

Tested by

no test coverage detected