Shahid Akhtar Khan

Shahid Akhtar Khan

169 Articles Published

Articles by Shahid Akhtar Khan

Page 17 of 17

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 968 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 compute the mean and standard deviation of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 6K+ Views

A PyTorch tensor is like a numpy array. The only difference is that a tensor utilizes the GPUs to accelerate numeric computations. The mean of a tensor is computed using the torch.mean() method. It returns the mean value of all the elements in the input tensor. We can also compute the mean row-wise and column-wise, providing suitable axis or dim.The standard deviation of a tensor is computed using torch.std(). It returns the standard deviation of all the elements in the tensor. Like mean, we can also compute the standard deviation, row or column-wise.StepsImport the required library. In all the following ...

Read More

How to perform element-wise division on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 5K+ Views

To perform element-wise division on two tensors in PyTorch, we can use the torch.div() method. It divides each element of the first input tensor by the corresponding element of the second tensor. We can also divide a tensor by a scalar. A tensor can be divided by a tensor with same or different dimension. The dimension of the final tensor will be same as the dimension of the higher-dimensional tensor. If we divide a 1D tensor by a 2D tensor, then the final tensor will a 2D tensor.StepsImport the required library. In all the following Python examples, the required Python ...

Read More

How to perform element-wise subtraction on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 5K+ Views

To perform element-wise subtraction on tensors, we can use the torch.sub() method of PyTorch. The corresponding elements of the tensors are subtracted. We can subtract a scalar or tensor from another tensor. We can subtract a tensor from a tensor with same or different dimension. The dimension of the final tensor will be same as the dimension of the higher-dimensional tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Define two or more PyTorch tensors and print them. If you want to subtract a scalar quantity, define ...

Read More

How to perform element-wise addition on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 9K+ Views

We can use torch.add() to perform element-wise addition on tensors in PyTorch. It adds the corresponding elements of the tensors. We can add a scalar or tensor to another tensor. We can add tensors with same or different dimensions. The dimension of the final tensor will be same as the dimension of the higher dimension tensor.StepsImport the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.Define two or more PyTorch tensors and print them. If you want to add a scalar quantity, define it.Add two or more tensors ...

Read More

How to resize a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 7K+ Views

To resize a PyTorch tensor, we use the .view() method. We can increase or decrease the dimension of the tensor, but we have to make sure that the total number of elements in a tensor must match before and after the resize.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.Resize the above-created tensor using .view() and assign the value to a variable. .view() does not resize the original tensor; it only gives a view with the new size, as its ...

Read More

How to access the metadata of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 910 Views

We access the size (or shape) of a tensor and the number of elements in the tensor as the metadata of the tensor. To access the size of a tensor, we use the .size() method and the shape of a tensor is accessed using .shape.Both .size() and .shape produce the same result. We use the torch.numel() function to find the total number of elements in the tensor.StepsImport the required library. Here, the required library is torch. Make sure that you have installed torch.Define a PyTorch tensor.Find the metadata of the tensor. Use .size() and .shape to access the size and ...

Read More

How to access and modify the values of a Tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 10K+ Views

We use Indexing and Slicing to access the values of a tensor.Indexing is used to access the value of a single element of the tensor, whereasSlicing is used to access the values of a sequence of elements.We use the assignment operator to modify the values of a tensor. Assigning new value/s using the assignment operator will modify the tensor with new value/s.StepsImport the required libraries. Here, the required library is torch.Define a PyTorch tensor.Access the value of a single element at particular index using indexing or access the values of sequence of elements using slicing.Modify the accessed values with new ...

Read More

How to convert an image to a PyTorch Tensor?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Nov-2021 16K+ Views

A PyTorch tensor is an n-dimensional array (matrix) containing elements of a single data type. A tensor is like a numpy array. The difference between numpy arrays and PyTorch tensors is that the tensors utilize the GPUs to accelerate the numeric computations. For the accelerated computations, the images are converted to the tensors.To convert an image to a PyTorch tensor, we can take the following steps −StepsImport the required libraries. The required libraries are torch, torchvision, Pillow.Read the image. The image must be either a PIL image or a numpy.ndarray (HxWxC) in the range [0, 255]. Here H, W, and ...

Read More
Showing 161–169 of 169 articles
« Prev 1 13 14 15 16 17 Next »
Advertisements