torch.triu(input, diagonal=0, out=None)

torch.triu(input, diagonal=0, out=None) → Tensor
返回矩阵上三角部分,其余部分定义为0。
Parameters:

  • input (Tensor) – the input tensor
  • diagonal (int, optional) – the diagonal to consider
  • out (Tensor, optional) – the output tensor
    如果diagonal为空,输入矩阵保留主对角线与主对角线以上的元素;
    如果diagonal为正数n,输入矩阵保留主对角线与主对角线以上除去n行的元素;
    如果diagonal为负数-n,输入矩阵保留主对角线与主对角线以上与主对角线下方h行对角线的元素;
>>> a = torch.randn(3, 3)
>>> a
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.2072, -1.0680,  0.6602],
        [ 0.3480, -0.5211, -0.4573]])
>>> torch.triu(a)
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.0000, -1.0680,  0.6602],
        [ 0.0000,  0.0000, -0.4573]])
>>> torch.triu(a, diagonal=1)
tensor([[ 0.0000,  0.5207,  2.0049],
        [ 0.0000,  0.0000,  0.6602],
        [ 0.0000,  0.0000,  0.0000]])
>>> torch.triu(a, diagonal=-1)
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.2072, -1.0680,  0.6602],
        [ 0.0000, -0.5211, -0.4573]])

你可能感兴趣的:(Python,PyTorch)