<jupyter_start><jupyter_text>Title Here Basic Functions on TensorsAn short introduction about PyTorch and about the chosen functions. - torch.tensor- torch.normal- torch.rand- torch.tensor.apply()- torch.tensor.dot()<jupyter_code># Import torch and other required modules
import torch<jupyter_output><empty_output><jupyter_text>Function 1 - torch.tensor2x2 tensor with only prime numbers. It is also a differentiable tensor. At the end I also print it out in "console"<jupyter_code># Example 1 - working (change this)
q = torch.tensor([[1, 5], [7, 11]], dtype=torch.float64, requires_grad=True)
print(q)
print(q.dtype)<jupyter_output>tensor([[ 1.,  5.],
        [ 7., 11.]], dtype=torch.float64, requires_grad=True)
torch.float64<jupyter_text>This is pretty much the same example but i changed the type of tensor, i mean, instead of a float it's a integer<jupyter_code># Example 2 - working
w = torch.tensor([[2, 4],
                  [6,8]], dtype=torch.int64)
print(w)
print(w.dtype)<jupyter_output>tensor([[2, 4],
        [6, 8]])
torch.int64<jupyter_text>I missed open and close a square parenthesis. For create a NxM matrix, it's necessary be careful with the number of [ and ] you use.<jupyter_code># Example 3 - breaking (to illustrate when it breaks)
w = torch.tensor([2, 4],
                 [6, 8])<jupyter_output><empty_output><jupyter_text>It threw error due to the the lack of [ and ]  Function 2 - normalIt creates a normal distributed tensorhttps://pytorch.org/docs/master/generated/torch.normal.htmlnormal_(mean=0, std=1, *, generator=None) → Tensor<jupyter_code># Example 1 - working
n = torch.normal(mean=torch.tensor(5.), std=3.)
n<jupyter_output><empty_output><jupyter_text>normal() receives one of: * (Tensor mean, Tensor std, *, torch.Generator generator, Tensor out) * (Tensor mean, float std, *, torch.Generator generator, Tensor out) * (float mean, Tensor std, *, torch.Generator generator, Tensor out) * (float mean, float std, tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)<jupyter_code># Example 2 - working
n = torch.normal(mean=torch.arange(10.0, 20.0), std=torch.tensor(100))
n<jupyter_output><empty_output><jupyter_text>the mean is an `torch.arange` from 10 to 20 and a StandarDev if `torch.tensor` = 100<jupyter_code># Example 3 - breaking (to illustrate when it breaks)
torch.normal(mean=6., std=2.)<jupyter_output><empty_output><jupyter_text>Invalid input, look details below: normal() receives one of: * (Tensor mean, Tensor std, *, torch.Generator generator, Tensor out) * (Tensor mean, float std, *, torch.Generator generator, Tensor out) * (float mean, Tensor std, *, torch.Generator generator, Tensor out) * (float mean, float std, tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)  Function 3 - torch.randrand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> TensorReturns a tensor filled with random numbers from a uniform distributionon the interval :math:`[0, 1)`The shape of the tensor is defined by the variable argument :attr:`size`.<jupyter_code># Example 1 - working
torch.rand(3,6)<jupyter_output><empty_output><jupyter_text>Now, we can see that it creates random matrices<jupyter_code># Example 2 - working
torch.rand(2,3,4)<jupyter_output><empty_output><jupyter_text>it's created a random tensor<jupyter_code># Example 3 - breaking (to illustrate when it breaks)
torch.rand(2,3, dtype=torch.int)<jupyter_output><empty_output><jupyter_text>_th_uniform_ not supported on CPUType for Intit means that the `dtype` can not be an integer Closing comments about when to use this function  Function 4 - applyApplies the function :attr:`callable` to each element in the tensor, replacingeach element with the value returned by :attr:`callable`.<jupyter_code># Example 1 - working
k = torch.rand(2,3)
print(k)

k.apply_(lambda x:x+1)
k<jupyter_output>tensor([[0.6821, 0.0198, 0.7574],
        [0.0461, 0.9894, 0.9080]])<jupyter_text>Sums 1 to each element of the matrix<jupyter_code># Example 2 - working
def by3(x):
    return x*3

k.apply_(by3)
k<jupyter_output><empty_output><jupyter_text>it works with user defined functions<jupyter_code># Example 3 - breaking (to illustrate when it breaks)
def wrong(x):
    return str(x)

k.apply_(wrong)
k<jupyter_output><empty_output><jupyter_text>Not all transformations to the elements of the matrices are allowed  Function 5 - dotPerforms the dot product between 2 VECTORS<jupyter_code># Example 1 - working
t1 = torch.tensor([10, 5, 9])
t2 = torch.tensor([2, 5, 9])

dot_product = t1.dot(t2)
print(dot_product)<jupyter_output>tensor(126)<jupyter_text>126 = 2*10 +5*5 + 9*9<jupyter_code># Example 2 - working
t3 = torch.rand(3)
t4 = torch.rand(3)

print(t3)
print(t4)

dot_product2 = t3.dot(t4)
dot_product2<jupyter_output>tensor([0.4361, 0.8183, 0.1025])
tensor([0.1819, 0.4905, 0.5753])<jupyter_text>Explanation about example<jupyter_code># Example 3 - breaking (to illustrate when it breaks)
t1 = torch.tensor([10, 5])
t2 = torch.tensor([2, 5, 9])

dot_product = t1.dot(t2)
print(dot_product)<jupyter_output><empty_output><jupyter_text>The matrices don't have the same size  ConclusionI have learnt the basic of PyTorch  Reference LinksProvide links to your references and other interesting articles about tensors* Official documentation for `torch.Tensor`: https://pytorch.org/docs/stable/tensors.html* Official documentation for `torch.normal` https://pytorch.org/docs/master/generated/torch.normal.html<jupyter_code>!pip install jovian --upgrade --quiet
import jovian
jovian.commit()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11858529819694869, 'fraction_numerical': 0.03415395284327323, 'mean_word_length': 5.653979238754325, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 65, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '165830', 'license': 'MIT', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1922, 'n_words': 578, 'path': '01-tensor-operations.ipynb', 'repo_name': 'kevinpulido89/Pytorch_Zero2GANS', 'size': '5768'}