Pytorch
Pytorch.tensor
A brief idea about "What is Pytorch ?"
pytorch is an open source machine learning library , it contain tensor library that enable to create a scalar, a vector ,a matrix or in short we can create an n-dimensional matrix . It is used in computer vision and natural language processing ,primary developed by facebook's Reseacrch Lab. It is open source software and released under the modified BSD(Barkley software Distribution)license.
There are a huge no. of functions inside Pytorch
,but regarding the first assignment of jovian.ml's free certification deep learning course ,the following are five intersting Pytorch functions.
- function 1 ->torch.tensor()
- function 2 ->fill_diagonal_()
- function 3 ->append(*size)
- function 4 ->index_copy()
- function 5 ->add()
#if you don't have pytorch in your system you can download it using the below command , but if you already have pytorch then you dont need to run the below command
!conda install pytorch cpuonly -c pytorch -y
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.8.2
latest version: 4.8.3
Please update conda by running
$ conda update -n base conda
## Package Plan ##
environment location: /srv/conda/envs/notebook
added / updated specs:
- cpuonly
- pytorch
The following packages will be downloaded:
package | build
---------------------------|-----------------
blas-1.0 | mkl 6 KB defaults
ca-certificates-2020.4.5.1 | hecc5488_0 146 KB conda-forge
certifi-2020.4.5.1 | py37hc8dfbb8_0 151 KB conda-forge
cpuonly-1.0 | 0 2 KB pytorch
intel-openmp-2020.1 | 217 780 KB defaults
libgfortran-ng-7.5.0 | hdf63c60_6 1.7 MB conda-forge
mkl-2020.1 | 217 129.0 MB defaults
mkl-service-2.3.0 | py37he904b0f_0 218 KB defaults
mkl_fft-1.0.15 | py37ha843d7b_0 154 KB defaults
mkl_random-1.1.1 | py37h0da4684_0 366 KB conda-forge
ninja-1.10.0 | hc9558a2_0 1.9 MB conda-forge
numpy-1.18.1 | py37h4f9e942_0 5 KB defaults
numpy-base-1.18.1 | py37hde5b4d6_1 4.2 MB defaults
openssl-1.1.1g | h516909a_0 2.1 MB conda-forge
python_abi-3.7 | 1_cp37m 4 KB conda-forge
pytorch-1.5.0 | py3.7_cpu_0 90.5 MB pytorch
------------------------------------------------------------
Total: 231.1 MB
The following NEW packages will be INSTALLED:
blas pkgs/main/linux-64::blas-1.0-mkl
cpuonly pytorch/noarch::cpuonly-1.0-0
intel-openmp pkgs/main/linux-64::intel-openmp-2020.1-217
libgfortran-ng conda-forge/linux-64::libgfortran-ng-7.5.0-hdf63c60_6
mkl pkgs/main/linux-64::mkl-2020.1-217
mkl-service pkgs/main/linux-64::mkl-service-2.3.0-py37he904b0f_0
mkl_fft pkgs/main/linux-64::mkl_fft-1.0.15-py37ha843d7b_0
mkl_random conda-forge/linux-64::mkl_random-1.1.1-py37h0da4684_0
ninja conda-forge/linux-64::ninja-1.10.0-hc9558a2_0
numpy pkgs/main/linux-64::numpy-1.18.1-py37h4f9e942_0
numpy-base pkgs/main/linux-64::numpy-base-1.18.1-py37hde5b4d6_1
python_abi conda-forge/linux-64::python_abi-3.7-1_cp37m
pytorch pytorch/linux-64::pytorch-1.5.0-py3.7_cpu_0
The following packages will be UPDATED:
ca-certificates 2019.11.28-hecc5488_0 --> 2020.4.5.1-hecc5488_0
certifi 2019.11.28-py37_0 --> 2020.4.5.1-py37hc8dfbb8_0
openssl 1.1.1d-h516909a_0 --> 1.1.1g-h516909a_0
Downloading and Extracting Packages
blas-1.0 | 6 KB | ##################################### | 100%
numpy-base-1.18.1 | 4.2 MB | ##################################### | 100%
pytorch-1.5.0 | 90.5 MB | ##################################### | 100%
cpuonly-1.0 | 2 KB | ##################################### | 100%
intel-openmp-2020.1 | 780 KB | ##################################### | 100%
certifi-2020.4.5.1 | 151 KB | ##################################### | 100%
openssl-1.1.1g | 2.1 MB | ##################################### | 100%
mkl-service-2.3.0 | 218 KB | ##################################### | 100%
ca-certificates-2020 | 146 KB | ##################################### | 100%
ninja-1.10.0 | 1.9 MB | ##################################### | 100%
python_abi-3.7 | 4 KB | ##################################### | 100%
numpy-1.18.1 | 5 KB | ##################################### | 100%
mkl_fft-1.0.15 | 154 KB | ##################################### | 100%
mkl-2020.1 | 129.0 MB | ##################################### | 100%
mkl_random-1.1.1 | 366 KB | ##################################### | 100%
libgfortran-ng-7.5.0 | 1.7 MB | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
# Import torch and other required modules
import torch
Function 1 - torch.tensor (data, dtype=None, device=None, requires_grad=False, pin_memory=False)
it creates a tensor of given shape
a tensor could be a scalar ,a vector ,a matrix or a n-dimensional matrix
we can define the data type of tensor elements by using dtype attribute
we can save the gradients by using requires_grad = True
# Example 1 - creating a vector using tensor
t1 = torch.tensor([1, 2,3,7,1]) #vector of int datatype
print(t1)
t2 = torch.tensor([4,2,5,.9]) #if one float can convert the whole tensor into flaat
print(t2)
t3 = torch.tensor([2,6,1,6,8.], dtype = torch.int32 #we can convert the tensor data type
print(t3)
tensor([1, 2, 3, 7, 1])
tensor([4.0000, 2.0000, 5.0000, 0.9000])
tensor([2, 6, 1, 6, 8], dtype=torch.int32)