减少行动

译者:ApacheCN

  1. torch.argmax(input, dim=None, keepdim=False)

返回维度上张量的最大值的索引。

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

参数:

  • 输入 (Tensor) - 输入张量
  • dim (int) - 降低的维数。如果None,则返回展平输入的argmax。
  • keepdim (bool) - 输出张量是否保留dim。如果dim=None,则忽略。

例:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[ 1.3398, 0.2663, -0.2686, 0.2450],
  4. [-0.7401, -0.8805, -0.3402, -1.1936],
  5. [ 0.4907, -1.3948, -1.0691, -0.3132],
  6. [-1.6092, 0.5419, -0.2993, 0.3195]])
  7. >>> torch.argmax(a, dim=1)
  8. tensor([ 0, 2, 0, 1])
  1. torch.argmin(input, dim=None, keepdim=False)

返回维度上张量的最小值的索引。

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

Parameters:

  • 输入 (Tensor) - 输入张量
  • dim (int) - 降低的维数。如果None,则返回展平输入的argmin。
  • keepdim (bool) - 输出张量是否保留dim。如果dim=None,则忽略。

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[ 0.1139, 0.2254, -0.1381, 0.3687],
  4. [ 1.0100, -1.1975, -0.0102, -0.4732],
  5. [-0.9240, 0.1207, -0.7506, -1.0213],
  6. [ 1.7809, -1.2960, 0.9384, 0.1438]])
  7. >>> torch.argmin(a, dim=1)
  8. tensor([ 2, 1, 3, 1])
  1. torch.cumprod(input, dim, dtype=None) Tensor

返回维度diminput元素的累积乘积。

例如,如果input是大小为N的向量,则结果也将是具有元素的大小为N的向量。

减少行动 - 图1

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 执行操作的维度
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:无。

Example:

  1. >>> a = torch.randn(10)
  2. >>> a
  3. tensor([ 0.6001, 0.2069, -0.1919, 0.9792, 0.6727, 1.0062, 0.4126,
  4. -0.2129, -0.4206, 0.1968])
  5. >>> torch.cumprod(a, dim=0)
  6. tensor([ 0.6001, 0.1241, -0.0238, -0.0233, -0.0157, -0.0158, -0.0065,
  7. 0.0014, -0.0006, -0.0001])
  8. >>> a[5] = 0.0
  9. >>> torch.cumprod(a, dim=0)
  10. tensor([ 0.6001, 0.1241, -0.0238, -0.0233, -0.0157, -0.0000, -0.0000,
  11. 0.0000, -0.0000, -0.0000])
  1. torch.cumsum(input, dim, out=None, dtype=None) Tensor

返回维度diminput的元素的累积和。

For example, if input is a vector of size N, the result will also be a vector of size N, with elements.

减少行动 - 图2

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 执行操作的维度
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:无。

Example:

  1. >>> a = torch.randn(10)
  2. >>> a
  3. tensor([-0.8286, -0.4890, 0.5155, 0.8443, 0.1865, -0.1752, -2.0595,
  4. 0.1850, -1.1571, -0.4243])
  5. >>> torch.cumsum(a, dim=0)
  6. tensor([-0.8286, -1.3175, -0.8020, 0.0423, 0.2289, 0.0537, -2.0058,
  7. -1.8209, -2.9780, -3.4022])
  1. torch.dist(input, other, p=2) Tensor

返回(input - other)的p范数

inputother的形状必须是可播放的

Parameters:

  • 输入 (Tensor) - 输入张量
  • 其他 (Tensor) - 右侧输入张量
  • p (漂浮 任选) - 要计算的范数

Example:

  1. >>> x = torch.randn(4)
  2. >>> x
  3. tensor([-1.5393, -0.8675, 0.5916, 1.6321])
  4. >>> y = torch.randn(4)
  5. >>> y
  6. tensor([ 0.0967, -1.0511, 0.6295, 0.8360])
  7. >>> torch.dist(x, y, 3.5)
  8. tensor(1.6727)
  9. >>> torch.dist(x, y, 3)
  10. tensor(1.6973)
  11. >>> torch.dist(x, y, 0)
  12. tensor(inf)
  13. >>> torch.dist(x, y, 1)
  14. tensor(2.6537)
  1. torch.logsumexp(input, dim, keepdim=False, out=None)

