[pytorch][Colab]Automatic differentiaton using autograd


#Automatic differentiation import torch #create tensors x = torch.autograd.Variable(torch.tensor([2.]), requires_grad=True) y = torch.autograd.Variable(torch.tensor([1.]), requires_grad=True) z = torch.autograd.Variable(torch.tensor([5.]), requires_grad=True) #let’s say f = (x-y)*z #assign x-y to another tensor a = x – y f = a * z print(“f = (x -y) * z”) print(“for the given x -> “, x ) print( ” ,ContinueContinue reading “[pytorch][Colab]Automatic differentiaton using autograd”

[pytorch][Colab]Linear algebra on tensors


#Linear algebra on tensors import torch #create 2 1D tensors tensor_1 = torch.tensor ([1,2,3]) tensor_2 = torch.tensor([2,4,6]) #Matrix multiplication (dot product) print(“Dot product(scalar) of “, tensor_1, ” & “, tensor_2 , ” is ” , torch.matmul(tensor_1, tensor_2)) #create 1 2D tensor (3*1) tensor_3 = torch.tensor([[4],[5],[6]]) print(“matrix multiplication of “, tensor_1, ” & “, tensor_3 ,ContinueContinue reading “[pytorch][Colab]Linear algebra on tensors”

[pytorch][Colab] arithmentic and reduction functions on tensors


#arithmentic and reduction functions on tensors import torch #create 2 tensors input_A = torch.tensor([1,2,3]) input_B = torch.tensor([2,4,6]) #Arithmetic operations print(input_A ,” + “, input_B, ” = “, input_A.add(input_B)) print(input_A ,” – “, input_B, ” = “, input_A.sub(input_B)) print(input_A ,” * “, input_B, ” = “, input_A.mul(input_B)) print(input_A ,” / “, input_B, ” = “, input_A.div(input_B))ContinueContinue reading “[pytorch][Colab] arithmentic and reduction functions on tensors”

[pytorch][Colab]Tensor-index,slice,combine and split


import torch #Access the tensor element via indexing #Create 1D tensor oneD_tensor = torch.tensor([12,24,36,48,60]) print(oneD_tensor[2]) #to fetch the value at the index 2, we should use item() for the conversion to value print(oneD_tensor[2].item()) #take 2-4 elements and form a new tensor — slicing subset_oneD = oneD_tensor[2:5] print(“sliced “, subset_oneD) #A little bit about 2D tensorContinueContinue reading “[pytorch][Colab]Tensor-index,slice,combine and split”

[pytorch][Colab]Step to make random tensor’s value unchanged


#How to make random tensor’s value unchanged import torch #Random tensor will have random values in each execution.To make the random value constant throughout , Use manual_seed() #for example torch.manual_seed(111) torch.rand(2,3) #this way we can make the random values constant , comment torch.manual_seed(111) and execute to see the difference . #every execution will give differentContinueContinue reading “[pytorch][Colab]Step to make random tensor’s value unchanged”

[pytorch][Colab]Learning tensor data types


learning tensor data types import torch declare tensor of data type 8bit int(char) sint8_tensor = torch.tensor([1.3,2,3.7,4,5],dtype=torch.int8) print the type of the newly created tensor print (“type of sint8_tensor is “, sint8_tensor.dtype)print (“data inside the sint8_tensor: “, sint8_tensor) declare tensor of data type float32 float_tensor = torch.tensor([1.3453,2,3,4,5],dtype=torch.float32)print (“type of float_tensor is “, float_tensor.dtype)print(“data inside the float_tensor:ContinueContinue reading “[pytorch][Colab]Learning tensor data types”

[pytorch][colab]Learning tensor attributes by a simple example


#Learning about tensor attributes #Hint: tensor can be treated as multi-dimensional array. import torch oneD_tensor = torch.tensor([10,12,14]) print(“1D tensor -> “, oneD_tensor) #print the location where tensor stored. print(“The device location of the given tensor is “, oneD_tensor.device) #print the data type of the tensor print(“The data type of the given tensor is”, oneD_tensor.dtype) #printContinueContinue reading “[pytorch][colab]Learning tensor attributes by a simple example”

[pytorch][Colab]Different ways to create tensors


#Different ways to create tensors import torch import numpy as np #initialise a tensor frm python list tensor_frm_list = torch.tensor([1,2,3]) #initialise a tensor frm a tuple tensor_frm_tuple = torch.tensor((8,9,10)) print(“tensor from list”,tensor_frm_list) print(“tensor from tuple”, tensor_frm_tuple) #initialise tensor from ndArray tensor_frm_ndArray = torch.tensor(np.array([6,8,10])) print(“tensor from ndArray”, tensor_frm_ndArray) #different functions to create tensors #create an emptyContinueContinue reading “[pytorch][Colab]Different ways to create tensors”

[pytorch][colab]Create tensor via GPU or CPU


#Hint – Change Runtime type option is used to select GPU or CPU import torch print(torch.version.__version__) print(torch.cuda.is_available()) if torch.cuda.is_available() : device = “cuda” else: device=”cpu” print(device) first_tensor = torch.tensor([[10,20,30],[15,25,35]],device=device) second_tensor = torch.tensor([[12,22,32],[17,27,37]], device=device) product_of_oneAndTwo_tensor = first_tensor * second_tensor print(product_of_oneAndTwo_tensor) print(product_of_oneAndTwo_tensor.size())

[Pytorch][Colab]Tensor creation


#Creating Tensor Example import torch first_tensor = torch.tensor([[10,20,30],[15,25,35]]) second_tensor = torch.tensor([[12,22,32],[17,27,37]]) Total_of_oneAndTwo_tensor = first_tensor + second_tensor print(Total_of_oneAndTwo_tensor) print(Total_of_oneAndTwo_tensor.size()) Difference_of_oneAndTwo_tensor = first_tensor – second_tensor print(Difference_of_oneAndTwo_tensor) print(Difference_of_oneAndTwo_tensor.size())

Design a site like this with WordPress.com
Get started