Analyze view and reshape in pytorch

In the code written by pytorch, the view method and the reshape method are very commonly used. But after my own experience of writing code, I found that things are not that simple, let's take a look at why I said that?

1. Basic cognition

Before that, I hope we have a common understanding of the following points:

  1. (B, C, H, W) This form of tensor is usually used as the input of the network, and each dimension has its meaning. For example, the first dimension B is a different picture (for the time being considered to be the input picture) , Which is the batch size in a batch, C is the channel of the tensor, H and W are the spatial size of the tensor, namely the height and width. For example, the following picture can be regarded as ( 2,3,2,2 )
    Example
  2. One more thing to recognize is that (2×2×3, 2) is different from (3×2×2, 2). Some people will say that they are all (12, 2). Those who say that need to go back to the first point and think again. They can also use the following picture to understand [so when looking at some tensor shapes, try to disassemble them as much as possible. Note: If
    The difference between (2×2×3, 2) and (3×2×2, 2)
    you don't understand it here, you can also see the following example.

Two, official documents

Now you can take a look at how these two usages are explained in the official documentation:

2.1 view()

view(*shape) → Tensor
pytorch official documentation for view()

2.2 reshape()

torch.reshape(input, shape) → Tensor
Official pytorch documentation of reshape()

3. Conclusion

  • The basic functions of the view function and the reshape function in our use are similar, and reshape can not report an error if it is not continuous
  • For -1the usage, both are: if the input (B, C, H, W) goes through view (B, -1) or reshape (B, -1), it will become (B, W×H×C), and It is not (B, C×H×W), which requires special attention. [That is the reverse]

Guess you like

Origin blog.csdn.net/laizi_laizi/article/details/108682858