比较行动

译者:ApacheCN

  1. torch.allclose(self, other, rtol=1e-05, atol=1e-08, equal_nan=False) bool

此函数检查所有selfother是否满足条件:

比较行动 - 图1

元素,对于selfother的所有元素。此函数的行为类似于 numpy.allclose

参数:

  • (tensor) - 首先进行张量比较
  • 其他 (Tensor) - 第二张量来比较
  • atol (漂浮 任选) - 绝对耐受。默认值:1e-08
  • rtol (漂浮 任选) - 相对耐受。默认值:1e-05
  • equal_nan (漂浮 任选) - 如果True,那么两个NaN s将是比较平等。默认值:False

例:

  1. >>> torch.allclose(torch.tensor([10000., 1e-07]), torch.tensor([10000.1, 1e-08]))
  2. False
  3. >>> torch.allclose(torch.tensor([10000., 1e-08]), torch.tensor([10000.1, 1e-09]))
  4. True
  5. >>> torch.allclose(torch.tensor([1.0, float('nan')]), torch.tensor([1.0, float('nan')]))
  6. False
  7. >>> torch.allclose(torch.tensor([1.0, float('nan')]), torch.tensor([1.0, float('nan')]), equal_nan=True)
  8. True
  1. torch.argsort(input, dim=None, descending=False)

返回按值按升序对给定维度的张量进行排序的索引。

这是 torch.sort() 返回的第二个值。有关此方法的确切语义,请参阅其文档。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int 可选) - 排序的维度
  • 降序 (bool 任选) - 控制排序顺序(升序或降序)

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[ 0.0785, 1.5267, -0.8521, 0.4065],
  4. [ 0.1598, 0.0788, -0.0745, -1.2700],
  5. [ 1.2208, 1.0722, -0.7064, 1.2564],
  6. [ 0.0669, -0.2318, -0.8229, -0.9280]])
  7. >>> torch.argsort(a, dim=1)
  8. tensor([[2, 0, 3, 1],
  9. [3, 2, 1, 0],
  10. [2, 1, 0, 3],
  11. [3, 2, 1, 0]])
  1. torch.eq(input, other, out=None) Tensor

计算元素明确的平等

第二个参数可以是数字或张量,其形状为可广播的第一个参数。

Parameters:

  • 输入 (Tensor) - 要比较的张量
  • 其他 (tensor 漂浮) - 张量或值比较
  • out (Tensor 可选) - 输出张量。必须是ByteTensor
返回: 在比较为真的每个位置包含1的torch.ByteTensor
返回类型: Tensor

