2015-07-11 171 views
2

我試圖完成以下功能,但我一直在遇到索引問題,導致「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 
+0

向我們展示更多'ValueError',特別是標有'---->'的行。關注具有「形狀(0,9)(5)」的操作數,以及試圖將它們組合的「操作」是什麼。 – hpaulj

+0

對不起,線路是輸出[I,J] = np.sum(ssd_difference [I-2:I + 2] * binomialFilter5()) 我明白,我不能一起相乘不同尺寸的矩陣,但我想不出任何其他方式來完成功能的目的(至少根據說明) – Runner

回答

1

正如我評論,你真的應該告訴我們,產生錯誤信息的行。

但我可以猜測,因爲只有幾行可以執行涉及廣播的操作。最有可能的是:

output[i,j] = np.sum(ssd_difference[i-2:i+2]*binomialFilter5()) 

你寫binomialFilter5()產生(5,1)陣列,而是一個(5,)錯誤會談。這裏可能沒有關係,但你應該保持直線的維數。有時(5,1)(5,)有明顯不同。

output具有形狀(ssd_difference.shape[0] - 4, ssd_difference.shape[1] - 4)。但是您在range(len(ssd_difference))上重複i,joutput[i,j]最終將導致index error。特別是在遍歷二維數組時,最好使用正確的shape元素,而不是len()

但我懷疑從ssd_difference[i-2:i+2]眼前的錯誤結果。當i==0時,這是ssd_difference[-2:2]。這是生成(0,9)數組,因爲-2索引意味着從最後一個數字大於2

我認爲你正打算從這個數組拉5行,其他數組中匹配的5個值。正確的迭代,我想爲:

for i in range(output.shape[0]): 
    for j in range(output.shape[1]): 
     .... 
     output[i,j] = np.sum(ssd_difference[i:i+5, :] * binomialFilter5()) 
     ... 

您應該測試這樣的表現在個別交互式shell,用i選擇的值。 ssd_difference[i:i+5, :]應該有形狀(5,9)binomialFilter5()應該是(5,1)

+0

這很有道理,非常感謝!抱歉,不包括錯誤指向的行。仍在嘗試學習正確的代碼/計算器禮節。 – Runner

+0

只是對可能有類似問題的任何人的更新。 Numpy.transpose()是你的朋友。 – Runner