Found 33676 Articles for Programming

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

How to apply a 2D Average Pooling in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 07:48:40

3K+ Views

We can apply a 2D Average Pooling over an input image composed of several input planes using the torch.nn.AvgPool2d() module. The input to a 2D Average Pooling layer must be of size [N, C, H, W] where N is the batch size, C is the number of channels, H and W are the height and width of the input image.The main feature of an Average Pooling operation is the filter or kernel size and stride. This module supports TensorFloat32.Syntaxtorch.nn.AvgPool2d(kernel_size)Parameterskernel_size – The size of the window to take an average over.Along with this parameter, there are some optional parameters also such ... Read More

Find Square Root under Modulo p (Shanks Tonelli algorithm) in C++

sudhir sharma
Updated on 25-Jan-2022 08:28:01

598 Views

In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p.Let's take an example to understand the problem, Input : n = 4, p = 11 Output : 9Solution ApproachHere, we will be using Tonelli-Shanks Algorithm.Tonelli-Shanks Algorithm is used in modular arithmetic to solve for a value x in congruence of the form x2 = n (mod p).The algorithm to find square root modulo using shank's Tonelli Algorithm −Step 1 − Find the value of $(n^{((p-1)/2)})(mod\:p)$, if its value is p -1, then modular square root is ... Read More

Find Square Root under Modulo p (When p is in form of 4*i + 3) in C++

sudhir sharma
Updated on 25-Jan-2022 07:46:35

146 Views

In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p (When p is in form of 4*i + 3). Here, p is of the form (4*i + 3) i.e. p % 4 = 3 for i > 1 and p being a prime number.Here are some numbers, 7, 11, 19, 23, 31...Let's take an example to understand the problem, Input : n = 3, p = 7 Output :Solution ApproachA simple solution to the problem is using a loop. We will loop from 2 to (p ... Read More

How to pad the input tensor boundaries with a constant value in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 07:40:43

532 Views

The torch.nn.ConstantPad2D() pads the input tensor boundaries with constant value. The size of the input tensor must be in 3D or 4D in (C, H, W) or (N, C, H, W) format respectively. Where N, C, H, W represents the mini batch size, number of channels, height and width respectively. The padding is done along the height and width of the input tensor.It takes the size of padding (padding) and constant values (value) as the parameters. The size of padding may be an integer or a tuple. The padding may be the same for all boundaries or different for each ... Read More

How to pad the input tensor boundaries with zero in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 07:31:55

3K+ Views

The torch.nn.ZeroPad2D() pads the input tensor boundaries with zeros. It takes the size of padding (padding) as a parameter. The size of padding may be an integer or a tuple. The padding may be the same for all boundaries or different for each boundary.The padding may be an integer or a tuple in (left, right, top, bottom) format. If it is an integer, then the padding along all the boundaries are the same. The height of the padded tensor is increased by top+bottom, whereas the width of the padded tensor is increased by left+right. It does not change the channel ... Read More

How to apply a 2D Max Pooling in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 07:17:45

5K+ Views

We can apply a 2D Max Pooling over an input image composed of several input planes using the torch.nn.MaxPool2d() module. The input to a 2D Max Pool layer must be of size [N, C, H, W] where N is the batch size, C is the number of channels, H and W are the height and width of the input image, respectively.The main feature of a Max Pool operation is the filter or kernel size and stride. This module supports TensorFloat32.Syntaxtorch.nn.MaxPool2d(kernel_size)Parameterskernel_size – The size of the window to take a max over.Along with this parameter, there are some optional parameters also ... Read More

How to apply a 2D transposed convolution operation in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 07:09:16

2K+ Views

We can apply a 2D transposed convolution operation over an input image composed of several input planes using the torch.nn.ConvTranspose2d() module. This module can be seen as the gradient of Conv2d with respect to its input.The input to a 2D transpose convolution layer must be of size [N, C, H, W] where N is the batch size, C is the number of channels, H and W are the height and width of the input image, respectively.Generally a 2D transposed convolution operation is applied on the image tensors. For a RGB image, the number of channels is 3. The main feature ... Read More

How to apply a 2D convolution operation in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 06:59:37

8K+ Views

We can apply a 2D convolution operation over an input image composed of several input planes using the torch.nn.Conv2d() module. It is implemented as a layer in a convolutional neural network (CNN). The input to a 2D convolution layer must be of size [N, C, H, W] where N is the batch size, C is the number of channels, H and W are the height and width of the input tensor.Generally a 2D convolution operation is applied on the image tensors. For an RGB image, the number of channels is 3. The main feature of a convolution operation is the ... Read More

Find the number of primitive roots modulo prime in C++.

sudhir sharma
Updated on 24-Jan-2022 13:34:13

762 Views

In this problem, we are given a prime number N. Our task is to find the number of primitive roots modulo prime.Primitive Root of a number − It is a number (r) smaller than N which has all values of r^x(mod N) different for all X in range [0, n-2].Let’s take an example to understand the problem, Input : N = 5 Output : 2Solution ApproachA simple solution to the problem is based on a trial method. We will check for all numbers from 2 to (N-1) for the conditions with x ranging from [0, n-2] and break if a value ... Read More

Advertisements