计算非负的int数组中每个值的频率。

    除非为空,否则箱数(大小为1)比input中的最大值大1,在这种情况下,结果是大小为0.如果指定minlength,则箱数为至少minlength并且如果input为空,则结果是填充零的大小minlength的张量。如果n是位置i的值,out[n] += weights[i]如果指定了weights,则out[n] += 1

    注意

    使用CUDA后端时,此操作可能会导致不容易关闭的不确定行为。有关背景,请参阅的注释。

    参数:

    • 输入 (Tensor) - 1-d int张量
    • 权重 () - 可选,输入张量中每个值的权重。应与输入张量大小相同。
    • minlength (int) - 可选的最小二进制数。应该是非负面的。

    例:

    1. >>> input = torch.randint(0, 8, (5,), dtype=torch.int64)
    2. >>> weights = torch.linspace(0, 1, steps=5)
    3. >>> input, weights
    4. (tensor([4, 3, 6, 3, 4]),
    5. tensor([ 0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
    6. >>> torch.bincount(input)
    7. tensor([0, 0, 0, 2, 2, 0, 1])
    8. >>> input.bincount(weights)
    9. tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])
    1. torch.broadcast_tensors(*tensors) List of Tensors

    根据_broadcasting-semantics广播给定的张量。

    Example:

    1. >>> x = torch.arange(3).view(1, 3)
    2. >>> y = torch.arange(2).view(2, 1)
    3. >>> a, b = torch.broadcast_tensors(x, y)
    4. >>> a.size()
    5. torch.Size([2, 3])
    6. >>> a
    7. tensor([[0, 1, 2],
    8. [0, 1, 2]])
    1. torch.cross(input, other, dim=-1, out=None) Tensor

    返回inputother的维度dim中矢量的叉积。

    inputother必须具有相同的尺寸,并且dim尺寸的大小应为3。

    如果未给出dim,则默认为找到大小为3的第一个维度。

    Parameters:

    • 输入 () - 输入张量
    • 其他 (Tensor) - 第二个输入张量
    • dim ( 任选) - 采取交叉积的维度。
    • out (Tensor 任选) - 输出张量

    Example:

    1. >>> a = torch.randn(4, 3)
    2. >>> a
    3. tensor([[-0.3956, 1.1455, 1.6895],
    4. [-0.5849, 1.3672, 0.3599],
    5. [-1.1626, 0.7180, -0.0521],
    6. [-0.1339, 0.9902, -2.0225]])
    7. >>> b = torch.randn(4, 3)
    8. >>> b
    9. tensor([[-0.0257, -1.4725, -1.2251],
    10. [-1.1479, -0.7005, -1.9757],
    11. [-1.3904, 0.3726, -1.1836],
    12. [-0.9688, -0.7153, 0.2159]])
    13. >>> torch.cross(a, b, dim=1)
    14. tensor([[ 1.0844, -0.5281, 0.6120],
    15. [-2.4490, -1.5687, 1.9792],
    16. [-0.8304, -1.3037, 0.5650],
    17. [-1.2329, 1.9883, 1.0551]])
    18. >>> torch.cross(a, b)
    19. tensor([[ 1.0844, -0.5281, 0.6120],
    20. [-2.4490, -1.5687, 1.9792],
    21. [-0.8304, -1.3037, 0.5650],
    22. [-1.2329, 1.9883, 1.0551]])
    1. torch.diag(input, diagonal=0, out=None) Tensor
    • 如果input是矢量(1-D张量),则返回2-D平方张量,其中input的元素作为对角线。
    • 如果input是矩阵(2-D张量),则返回具有input的对角元素的1-D张量。

    参数 控制要考虑的对角线:

    • 如果 diagonal = 0,则它是主对角线。
    • 如果 > 0,它在主对角线上方。
    • 如果 diagonal < 0,它在主对角线下面。

    Parameters:

    • 输入 () - 输入张量
    • 对角线 (int 可选) - 要考虑的对角线
    • out ( 任选) - 输出张量

    也可以看看

    torch.diagonal() 始终返回其输入的对角线。

    总是构造一个由输入指定的对角元素的张量。

    例子:

    获取输入向量为对角线的方阵:

    1. >>> a = torch.randn(3)
    2. >>> a
    3. tensor([ 0.5950,-0.0872, 2.3298])
    4. >>> torch.diag(a)
    5. tensor([[ 0.5950, 0.0000, 0.0000],
    6. [ 0.0000,-0.0872, 0.0000],
    7. [ 0.0000, 0.0000, 2.3298]])
    8. >>> torch.diag(a, 1)
    9. tensor([[ 0.0000, 0.5950, 0.0000, 0.0000],
    10. [ 0.0000, 0.0000,-0.0872, 0.0000],
    11. [ 0.0000, 0.0000, 0.0000, 2.3298],
    12. [ 0.0000, 0.0000, 0.0000, 0.0000]])

    获取给定矩阵的第k个对角线:

    1. >>> a = torch.randn(3, 3)
    2. >>> a
    3. tensor([[-0.4264, 0.0255,-0.1064],
    4. [ 0.8795,-0.2429, 0.1374],
    5. [ 0.1029,-0.6482,-1.6300]])
    6. >>> torch.diag(a, 0)
    7. tensor([-0.4264,-0.2429,-1.6300])
    8. >>> torch.diag(a, 1)
    9. tensor([ 0.0255, 0.1374])
    1. torch.diag_embed(input, offset=0, dim1=-2, dim2=-1) Tensor

    创建一个张量,其某些2D平面的对角线(由dim1dim2指定)由input填充。为了便于创建批量对角矩阵,默认选择由返回张量的最后两个维度形成的2D平面。

    参数offset控制要考虑的对角线:

    • 如果offset = 0,则它是主对角线。
    • 如果offset> 0,它在主对角线上方。
    • 如果offset< 0,它在主对角线下面。

    将计算新矩阵的大小以使得指定的对角线具有最后输入维度的大小。注意,对于 以外的offsetdim1dim2的顺序很重要。交换它们相当于改变offset的符号。

    将 应用于具有相同参数的此函数的输出,将产生与输入相同的矩阵。但是, torch.diagonal() 具有不同的默认尺寸,因此需要明确指定。

    • 输入 () - 输入张量。必须至少是一维的。
    • 偏移 (int 任选) - 对角线考虑。默认值:0(主对角线)。
    • dim1 ( 任选) - 相对于其采取对角线的第一维度。默认值:-2。
    • dim2 (int 任选) - 相对于其采取对角线的第二维度。默认值:-1。

    Example:

    1. >>> a = torch.randn(2, 3)
    2. >>> torch.diag_embed(a)
    3. tensor([[[ 1.5410, 0.0000, 0.0000],
    4. [ 0.0000, -0.2934, 0.0000],
    5. [ 0.0000, 0.0000, -2.1788]],
    6. [ 0.0000, -1.0845, 0.0000],
    7. [ 0.0000, 0.0000, -1.3986]]])
    8. >>> torch.diag_embed(a, offset=1, dim1=0, dim2=2)
    9. tensor([[[ 0.0000, 1.5410, 0.0000, 0.0000],
    10. [ 0.0000, 0.5684, 0.0000, 0.0000]],
    11. [[ 0.0000, 0.0000, -0.2934, 0.0000],
    12. [ 0.0000, 0.0000, -1.0845, 0.0000]],
    13. [[ 0.0000, 0.0000, 0.0000, -2.1788],
    14. [[ 0.0000, 0.0000, 0.0000, 0.0000],
    15. [ 0.0000, 0.0000, 0.0000, 0.0000]]])
    1. torch.diagflat(input, diagonal=0) Tensor
    • 如果input是矢量(1-D张量),则返回2-D平方张量,其中input的元素作为对角线。
    • 如果input是一个具有多个维度的张量,则返回一个二维张量,其对角线元素等于一个展平的input

    The argument offset controls which diagonal to consider:

    • 如果offset = 0,则它是主对角线。
    • 如果offset> 0,它在主对角线上方。
    • 如果offset< 0,它在主对角线下面。

    Parameters:

    • 输入 () - 输入张量
    • 偏移 (int 任选) - 对角线考虑。默认值:0(主对角线)。

    Examples:

    1. torch.diagonal(input, offset=0, dim1=0, dim2=1) Tensor

    返回input的局部视图,其对角线元素相对于dim1dim2作为形状末尾的尺寸附加。

    The argument offset controls which diagonal to consider:

    • 如果offset = 0,则它是主对角线。
    • 如果offset> 0,它在主对角线上方。
    • 如果offset< 0,它在主对角线下面。

    将 应用于具有相同参数的此函数的输出,将生成带有输入对角线条目的对角矩阵。但是, torch.diag_embed() 具有不同的默认尺寸,因此需要明确指定。

    Parameters:

    • 输入 () - 输入张量。必须至少是二维的。
    • 偏移 (int 任选) - 对角线考虑。默认值:0(主对角线)。
    • dim1 ( 任选) - 相对于其采取对角线的第一维度。默认值:0。
    • dim2 (int 任选) - 相对于其采取对角线的第二维度。默认值:1。

    Note

    要采用批对角线,传入dim1 = -2,dim2 = -1。

    Examples:

    1. >>> a = torch.randn(3, 3)
    2. >>> a
    3. tensor([[-1.0854, 1.1431, -0.1752],
    4. [ 0.8536, -0.0905, 0.0360],
    5. [ 0.6927, -0.3735, -0.4945]])
    6. >>> torch.diagonal(a, 0)
    7. tensor([-1.0854, -0.0905, -0.4945])
    8. >>> torch.diagonal(a, 1)
    9. tensor([ 1.1431, 0.0360])
    10. >>> x = torch.randn(2, 5, 4, 2)
    11. >>> torch.diagonal(x, offset=-1, dim1=1, dim2=2)
    12. tensor([[[-1.2631, 0.3755, -1.5977, -1.8172],
    13. [-1.1065, 1.0401, -0.2235, -0.7938]],
    14. [[-1.7325, -0.3081, 0.6166, 0.2335],
    15. [ 1.0500, 0.7336, -0.3836, -1.1015]]])
    1. torch.einsum(equation, *operands) Tensor

    该函数提供了一种使用爱因斯坦求和约定来计算多线性表达式(即乘积和)的方法。

    Parameters:

    • 等式 (string ) - 该等式根据与操作数和结果的每个维度相关联的小写字母(索引)给出。左侧列出了操作数尺寸,以逗号分隔。每个张量维度应该有一个索引字母。右侧跟在->之后,并给出输出的索引。如果省略->和右侧,则它隐式地定义为在左侧恰好出现一次的所有索引的按字母顺序排序的列表。在操作数输入之后,将输出中未显示的索引求和。如果索引对同一操作数多次出现,则采用对角线。省略号表示固定数量的维度。如果推断出右侧,则省略号维度位于输出的开头。
    • 操作数(张量列表) - 计算爱因斯坦和的操作数。请注意,操作数作为列表传递,而不是作为单个参数传递。

    Examples:

    1. >>> x = torch.randn(5)
    2. >>> y = torch.randn(4)
    3. >>> torch.einsum('i,j->ij', x, y) # outer product
    4. tensor([[-0.0570, -0.0286, -0.0231, 0.0197],
    5. [ 1.2616, 0.6335, 0.5113, -0.4351],
    6. [ 1.4452, 0.7257, 0.5857, -0.4984],
    7. [-0.4647, -0.2333, -0.1883, 0.1603],
    8. [-1.1130, -0.5588, -0.4510, 0.3838]])
    9. >>> A = torch.randn(3,5,4)
    10. >>> l = torch.randn(2,5)
    11. >>> r = torch.randn(2,4)
    12. >>> torch.einsum('bn,anm,bm->ba', l, A, r) # compare torch.nn.functional.bilinear
    13. tensor([[-0.3430, -5.2405, 0.4494],
    14. [ 0.3311, 5.5201, -3.0356]])
    15. >>> As = torch.randn(3,2,5)
    16. >>> Bs = torch.randn(3,5,4)
    17. >>> torch.einsum('bij,bjk->bik', As, Bs) # batch matrix multiplication
    18. tensor([[[-1.0564, -1.5904, 3.2023, 3.1271],
    19. [-1.6706, -0.8097, -0.8025, -2.1183]],
    20. [[ 4.2239, 0.3107, -0.5756, -0.2354],
    21. [-1.4558, -0.3460, 1.5087, -0.8530]],
    22. [[ 2.8153, 1.8787, -4.3839, -1.2112],
    23. [ 0.3728, -2.1131, 0.0921, 0.8305]]])
    24. >>> A = torch.randn(3, 3)
    25. >>> torch.einsum('ii->i', A) # diagonal
    26. tensor([-0.7825, 0.8291, -0.1936])
    27. >>> A = torch.randn(4, 3, 3)
    28. >>> torch.einsum('...ii->...i', A) # batch diagonal
    29. tensor([[-1.0864, 0.7292, 0.0569],
    30. [-0.9725, -1.0270, 0.6493],
    31. [ 0.5832, -1.1716, -1.5084],
    32. [ 0.4041, -1.1690, 0.8570]])
    33. >>> A = torch.randn(2, 3, 4, 5)
    34. >>> torch.einsum('...ij->...ji', A).shape # batch permute
    35. torch.Size([2, 3, 5, 4])
    1. torch.flatten(input, start_dim=0, end_dim=-1) Tensor

    在张量中展平连续的一系列变暗。

    Parameters:

    • 输入 () - 输入张量
    • start_dim (int) - 第一个暗淡变平
    • end_dim () - 最后的暗淡变平

    Example:

    1. >>> t = torch.tensor([[[1, 2],
    2. [3, 4]],
    3. [[5, 6],
    4. [7, 8]]])
    5. >>> torch.flatten(t)
    6. tensor([1, 2, 3, 4, 5, 6, 7, 8])
    7. >>> torch.flatten(t, start_dim=1)
    8. tensor([[1, 2, 3, 4],
    9. [5, 6, 7, 8]])
    1. torch.flip(input, dims) Tensor

    在dims中沿给定轴反转n-D张量的顺序。

    Parameters:

    • 输入 (Tensor) - 输入张量
    • 暗淡(一个列表 - 轴要翻转

    Example:

    1. >>> x = torch.arange(8).view(2, 2, 2)
    2. >>> x
    3. tensor([[[ 0, 1],
    4. [ 2, 3]],
    5. [ 6, 7]]])
    6. tensor([[[ 6, 7],
    7. [ 4, 5]],
    8. [[ 2, 3],
    9. [ 0, 1]]])
    1. torch.histc(input, bins=100, min=0, max=0, out=None) Tensor

    计算张量的直方图。

    元素在 min 和 之间分成相等的宽度区间。如果 min 和 都为零,则使用数据的最小值和最大值。

    Parameters:

    • 输入 (Tensor) - 输入张量
    • () - 直方图箱数
    • min (int) - 范围的下限(含)
    • max () - 范围的上限(含)
    • out (Tensor 任选) - 输出张量

    Example:

    1. >>> torch.histc(torch.tensor([1., 2, 1]), bins=4, min=0, max=3)
    2. tensor([ 0., 2., 1., 0.])
    1. torch.meshgrid(*tensors, **kwargs)

    取 张量,每个张量可以是标量或1维向量,并创建 N维网格,其中:math:i通过扩展:math:[](#id4)i` th输入定义由其他输入定义的维度来定义网格。

    1. Returns:

    seq(张量序列):如果输入的 Other Operations - 图5 张量大小为 ,那么输出也会有 Other Operations - 图7 张量,其中所有张量均为 。

    Example:

    1. >>> x = torch.tensor([1, 2, 3])
    2. >>> y = torch.tensor([4, 5, 6])
    3. >>> grid_x, grid_y = torch.meshgrid(x, y)
    4. >>> grid_x
    5. tensor([[1, 1, 1],
    6. [2, 2, 2],
    7. [3, 3, 3]])
    8. >>> grid_y
    9. tensor([[4, 5, 6],
    10. [4, 5, 6],
    11. [4, 5, 6]])
    1. torch.renorm(input, p, dim, maxnorm, out=None) Tensor

    返回张量,其中沿着维度diminput的每个子张量被归一化,使得子张量的p - 范数低于值maxnorm

    Note

    如果行的范数低于maxnorm,则该行不变

    Parameters:

    • 输入 (Tensor) - 输入张量
    • p () - 规范计算的动力
    • dim (int) - 切片以获得子张量的维数
    • maxnorm () - 保持每个子张量的最大范数
    • out (Tensor 任选) - 输出张量

    Example:

    1. >>> x = torch.ones(3, 3)
    2. >>> x[1].fill_(2)
    3. tensor([ 2., 2., 2.])
    4. >>> x[2].fill_(3)
    5. tensor([ 3., 3., 3.])
    6. >>> x
    7. tensor([[ 1., 1., 1.],
    8. [ 2., 2., 2.],
    9. [ 3., 3., 3.]])
    10. >>> torch.renorm(x, 1, 0, 5)
    11. tensor([[ 1.0000, 1.0000, 1.0000],
    12. [ 1.6667, 1.6667, 1.6667],
    13. [ 1.6667, 1.6667, 1.6667]])
    1. torch.tensordot(a, b, dims=2)

    返回多维度上a和b的收缩。

    实现了矩阵乘积的推广。

    Parameters:

    • a (tensor) - 左张量收缩
    • b () - 右张量收缩
    • 暗淡 (int 元组的两个python列表:整数) - 要收缩的维数或ab的明确维度列表

    当用整数参数dims = 调用时,ab的维数是 和 ,它分别计算

    当使用列表形式的dims调用时,将收缩给定的维度来代替a的最后 和的第一个 ] Other Operations - 图15 。这些尺寸的尺寸必须匹配,但 tensordot 将处理广播尺寸。

    Examples:

    1. >>> a = torch.arange(60.).reshape(3, 4, 5)
    2. >>> b = torch.arange(24.).reshape(4, 3, 2)
    3. >>> torch.tensordot(a, b, dims=([1, 0], [0, 1]))
    4. tensor([[4400., 4730.],
    5. [4532., 4874.],
    6. [4664., 5018.],
    7. [4796., 5162.],
    8. [4928., 5306.]])
    9. >>> a = torch.randn(3, 4, 5, device='cuda')
    10. >>> b = torch.randn(4, 5, 6, device='cuda')
    11. >>> c = torch.tensordot(a, b, dims=2).cpu()
    12. tensor([[ 8.3504, -2.5436, 6.2922, 2.7556, -1.0732, 3.2741],
    13. [ 3.3161, 0.0704, 5.0187, -0.4079, -4.3126, 4.8744],
    14. [ 0.8223, 3.9445, 3.2168, -0.2400, 3.4117, 1.7780]])
    1. torch.trace(input) Tensor

    返回输入2-D矩阵的对角线元素的总和。

    Example:

    1. >>> x = torch.arange(1., 10.).view(3, 3)
    2. >>> x
    3. tensor([[ 1., 2., 3.],
    4. [ 4., 5., 6.],
    5. [ 7., 8., 9.]])
    6. >>> torch.trace(x)
    7. tensor(15.)
    1. torch.tril(input, diagonal=0, out=None) Tensor

    返回矩阵的下三角部分(2-D张量)input,结果张量out的其他元素设置为0。

    矩阵的下三角形部分被定义为对角线上和下方的元素。

    参数 控制要考虑的对角线。如果 diagonal = 0,则保留主对角线上和下方的所有元素。正值包括主对角线上方的对角线数量,同样负值也不包括主对角线下方的对角线数量。主对角线是 的指数 Other Operations - 图17 的集合,其中 是基质的维度。

    Parameters:

    • 输入 (Tensor) - 输入张量
    • 对角线 ( 可选) - 要考虑的对角线
    • out (Tensor 任选) - 输出张量

    Example:

    1. >>> a = torch.randn(3, 3)
    2. >>> a
    3. tensor([[-1.0813, -0.8619, 0.7105],
    4. [ 0.0935, 0.1380, 2.2112],
    5. [-0.3409, -0.9828, 0.0289]])
    6. >>> torch.tril(a)
    7. tensor([[-1.0813, 0.0000, 0.0000],
    8. [ 0.0935, 0.1380, 0.0000],
    9. [-0.3409, -0.9828, 0.0289]])
    10. >>> b = torch.randn(4, 6)
    11. >>> b
    12. tensor([[ 1.2219, 0.5653, -0.2521, -0.2345, 1.2544, 0.3461],
    13. [ 0.4785, -0.4477, 0.6049, 0.6368, 0.8775, 0.7145],
    14. [ 1.1502, 3.2716, -1.1243, -0.5413, 0.3615, 0.6864],
    15. [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024, 0.0978]])
    16. >>> torch.tril(b, diagonal=1)
    17. tensor([[ 1.2219, 0.5653, 0.0000, 0.0000, 0.0000, 0.0000],
    18. [ 0.4785, -0.4477, 0.6049, 0.0000, 0.0000, 0.0000],
    19. [ 1.1502, 3.2716, -1.1243, -0.5413, 0.0000, 0.0000],
    20. [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024, 0.0000]])
    21. >>> torch.tril(b, diagonal=-1)
    22. tensor([[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
    23. [ 0.4785, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
    24. [ 1.1502, 3.2716, 0.0000, 0.0000, 0.0000, 0.0000],
    25. [-0.0614, -0.7344, -1.3164, 0.0000, 0.0000, 0.0000]])
    1. torch.triu(input, diagonal=0, out=None) Tensor

    返回矩阵的上三角部分(2-D张量)input,结果张量out的其他元素设置为0。

    矩阵的上三角形部分被定义为对角线上方和上方的元素。

    参数 控制要考虑的对角线。如果 = 0,则保留主对角线上和下方的所有元素。正值排除了主对角线上方的对角线数量,同样负值也包括主对角线下方的对角线数量。主对角线是 的指数 的集合,其中 是基质的维度。

    Parameters:

    • 输入 (Tensor) - 输入张量
    • 对角线 ( 可选) - 要考虑的对角线
    • out (Tensor 任选) - 输出张量