快捷方式

torch.median

torch.median(input) 張量

返回 input 中的值的中位數。

注意

對於元素數量為偶數的 input 張量,中位數不是唯一的。在這種情況下,將返回兩個中位數中較低的一個。要計算兩個中位數的平均值,請改用帶有 q=0.5torch.quantile()

警告

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

引數

input (張量) – 輸入張量。

示例

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 1.5219, -1.5212,  0.2202]])
>>> torch.median(a)
tensor(0.2202)
torch.median(input, dim=-1, keepdim=False, *, out=None)

返回一個命名元組 (values, indices),其中 values 包含 input 在維度 dim 中每行的中位數,而 indices 包含在維度 dim 中找到的中位數值的索引。

預設情況下,diminput 張量的最後一個維度。

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

注意

對於在維度 dim 中元素數量為偶數的 input 張量,中位數不是唯一的。在這種情況下,將返回兩個中位數中較低的一個。要計算 input 中兩個中位數的平均值,請改用帶有 q=0.5torch.quantile()

警告

indices 不一定包含找到的每個中位數值的第一次出現,除非該值是唯一的。具體的實現細節取決於裝置。通常不要期望在 CPU 和 GPU 上執行時得到相同的結果。出於同樣的原因,不要期望梯度是確定性的。

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

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

  • keepdim (bool) – 輸出張量是否保留維度 dim

關鍵字引數

out ((張量, 張量), 可選) – 第一個張量將填充中位數值,第二個張量(必須是 long 型別)將填充它們在 input 的維度 dim 中的索引。

示例

>>> a = torch.randn(4, 5)
>>> a
tensor([[ 0.2505, -0.3982, -0.9948,  0.3518, -1.3131],
        [ 0.3180, -0.6993,  1.0436,  0.0438,  0.2270],
        [-0.2751,  0.7303,  0.2192,  0.3321,  0.2488],
        [ 1.0778, -1.9510,  0.7048,  0.4742, -0.7125]])
>>> torch.median(a, 1)
torch.return_types.median(values=tensor([-0.3982,  0.2270,  0.2488,  0.4742]), indices=tensor([1, 4, 4, 3]))

文件

查閱 PyTorch 全面的開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源