Found 135 Articles for PyTorch

How to apply a 2D Max Pooling in PyTorch?

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

4K+ 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

7K+ 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

How to apply linear transformation to the input data in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 08:21:13

675 Views

We can apply a linear transformation to the input data using the torch.nn.Linear() module. It supports input data of type TensorFloat32. This is applied as a layer in the deep neural networks to perform linear transformation. The linear transform used −y = x * W ^ T + bHere x is the input data, y is the output data after linear transform. W is the weight matrix and b is biases. The weights W have shape (out_features, in_features) and biases b have shape (out_features). They are initialized randomly and updated during the training of a Neural Network.Syntaxtorch.nn.Linear(in_features, out_features)Parametersin_features - It ... Read More

How to upsample a given multi-channel temporal, spatial or volumetric data in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 08:28:44

949 Views

A temporal data can be represented as a 1D tensor, and spatial data as 2D tensor while a volumetric data can be represented as a 3D tensor. The Upsample class provided by torch.nn module supports these types of data to be upsampled. But these data must be in the form N ☓ C ☓ D (optional) ☓ H (optional) ☓ W (optional), Where N is the minibatch size, C is the numberchannels, D, H and W are depth, height and width of the data, respectively. Hence, to upsample a temporal data (1D), we need it to be in 3D in ... Read More

How to adjust saturation of an image in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 08:25:31

828 Views

The saturation of an image refers to the intensity of a color. The higher the saturation of a color, the more vivid it is. The lower the saturation of a color, the closer it is to gray.To adjust the saturation of an image, we apply adjust_saturation(). It's one of the functional transforms provided by the torchvision.transforms module. adjust_saturation() transformation accepts both PIL and tensor images. A tensor image is a PyTorch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.This transform also accepts a batch ... Read More

How to flatten an input tensor by reshaping it in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 08:08:43

5K+ Views

A tensor can be flattened into a one-dimensional tensor by reshaping it using the method torch.flatten(). This method supports both real and complex-valued input tensors. It takes a torch tensor as its input and returns a torch tensor flattened into one dimension.It takes two optional parameters, start_dim and end_dim. If these parameters are passed, only those dimensions starting with start_dim and ending with end_dim are flattened.The order of elements in the input tensor is not changed. This function may return the original object, a view, or copy. In the following examples, we cover all the aspects of flattening the tensor ... Read More

How to compute the cross entropy loss between input and target tensors in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 07:57:35

9K+ Views

To compute the cross entropy loss between the input and target (predicted and actual) values, we apply the function CrossEntropyLoss(). It is accessed from the torch.nn module. It creates a criterion that measures the 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. CrossEntropyLoss() is very useful in training multiclass classification problems. The input is expected to contain unnormalized scores for each class.The target tensor may contain class indices in the range of [0, C-1] where C is the number ... Read More

How to measure the mean squared error(squared L2 norm) in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 07:53:40

5K+ Views

Mean squared error is computed as the mean of the squared differences between the input and target (predicted and actual) values. To compute the mean squared error in PyTorch, we apply the MSELoss() function provided by the torch.nn module. It creates a criterion that measures the mean squared error. It is also known as the squared L2 norm.Both the actual and predicted values are torch tensors having the same number of elements. Both tensors may have any number of dimensions. This function returns a tensor of a scalar value. It is a type of loss function provided by the torch.nn ... Read More

How to compute pairwise distance between two vectors in PyTorch?

Shahid Akhtar Khan
Updated on 20-Jan-2022 07:46:51

2K+ Views

A vector in PyTorch is a 1D tensor. To compute pairwise distance between two vectors, we can use the PairwiseDistance() function. It uses p-norm to compute the pairwise distance. PairwiseDistance is basically a class provided by the torch.nn module.The size of both the vectors must be same.Pairwise distance can be computed for both real and complex-valued inputs.The vectors must be in [N, D] shape, where N is the batch dimension and D is the vector dimension.Syntaxtorch.nn.PairwiseDistance(p=2)The default p is set to 2.StepsYou could use the following steps to compute the pairwise distance between two vectorsImport the required library. In all the ... Read More

Previous 1 ... 3 4 5 6 7 ... 14 Next
Advertisements