aaliyan00/assignment-1 - Jovian
Learn data science and machine learning by building real-world projects on Jovian

Title Here

Subtitle Here

An short introduction about PyTorch and about the chosen functions.

  • function 1
  • function 2
  • function 3
  • function 4
  • function 5
# Import torch and other required modules
!conda install numpy pytorch torchvision cpuonly -c pytorch -y
import torch
Collecting package metadata (current_repodata.json): ...working... done Solving environment: ...working... done # All requested packages already installed.

Function 1 - torch.ceil()

Greatest integer function

# Example 1 - working (change this)
a = torch.randn(4)
print(a)
torch.ceil(a)
tensor([-0.5237, -0.0814, 1.0773, 1.0033])
tensor([-0., -0., 2., 2.])

Explanation about example

# Example 2 - working
a = torch.randn(4,2)
print(a)
torch.ceil(a)
tensor([[ 0.5816, 0.1443], [ 0.3552, 0.2337], [-0.9833, 0.0276], [ 1.4207, -1.0113]])
tensor([[ 1.,  1.],
        [ 1.,  1.],
        [-0.,  1.],
        [ 2., -1.]])

Explanation about example

# Example 3 - breaking (to illustrate when it breaks)
a = torch.tensor(0.56)
print(a)
torch.ceil(a)
tensor(0.5600)
tensor(1.)

Explanation about example

This function never breaks. Valid for all real numbers.

Function 2 - torch.clamp

Clamps results into a range

# Example 1 - working
torch.clamp(input=a,max=2,min=1)
tensor([1.0000, 1.0000, 1.0773, 1.0033])

Explanation about example

# Example 2 - working
a = torch.randn(4,2)
print(a)
torch.clamp(input=a,max=9,min=1)
tensor([[-1.7235, -0.9733], [ 1.3332, 1.6049], [ 1.2498, -0.2237], [-1.0387, -0.1093]])
tensor([[1.0000, 1.0000],
        [1.3332, 1.6049],
        [1.2498, 1.0000],
        [1.0000, 1.0000]])

Explanation about example

# Example 3 - breaking (to illustrate when it breaks)
torch.clamp(input=a)
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-45-306d4d7ab6e0> in <module> 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 torch.clamp(input=a) RuntimeError: At least one of 'min' or 'max' must not be None

Min- Max is essential.

Closing comments about when to use this function

Function 3 - torch.rand()

Randomly generate tensor values

# Example 1 - working
torch.rand(2,3)
tensor([[0.1387, 0.9802, 0.2981],
        [0.6336, 0.2652, 0.3142]])

Explanation about example

# Example 2 - working
torch.rand(2,3,2)
tensor([[[0.0409, 0.1381],
         [0.2538, 0.8235],
         [0.6179, 0.0868]],

        [[0.3323, 0.9801],
         [0.7534, 0.5072],
         [0.3219, 0.2887]]])

Explanation about example

# Example 3 - breaking (to illustrate when it breaks)
torch.rand(1.5,2.5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-16-4563d4ae42d7> in <module> 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 torch.rand(1.5,2.5) TypeError: rand() received an invalid combination of arguments - got (float, float), but expected one of: * (tuple of ints size, *, tuple of names names, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) * (tuple of ints size, *, torch.Generator generator, tuple of names names, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) * (tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) * (tuple of ints size, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

Explanation about example Dimesion cannot be in float

Closing comments about when to use this function

Function 4 - torch.mvlgamma

Computes the multivariate log-gamma function

# Example 1 - working
a=torch.tensor([4.,9.,7,2])
torch.mvlgamma(a, 2)
tensor([ 3.5651, 20.7262, 12.8142,  0.4516])

Explanation about example

# Example 2 - working
a=torch.tensor([4.,9.5,78,29])
torch.mvlgamma(a, 8)
tensor([  20.4484,   80.9583, 2039.8702,  512.8800])

Explanation about example

# Example 3 - breaking (to illustrate when it breaks)
a=[4.,9.5,78,29
torch.mvlgamma(a, 8)
File "<ipython-input-63-146c6799fdaa>", line 3 torch.mvlgamma(a, 8) ^ SyntaxError: invalid syntax

Only works with tensor elements

Closing comments about when to use this function

Function 5 - torch.logsumexp

Returns the log of summed exponentials of each row of the input tensor

# Example 1 - working
a = torch.randn(3, 3)
torch.logsumexp(a, 1)
tensor([0.0646, 0.3902, 1.6236])

Explanation about example

# Example 2 - working
a = torch.randn(8, 8)
torch.logsumexp(a, -2)
tensor([2.2687, 3.0811, 3.0577, 2.1462, 2.0659, 2.5851, 2.5604, 2.1103])

Explanation about example

# Example 3 - breaking (to illustrate when it breaks)
a = torch.tensor([2,4.6,9,7])
torch.logsumexp(a, -2)
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-69-7df9f12883e7> in <module> 1 # Example 3 - breaking (to illustrate when it breaks) 2 a = torch.tensor([2,4.6,9,7]) ----> 3 torch.logsumexp(a, -2) IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)

Explanation about example

Closing comments about when to use this function

Conclusion

Summarize what was covered in this notebook, and where to go next

Reference Links

Provide links to your references and other interesting articles about tensors

!pip install jovian --upgrade --quiet
import jovian
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Please enter your API key ( from https://jovian.ml/ ): API KEY: