如何使用当前的timestep功能培训LSTM?

2024-04-27 03:36:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个时间序列数据集的训练数据集,如下所示:

import numpy as np
import pandas as pd
train_df = pd.DataFrame(np.random.randint(0,100,size=(100, 16)))
train_df.columns=['var1(t-3)','var2(t-3)','var3(t-3)','var4(t-3)','var1(t-2)','var2(t-2)','var3(t-2)','var4(t-2)','var1(t-1)','var2(t-1)','var3(t-1)','var4(t-1)','var1(t)','var2(t)','var3(t)','var4(t)']
train_X=train_df.drop(['var1(t)'],axis=1)
train_y=train_df[['var1(t)']]

正如你们在培训中看到的,我正在输入网络,包括所有4个变量的过去三个时间步和剩余三个变量的当前时间步,这三个变量构成了15个变量

我想在Keras中使用函数API的LSTM中对其进行培训,因为我不能在我的示例中使用顺序API。因此,我尝试了以下方法:

import tensorflow
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
visible = Input(shape=(100,15))
hidden1 = LSTM(10)(visible)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(1, activation='sigmoid')(hidden2)
model = Model(inputs=visible, outputs=output)
model.compile(loss='mae', optimizer='adam')


history = model.fit(train_X, train_y, batch_size=64, epochs=2, validation_split=0.2)

然后,我对列车进行改造,并进行如下测试:

train_X=train_X.reshape(train_X.shape[0],1,train_X.shape[1])
train_y = train_y.reshape(train_y.shape[0],1)


train_X shape is (100, 1, 15)
train_y shape is (100,1)

但这给出了一个非常大的MAPE和RMSE。所以我认为我的重塑是错误的

感谢您的帮助


Tags: fromimportdfmodellayerstensorflow时间train
1条回答
网友
1楼 · 发布于 2024-04-27 03:36:11

原始答案:

LSTM接受3D输入,您将传递一个2D输入,该输入解释了ValueError。您应该预处理数据,使其满足要求[batch, timesteps, feature]。您的数据应该以每行表示一个时间步的方式呈现

让我们用4个变量创建虚拟数据

train_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)))
train_df.columns =['val1','val2','val3','val4']
train_df.head()

输出:

    val1  val2  val3  val4
t0    93    78    78    53
t1    38    30    48    39
t2    92    88    18     5
t3    56     8    36    59
t4    13     1    20    22

每行表示时间t的值。您需要回溯3个时间步来预测t=4时的val1

现在我将对数据进行预处理,使其满足三维张量的要求。 我创建了一个函数,它将按如下方式进行预处理:

def create_time_series(data, time_steps, look_forward, target_col, n_features):

    x = []
    y = []
    
    for i in range(time_steps, len(data)):
        x.append(data[i-time_steps:i,:])
        y.append(data[i:i+look_forward, target_col])
    
    x, y = np.array(x), np.array(y)

    x = np.reshape(x, (x.shape[0], x.shape[1], n_features))
    y = np.reshape(y, (y.shape[0], y.shape[1]))

    return x, y

data = train_df.values 
time_steps = 3  #We want to use previous 3 values to predict the next value
look_forward = 1 #We want to predict 1 time step ahead.
target_col = 0 #Target column is the 1st column i.e (Val1).
n_features = 4 #We are using 4 features

x_train, y_train = create_time_series(data,time_steps,look_forward,target_col,n_features)

现在我们已经创建了3D张量,可以通过LSTM层了

visible = Input(shape=(x_train.shape[1],x_train.shape[2]))
hidden1 = LSTM(10)(visible)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(1, activation='sigmoid')(hidden2)
model = Model(inputs=visible, outputs=output)
model.compile(loss='mae', optimizer='adam')
model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2)

希望这有帮助

编辑:

假设有3列具有特征,第4列包含目标值,则可以使用

train_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)))
train_df.columns =['val1','val2','val3','target']

由于您希望传递3个特性val1val2&val3利用它们在时间上的值{}要预测在时间上的{}值{},您可以再创建3个表示{}、val2中移位的特征&val3使用

train_df['val1shift'] = train_df['val1'].shift(-1)
train_df['val2shift'] = train_df['val2'].shift(-1)
train_df['val3shift'] = train_df['val3'].shift(-1)
train_df = train_df[['val1',  'val2',  'val3', 'val1shift',  'val2shift',  'val3shift', 'target']]

输出:

    val1   val2  val3   val1shift   val2shift   val3shift  target
t0    63    17    86       13.0       57.0       63.0      14
t1    13    57    63       85.0       20.0       60.0      62
t2    85    20    60       72.0        4.0       58.0      74
t3    72     4    58       70.0       22.0       25.0       6
t4    70    22    25       23.0       15.0       49.0      52

现在,每行将包含val1val2&val3在时间{}和{}或者你可以说时间{}&;时间t

创建此数据帧后,您可以创建时间序列,该时间序列将使用

data = train_df.values 
time_steps = 3  #We want to use previous 3 values to predict the next value
look_forward = 1 #We want to predict 1 time step ahead.
target_col = 6 #Target column is the last column.
n_features = 7 #We are using 7 features

x_train, y_train = create_time_series(data,time_steps,look_forward,target_col,n_features)

相关问题 更多 >