Working with the torch.matmul() function in PyTorch
Updated: Jul 07, 2023
Overview The behavior of the torch.matmul() function The torch.matmul() function performs a matrix product of two tensors. The behavior depends on the dimensionality of the tensors as follows: If both tensors are......
How to Transpose a Tensor in PyTorch
Updated: Jul 07, 2023
In the world of PyTorch and deep learning, transposing a tensor means rearranging its dimensions. For example, a tensor with a shape (3, 4) becomes (4, 3) after transposing. Transposition is useful when the original tensor’s shape......
PyTorch tensor shape, rank, and element count
Updated: Apr 18, 2023
The shape of a PyTorch tensor The shape of a PyTorch tensor is the number of elements in each dimension. You can use the shape attribute or the size() method to get the shape of a tensor as a torch.Size object, which is a subclass......
PyTorch: Determine the memory usage of a tensor (in bytes)
Updated: Apr 18, 2023
In order to calculate the memory used by a PyTorch tensor in bytes, you can use the following formula: memory_in_bytes = tensor.element_size() * tensor.nelement() his will give you the size of the tensor data in memory, whether......
PyTorch: How to change the data type of a tensor
Updated: Apr 17, 2023
When working with PyTorch, there might be cases where you need to change the data type of a tensor for some reason, such as to match the data type of another tensor or a scalar operand in an arithmetic operation or to reduce the memory......
How to Create a Tensor Range in PyTorch
Updated: Apr 15, 2023
A tensor range in PyTorch is a sequence of numbers that are stored in a one-dimensional tensor. In order to create a tensor range, you can use the torch.arange() function. Syntax Below is the syntax of the torch.arange()......
PyTorch: How to create tensors with zeros and ones
Updated: Apr 15, 2023
PyTorch is a popular framework for creating and manipulating tensors for deep learning and other applications. Tensors are multidimensional arrays that can store numerical data and perform various operations on them. This practical,......
Ways to Create Random Tensors in PyTorch
Updated: Apr 15, 2023
When developing neural networks with PyTorch, there might be cases where you find random tensors are useful, such as when you need synthetic data for testing or experimentation purposes or when you want to sample from a probability......
Convert a NumPy array to a PyTorch tensor and vice versa
Updated: Apr 14, 2023
This concise, practical article shows you how to convert NumPy arrays into PyTorch tensors and vice versa. Without any further ado, let’s get straight to the main points. Turning NumPy arrays into PyTorch tensors There are......
PyTorch: How to compare 2 tensors
Updated: Apr 14, 2023
Exact equality comparison Exactly equality comparison means checking if two PyTorch tensors have the same shape, dtype, and values. It returns True if they are exactly the same and False otherwise. To deal with this kind of......
Using manual_seed() function in PyTorch
Updated: Apr 14, 2023
Why use torch.manual_seed() torch.manual_seed() is a function that helps you control the randomness in PyTorch. A lot of times, you want to use random numbers in your code, such as when you create a tensor with torch.rand() or when......
PyTorch: How to create a tensor from a Python list
Updated: Apr 14, 2023
When working with PyTorch, there might be cases where you want to create a tensor from a Python list. For example, you want to create a custom tensor with some specific values that are not easily generated by the built-in tensor creation......