In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What are the five very useful tensor operations in Pytorch? For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more friends who want to solve this problem find a simpler and easier way.
introduction
Although there are other ways to achieve the same effect, these actions can make it easier to use.
PyTorch is a Python-based scientific package for performing advanced operations using a special data type called tensor. Tensors are numbers, vectors, matrices, or multidimensional arrays with regular shapes and the same data type. PyTorch is another option for NumPy packages that can be used under GPUs. It is also used as a framework for conducting deep learning research.
These five actions are:
expand()permute()tolist()narrow()where()1. expand()
Extends an existing tensor to a new dimension along a dimension with a value of 1. Tensors can be expanded simultaneously along any one or more dimensions. If you don't want to expand the tensor along a particular dimension, you can set its parameter value to-1.
Note: Only a single dimension can be extended
# Example 1 - working
a=torch.tensor([[[1,2,3],[4,5,6]]])
a.size()
>>torch.Size([1, 2, 3])
a.expand(2,2,3)
>>tensor([[[1, 2, 3],
[4, 5, 6]],
[[1, 2, 3],
[4, 5, 6]]])
In this example, the original dimension of the tensor is [1, 2, 3]. It is extended to [2, 2, 3].
2. permute()
This function returns a view of a tensor whose dimensions vary depending on our choice. For example, if the original dimension is [1,2,3], we can change it to [3,2,1]. This function takes as parameters the desired order of dimensions.
# Example 1 - working
a=torch.tensor([[[1,2,3],[4,5,6]]])
a.size()
>>torch.Size([1, 2, 3])
a.permute(2,1,0).size()
>>torch.Size([3, 2, 1])
a.permute(2,1,0)
>>tensor([[[1],
[4]],
[[2],
[5]],
[[3],
[6]]])
In this example, the dimensions of the original tensor are [1, 2, 3]. With permuting, I set the order to (2,1,0), which means the new dimension should be [3,2,1]. As shown, the new view of the tensor rearranges the numbers so that the dimensions of the tensor are [3, 2, 1].
This function can be used when we want to reorder tensors of different dimensions or perform matrix multiplication with matrices of different orders.
3. tolist()
This function returns tensors as Python numbers, lists, or nested lists. After that, we can perform any Python logic and operations on it.
# Example 1 - working
a=torch.tensor([[1,2,3],[4,5,6]])
a.tolist()
>> [[1, 2, 3], [4, 5, 6]]
In this example, tensors are returned as nested lists.
4. narrow()
This function returns a new tensor that is a smaller version of the original tensor. The parameters of this function are the input tensor, the dimension to be reduced, the starting index, and the length of the new tensor along that dimension. It returns elements from index start to index (start+length-1).
# Example 1 - working
a=torch.tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12],[14,15,16,17]])
torch.narrow(a,1,2,2)
>> tensor([[ 3, 4],
[ 7, 8],
[11, 12],
[16, 17]])
In this example, the tensor is reduced along the second, innermost dimension. It accepts elements from the list, starting at index 2 and ending at index 3(=2+2 − 1, i.e. start+length − 1).
Narrow() works similar to advanced indexing. For example, in a 2D tensor, use [:,0:5] to select all rows in columns 0 through 5. Similarly, torch.narrow(1,0,5) can be used. However, in high-dimensional tensors, it is cumbersome to use the range operator for every dimension. This can be done faster and more easily with narrow().
5. where()
This function returns a new tensor whose value changes at each index according to a given condition. The parameters of this function are: condition, first tensor and second tensor. Check the condition (used in the condition) on the value of each tensor and replace it with the value at the same position in the first tensor if true, and replace it with the value at the same position in the second tensor if false.
# Example 1 - working
a=torch.tensor([[[1,2,3],[4,5,6]]]).to(torch.float32)
b=torch.zeros(1,2,3)
torch.where(a%2==0,b,a)
>>tensor([[[1., 0., 3.],
[0., 5., 0.]]])
Here, it checks whether the value of tensor a is even. If so, replace it with the value in tensor b, where all values in b are 0, otherwise it's the same as before.
This function can be used to set thresholds. If the values in the tensors are greater or less than a certain number, they can easily be replaced.
About Pytorch 5 very useful tensor operations are what the answer to the question is shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.