Learn data science and machine learning by building real-world projects on Jovian

# abdullah-aslam/jovian-tensoroperations

8 months ago
In [1]:
``!pip install jovian --upgrade --quiet``
``` |████████████████████████████████| 71kB 3.4MB/s eta 0:00:011 Building wheel for uuid (setup.py) ... done ```
In [2]:
``import jovian``
In [3]:
``import torch``

#Function 1 - torch.tensor()

torch.tensor() is used to create a tensor

In [4]:
``````# Example 1 - working

t1 = torch.tensor([[0.2, 0.5],
[0.4, 0.6]])
t1
``````
Out[4]:
``````tensor([[0.2000, 0.5000],
[0.4000, 0.6000]])``````
In [5]:
``````# Example 2 - working

t2 = torch.tensor([[10., 5.],
[4., 0.],
t2
``````
Out[5]:
``````tensor([[10.,  5.],
[ 4.,  0.],

All the rows of a Tensor should have the same number of elements

In [ ]:
``````# Example 3 - not working

t3 = torch.tensor([[0.2, 0.5],
[0.4, 0.6, 0.6]])
t3
``````
```--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-be4a333fac46> in <module>() 2 3 t3 = torch.tensor([[0.2, 0.5], ----> 4 [0.4, 0.6, 0.6]]) 5 t3 ValueError: expected sequence of length 2 at dim 1 (got 3)```

#Function 2 - torch.mean()

This function is used to get the mean of all the elements of the tensor

In [6]:
``````# Example 1 - working

t4 = torch.rand(2,3)
torch.mean(t4)
``````
Out[6]:
``tensor(0.7626)``
In [7]:
``````# Example 2 - working

t5 = torch.empty((2, 3))
t5.random_(0,10)
torch.mean(t5)
``````
Out[7]:
``tensor(5.3333)``
In [8]:
``````# Example 3 - not working

t6 = torch.empty((2, 3), dtype=int)
t6.random_(0,10)
torch.mean(t6)
``````
```--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-8-694f6988812d> in <module>() 3 t6 = torch.empty((2, 3), dtype=int) 4 t6.random_(0,10) ----> 5 torch.mean(t6) RuntimeError: Can only calculate the mean of floating types. Got Long instead.```

#Function 3 - torch.exp()

This function is used to get the exponent of all the elements of the tensor

In [9]:
``````# Example 1 - working

t = torch.rand(2,3)
torch.exp(t)
``````
Out[9]:
``````tensor([[2.0768, 1.8824, 1.9556],
[2.4777, 1.2042, 1.6205]])``````
In [10]:
``````# Example 2 - working

t = torch.rand(5, 5)
torch.exp(t)
``````
Out[10]:
``````tensor([[1.9577, 2.1649, 2.4221, 1.2105, 2.0875],
[1.0532, 1.6395, 1.0534, 2.7052, 1.9049],
[1.6756, 1.2287, 2.4513, 1.3066, 1.5015],
[1.0717, 1.6687, 2.6902, 2.1645, 2.2169],
[1.3200, 1.3403, 1.2996, 2.2516, 2.3496]])``````
In [11]:
``````# Example 3 - not working

t = torch.tensor([10, 12, 15])
torch.exp(t)
``````
```--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-11-31a04331c5ea> in <module>() 2 3 t = torch.tensor([10, 12, 15]) ----> 4 torch.exp(t) RuntimeError: exp_vml_cpu not implemented for 'Long'```

#Function 4 - torch.sigmoif()

This function is used to get the sigmoid activation output of all the elements of the tensor

In [12]:
``````# Example 1 - working

t = torch.rand(3, 5)
torch.sigmoid(t)
``````
Out[12]:
``````tensor([[0.5672, 0.6204, 0.6254, 0.5239, 0.7259],
[0.6139, 0.7189, 0.7107, 0.6688, 0.6625],
[0.5860, 0.5043, 0.5842, 0.5284, 0.5509]])``````
In [13]:
``````# Example 2 - working

t = torch.rand(10, 10)
torch.sigmoid(t)
``````
Out[13]:
``````tensor([[0.6630, 0.7291, 0.6122, 0.6865, 0.6735, 0.6454, 0.6584, 0.6683, 0.6233,
0.6028],
[0.6597, 0.5898, 0.5244, 0.6238, 0.6892, 0.6673, 0.6411, 0.6625, 0.6479,
0.5007],
[0.6652, 0.5295, 0.6688, 0.5747, 0.7284, 0.5783, 0.6715, 0.6155, 0.6346,
0.6937],
[0.5754, 0.7075, 0.6653, 0.5484, 0.6427, 0.5864, 0.6867, 0.6701, 0.5469,
0.6814],
[0.6213, 0.5209, 0.7042, 0.5942, 0.6112, 0.5111, 0.6319, 0.6360, 0.5418,
0.5650],
[0.6657, 0.6454, 0.7216, 0.6022, 0.5794, 0.7082, 0.5054, 0.7007, 0.5492,
0.6539],
[0.6811, 0.6207, 0.5455, 0.5563, 0.6257, 0.6767, 0.7154, 0.5323, 0.7291,
0.6459],
[0.6400, 0.5714, 0.5445, 0.7083, 0.5155, 0.6532, 0.5962, 0.6253, 0.6760,
0.6690],
[0.5035, 0.7166, 0.6567, 0.5555, 0.6852, 0.6097, 0.5672, 0.6182, 0.5807,
0.5555],
[0.7220, 0.7022, 0.7285, 0.5525, 0.5170, 0.7201, 0.7238, 0.6993, 0.6382,
0.5379]])``````
In [14]:
``````# Example 3 - not working

t = torch.tensor([10, 12, 15])
torch.sigmoid(t)
``````
```--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-14-3a8b445e7fbd> in <module>() 2 3 t = torch.tensor([10, 12, 15]) ----> 4 torch.sigmoid(t) RuntimeError: "sigmoid_cpu" not implemented for 'Long'```

#Function 4 - torch.reshape()

This function is used to reshape the tensor

In [15]:
``````# Example 1 - working

t = torch.rand(2, 5)
torch.reshape(t, (1, 10))``````
Out[15]:
``````tensor([[0.6285, 0.1467, 0.2982, 0.1329, 0.2327, 0.1762, 0.6228, 0.1510, 0.5922,
0.8110]])``````
In [16]:
``````# Example 2 - working

t = torch.rand(2, 3, 5)
torch.reshape(t, (2, 15))``````
Out[16]:
``````tensor([[0.2560, 0.0383, 0.3982, 0.1040, 0.0760, 0.0583, 0.9024, 0.8584, 0.6486,
0.0037, 0.3634, 0.2023, 0.5500, 0.8208, 0.1139],
[0.6128, 0.9077, 0.0023, 0.8671, 0.9557, 0.7058, 0.4335, 0.6093, 0.3674,
0.6011, 0.8147, 0.2293, 0.9676, 0.9923, 0.0188]])``````
In [17]:
``````# Example 3 - not working

t = torch.rand(2, 3, 5)
torch.reshape(t, (10, 4))``````
```--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-17-78309f730432> in <module>() 2 3 t = torch.rand(2, 3, 5) ----> 4 torch.reshape(t, (10, 4)) RuntimeError: shape '[10, 4]' is invalid for input of size 30```
In [19]:
``jovian.submit(assignment="zerotogans-a1", notebook_url='https://colab.research.google.com/drive/1M2yupKQXmXb-WwywXoCPoxB-lKhbzm3Z?usp=sharing')``
```[jovian] Please enter your API key ( from https://jovian.ai/ ): API KEY: ·········· [jovian] Submitting assignment.. ```
```[jovian] Error: Jovian submit failed. (HTTP 400) “https://colab.research.google.com/drive/1M2yupKQXmXb-WwywXoCPoxB-lKhbzm3Z?usp=sharing” is not a valid Jovian Notebook link ```
In [ ]:
`` ``