Found 26504 Articles for Server Side Programming

How to construct a complex tensor with the given real and imaginary parts in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:51:23

2K+ Views

With given real and imaginary parts, we can construct a complex number in PyTorch using torch.complex() method. The real and imaginary parts must be float or double. Both the real and imaginary parts must be of the same type. If the real part is float, then the imaginary must also be float.If the inputs are torch.float32, then the constructed complex tensor must be torch.complex64.If the inputs are torch.float64, then the complex tensor must be torch.complex128.Syntaxtorch.complex(real, imag)Parametersreal and imag − Real and imaginary parts of the complex tensor. Both must be of the same dtype, float or double only.StepsWe could use the ... Read More

Find sub-matrix with the given sum in C++

sudhir sharma
Updated on 25-Jan-2022 11:16:54

1K+ Views

In this problem, we are given a 2D matrix of size N*N and two variables sum and size. Our task is to find a sub-matrix with the given sum.We need to find a sub-matrix of size*size with element sum equal to sum.Let's take an example to understand the problem, Input : mat[][] = {    {1, 5, 7, 9}    {2, 4, 6, 8}    {1, 2, 5, 6}    {3, 6, 9, 3} } sum = 22 Size = 2 Output : YESExplanation −The submatrix of size k with sum 22 is {5, 7} {4, 6}Solution ApproachA simple solution ... Read More

How to define a simple Convolutional Neural Network in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:45:27

356 Views

To define a simple convolutional neural network (CNN), we could use the following steps −StepsFirst we import the important libraries and packages. We try to implement a simple CNN in PyTorch. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torch import torch.nn as nn import torch.nn.functional as FOur next step is to build a simple CNN model. Here, we use the nn package to implement our model. For this, we define a class MyNet and pass nn.Module as the parameter.class MyNet(nn.Module):We need to create two functions inside the class to ... Read More

How to define a simple artificial neural network in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:39:11

571 Views

To define a simple artificial neural network (ANN), we could use the following steps −StepsFirst we import the important libraries and packages. We try to implement a simple ANN in PyTorch. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torch import torch.nn as nnOur next step is to build a simple ANN model. Here, we use the nn package to implement our model. For this, we define a class MyNetwork and pass nn.Module as the parameter.class MyNetwork(nn.Module):We need to create two functions inside the class to get our model ready. ... Read More

Find start and ending index of an element in an unsorted array in C++

sudhir sharma
Updated on 25-Jan-2022 08:48:09

936 Views

In this problem, we are given an array aar[] of n integer values which are not sorted and an integer val. Our task is to find the start and ending index of an element in an unsorted array.For the occurrence of the element in the array, we will return, "Starting index and ending index " if it is found in the array twice or more."Single index " if it is found in the array once."Element not present " if it is not present in the array.Let's take an example to understand the problem, Example 1Input : arr[] = {2, 1, ... Read More

How to load a Computer Vision dataset in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:33:23

786 Views

There are many datasets available in Pytorch related to computer vision tasks. The torch.utils.data.Dataset provides different types of datasets. The torchvision.datasets is a subclass of torch.utils.data.Dataset and has many datasets related to images and videos. PyTorch also provides us a torch.utils.data.DataLoader which is used to load multiple samples from a dataset.StepsWe could use the following steps to load computer vision datasets −Import the required libraries. In all the following examples, the required Python libraries are torch, Matplotlib, and torchvision. Make sure you have already installed them.import torch import torchvision from torchvision import datasets from torchvision.transforms import ToTensor import matplotlib.pyplot as ... Read More

How to measure the Binary Cross Entropy between the target and the input probabilities in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:13:43

1K+ Views

We apply the BCELoss() method to compute the binary cross entropy loss between the input and target (predicted and actual) probabilities. BCELoss() is accessed from the torch.nn module. It creates a criterion that measures the binary cross entropy loss. It is a type of loss function provided by the torch.nn module.The loss functions are used to optimize a deep neural network by minimizing the loss. Both the input and target should be torch tensors having the class probabilities. Make sure that the target is between 0 and 1. Both the input and target tensors may have any number of dimensions. ... Read More

torch.nn.Dropout() Method in Python PyTorch

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:08:00

983 Views

Making some of the random elements of an input tensor zero has been proven to be an effective technique for regularization during the training of a neural network. To achieve this task, we can apply torch.nn.Dropout(). It zeroes some of the elements of the input tensor.An element will be zeroed with the given probability p. It uses a Bernoulli distribution to take samples of the element being zeroed. It does not support complex-valued inputs.Syntaxtorch.nn.Dropout(p=0.5)The default probability of an element to zeroed is set to 0.5StepsWe could use the following steps to randomly zero some of the elements of an input ... Read More

How to rescale a tensor in the range [0, 1] and sum to 1 in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:03:26

2K+ Views

We can rescale an n-dimensional input Tensor such that the elements lie within the range [0, 1] and sum to 1. To do this, we can apply the Softmax() function. We can rescale the n-dimensional input tensor along a particular dimension. The size of the output tensor is the same as the input tensor.Syntaxtorch.nn.Softmax(dim)Parametersdim – The dimension along which the Softmax is computed.StepsWe could use the following steps to crop an image at random location with given size −Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import ... Read More

How to apply rectified linear unit function element-wise in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 07:59:27

3K+ Views

To apply a rectified linear unit (ReLU) function element-wise on an input tensor, we use torch.nn.ReLU(). It replaces all the negative elements in the input tensor with 0 (zero), and all the non-negative elements are left unchanged. It supports only real-valued input tensors. ReLU is used as an activation function in neural networks.Syntaxrelu = torch.nn.ReLU() output = relu(input)StepsYou could use the following steps to apply rectified linear unit (ReLU) function element-wise −Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torch import torch.nn as nnDefine input tensor ... Read More

Advertisements