2016-08-15 115 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

这样做了,谢谢! –