Creation
Creation (basic)¤
empty
staticmethod
¤
empty(
*shape,
device: str | tuple[str, ...] | None = None,
dtype: DTypeLike | None = None
) -> Tensor
Creates an empty tensor with the given shape.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
t = Tensor.empty(2, 3)
print(t.shape)
(2, 3)
Source code in tinygrad/tensor.py
448 449 450 451 452 453 454 455 456 457 458 459 460 | |
zeros
classmethod
¤
zeros(*shape, **kwargs) -> Self
Creates a tensor with the given shape, filled with zeros.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
print(Tensor.zeros(2, 3).numpy())
[[0. 0. 0.]
[0. 0. 0.]]
print(Tensor.zeros(2, 3, dtype=dtypes.int32).numpy())
[[0 0 0]
[0 0 0]]
Source code in tinygrad/mixin/__init__.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
ones
classmethod
¤
ones(*shape, **kwargs) -> Self
Creates a tensor with the given shape, filled with ones.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
print(Tensor.ones(2, 3).numpy())
[[1. 1. 1.]
[1. 1. 1.]]
print(Tensor.ones(2, 3, dtype=dtypes.int32).numpy())
[[1 1 1]
[1 1 1]]
Source code in tinygrad/mixin/__init__.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
full
classmethod
¤
full(
shape: tuple[sint, ...],
fill_value: ConstType | UOp,
dtype: DTypeLike | None = None,
device: str | tuple[str, ...] | None = None,
buffer=True,
) -> Self
Creates a tensor with the given shape, filled with the given value.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Pass buffer=False to get a broadcast const value instead of a materialized buffer.
print(Tensor.full((2, 3), 42).numpy())
[[42 42 42]
[42 42 42]]
print(Tensor.full((2, 3), False).numpy())
[[False False False]
[False False False]]
Source code in tinygrad/mixin/__init__.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
arange
classmethod
¤
arange(
start, stop=None, step=1, dtype: DTypeLike | None = None
) -> Self
Returns a 1-D tensor of size ceil((stop - start) / step) with values from [start, stop), with spacing between values given by step.
If stop is not specified, values are generated from [0, start) with the given step.
If stop is specified, values are generated from [start, stop) with the given step.
print(Tensor.arange(5).numpy())
[0 1 2 3 4]
print(Tensor.arange(5, 10).numpy())
[5 6 7 8 9]
print(Tensor.arange(5, 10, 2).numpy())
[5 7 9]
print(Tensor.arange(5.5, 10, 2).numpy())
[5.5 7.5 9.5]
Source code in tinygrad/mixin/__init__.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
linspace
classmethod
¤
linspace(
start: int | float,
stop: int | float,
steps: int,
dtype: DTypeLike | None = None,
) -> Self
Returns a 1-D tensor of steps evenly spaced values from start to stop, inclusive.
print(Tensor.linspace(0, 10, 5).numpy())
[ 0. 2.5 5. 7.5 10. ]
print(Tensor.linspace(-1, 1, 5).numpy())
[-1. -0.5 0. 0.5 1. ]
Source code in tinygrad/mixin/__init__.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
eye
classmethod
¤
Returns a 2-D tensor with n rows and m columns, with ones on the diagonal and zeros elsewhere.
print(Tensor.eye(3).numpy())
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
print(Tensor.eye(2, 4).numpy())
[[1. 0. 0. 0.]
[0. 1. 0. 0.]]
Source code in tinygrad/mixin/__init__.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
full_like
¤
Creates a tensor with the same shape as self, filled with the given value.
If dtype is not specified, the dtype of self is used.
You can pass in the device keyword argument to control device of the tensor.
t = Tensor.ones(2, 3)
print(Tensor.full_like(t, 42).numpy())
[[42. 42. 42.]
[42. 42. 42.]]
Source code in tinygrad/tensor.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
zeros_like
¤
zeros_like(**kwargs) -> Self
Creates a tensor with the same shape as self, filled with zeros.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
t = Tensor.ones(2, 3)
print(Tensor.zeros_like(t).numpy())
[[0. 0. 0.]
[0. 0. 0.]]
Source code in tinygrad/mixin/creation.py
12 13 14 15 16 17 18 19 20 21 22 23 | |
ones_like
¤
ones_like(**kwargs) -> Self
Creates a tensor with the same shape as self, filled with ones.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
t = Tensor.zeros(2, 3)
print(Tensor.ones_like(t).numpy())
[[1. 1. 1.]
[1. 1. 1.]]
Source code in tinygrad/mixin/creation.py
25 26 27 28 29 30 31 32 33 34 35 36 | |
Creation (external)¤
from_blob
staticmethod
¤
Exposes the pointer as a Tensor without taking ownership of the original data. The pointer must remain valid for the entire lifetime of the created Tensor.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Source code in tinygrad/tensor.py
469 470 471 472 473 474 475 476 477 478 479 480 481 | |
from_url
staticmethod
¤
Creates a Tensor from a URL.
This is the preferred way to access Internet resources. It currently returns a DISK Tensor, but in the future it may return an HTTP Tensor. This also will soon become lazy (when possible) and not print progress without DEBUG.
The gunzip flag will gzip extract the resource and return an extracted Tensor.
Source code in tinygrad/tensor.py
483 484 485 486 487 488 489 490 491 492 493 494 | |
Creation (random)¤
manual_seed
staticmethod
¤
manual_seed(seed=0) -> None
Sets the seed for random operations.
Tensor.manual_seed(42)
print(Tensor.rand(5).numpy())
print(Tensor.rand(5).numpy())
[0.381 0.0098 0.1128 0.1177 0.5054]
[0.8984 0.9686 0.5969 0.9117 0.9869]
Tensor.manual_seed(42) # reset to the same seed
print(Tensor.rand(5).numpy())
print(Tensor.rand(5).numpy())
[0.381 0.0098 0.1128 0.1177 0.5054]
[0.8984 0.9686 0.5969 0.9117 0.9869]
Source code in tinygrad/tensor.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
rand
staticmethod
¤
rand(
*shape,
device: str | None = None,
dtype: DTypeLike | None = None,
contiguous: bool = True
) -> Tensor
Creates a tensor with the given shape, 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.
Tensor.manual_seed(42)
t = Tensor.rand(2, 3)
print(t.numpy())
[[0.381 0.0098 0.1128]
[0.1177 0.5054 0.3721]]
Source code in tinygrad/tensor.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
rand_like
¤
rand_like(**kwargs) -> Tensor
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 arguments are passed to the constructor of the tensor.
t = Tensor.ones(2, 3)
print(Tensor.rand_like(t).numpy())
[[0.2103 0.611 0.1345]
[0.0131 0.368 0.9245]]
Source code in tinygrad/tensor.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
randn
staticmethod
¤
randn(
*shape, dtype: DTypeLike | None = None, **kwargs
) -> Tensor
Creates a tensor with the given shape, filled with random values from a normal distribution with mean 0 and standard deviation 1.
If dtype is not specified, the default type is used.
You can pass in the device keyword argument to control device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.randn(2, 3).numpy())
[[ 1.9576 -0.1859 1.6404]
[-0.7647 -0.8695 -0.4379]]
Source code in tinygrad/tensor.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | |
randn_like
¤
randn_like(
dtype: DTypeLike | None = None, **kwargs
) -> Tensor
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 arguments are passed to the constructor of the tensor.
t = Tensor.ones(2, 3)
print(Tensor.randn_like(t).numpy())
[[-0.7382 1.5164 -0.3065]
[-0.7862 0.5411 -0.4394]]
Source code in tinygrad/tensor.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
randint
staticmethod
¤
Creates a tensor with the given shape, filled with random integer values generated uniformly from the interval [low, high).
Requires low < high. If dtype is not specified, the default type is used.
You can pass in the device keyword argument to control device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.randint(2, 3, low=5, high=10).numpy())
[[6 5 5]
[5 7 6]]
Source code in tinygrad/tensor.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
randperm
staticmethod
¤
Returns a tensor with a random permutation of integers from 0 to n-1.
Tensor.manual_seed(42)
print(Tensor.randperm(6).numpy())
[1 2 3 5 0 4]
Source code in tinygrad/tensor.py
747 748 749 750 751 752 753 754 755 756 757 | |
normal
staticmethod
¤
normal(*shape, mean=0.0, std=1.0, **kwargs) -> Tensor
Creates a tensor with the given shape, filled with random values from a normal distribution with the given mean and standard deviation std.
Requires std >= 0.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.normal(2, 3, mean=10, std=2).numpy())
[[13.9153 9.6281 13.2808]
[ 8.4707 8.261 9.1242]]
Source code in tinygrad/tensor.py
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
uniform
staticmethod
¤
uniform(
*shape,
low=0.0,
high=1.0,
dtype: DTypeLike | None = None,
**kwargs
) -> Tensor
Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval [low, high).
Requires low < high.
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.uniform(2, 3, low=2, high=10).numpy())
[[5.0483 2.0782 2.9024]
[2.9416 6.0429 4.9769]]
Source code in tinygrad/tensor.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | |
scaled_uniform
staticmethod
¤
scaled_uniform(*shape, **kwargs) -> Tensor
Creates a tensor with the given shape, filled with random values from a uniform distribution
over the interval [-prod(shape)**-0.5, prod(shape)**-0.5).
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.scaled_uniform(2, 3).numpy())
[[-0.0971 -0.4003 -0.3161]
[-0.3121 0.0044 -0.1044]]
Source code in tinygrad/tensor.py
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |
glorot_uniform
staticmethod
¤
glorot_uniform(*shape, **kwargs) -> Tensor
https://www.tensorflow.org/api_docs/python/tf/keras/initializers/GlorotUniform
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.glorot_uniform(2, 3).numpy())
[[-0.2606 -1.074 -0.8483]
[-0.8376 0.0117 -0.2802]]
Source code in tinygrad/tensor.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
kaiming_uniform
staticmethod
¤
https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_uniform_
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.kaiming_uniform(2, 3).numpy())
[[-0.3364 -1.3865 -1.0951]
[-1.0813 0.0152 -0.3617]]
Source code in tinygrad/tensor.py
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | |
kaiming_normal
staticmethod
¤
https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_normal_
You can pass in dtype and device keyword arguments to control the data type and device of the tensor.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
print(Tensor.kaiming_normal(2, 3).numpy())
[[ 1.5983 -0.1518 1.3393]
[-0.6243 -0.7099 -0.3575]]
Source code in tinygrad/tensor.py
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |