Shahid Akhtar Khan has Published 217 Articles

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

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2021 09:45:52

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 ... Read More

How to resize a tensor in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2021 09:44:33

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 ... Read More

How to join tensors in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2021 09:42:37

We can join two or more tensors using torch.cat() and torch.stack(). torch.cat() is used to concatenate two or more tensors, whereas torch.stack() is used to stack the tensors. We can join the tensors in different dimensions such as 0 dimension, -1 dimension.Both torch.cat() and torch.stack() are used to join the ... Read More

How to access the metadata of a tensor in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2021 09:39:31

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 ... Read More

How to convert a NumPy ndarray to a PyTorch Tensor and vice versa?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2021 09:37:24

A PyTorch tensor is like numpy.ndarray. The difference between these two is that a tensor utilizes the GPUs to accelerate numeric computation. We convert a numpy.ndarray to a PyTorch tensor using the function torch.from_numpy(). And a tensor is converted to numpy.ndarray using the .numpy() method.StepsImport the required libraries. Here, the ... 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 09:35:20

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 ... Read More

How to convert an image to a PyTorch Tensor?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2021 09:31:58

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 ... Read More

Advertisements