捷徑

torch.max

torch.max(input) Tensor

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

警告

此函式會產生確定性的(子)梯度,不像 max(dim=0)

參數

input (Tensor) – 輸入張量 (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 會被擠壓(squeeze)(請參閱 torch.squeeze()),導致輸出張量的維度比 input 少 1。

注意

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

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

  • dim (int) – 要縮減的維度。

  • keepdim (bool) – 指示是否保留輸出張量的 dim。預設值:False

關鍵字引數

out (tuple, optional) – 兩個輸出張量 (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]))
torch.max(input, other, *, out=None) Tensor

請參閱 torch.maximum()

文件

存取 PyTorch 的全面開發人員文件

檢視文件

教學

取得適用於初學者和高級開發人員的深入教學課程

檢視教學

資源

尋找開發資源並獲得您的問題解答

檢視資源