Python 多个同时序列的Keras序列预测

Python 多个同时序列的Keras序列预测,python,numpy,tensorflow,keras,lstm,Python,Numpy,Tensorflow,Keras,Lstm,我的问题与它似乎在问的问题非常相似,尽管那篇文章并没有提出令人满意的解决方案。为了详细说明,我目前正在使用带有tensorflow后端和顺序LSTM模型的keras。最终目标是我有n个时间相关序列,具有相等的时间步长(每个序列上相同数量的点,且点之间的时间间隔相同),我希望将所有n个序列输入到相同的网络中,以便它可以使用序列之间的相关性更好地预测每个序列的下一步。我的理想输出是一个n元素的一维数组,数组[0]对应于序列_1的下一步预测,数组[1]对应于序列_2,依此类推 我的输入是单值序列,因此

我的问题与它似乎在问的问题非常相似,尽管那篇文章并没有提出令人满意的解决方案。为了详细说明,我目前正在使用带有tensorflow后端和顺序LSTM模型的keras。最终目标是我有n个时间相关序列,具有相等的时间步长(每个序列上相同数量的点,且点之间的时间间隔相同),我希望将所有n个序列输入到相同的网络中,以便它可以使用序列之间的相关性更好地预测每个序列的下一步。我的理想输出是一个n元素的一维数组,数组[0]对应于序列_1的下一步预测,数组[1]对应于序列_2,依此类推

我的输入是单值序列,因此n个输入中的每一个都可以解析为1-D数组

我能够使用Jakob Aungiers最后的代码为每个序列独立地获得一个工作模型,尽管我的困难是使其适应于同时接受多个序列并在它们之间进行关联(即并行分析)。我相信这个问题与我的输入数据的形状有关,它目前是一个4-D numpy数组的形式,因为Jakob的指南是如何将输入分割成30个元素的子序列,每个元素以增量方式进行分析的,尽管我也可能完全没有达到目标。我的代码(大部分是Jakob的代码,不想为任何不属于我的东西争光)目前看起来如下:

正如“ValueError:Error when check target:expected activation_1 have shape(None,4),but get array with shape(4490)”中所抱怨的那样,我确信还有很多其他问题,但我想知道如何实现我所描述的目标。有什么东西能马上发现吗?您能给予的任何帮助都将不胜感激

谢谢


-Eric

Keras已经准备好处理包含许多序列的批次,这根本没有秘密

不过,有两种可能的方法:

  • 输入整个序列(一次输入所有步骤)并预测n个结果
  • 您只输入所有序列中的一个步骤,并预测循环中的下一个步骤
假设: 第一步:
stateful=False
: 像这样训练:

model.fit(inputArray,outputArray,....)
newStep = model.predict(inputArray)
model.reset_states() #you need this in stateful=True models
    #if you don't reset states, 
    #the stateful model will think that your inputs are new steps of the same previous sequences
for step in range(inputArray.shape[1]): #for each time step
    model.fit(inputArray[:,step:step+1], outputArray[:,step:step+1],shuffle=False,...)
model.reset_states()

predictions = np.empty(inputArray.shape)
for step in range(inputArray.shape[1]): #for each time step
    predictions[:,step] = model.predict(inputArray[:,step:step+1]) 
这样预测:

model.fit(inputArray,outputArray,....)
newStep = model.predict(inputArray)
model.reset_states() #you need this in stateful=True models
    #if you don't reset states, 
    #the stateful model will think that your inputs are new steps of the same previous sequences
for step in range(inputArray.shape[1]): #for each time step
    model.fit(inputArray[:,step:step+1], outputArray[:,step:step+1],shuffle=False,...)
model.reset_states()

predictions = np.empty(inputArray.shape)
for step in range(inputArray.shape[1]): #for each time step
    predictions[:,step] = model.predict(inputArray[:,step:step+1]) 
第二种方法:
stateful=True
: 像这样训练:

model.fit(inputArray,outputArray,....)
newStep = model.predict(inputArray)
model.reset_states() #you need this in stateful=True models
    #if you don't reset states, 
    #the stateful model will think that your inputs are new steps of the same previous sequences
for step in range(inputArray.shape[1]): #for each time step
    model.fit(inputArray[:,step:step+1], outputArray[:,step:step+1],shuffle=False,...)
model.reset_states()

predictions = np.empty(inputArray.shape)
for step in range(inputArray.shape[1]): #for each time step
    predictions[:,step] = model.predict(inputArray[:,step:step+1]) 
这样预测:

model.fit(inputArray,outputArray,....)
newStep = model.predict(inputArray)
model.reset_states() #you need this in stateful=True models
    #if you don't reset states, 
    #the stateful model will think that your inputs are new steps of the same previous sequences
for step in range(inputArray.shape[1]): #for each time step
    model.fit(inputArray[:,step:step+1], outputArray[:,step:step+1],shuffle=False,...)
model.reset_states()

predictions = np.empty(inputArray.shape)
for step in range(inputArray.shape[1]): #for each time step
    predictions[:,step] = model.predict(inputArray[:,step:step+1]) 

我相信我想要的是第一种方法,因为我想最大限度地提高检测输入序列之间模式的能力-我已经基于此添加了我当前的模型,但是我目前得到了一个维度错误。正如我得到的,第一个LSTM层期望dim3,得到dim4;如果我删除输入_形状中的1,那么它会通过,但第三层抱怨它需要dim3,得到dim2,我相信这与我的第二层有关。没关系,我的错,我最初误读了你的帖子-我解决了问题。