PyTorch-スクイーズを解除してスクイーズします



Pytorch Unsqueeze



PyTorch-スクイーズを解除してスクイーズします

フライフィッシング



import torch import torch.nn as nn import torch.nn.functional as F #a=torch.randn(3,2) a=torch.randn(3,1) b0=a.unsqueeze(-2) b1=a.unsqueeze(-1) b2=a.unsqueeze(0) b3=a.unsqueeze(1) b4=a.unsqueeze(2) e0=a.squeeze(-2) e1=a.squeeze(-1) e2=a.squeeze(0) e3=a.squeeze(1) print(a.shape) print(b0.shape) print(b1.shape) print(b2.shape) print(b3.shape) print(e0.shape) print(e1.shape) print(e2.shape) print(e3.shape) #function description #torch.unsqueeze(input, dim, out=None) Dimension expansion #return a new tensor, insert dimension 1 for the specified position of the input 1 #If dim is negative, it will be converted to dim+input.dim()+1 #torch.squeeze(input, dim=None, out=None) Dimensional compression # Remove 1 from the input tensor shape and return. If the input is in the form (A × 1 × B × 1 × C × 1 × D) #, then the output shape is: (A × B × C × D) # When given dim, then the squeeze operation is only on the given dimension. For example, the input shape is: (A × 1 × B) #, squeeze(input, 0) will keep the tensor unchanged. Only with squeeze(input, 1), the shape will become (A×B). #Note: The return tensor shares the memory with the input tensor, so changing one of the contents will change the other. # result a=torch.randn(3,1) # torch.Size([3, 1]) # # torch.Size([3, 1, 1]) # torch.Size([3, 1, 1]) # torch.Size([1, 3, 1]) # torch.Size([3, 1, 1]) # torch.Size([3, 1, 1]) # # torch.Size([3, 1]) # torch.Size([3]) # torch.Size([3, 1]) # torch.Size([3]) # result a=torch.randn(3,2) # torch.Size([3, 2]) # torch.Size([3, 1, 2]) # torch.Size([3, 2, 1]) # torch.Size([1, 3, 2]) # torch.Size([3, 1, 2]) # torch.Size([3, 2, 1]) # torch.Size([3, 2]) # torch.Size([3, 2]) # torch.Size([3, 2]) # torch.Size([3, 2]) # =============================================================================