Server Side Programming Articles - Page 760 of 2650

Extract a data frame column values as a vector by matching a vector in R.

Nizamuddin Siddiqui
Updated on 08-Nov-2021 07:39:56

1K+ Views

To extract a data frame column values as a vector by matching a vector in R, we can use subsetting with %in% operator.For Example, if we have a data frame called df having a column say C and a vector V and we want to extract values in C if they match with V then we can use the command given below −df[df$C %in% V,"C"]Example 1Following snippet creates a sample data frame and vector −x1

How to compute the Logarithm of elements of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:08:01

1K+ Views

To compute the logarithm of elements of a tensor in PyTorch, we use the torch.log() method. It returns a new tensor with the natural logarithm values of the elements of the original input tensor. It takes a tensor as the input parameter and outputs a tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Create a tensor and print it.Compute torch.log(input). It takes input, a tensor, as the input parameter and returns a new tensor with the natural logarithm values of elements of the input.Print the tensor ... Read More

How to get the data type of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:06:20

14K+ Views

A PyTorch tensor is homogenous, i.e., all the elements of a tensor are of the same data type. We can access the data type of a tensor using the ".dtype" attribute of the tensor. It returns the data type of the tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Create a tensor and print it.Compute T.dtype. Here T is the tensor of which we want to get the data type.Print the data type of the tensor.Example 1The following Python program shows how to get the data ... Read More

How to compute the sine of elements of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:15:17

552 Views

To compute the sine of elements of a tensor, we use the torch.sin() method. It returns a new tensor with the sine values of the elements of the original input tensor. It takes a tensor as the input parameter and outputs a tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Create a tensor and print it.Compute torch.sin(input). It takes input, a tensor as input parameter, and returns a new tensor with the sine values of elements of the input.Print the tensor with the sine values of ... Read More

How to squeeze and unsqueeze a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:03:48

5K+ Views

To squeeze a tensor, we use the torch.squeeze() method. It returns a new tensor with all the dimensions of the input tensor but removes size 1. For example, if the shape of the input tensor is (M ☓ 1 ☓ N ☓ 1 ☓ P), then the squeezed tensor will have the shape (M ☓ M ☓ P).To unsqueeze a tensor, we use the torch.unsqueeze() method. It returns a new tensor dimension of size 1 inserted at specific position.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed ... Read More

How to compute the histogram of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:02:22

2K+ Views

The histogram of a tensor is computed using torch.histc(). It returns a histogram represented as a tensor. It takes four parameters: input, bins, min and max. It sorts the elements into equal width bins between min and max. It ignores the elements smaller than the min and greater than the max.StepsImport the required library. In all the following Python examples, the required Python libraries are torch and Matplotlib. Make sure you have already installed them.Create a tensor and print it.Compute torch.histc(input, bins=100, min=0, max=100). It returns a tensor of histogram values. Set bins, min, and max to appropriate values according ... Read More

How to find mean across the image channels in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:00:45

2K+ Views

RGB images have three channels, Red, Green, and Blue. We need to compute the mean of the image pixel values across these image channels. For this purpose, we use the method torch.mean(). But the input parameter to this method is a PyTorch tensor. So, we first convert the image to the PyTorch tensor and then apply this method. It returns the mean value of all the elements in the tensor. To find the mean across the image channels, we set the parameter dim = [1, 2].StepsImport the required library. In all the following Python examples, the required Python libraries are ... Read More

How to compare two tensors in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:13:06

11K+ Views

To compare two tensors element-wise in PyTorch, we use the torch.eq() method. It compares the corresponding elements and returns "True" if the two elements are same, else it returns "False". We can compare two tensors with same or different dimensions, but the size of both the tensors must match at non-singleton dimension.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Create a PyTorch tensor and print it.Compute torch.eq(input1, input2). It returns a tensor of "True" and/or "False". It compares the tensor element-wise, and returns True if the corresponding ... Read More

How to find the k-th and the top "k" elements of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 09:56:34

905 Views

PyTorch provides a method torch.kthvalue() to find the k-th element of a tensor. It returns the value of the k-th element of tensor sorted in ascending order, and the index of the element in the original tensor.torch.topk() method is used to find the top "k" elements. It returns the top "k" or largest "k" elements in the tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Create a PyTorch tensor and print it.Compute torch.kthvalue(input, k). It returns two tensors. Assign these two tensors to two new variables ... Read More

How to sort the elements of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Nov-2021 10:10:46

2K+ Views

To sort the elements of a tensor in PyTorch, we can use the torch.sort() method. This method returns two tensors. The first tensor is a tensor with sorted values of the elements and the second tensor is a tensor of indices of elements in the original tensor. We can compute the 2D tensors, row-wise and column-wise.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Create a PyTorch tensor and print it.To sort the elements of the above-created tensor, compute torch.sort(input, dim). Assign this value to a new ... Read More

Advertisements