2017-06-04 47 views
2

目標從2開始。從給定的數組我們接受輸入,目標是它的下一個元素。由於形狀只有到12輸入即將到來。我需要重塑形狀數組的輸入(批次數,2,2,3),其中批次數是len (文本)//(2 * 2 * 3),所以輸入的數量將被輸入[:批次數* 2 * 2 * 3] 將[ 8,9,10,11,12,13,14,15]轉換成爲RNN分批重塑ndarray

[ 
     # First Batch 
     [ 
     # Batch of Input 
     [[ 1 2 3], [ 7 8 9]], 
     # Batch of targets 
     [[ 2 3 4], [ 8 9 10]] 
     ], 

     # Second Batch 
     [ 
     # Batch of Input 
     [[ 4 5 6], [10 11 12]], 
     # Batch of targets 
     [[ 5 6 7], [11 12 13]] 
     ] 
    ] 

目標從2開始。從給定的數組我們接受輸入,目標是它的下一個元素。由於形狀只有到12輸入即將到來。我需要重塑形狀數組的輸入(批次數,2,2,3),其中批次數是len (文本)//(2 * 2 * 3),因此沒有輸入將被輸入[:無批次* 2 * 2 * 3]

回答

0

可以使用步幅


from numpy.lib.stride_tricks import as_strided as strided 

a = np.arange(1, 16) 
a 

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) 

s = a.strides[0] 
strided(a, (2, 2, 2, 3), (s * 3, s, s * 6, s)) 

array([[[[ 1, 2, 3], 
     [ 7, 8, 9]], 

     [[ 2, 3, 4], 
     [ 8, 9, 10]]], 


     [[[ 4, 5, 6], 
     [10, 11, 12]], 

     [[ 5, 6, 7], 
     [11, 12, 13]]]])