- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 135 Articles for PyTorch
1K+ 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
5K+ 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
4K+ 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
13K+ Views
torch.mul() method is used to perform element-wise multiplication on tensors in PyTorch. It multiplies the corresponding elements of the tensors. We can multiply two or more tensors. We can also multiply scalar and tensors. Tensors with same or different dimensions can also be multiplied. The dimension of the final tensor will be same as the dimension of higher-dimensional tensor. Element-wise multiplication on tensors are also known as Hadamard product.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 ... Read More
4K+ 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
8K+ 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
6K+ 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
27K+ Views
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 tensors. So, what is the basic difference between these two methods?torch.cat() concatenates a sequence of tensors along an existing dimension, hence not changing the dimension of the tensors.torch.stack() stacks the tensors along a new dimension, as a result, it increases the dimension.StepsImport the required library. In all the following examples, ... Read More
567 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
29K+ Views
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 required libraries are torch and numpy.Create a numpy.ndarray or a PyTorch tensor.Convert the numpy.ndarray to a PyTorch tensor using torch.from_numpy() function or convert the PyTorch tensor to numpy.ndarray using the .numpy() method.Finally, print the converted tensor or numpy.ndarray.Example 1The following Python program converts a numpy.ndarray to a PyTorch tensor.# import ... Read More