将2d输入重新整形为3d LSTM序列

时间:2016-12-08 02:56:29

标签: python numpy lstm

我正在尝试重塑2d阵列,例如。 (100000,100)进入具有10个步骤的LSTM序列,即。 (100000,10,100)。我使用以下代码:

add(backToTheMenu);

以上似乎需要花费大量时间来处理。我能否以更有效的方式提出建议?

1 个答案:

答案 0 :(得分:0)

import time
import numpy as np


def _2d_to_3d(X, n_steps=10):
    _X = np.zeros((X.shape[0]//n_steps,n_steps,X.shape[1]))
    for i in range(len(X)//n_steps):
        _X[i,:] = X[i*n_steps:i*n_steps+n_steps]
    return _X

def time_function():
    a = np.arange(10000000).reshape(100000,100)
    start = time.time()
    b = _2d_to_3d(a, 10)
    total = time.time() - start
    print('time: {}'.format(total))
    print('original shape: {}'.format(a.shape))
    print('new shape: {}'.format(b.shape))

time_function()
time: 0.10249948501586914
original shape: (100000, 100)
new shape: (10000, 10, 100)