返回给定维diminput张量的每一行的求和指数的对数。计算在数值上是稳定的。

对于由dim和其他指数 减少行动 - 图3 给出的总和指数 减少行动 - 图4 ,结果是

减少行动 - 图5

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

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int 元组python:整数) - 要减少的维度或维度
  • keepdim (bool) - 输出张量是否保留dim
  • out (Tensor 任选) - 输出张量
  1. Example::
  1. >>> a = torch.randn(3, 3)
  2. >>> torch.logsumexp(a, 1)
  3. tensor([ 0.8442, 1.4322, 0.8711])
  1. torch.mean()
  1. torch.mean(input) Tensor

返回input张量中所有元素的平均值。

参数: 输入 (Tensor) - 输入张量

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[ 0.2294, -0.5481, 1.3288]])
  4. >>> torch.mean(a)
  5. tensor(0.3367)
  1. torch.mean(input, dim, keepdim=False, out=None) Tensor

返回给定维diminput张量的每一行的平均值。如果dim是维度列表,请减少所有维度。

如果keepdimTrue,则输出张量与input的大小相同,但尺寸为1的尺寸dim除外。dim被挤压(见torch.squeeze()),导致输出张量具有1(或len(dim))更少的维度。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool 可选) - 输出张量是否保留dim
  • out (Tensor) - 输出张量

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[-0.3841, 0.6320, 0.4254, -0.7384],
  4. [-0.9644, 1.0131, -0.6549, -1.4279],
  5. [-0.2951, -1.3350, -0.7694, 0.5600],
  6. [ 1.0842, -0.9580, 0.3623, 0.2343]])
  7. >>> torch.mean(a, 1)
  8. tensor([-0.0163, -0.5085, -0.4599, 0.1807])
  9. >>> torch.mean(a, 1, True)
  10. tensor([[-0.0163],
  11. [-0.5085],
  12. [-0.4599],
  13. [ 0.1807]])
  1. torch.median()
  1. torch.median(input) Tensor

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

Parameters: input (Tensor) – the input tensor

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[ 1.5219, -1.5212, 0.2202]])
  4. >>> torch.median(a)
  5. tensor(0.2202)
  1. torch.median(input, dim=-1, keepdim=False, values=None, indices=None) -> (Tensor, LongTensor)

返回给定维diminput张量的每一行的中值。还将中值的索引位置返回为LongTensor

默认情况下,diminput张量的最后一个维度。

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

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • (tensor 可选) - 输出张量
  • 指数 (tensor 任选) - 输出指数张量

Example:

  1. >>> a = torch.randn(4, 5)
  2. >>> a
  3. tensor([[ 0.2505, -0.3982, -0.9948, 0.3518, -1.3131],
  4. [ 0.3180, -0.6993, 1.0436, 0.0438, 0.2270],
  5. [-0.2751, 0.7303, 0.2192, 0.3321, 0.2488],
  6. [ 1.0778, -1.9510, 0.7048, 0.4742, -0.7125]])
  7. >>> torch.median(a, 1)
  8. (tensor([-0.3982, 0.2270, 0.2488, 0.4742]), tensor([ 1, 4, 4, 3]))
  1. torch.mode(input, dim=-1, keepdim=False, values=None, indices=None) -> (Tensor, LongTensor)

返回给定维diminput张量的每一行的模式值。还将模式值的索引位置作为LongTensor返回。

By default, dim is the last dimension of the input tensor.

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

注意

尚未为torch.cuda.Tensor定义此功能。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • (tensor 可选) - 输出张量
  • 指数 (tensor 任选) - 输出指数张量

Example:

  1. >>> a = torch.randn(4, 5)
  2. >>> a
  3. tensor([[-1.2808, -1.0966, -1.5946, -0.1148, 0.3631],
  4. [ 1.1395, 1.1452, -0.6383, 0.3667, 0.4545],
  5. [-0.4061, -0.3074, 0.4579, -1.3514, 1.2729],
  6. [-1.0130, 0.3546, -1.4689, -0.1254, 0.0473]])
  7. >>> torch.mode(a, 1)
  8. (tensor([-1.5946, -0.6383, -1.3514, -1.4689]), tensor([ 2, 2, 3, 2]))
  1. torch.norm(input, p='fro', dim=None, keepdim=False, out=None)

返回给定张量的矩阵范数或向量范数。

