第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何選擇 numpy 張量軸

如何選擇 numpy 張量軸

慕運(yùn)維8079593 2023-10-26 10:21:14
我有兩個(gè) numpy 的 shape 數(shù)組(436, 1024, 2)。最后一個(gè)維度 ( 2) 表示 2D 向量。我想按元素比較兩個(gè) numpy 數(shù)組的二維向量,以便找到平均角度誤差。為此,我想使用點(diǎn)積,它在循環(huán)數(shù)組的前兩個(gè)維度時(shí)工作得很好(forPython 中的循環(huán)可能很慢)。因此我想使用 numpy 函數(shù)。我發(fā)現(xiàn)這np.tensordot允許按元素執(zhí)行點(diǎn)積。但是,我沒(méi)有成功地使用它的axes論點(diǎn):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)我有以下錯(cuò)誤:ValueError: shape-mismatch for sum下面是我的函數(shù),它正確計(jì)算平均角度誤差: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貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

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


手動(dòng)進(jìn)行求和縮減(使用+和*):


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都要求第一個(gè)數(shù)組的最后一個(gè)維度與第二個(gè)數(shù)組的倒數(shù)第二個(gè)維度匹配,就像普通矩陣乘法一樣。None是 的別名np.newaxis,它在您選擇的位置引入一個(gè)大小為 1 的新軸。在本例中,我制作了第一個(gè)數(shù)組(w, h, 1, 2)和第二個(gè)數(shù)組(w, h, 2, 1)。這確保最后兩個(gè)維度在每個(gè)相應(yīng)元素處作為轉(zhuǎn)置向量和正則向量相乘。


用途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 。并保留兩個(gè)數(shù)組的未更改維度,如前所述。將它們一起廣播,這就是您想要的。np.tensordotdottensordotmatmul


查看完整回答
反對(duì) 回復(fù) 2023-10-26
  • 1 回答
  • 0 關(guān)注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)