目录
函数说明:Pytorch官网
持续更新…
torch.squeeze
torch.squeeze(input, dim=None) → Tensor
Returns a tensor with all specified dimensions of input of size 1 removed.
例如,如果input的形状为: (A×1×B×C×1×D),那么 input.squeeze() 将具有以下形状: (A×B×C×D) 。
当给出 dim 时,仅在给定的维度上进行压缩操作。如果输入的形状为: (A×1×B) ,squeeze(input, 0) 保持张量不变,但squeeze(input, 1) 会将张量压缩到形状 (A×B) 。
注意:返回的张量与输入张量共享存储,因此更改一个张量的内容将更改另一个张量的内容。
torch.unsqueeze
torch.unsqueeze(input, dim) → Tensor
Returns a new tensor with a dimension of size one inserted at the specified position.
返回的张量与该张量共享相同的基础数据。
可以使用 [-input.dim() - 1, input.dim() + 1) 范围内的dim值。负dim等效于在dim = dim + input.dim() + 1 处应用unsqueeze()。
torch.max
torch.max(input) → Tensor
Returns the maximum value of all elements in the input tensor.
>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.6763, 0.7445, -2.2369]])
>>> torch.max(a)
tensor(0.7445)
torch.max(input, dim, keepdim=False, *, out=None)
Parameters:
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce.
keepdim (bool) – whether the output tensor has dim retained or not. Default: False.
Keyword Arguments:
out (tuple, optional) – the result tuple of two output tensors (max, max_indices)
返回一个命名元组(values,indices),其中values是给定维度dim中输入张量每行的最大值。 indexs 是找到的每个最大值(argmax)的索引位置。
如果 keepdim 为 True,则输出张量与输入的大小相同,但维度 dim 的大小为 1。否则,dim 会被压缩,导致输出张量的维度比输入减1。
>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222, 0.8475],