我試圖完成以下功能,但我一直在遇到索引問題,導致「ValueError:操作數無法與形狀(0,9)一起廣播( 5)」。Numpy數組,索引和卷積混淆
我想我的錯誤可能來自於我如何從ssd_difference []調用值,但我不完全確定。
另外,我將如何使用基於下面給出的提示的convolve2d?我知道numpy有它的功能,但我不知道我需要投入什麼才能使它工作。
附加信息:binomialFilter5()返回表示二項式濾波器的dtype float的5x1 numpy數組。我還假設「權重[]」是ssd_difference []值。
def transitionDifference(ssd_difference):
「」」計算幀之間的過渡成本,同時考慮動力學到 帳戶
說明:通過行和SSD差的列 1.迭代,忽略 前兩個值和最後兩個值
1a。對於i,j中的每個值,乘以長度爲 五(稍後在代碼中實施)的二項式濾波器,乘以開始兩幀之前的兩幀之前的兩個幀,並取其中的那些 產品。
i.e. Your weights for frame i are:
[weight[i - 2, j - 2],
weight[i - 1, j - 1],
weight[i, j],
weight[i + 1, j + 1],
weight[i + 2, j + 2]]
乘上在每個我的二項式濾波器的權重,j來取得 你的輸出。
可能需要了解一點點明白爲什麼我們 計算這一點,簡單的解釋是,從 框架4更改爲5,讓調用這個CH(4,5),和我們做這個重量:
CH(4,5)= CH(2,3)+ CH(3,4)+ CH(4,5)+ CH(5,6)+ CH(6,7)
考慮當前幀時,這會考慮以前更改和未來 中的權重。
當然,我們權衡所有這些款項由二項式濾波器,所以 的重量CH(4,5)仍是最重要的一個,但 希望,讓你更好的理解。
Args:
ssd_difference (numpy.ndarray): A difference matrix as produced by your
ssd function.
Returns:
output (numpy.ndarray): A difference matrix that takes preceding and
following frames into account. The output
difference matrix should have the same dtype as
the input, but be 4 rows and columns smaller,
corresponding to only the frames that have valid
dynamics.
Hint: There is an efficient way to do this with 2d convolution. Think about
the coordinates you are using as you consider the preceding and
following frame pairings.
"""
output = np.zeros((ssd_difference.shape[0] - 4,
ssd_difference.shape[1] - 4), dtype=ssd_difference.dtype)
# WRITE YOUR CODE HERE.
for i in range(len(ssd_difference)):
for j in range(len(ssd_difference)):
if i == 0:
if j > 1:
output[i,j] = np.sum(ssd_difference[i-2:i+2]*binomialFilter5())
elif i == ssd_difference.shape[0] - 1:
if j < ssd_difference.shape[1] - 2:
output[i,j] = np.sum(ssd_difference[i-2:i+2]*binomialFilter5())
else:
output[i,j] = np.sum(ssd_difference[i-2:i+2]*binomialFilter5())
# END OF FUNCTION.
return output
向我們展示更多'ValueError',特別是標有'---->'的行。關注具有「形狀(0,9)(5)」的操作數,以及試圖將它們組合的「操作」是什麼。 – hpaulj
對不起,線路是輸出[I,J] = np.sum(ssd_difference [I-2:I + 2] * binomialFilter5()) 我明白,我不能一起相乘不同尺寸的矩陣,但我想不出任何其他方式來完成功能的目的(至少根據說明) – Runner