Parameters:

  • 输入 (Tensor) - 输入张量

  • p (int 漂浮 ] inf -inf ‘来’__, ‘nuc’__, 任选) -

    规范的顺序。默认值:'fro'可以计算以下规范:

    | ord |矩阵规范|矢量规范| | —- | —- | —- | |没有| Frobenius规范| 2范数| | ‘来’| Frobenius规范| - | | ‘nuc’|核规范| - | |其他|当昏暗是无|时,作为vec规范sum(abs(x) ord)(1./ord)|

  • 昏暗 (int 2元组python:ints 2-list of python:ints 可选) - 如果是int,将计算向量范数,如果是2元组的int,将计算矩阵范数。如果值为None,则当输入张量仅具有两个维度时将计算矩阵范数,当输入张量仅具有一个维度时将计算向量范数。如果输入张量具有两个以上的维度,则向量范数将应用于最后一个维度。

  • keepdim (bool 任选) - 输出张量是否保留dim。如果dim = Noneout = None,则忽略。默认值:False

  • out (Tensor 可选) - 输出张量。如果dim = Noneout = None,则忽略。

Example:

  1. >>> import torch
  2. >>> a = torch.arange(9, dtype= torch.float) - 4
  3. >>> b = a.reshape((3, 3))
  4. >>> torch.norm(a)
  5. tensor(7.7460)
  6. >>> torch.norm(b)
  7. tensor(7.7460)
  8. >>> torch.norm(a, float('inf'))
  9. tensor(4.)
  10. >>> torch.norm(b, float('inf'))
  11. tensor([4., 3., 4.])
  12. >>> c = torch.tensor([[ 1, 2, 3],[-1, 1, 4]] , dtype= torch.float)
  13. >>> torch.norm(c, dim=0)
  14. tensor([1.4142, 2.2361, 5.0000])
  15. >>> torch.norm(c, dim=1)
  16. tensor([3.7417, 4.2426])
  17. >>> torch.norm(c, p=1, dim=1)
  18. tensor([6., 6.])
  19. >>> d = torch.arange(8, dtype= torch.float).reshape(2,2,2)
  20. >>> torch.norm(d, dim=(1,2))
  21. tensor([ 3.7417, 11.2250])
  22. >>> torch.norm(d[0, :, :]), torch.norm(d[1, :, :])
  23. (tensor(3.7417), tensor(11.2250))
  1. torch.prod()
  1. torch.prod(input, dtype=None) Tensor

返回input张量中所有元素的乘积。

Parameters:

  • 输入 (Tensor) - 输入张量
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:无。

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[-0.8020, 0.5428, -1.5854]])
  4. >>> torch.prod(a)
  5. tensor(0.6902)
  1. torch.prod(input, dim, keepdim=False, dtype=None) Tensor

返回给定维diminput张量的每一行的乘积。

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

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:无。

Example:

  1. >>> a = torch.randn(4, 2)
  2. >>> a
  3. tensor([[ 0.5261, -0.3837],
  4. [ 1.1857, -0.2498],
  5. [-1.1646, 0.0705],
  6. [ 1.1131, -1.0629]])
  7. >>> torch.prod(a, 1)
  8. tensor([-0.2018, -0.2962, -0.0821, -1.1831])
  1. torch.std()
  1. torch.std(input, unbiased=True) Tensor

返回input张量中所有元素的标准偏差。

如果unbiasedFalse,则将通过偏差估算器计算标准偏差。否则,将使用贝塞尔的修正。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 无偏 (bool) - 是否使用无偏估计

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[-0.8166, -1.3802, -0.3560]])
  4. >>> torch.std(a)
  5. tensor(0.5130)
  1. torch.std(input, dim, keepdim=False, unbiased=True, out=None) Tensor

返回给定维diminput张量的每一行的标准偏差。

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

If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • 无偏 (bool) - 是否使用无偏估计
  • out (Tensor 任选) - 输出张量

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[ 0.2035, 1.2959, 1.8101, -0.4644],
  4. [ 1.5027, -0.3270, 0.5905, 0.6538],
  5. [-1.5745, 1.3330, -0.5596, -0.6548],
  6. [ 0.1264, -0.5080, 1.6420, 0.1992]])
  7. >>> torch.std(a, dim=1)
  8. tensor([ 1.0311, 0.7477, 1.2204, 0.9087])
  1. torch.sum()
  1. torch.sum(input, dtype=None) Tensor

