tensor function
torch.permute()
1 | A = torch.randn(2,3,5,7) |
torch.max() &torch.tensor.max()
if x is a tensor, there are 2 ways: torch.max(x,dim=n,keepdim=True) or x.max(dim=n, keepdim=True
for instance, if x is a tensor of dimension (3,4,5), dim =1 means that finding the max value of x[0][i][0](i=0,1,2,3).
if we set keepdim=False(which is default), then we can get a tensor of dimension (3,5), while if we set keepdim=True, then we can get a tensor of dimension (3,1,5).
the .max() returns a namedtuple (value, indices) where value is the maximum value of each row of the input tensor in the given dimension dim, and indices is the index location of each maximum value found.
torch.tensor.expand()
tensor.expand(sizes) returns tensor
returns a new view of the self tensor with singleton dimensions expanded to a larger size.
passing -1 as the size for a dimension means not changing the size of that dimension.
1 | x = torch.tensor([[1],[2],[3]]) |
torch.cat()
torch.cat(tensors,dim=0,*,out=None) returns Tensor
tensors: sequence of tensors (any python sequence of tensors of the same type.) Non-empty tensors provided must have the same shape except in the cat dimension.
dim: int, optional. the dimension over which the tensor are concatenated.
out: Tensor, optional. the output tensor.
1 | x=torch.tensor([[1,2,3],[4,5,6]]) |
torch.select or tensor[:,:,…,index]
select is equal to slide. for instance, tensor.select(0,index) is equivalent to tensor[index], and tensor.select(2,index) is equivalent to tensor[:,:,index]
torch.detach()
returns a new tensor detached from the current graph. the result will never require gradient.