我的问题与这个职位的问题非常相似,尽管这篇文章并没有给出一个令人满意的解决方案。为了详细说明,我目前正在使用带有tensorflow后端和顺序LSTM模型的keras。最终目标是我有n个与时间相关的序列,它们具有相同的时间步长(每个序列上的点数相同,所有的点都是相同的时间间隔),我想把所有的n个序列输入到同一个网络中,这样它就可以利用序列之间的相关性来更好地预测每个序列的下一步。我理想的输出是n元素一维数组,数组对应于sequence_1的下一步预测,array1用于sequence_2,等等。
我的输入是单个值的序列,所以每个n个输入都可以被解析成一个一维数组。
我能够独立地使用Jakob在本指南末尾的代码为每个序列获得一个工作模型,尽管我的困难是将它调整为一次接受多个序列并在它们之间进行关联(即并行分析)。我相信这个问题与我的输入数据的形状有关,它目前以4维的numpy数组的形式存在,因为Jakob的指南是如何将输入分成30个元素的子序列来进行增量分析,尽管我也完全忽略了这里的目标。我的代码(主要是Jakob的代码,不试图为任何不是我的代码取功劳)现在看起来像这
因为-这是抱怨"ValueError:错误时,检查目标:期望的activation_1有形状(无,4),但得到数组与形状(4,490)",我肯定还有很多其他问题,但我想知道一些方向,如何实现我所描述的。有什么东西会立即出现在任何人身上吗?你所能给予的任何帮助都将不胜感激。
谢谢!
-Eric
发布于 2017-10-23 13:20:26
Keras已经准备好处理包含许多序列的批处理,根本不存在任何秘密。
不过,有两种可能的办法:
假设:
nSequences = 30
timeSteps = 50
features = 1 #(as you said: single values per step)
outputFeatures = 1第一批:stateful=False
inputArray = arrayWithShape((nSequences,timeSteps,features))
outputArray = arrayWithShape((nSequences,outputFeatures))
input_shape = (timeSteps,features)
#use layers like this:
LSTM(units) #if the first layer in a Sequential model, add the input_shape
#if you want to return the same number of steps (like a new sequence parallel to the input, use return_sequences=True像这样的火车:
model.fit(inputArray,outputArray,....)像这样预测:
newStep = model.predict(inputArray)第二种方法:stateful=True
inputArray = sameAsBefore
outputArray = inputArray[:,1:] #one step after input array
inputArray = inputArray[:,:-1] #eliminate the last step
batch_input = (nSequences, 1, features) #stateful layers require the batch size
#use layers like this:
LSMT(units, stateful=True) #if the first layer in a Sequential model, add input_shape像这样的火车:
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]) https://stackoverflow.com/questions/46880887
复制相似问题