快捷方式

torch.max

torch.max(input) Tensor

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

引數

input (Tensor) – 輸入張量。

示例

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

返回一個命名元組 (values, indices),其中 valuesinput 張量在給定維度 dim 上每行的最大值。indices 是找到的每個最大值的索引位置 (argmax)。

如果 keepdimTrue,則輸出張量的大小與 input 相同,除了維度 dim 的大小為 1。否則,維度 dim 會被壓縮(參見 torch.squeeze()),導致輸出張量的維度比 input 少 1。

注意

如果在縮減後的行中存在多個最大值,則返回第一個最大值的索引。

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

  • dim (intinttuple可選) – 要縮減的維度。如果為 None,則所有維度都將縮減。

  • keepdim (bool可選) – 輸出張量是否保留 dim 維度。預設值: False

關鍵字引數

out (tuple可選) – 包含兩個輸出張量 (max, max_indices) 的結果元組

示例

>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222,  0.8475],
        [ 1.1949, -1.1127, -2.2379, -0.6702],
        [ 1.5717, -0.9207,  0.1297, -1.8768],
        [-0.6172,  1.0036, -0.6060, -0.2432]])
>>> torch.max(a, 1)
torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))
>>> a = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> a.max(dim=1, keepdim=True)
torch.return_types.max(
values=tensor([[2.], [4.]]),
indices=tensor([[1], [1]]))
>>> a.max(dim=1, keepdim=False)
torch.return_types.max(
values=tensor([2., 4.]),
indices=tensor([1, 1]))
torch.max(input, other, *, out=None) Tensor

參見 torch.maximum()

文件

查閱 PyTorch 的完整開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源