Python 将二维输入重塑为三维LSTM序列

Python 将二维输入重塑为三维LSTM序列,python,numpy,lstm,Python,Numpy,Lstm,我正在尝试用10个步骤(100000,10100)将2d数组(例如10000100)重塑为LSTM序列。我使用以下代码: n_input = 100 n_steps = 10 a = np.arange(10000000).reshape(100000,100) b = np.empty([0,n_input]) c = np.empty([0,n_steps,n_input]) for i in range(a.shape[0]-n_steps+1): b = np.empty([

我正在尝试用10个步骤(100000,10100)将2d数组(例如10000100)重塑为LSTM序列。我使用以下代码:

n_input = 100
n_steps = 10 
a = np.arange(10000000).reshape(100000,100)
b = np.empty([0,n_input])
c = np.empty([0,n_steps,n_input])

for i in range(a.shape[0]-n_steps+1):
    b = np.empty([0,n_input])
    for j in range(n_steps):
        b = np.vstack((b,a[j+1,]))
    c = np.concatenate((c, b[np.newaxis,...]), axis=0)

以上这些似乎需要花费大量的时间来处理。我可以问一些关于更有效的写作方法的建议吗

缩小问题范围以进行测试。。。是的,参数越小越好,但我更喜欢避免在python中使用for循环,如果可能的话,如果东西被交换到磁盘上,将问题划分成块可能会更快,这就是我所指的
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)