Python 为RNN分批重新塑造ndarray

Python 为RNN分批重新塑造ndarray,python,numpy,recurrent-neural-network,Python,Numpy,Recurrent Neural Network,目标从2开始。我们从给定的数组中获取输入,目标是它的下一个元素。由于只有12个输入才是shape。我需要将输入重塑为shape数组(批数,2,2,3),其中批数为len(text)/(2*2*3),因此输入数将被输入[:批数*2*2*3] 将[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]转换为 [ # First Batch [ # Batch of Input [[ 1 2 3], [ 7 8

目标从2开始。我们从给定的数组中获取输入,目标是它的下一个元素。由于只有12个输入才是shape。我需要将输入重塑为shape数组(批数,2,2,3),其中批数为len(text)/(2*2*3),因此输入数将被输入[:批数*2*2*3] 将[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]转换为

    [
      # 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个输入才是shape。我需要将输入重塑为shape数组(批次数,2,2,3),其中批次数为len(text)/(2*2*3),因此输入数将为[:批次数*2*2*3]

您可以使用跨步



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]]]])