Adjust Saturation of an Image in PyTorch

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

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

Apply Linear Transformation to Input Data in PyTorch

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

863 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

Flatten Input Tensor by Reshaping in PyTorch

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

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

Compute Cross-Entropy Loss Between Input and Target Tensors in PyTorch

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

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

Measure Mean Squared Error (Squared L2 Norm) in PyTorch

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

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

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

Adjust the Hue of an Image in PyTorch

Shahid Akhtar Khan
Updated on 20-Jan-2022 07:43:56

2K+ Views

The hue of an image refers to the three primary colors (red, blue, and yellow) and the three secondary colors (orange, green, and violet). To adjust the hue of an image, we apply adjust_hue(). It's one of the functional transforms provided by the torchvision.transforms module.adjust_hue() 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 of tensor images.The image hue is adjusted by converting the image to ... Read More

Compute Cosine Similarity Between Two Tensors in PyTorch

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

13K+ Views

To compute the cosine similarity between two tensors, we use the CosineSimilarity() function provided by the torch.nn module. It returns the cosine similarity value computed along dim.dim is an optional parameter to this function along which cosine similarity is computed.For 1D tensors, we can compute the cosine similarity along dim=0 only.For 2D tensors, we can compute cosine similarity along dim=0 or 1.The size of both tensors must be the same to compute the cosine similarity. Both tensors must be real-valued. Cosine similarity is often used to measure document similarity in text analysis.Syntaxtorch.nn.CosineSimilarity(dim=1)The default dim is set to 1. But if ... Read More

PyTorch Clamp Method

Shahid Akhtar Khan
Updated on 20-Jan-2022 07:37:13

6K+ Views

torch.clamp() is used to clamp all the elements in an input into the range [min, max]. It takes three parameters: the input tensor, min, and max values. The values less than the min are replaced by the min and the values greater than the max are replaced by the max.If min is not given, then there is no lower bound. If max is not given, then there is no upper bound. Suppose we set min=−0.5 and max=0.4, then the values less than −0.5 are replaced by −0.5 and values greater than 0.4 are replaced by 0.4. The values between these ... Read More

Create Identity Matrix in PyTorch

Shahid Akhtar Khan
Updated on 20-Jan-2022 07:27:06

2K+ Views

Identity matrix, also known as Unit matrix, is a "n ☓ n" square matrix with 1's on the main diagonal and 0's elsewhere. It is the multiplicative identity of square matrices. because any square matrix multipliedUnit matrix is also called the identity matrix. Unit matrix is used as the multiplicative identity of square matrices in the matrices concept. When any square matrix is multiplied by the identity matrix, then the result doesn't change. In linear algebra, the unit matrix of size n is the n ☓ n square matrix with ones on the main diagonal and zeros elsewhere.To create an ... Read More

Advertisements