【问题标题】:Reshape ndarray in batches for RNN为 RNN 批量重塑 ndarray
【发布时间】:2017-11-05 03:26:21
【问题描述】:

目标从 2 开始。从给定的数组中,我们获取输入,目标是它的下一个元素。由于形状只有到 12 个输入即将到来。我需要将输入重塑为形状数组(批次数,2,2,3),其中批次数为 len (文本)//(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 个输入即将到来。我需要将输入重塑为形状数组(批次数,2,2,3),其中批次数为 len (text)//(2*2*3) 所以输入的数量将是 input[:no of batches*2*2*3]

【问题讨论】:

    标签: python numpy recurrent-neural-network


    【解决方案1】:

    你可以使用步幅


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-07
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2023-03-19
      • 1970-01-01
      • 2018-10-28
      相关资源
      最近更新 更多