Understand the basic 6 functions in PyTorch
When I started to learn PyTorch, I found that there are various functions which seem vague to understand for me. Today I would like to summarize them with examples, which I think helpful greatly.
The functions are :
- unsqueeze() and a[None],
You can also check the explanations from the official website one by one, but summarizing them together helps me.
squeeze(i): it is kind of dimension reduction: if the original dimension is 1, then it can be reduced. Let’s check an example:
In the example, the original tensor shape is [2,1,4]. Because the dimension of index 0 is 2, it can’t be reduced and the size keeps the same, but the dimension of index 1 is 1, it can be reduced and the tensor from 3 dimensions into 2 dimension matrix.
2. unsqueeze() and tesor[None]
unsqueeze(i): it is used to increase dimension according to the index of i. Let’s check example:
i is the dimension index that will be extended.
There is another way to increase dimension which is a[None]. Put None to the index position i to extend the dimension, which has the same effect as unsqueeze(i).
From the official website, this function returns the maximum value of all elements in the
Let’s take examples from different dimensions:
For 1 dimension tensor:
c = torch.randn(3)
If the dimension is higher than 1 dimension:
c = torch.randn(2, 3)
- max(c): get the max value from one dimension tensor
- max(c,0): if the tensor is more than 1 dimension, it gets the max values in columns and indices
- max(c,1): if the tensor is more than 1 dimension, it gets the max values in rows and indices
It returns the indices of the maximum value of all elements in the
input tensor, and it is the second value returned by
If tensor is 1 dimension:
d = torch.randn(3)
If tensor is higher than 1dimension:
d = torch.randn(2,3)
PyTorch allows a tensor to be a
View of an existing tensor. But how? Let’s take examples:
The above three views doesn’t change the original view of the tensor. Now let’s change the view as below:
Conclusion: view(i,j) will transfer the origin tensor to the form of i rows and j columns, -1 means no limit.
Hope it helps you a bit. Thanks for your reading.