2016-08-15 105 views
2

我有一個4D numpy陣列,但每個元素都是可變大小的3D體積。本質上它是一個3D卷的列表。所以numpy的陣列有形狀...Numpy填充4D單元全部爲零

(Pdb) batch_x.shape 
(3,) 

並採取元素i在該列表中,它看起來像這樣...

(Pdb) batch_x[i].shape 
(7, 70, 66) 

我想墊每個3D體積零,用下面的代碼...

for i in range(batch_size): 
    pdb.set_trace() 
    batch_x[i] = np.lib.pad(batch_x[i], (n_input_z - int(batch_x[i][:,0,0].shape[0]), 
                n_input_x - int(batch_x[i][0,:,0].shape[0]), 
                n_input_y - int(batch_x[i][0,0,:].shape[0])), 
            'constant', constant_values=(0,0,0)) 
    batch_y[i] = np.lib.pad(batch_y[i], (n_input_z - int(batch_y[i][:,0,0].shape[0]), 
                n_input_x - int(batch_y[i][0,:,0].shape[0]), 
                n_input_y - int(batch_y[i][0,0,:].shape[0])), 
            'constant', constant_values=(0,0,0)) 

還有就是如下錯誤...

*** ValueError: Unable to create correctly shaped tuple from (3, 5, 9)

我想填充每個3D卷,使它們都具有相同的形狀 - [10,75,75]。請記住,就像我上面顯示的那樣,batch_x[i].shape = (7,70,66)因此,錯誤消息至少告訴我我的尺寸應該是正確的。

證據,調試......

(Pdb) int(batch_x[i][:,0,0].shape[0]) 
7 
(Pdb) n_input_z 
10 
(Pdb) (n_input_z - int(batch_x[i][:,0,0].shape[0])) 
3 

回答

4

因此剝奪了外來的東西,問題是:

In [7]: x=np.ones((7,70,66),int) 
In [8]: np.pad(x,(3,5,9),mode='constant',constant_values=(0,0,0)) 
... 
ValueError: Unable to create correctly shaped tuple from (3, 5, 9) 

看起來像定義輸入pad問題。我沒有用太多,但我記得它需要每個維度的開始和結束墊大小。

從它的文檔:

pad_width : {sequence, array_like, int} 
    Number of values padded to the edges of each axis. 
    ((before_1, after_1), ... (before_N, after_N)) unique pad widths 
    for each axis. 

所以讓我們嘗試元組的元組:

In [13]: np.pad(x,((0,3),(0,5),(0,9)), mode='constant', constant_values=0).shape 
Out[13]: (10, 75, 75) 

你可以把它從那裏?

+0

這樣做了,謝謝! –