通过使用Keras中的LSTM架构一次填充一个时间步进行预测

时间:2018-09-05 21:14:31

标签: python keras lstm masking

我有一个多维数据集,具有多个功能序列和不同的时间步长。它是一个具有多个示例的多元时间序列数据,用于训练LSTM,Y为0或1。当前,我将填充序列层与遮罩层结合使用,以使每个样本具有相同数量的时间步长。

为了进行预测,我必须填充测试数据以使其与火车数据保持一致,并且我必须提供一个包含3个时间步长的序列。有没有一种方法可以通过在训练序列后只进一个时间步来做出预测?例如,我是否可以在时间步骤3中填充1和2,而不是由1、2和3组成的完整序列进行预测。

# The input sequences are
trainX = np.array([
        [
            # Input features at timestep 1 with padding
            [0, 0, 0],                
            # Input features at timestep 2 with padding
            [6, 2, 1],
            # Input features at timestep 3
            [5, 2, 3]
        ],
        # Datapoint 2
        [
            # Features at timestep 1 with padding
            [0, 0, 0],
            # Features at timestep 2
            [9, 8, 9],
            # Features at timestep 3
            [7, 6, 1]
        ]
    ])

# The desired model outputs are as follows:
trainY = np.array([
        # Datapoint 1
        [
            # Target class at timestep 1
            [0],
            # Target class at timestep 2
            [1] 
        ],
        # Datapoint 2
        [
            # Target class at timestep 1
            [0],
            # Target class at timestep 2
            [0]
            # Target class at time step 3
            [0]
        ]
    ])

timesteps = 3
# Create neural network architecture
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(timesteps, trainX.shape[2]), name='Mask'))
model.add(LSTM(98, kernel_initializer ='uniform', return_sequences=True, batch_input_shape=(None, timesteps, trainX.shape[2]), 
               kernel_constraint=maxnorm(3), name='LSTM'))
model.add(Dropout(0.2))
model.add(LSTM(98, return_sequences=False, kernel_constraint=maxnorm(3), name='LSTM-2'))
model.add(Dropout(0.2))
model.add(Dense(timesteps, activation='sigmoid', name='Dense'))
# set optimization parameters'
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#compile model
model.compile(loss="mse", optimizer="sgd", metrics=["mse"])
fitness = model.fit(trainX, trainY, epochs=200, batch_size=20, validation_data=(tstX, tstY), verbose=1)
predY = model.predict(tstX)

我希望能够只按以下时间步进行预测:

testX = np.array([
        [
            # Input features at timestep 1 with padding
            [0, 0, 0],                
            # Input features at timestep 2 with padding
            [0, 0, 0],
            # Input features at timestep 3
            [5, 2, 3]
        ]]

每次我在最后一个步骤中以不同的值输入testX时,出现的概率都是相同的。似乎概率取决于未屏蔽的真实数据的时间步长,而不是数据本身。如果我按如下方式复制上一个步骤,则即使数据相同,概率也会发生变化。我需要屏蔽测试数据吗?为什么我需要输入一个序列而不是一个时间步才能获得正确的预测?

testX = np.array([
        [
            # Input features at timestep 1 with padding
            [5, 2, 3],                
            # Input features at timestep 2 with padding
            [5, 2, 3],
            # Input features at timestep 3
            [5, 2, 3]
        ]]

我对此进行了培训,但希望仅提供最后一步来进行预测

trainX = np.array([
        [
            # Input features at timestep 1 with padding
            [0, 0, 0],                
            # Input features at timestep 2 with padding
            [6, 2, 1],
            # Input features at timestep 3
            [5, 2, 3]
        ]]

0 个答案:

没有答案