返回input张量中所有元素的总和。

Parameters:

  • 输入 (Tensor) - 输入张量
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:无。

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[ 0.1133, -0.9567, 0.2958]])
  4. >>> torch.sum(a)
  5. tensor(-0.5475)
  1. torch.sum(input, dim, keepdim=False, dtype=None) Tensor

返回给定维diminput张量的每一行的总和。如果dim是维度列表,请减少所有维度。

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int 元组python:整数) - 要减少的维度或维度
  • keepdim (bool) - 输出张量是否保留dim
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:无。

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[ 0.0569, -0.2475, 0.0737, -0.3429],
  4. [-0.2993, 0.9138, 0.9337, -1.6864],
  5. [ 0.1132, 0.7892, -0.1003, 0.5688],
  6. [ 0.3637, -0.9906, -0.4752, -1.5197]])
  7. >>> torch.sum(a, 1)
  8. tensor([-0.4598, -0.1381, 1.3708, -2.6217])
  9. >>> b = torch.arange(4 * 5 * 6).view(4, 5, 6)
  10. >>> torch.sum(b, (2, 1))
  11. tensor([ 435., 1335., 2235., 3135.])
  1. torch.unique(input, sorted=False, return_inverse=False, dim=None)

返回输入张量的唯一标量元素作为1-D张量。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 排序 (bool) - 是否在返回作为输出之前按升序对唯一元素进行排序。
  • return_inverse (bool) - 是否还返回原始输入中元素在返回的唯一列表中结束的索引。
  • dim (int) - 应用唯一的维度。如果是None,则返回展平输入的唯一值。默认值:None

|返回:|包含张量的张量或元组

> 输出 (Tensor ):唯一标量元素的输出列表。 > inverse_indices (Tensor ):(可选)如果return_inverse为True,将会有第二个返回的张量(与输入相同的形状),表示原始元素的索引输入映射到输出中;否则,此函数只返回单个张量。

返回类型: (TensorTensor (可选))

Example:

  1. >>> output = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long))
  2. >>> output
  3. tensor([ 2, 3, 1])
  4. >>> output, inverse_indices = torch.unique(
  5. torch.tensor([1, 3, 2, 3], dtype=torch.long), sorted=True, return_inverse=True)
  6. >>> output
  7. tensor([ 1, 2, 3])
  8. >>> inverse_indices
  9. tensor([ 0, 2, 1, 2])
  10. >>> output, inverse_indices = torch.unique(
  11. torch.tensor([[1, 3], [2, 3]], dtype=torch.long), sorted=True, return_inverse=True)
  12. >>> output
  13. tensor([ 1, 2, 3])
  14. >>> inverse_indices
  15. tensor([[ 0, 2],
  16. [ 1, 2]])
  1. torch.var()
  1. torch.var(input, unbiased=True) Tensor

返回input张量中所有元素的方差。

如果unbiasedFalse,则通过偏差估计器计算方差。否则,将使用贝塞尔的修正。

Parameters:

  • 输入 (Tensor) - 输入张量
  • 无偏 (bool) - 是否使用无偏估计

Example:

  1. >>> a = torch.randn(1, 3)
  2. >>> a
  3. tensor([[-0.3425, -1.2636, -0.4864]])
  4. >>> torch.var(a)
  5. tensor(0.2455)
  1. torch.var(input, dim, keepdim=False, unbiased=True, out=None) Tensor

返回给定维diminput张量的每一行的方差。

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 outputs tensor having 1 fewer dimension than input.

If unbiased is False, then the variance will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Parameters:

  • 输入 (Tensor) - 输入张量
  • 昏暗 (int) - 减少的维度
  • keepdim (bool) - 输出张量是否保留dim
  • 无偏 (bool) - 是否使用无偏估计
  • out (Tensor 任选) - 输出张量

Example:

  1. >>> a = torch.randn(4, 4)
  2. >>> a
  3. tensor([[-0.3567, 1.7385, -1.3042, 0.7423],
  4. [ 1.3436, -0.1015, -0.9834, -0.8438],
  5. [ 0.6056, 0.1089, -0.3112, -1.4085],
  6. [-0.7700, 0.6074, -0.1469, 0.7777]])
  7. >>> torch.var(a, 1)
  8. tensor([ 1.7444, 1.1363, 0.7356, 0.5112])