亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何選擇 numpy 張量軸

如何選擇 numpy 張量軸

慕運維8079593 2023-10-26 10:21:14
我有兩個 numpy 的 shape 數組(436, 1024, 2)。最后一個維度 ( 2) 表示 2D 向量。我想按元素比較兩個 numpy 數組的二維向量,以便找到平均角度誤差。為此,我想使用點積,它在循環數組的前兩個維度時工作得很好(forPython 中的循環可能很慢)。因此我想使用 numpy 函數。我發現這np.tensordot允許按元素執行點積。但是,我沒有成功地使用它的axes論點:import numpy as npdef average_angular_error_vec(estimated_oc : np.array, target_oc : np.array):    estimated_oc = np.float64(estimated_oc)    target_oc = np.float64(target_oc)    norm1 = np.linalg.norm(estimated_oc, axis=2)    norm2 = np.linalg.norm(target_oc, axis=2)    norm1 = norm1[..., np.newaxis]    norm2 = norm2[..., np.newaxis]    unit_vector1 = np.divide(estimated_oc, norm1)    unit_vector2 = np.divide(target_oc, norm2)    dot_product = np.tensordot(unit_vector1, unit_vector2, axes=2)    angle = np.arccos(dot_product)    return np.mean(angle)我有以下錯誤:ValueError: shape-mismatch for sum下面是我的函數,它正確計算平均角度誤差:def average_angular_error(estimated_oc : np.array, target_oc : np.array):    h, w, c = target_oc.shape    r = np.zeros((h, w), dtype="float64")    estimated_oc = np.float64(estimated_oc)    target_oc = np.float64(target_oc)    for i in range(h):        for j in range(w):            unit_vector_1 = estimated_oc[i][j] / np.linalg.norm(estimated_oc[i][j])            unit_vector_2 = target_oc[i][j] / np.linalg.norm(target_oc[i][j])            dot_product = np.dot(unit_vector_1, unit_vector_2)            angle = np.arccos(dot_product)            r[i][j] = angle           return np.mean(r)
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

這個問題可能比你提出的問題簡單得多。如果您沿最后np.tensordot一個軸應用一對 shape 數組(w, h, 2),您將得到 shape 的結果(w, h, w, h)。這不是你想要的。這里有三種簡單的方法。除了顯示選項之外,我還展示了一些提示和技巧,可以在不更改任何基本功能的情況下使代碼更簡單:


手動進行求和縮減(使用+和*):


def average_angular_error(estimated_oc : np.ndarray, target_oc : np.ndarray):

    # If you want to do in-place normalization, do x /= ... instead of x = x / ...

    estimated_oc = estimated_oc / np.linalg.norm(estimated_oc, axis=-1, keepdims=True)

    target_oc = target_oc / np.linalg.norm(target_oc, axis=-1, keepdims=True)

    # Use plain element-wise multiplication

    dots = np.sum(estimated_oc * target_oc, axis=-1)

    return np.arccos(dots).mean()

使用np.matmul(aka @) 和正確廣播的維度:


def average_angular_error(estimated_oc : np.ndarray, target_oc : np.ndarray):

    estimated_oc = estimated_oc / np.linalg.norm(estimated_oc, axis=-1, keepdims=True)

    target_oc = target_oc / np.linalg.norm(target_oc, axis=-1, keepdims=True)

    # Matrix multiplication needs two dimensions to operate on

    dots = estimated_oc[..., None, :] @ target_oc[..., :, None]

    return np.arccos(dots).mean()

np.matmul兩者np.dot都要求第一個數組的最后一個維度與第二個數組的倒數第二個維度匹配,就像普通矩陣乘法一樣。None是 的別名np.newaxis,它在您選擇的位置引入一個大小為 1 的新軸。在本例中,我制作了第一個數組(w, h, 1, 2)和第二個數組(w, h, 2, 1)。這確保最后兩個維度在每個相應元素處作為轉置向量和正則向量相乘。


用途np.einsum:


def average_angular_error(estimated_oc : np.ndarray, target_oc : np.ndarray):

    estimated_oc = estimated_oc / np.linalg.norm(estimated_oc, axis=-1, keepdims=True)

    target_oc = target_oc / np.linalg.norm(target_oc, axis=-1, keepdims=True)

    # Matrix multiplication needs two dimensions to operate on

    dots = np.einsum('ijk,ijk->ik', estimated_oc, target_oc)

    return np.arccos(dots).mean()

您不能為此使用np.dotor 。并保留兩個數組的未更改維度,如前所述。將它們一起廣播,這就是您想要的。np.tensordotdottensordotmatmul


查看完整回答
反對 回復 2023-10-26
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號