Example:

  1. >>> torch.eq(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  2. tensor([[ 1, 0],
  3. [ 0, 1]], dtype=torch.uint8)
  1. torch.equal(tensor1, tensor2) bool

True如果两个张量具有相同的尺寸和元素,则False

Example:

  1. >>> torch.equal(torch.tensor([1, 2]), torch.tensor([1, 2]))
  2. True
  1. torch.ge(input, other, out=None) Tensor

按元素计算 比较行动 - 图2

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:

  • 输入 (Tensor) - 要比较的张量
  • 其他 (tensor 漂浮) - 张量或值比较
  • out (Tensor 任选) - 输出张量必须是ByteTensor
Returns: A torch.ByteTensor containing a 1 at each location where comparison is true
Return type: Tensor

Example:

  1. >>> torch.ge(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  2. tensor([[ 1, 1],
  3. [ 0, 1]], dtype=torch.uint8)
  1. torch.gt(input, other, out=None) Tensor

按元素计算 比较行动 - 图3

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:

  • 输入 (Tensor) - 要比较的张量
  • 其他 (tensor 漂浮) - 张量或值比较
  • out (Tensor 任选) - 输出张量必须是ByteTensor
Returns: A torch.ByteTensor containing a 1 at each location where comparison is true
Return type: Tensor

Example:

  1. >>> torch.gt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  2. tensor([[ 0, 1],
  3. [ 0, 0]], dtype=torch.uint8)
  1. torch.isfinite(tensor)

返回一个新的张量,其布尔元素表示每个元素是否为Finite

参数: 张量 (tensor) - 张量来检查
返回: torch.ByteTensor在有限元的每个位置包含1,否则为0
Return type: Tensor

Example:

  1. >>> torch.isfinite(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
  2. tensor([ 1, 0, 1, 0, 0], dtype=torch.uint8)
  1. torch.isinf(tensor)

返回一个新的张量,其布尔元素表示每个元素是否为+/-INF

Parameters: tensor (Tensor) – A tensor to check
Returns: torch.ByteTensor+/-INF元素的每个位置包含1,否则为0
Return type: Tensor

Example:

  1. >>> torch.isinf(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
  2. tensor([ 0, 1, 0, 1, 0], dtype=torch.uint8)
  1. torch.isnan(tensor)

返回一个新的张量,其布尔元素表示每个元素是否为NaN

Parameters: tensor (Tensor) – A tensor to check
Returns: torch.ByteTensorNaN元素的每个位置包含1。
Return type: Tensor

Example:

  1. >>> torch.isnan(torch.tensor([1, float('nan'), 2]))
  2. tensor([ 0, 1, 0], dtype=torch.uint8)
  1. torch.kthvalue(input, k, dim=None, keepdim=False, out=None) -> (Tensor, LongTensor)

返回给定维度上给定input张量的k个最小元素。

如果未给出dim,则选择input的最后一个尺寸。

返回(values, indices)的元组,其中indices是维度dim中原始input张量中第k个最小元素的索引。

如果keepdimTruevaluesindices张量都与input的尺寸相同,但尺寸为dim的尺寸除外。否则,dim被挤压(见 torch.squeeze()),导致valuesindices张量的尺寸比input张量小1。

Parameters:

  • 输入 (Tensor) - 输入张量
  • k (int) - k为第k个最小元素
  • 昏暗 (int 可选) - 找到kth值的维度
  • keepdim (bool) - 输出张量是否保留dim
  • out (元组 任选) - (Tensor,LongTensor)的输出元组可以任意给出用作输出缓冲区

Example:

  1. >>> x = torch.arange(1., 6.)
  2. >>> x
  3. tensor([ 1., 2., 3., 4., 5.])
  4. >>> torch.kthvalue(x, 4)
  5. (tensor(4.), tensor(3))
  6. >>> x=torch.arange(1.,7.).resize_(2,3)
  7. >>> x
  8. tensor([[ 1., 2., 3.],
  9. [ 4., 5., 6.]])
  10. >>> torch.kthvalue(x,2,0,True)
  11. (tensor([[ 4., 5., 6.]]), tensor([[ 1, 1, 1]]))
  1. torch.le(input, other, out=None) Tensor

按元素计算 比较行动 - 图4

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:

  • 输入 (Tensor) - 要比较的张量
  • 其他 (tensor 漂浮) - 张量或值比较
  • out (Tensor 任选) - 输出张量必须是ByteTensor
Returns: A torch.ByteTensor containing a 1 at each location where comparison is true
Return type: Tensor

Example:

  1. >>> torch.le(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  2. tensor([[ 1, 0],
  3. [ 1, 1]], dtype=torch.uint8)
  1. torch.lt(input, other, out=None) Tensor

按元素计算 比较行动 - 图5

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:

  • 输入 (Tensor) - 要比较的张量
  • 其他 (tensor 漂浮) - 张量或值比较
  • out (Tensor 任选) - 输出张量必须是ByteTensor
Returns: A torch.ByteTensor containing a 1 at each location where comparison is true
Return type: Tensor

Example:

  1. >>> torch.lt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  2. tensor([[ 0, 0],
  3. [ 1, 0]], dtype=torch.uint8)
  1. torch.max()
  1. torch.max(input) Tensor

返回input张量中所有元素的最大值。

Parameters: 输入 (Tensor) - 输入张量

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[ 0.6763, 0.7445, -2.2369]])
  4. >>> torch.max(a)
  5. tensor(0.7445)
  1. torch.max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)

返回给定维diminput张量的每一行的最大值。第二个返回值是找到的每个最大值的索引位置(argmax)。

如果keepdimTrue,则输出张量与input的尺寸相同,但尺寸为dim的尺寸为1.否则,dim被挤压(参见 torch.squeeze()),导致输出张量的尺寸比input少1。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • out (元组 可选) - 两个输出张量的结果元组(max,max_indices)

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[-1.2360, -0.2942, -0.1222, 0.8475],
  4. [ 1.1949, -1.1127, -2.2379, -0.6702],
  5. [ 1.5717, -0.9207, 0.1297, -1.8768],
  6. [-0.6172, 1.0036, -0.6060, -0.2432]])
  7. >>> torch.max(a, 1)
  8. (tensor([ 0.8475, 1.1949, 1.5717, 1.0036]), tensor([ 3, 0, 0, 1]))
  1. torch.max(input, other, out=None) Tensor

张量input的每个元素与张量other的对应元素进行比较,并采用逐元素最大值。

inputother的形状不需要匹配,但它们必须是可广播的

比较行动 - 图6

注意

当形状不匹配时,返回的输出张量的形状遵循广播规则

Parameters:

  • 输入 (Tensor) - 输入张量
  • 其他 (Tensor) - 第二个输入张量
  • out (Tensor 任选) - 输出张量

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.2942, -0.7416, 0.2653, -0.1584])
  4. >>> b = torch.randn(4)
  5. >>> b
  6. tensor([ 0.8722, -1.7421, -0.4141, -0.5055])
  7. >>> torch.max(a, b)
  8. tensor([ 0.8722, -0.7416, 0.2653, -0.1584])
  1. torch.min()
  1. torch.min(input) Tensor

返回input张量中所有元素的最小值。

