Creation
Creation (basic)¤
empty
staticmethod
¤
empty(
*shape,
device: str | tuple[str, ...] | None = None,
dtype: DTypeLike | None = None,
**kwargs
) -> 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.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
t = Tensor.empty(2, 3)
print(t.shape)
(2, 3)
Source code in tinygrad/tensor.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | |
zeros
staticmethod
¤
zeros(*shape, **kwargs) -> Tensor
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/tensor.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 | |
ones
staticmethod
¤
ones(*shape, **kwargs) -> Tensor
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/tensor.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | |
full
staticmethod
¤
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.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
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/tensor.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
arange
staticmethod
¤
arange(start, stop=None, step=1, **kwargs) -> Tensor
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.
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.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/tensor.py
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
linspace
staticmethod
¤
Returns a 1-D tensor of steps evenly spaced values from start to stop, inclusive.
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.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/tensor.py
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | |
eye
staticmethod
¤
eye(
n: int,
m: int | None = None,
dtype=None,
device=None,
requires_grad: bool | None = None,
) -> Tensor
Returns a 2-D tensor with n rows and m columns, with ones on the diagonal and zeros elsewhere.
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.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/tensor.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | |
full_like
¤
full_like(fill_value: PyConst, **kwargs) -> Tensor
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.
Additionally, all other keyword arguments are passed to the constructor 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
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
zeros_like
¤
zeros_like(**kwargs) -> Tensor
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.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
t = Tensor.ones(2, 3)
print(Tensor.zeros_like(t).numpy())
[[0. 0. 0.]
[0. 0. 0.]]
Source code in tinygrad/tensor.py
779 780 781 782 783 784 785 786 787 788 789 790 791 | |
ones_like
¤
ones_like(**kwargs) -> Tensor
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.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
t = Tensor.zeros(2, 3)
print(Tensor.ones_like(t).numpy())
[[1. 1. 1.]
[1. 1. 1.]]
Source code in tinygrad/tensor.py
793 794 795 796 797 798 799 800 801 802 803 804 805 | |
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
526 527 528 529 530 531 532 533 534 535 536 537 538 | |
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
540 541 542 543 544 545 546 547 548 549 550 551 | |
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.997 0.5899 0.2225 0.7551 0.9057]
[0.6162 0.6213 0.9791 0.7851 0.4178]
Tensor.manual_seed(42) # reset to the same seed
print(Tensor.rand(5).numpy())
print(Tensor.rand(5).numpy())
[0.997 0.5899 0.2225 0.7551 0.9057]
[0.6162 0.6213 0.9791 0.7851 0.4178]
Source code in tinygrad/tensor.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | |
rand
staticmethod
¤
rand(
*shape,
device: str | None = None,
dtype: DTypeLike | None = None,
contiguous: bool = True,
**kwargs
) -> 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.
Additionally, all other keyword arguments are passed to the constructor of the tensor.
Tensor.manual_seed(42)
t = Tensor.rand(2, 3)
print(t.numpy())
[[0.997 0.5899 0.2225]
[0.7551 0.9057 0.8649]]
Source code in tinygrad/tensor.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
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.6213 0.9791 0.8408]
[0.4178 0.6334 0.9325]]
Source code in tinygrad/tensor.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 | |
randn
staticmethod
¤
randn(
*shape,
dtype: DTypeLike | None = None,
requires_grad: bool | 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())
[[ 0.9779 0.4678 0.5526]
[-0.3288 -0.8555 0.2753]]
Source code in tinygrad/tensor.py
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
randn_like
¤
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.0229 -0.8954 0.415 ]
[-1.5933 0.96 -1.2354]]
Source code in tinygrad/tensor.py
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 | |
randint
staticmethod
¤
Creates a tensor with the given shape, filled with random integer values generated uniformly from the interval [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())
[[9 7 6]
[8 9 9]]
Source code in tinygrad/tensor.py
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 | |
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())
[2 1 3 5 4 0]
Source code in tinygrad/tensor.py
971 972 973 974 975 976 977 978 979 980 981 | |
normal
staticmethod
¤
Creates a tensor with the given shape, filled with random values from a normal distribution with the given mean and standard deviation std.
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())
[[11.9557 10.9356 11.1053]
[ 9.3423 8.289 10.5505]]
Source code in tinygrad/tensor.py
875 876 877 878 879 880 881 882 883 884 885 886 887 888 | |
uniform
staticmethod
¤
uniform(
*shape,
low=0.0,
high=1.0,
dtype: DTypeLike | None = None,
requires_grad: bool | None = None,
**kwargs
) -> Tensor
Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval [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())
[[9.9763 6.7193 3.7804]
[8.0404 9.2452 8.9191]]
Source code in tinygrad/tensor.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 | |
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.4058 0.0734 -0.2265]
[ 0.2082 0.3312 0.2979]]
Source code in tinygrad/tensor.py
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 | |
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())
[[ 1.0889 0.197 -0.6079]
[ 0.5588 0.8887 0.7994]]
Source code in tinygrad/tensor.py
922 923 924 925 926 927 928 929 930 931 932 933 934 935 | |
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())
[[ 1.4058 0.2543 -0.7847]
[ 0.7214 1.1473 1.032 ]]
Source code in tinygrad/tensor.py
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 | |
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())
[[ 0.7984 0.3819 0.4512]
[-0.2685 -0.6985 0.2247]]
Source code in tinygrad/tensor.py
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 | |