評價此頁

torch.renorm#

torch.renorm(input, p, dim, maxnorm, *, out=None) Tensor#

返回一個張量,其中沿維度 diminput 的每個子張量都被歸一化,使得子張量的 p-範數小於 maxnorm 值。

注意

如果行的範數低於 maxnorm,則該行保持不變。

引數
  • input (Tensor) – 輸入張量。

  • p (float) – 用於範數計算的冪。

  • dim (int) – 用於切分以獲取子張量的維度。

  • maxnorm (float) – 使每個子張量保持在此值之下的最大範數。

關鍵字引數

out (Tensor, optional) – 輸出張量。

示例

>>> x = torch.ones(3, 3)
>>> x[1].fill_(2)
tensor([ 2.,  2.,  2.])
>>> x[2].fill_(3)
tensor([ 3.,  3.,  3.])
>>> x
tensor([[ 1.,  1.,  1.],
        [ 2.,  2.,  2.],
        [ 3.,  3.,  3.]])
>>> torch.renorm(x, 1, 0, 5)
tensor([[ 1.0000,  1.0000,  1.0000],
        [ 1.6667,  1.6667,  1.6667],
        [ 1.6667,  1.6667,  1.6667]])