Parameters: input (Tensor) – the input tensor

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[ 0.6750, 1.0857, 1.7197]])
  4. >>> torch.min(a)
  5. tensor(0.6750)
  1. torch.min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)

返回给定维diminput张量的每一行的最小值。第二个返回值是找到的每个最小值的索引位置(argmin)。

If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensors having 1 fewer dimension than input.

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • out (元组 任选) - 两个输出张量的元组(min,min_indices)

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[-0.6248, 1.1334, -1.1899, -0.2803],
  4. [-1.4644, -0.2635, -0.3651, 0.6134],
  5. [ 0.2457, 0.0384, 1.0128, 0.7015],
  6. [-0.1153, 2.9849, 2.1458, 0.5788]])
  7. >>> torch.min(a, 1)
  8. (tensor([-1.1899, -1.4644, 0.0384, -0.1153]), tensor([ 2, 0, 1, 0]))
  1. torch.min(input, other, out=None) Tensor

将张量input的每个元素与张量other的对应元素进行比较,并采用逐元素最小值。返回结果张量。

The shapes of input and other don’t need to match, but they must be broadcastable.

比较行动 - 图7

Note

When the shapes do not match, the shape of the returned output tensor follows the broadcasting rules.

Parameters:

  • 输入 (Tensor) - 输入张量
  • 其他 (Tensor) - 第二个输入张量
  • out (Tensor 任选) - 输出张量

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.8137, -1.1740, -0.6460, 0.6308])
  4. >>> b = torch.randn(4)
  5. >>> b
  6. tensor([-0.1369, 0.1555, 0.4019, -0.1929])
  7. >>> torch.min(a, b)
  8. tensor([-0.1369, -1.1740, -0.6460, -0.1929])
  1. torch.ne(input, other, out=None) Tensor

按元素计算 比较行动 - 图8

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:

  • 输入 (Tensor) - 要比较的张量
  • 其他 (tensor 漂浮) - 张量或值比较
  • out (Tensor 任选) - 输出张量必须是ByteTensor
Returns: 在比较为真的每个位置包含1的torch.ByteTensor
Return type: Tensor

Example:

  1. >>> torch.ne(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  2. tensor([[ 0, 1],
  3. [ 1, 0]], dtype=torch.uint8)
  1. torch.sort(input, dim=None, descending=False, out=None) -> (Tensor, LongTensor)

按值按升序对给定维度的input张量元素进行排序。

If dim is not given, the last dimension of the input is chosen.

如果descendingTrue,则元素按值按降序排序。

返回元组(sorted_tensor,sorted_indices),其中sorted_indices是原始input张量中元素的索引。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int 可选) - 排序的维度
  • 降序 (bool 任选) - 控制排序顺序(升序或降序)
  • out (元组 任选) - (TensorLongTensor)的输出元组可以选择将其用作输出缓冲区

Example:

  1. >>> x = torch.randn(3, 4)
  2. >>> sorted, indices = torch.sort(x)
  3. >>> sorted
  4. tensor([[-0.2162, 0.0608, 0.6719, 2.3332],
  5. [-0.5793, 0.0061, 0.6058, 0.9497],
  6. [-0.5071, 0.3343, 0.9553, 1.0960]])
  7. >>> indices
  8. tensor([[ 1, 0, 2, 3],
  9. [ 3, 1, 0, 2],
  10. [ 0, 3, 1, 2]])
  11. >>> sorted, indices = torch.sort(x, 0)
  12. >>> sorted
  13. tensor([[-0.5071, -0.2162, 0.6719, -0.5793],
  14. [ 0.0608, 0.0061, 0.9497, 0.3343],
  15. [ 0.6058, 0.9553, 1.0960, 2.3332]])
  16. >>> indices
  17. tensor([[ 2, 0, 0, 1],
  18. [ 0, 1, 1, 2],
  19. [ 1, 2, 2, 0]])
  1. torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)

返回给定维度上给定input张量的k最大元素。

If dim is not given, the last dimension of the input is chosen.

如果largestFalse,则返回k最小元素。

返回(values, indices)元组,其中indices是原始input张量中元素的索引。

布尔选项sorted如果True,将确保返回的k元素本身已排序

Parameters:

  • 输入 (Tensor) - 输入张量
  • k (int) - “top-k”中的k
  • 昏暗 (int 可选) - 排序的维度
  • 最大 (bool 可选) - 控制是否返回最大或最小元素
  • 排序 (bool 可选) - 控制是否按排序顺序返回元素
  • out (元组 任选) - (Tensor,LongTensor)的输出元组,可以选择性给予用作输出缓冲区

Example:

  1. >>> x = torch.arange(1., 6.)
  2. >>> x
  3. tensor([ 1., 2., 3., 4., 5.])
  4. >>> torch.topk(x, 3)
  5. (tensor([ 5., 4., 3.]), tensor([ 4, 3, 2]))