【问题标题】:How to set up LSTM network for predict multi-sequence?如何建立 LSTM 网络来预测多序列?
【发布时间】:2019-09-19 05:59:14
【问题描述】:

我正在学习如何设置 RNN-LSTM 网络进行预测。我用一个输入变量创建了数据集。

x  y
1  2.5
2  6
3  8.6
4  11.2
5  13.8
6  16.4
...

通过以下python代码,我创建了窗口数据,如[x(t-2),x(t-1),x(t)]来预测[y(t)]:

df= pd.read_excel('dataset.xlsx')

# split a univariate dataset into train/test sets
def split_dataset(data):
    train, test = data[:-328], data[-328:-6]
    return train, test

train, test  = split_dataset(df.values)

# scale train and test data
def scale(train, test):
    # fit scaler
    scaler = MinMaxScaler(feature_range=(0,1))
    scaler = scaler.fit(train)
    # transform train
    #train = train.reshape(train.shape[0], train.shape[1])
    train_scaled = scaler.transform(train)
    # transform test
    #test = test.reshape(test.shape[0], test.shape[1])
    test_scaled = scaler.transform(test)
    return scaler, train_scaled, test_scaled

scaler, train_scaled, test_scaled = scale(train, test)

def to_supervised(train, n_input, n_out=7):
    # flatten data
    data = train
    X, y = list(), list()
    in_start = 0
    # step over the entire history one time step at a time
    for _ in range(len(data)):
        # define the end of the input sequence
        in_end = in_start + n_input
        out_end = in_end + n_out
        # ensure we have enough data for this instance
        if out_end <= len(data):
            x_input = data[in_start:in_end, 0]
            x_input = x_input.reshape((len(x_input), 1))
            X.append(x_input)
            y.append(data[in_end:out_end, 0])
        # move along one time step
        in_start += 1
    return np.array(X), np.array(y)
train_x, train_y = to_supervised(train_scaled, n_input = 3, n_out = 1)
test_x, test_y =  to_supervised(test_scaled, n_input = 3, n_out = 1)

verbose, epochs, batch_size = 0, 20, 16
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]


model = Sequential()
model.add(LSTM(200, return_sequences= False, input_shape = (train_x.shape[1],train_x.shape[2])))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
history = model.fit(train_x, train_y, epochs=epochs, verbose=verbose, validation_data = (test_x, test_y))

但是,我对此还有其他疑问:

Q1:LSTM中的units是什么意思? [model.add(LSTM(units, ...))]

(我已经为模型尝试了不同的单位,随着单位的增加会更准确。)

Q2:我应该设置多少层?

Q3:如何预测多步?例如基于 (x(t),x(t-1)) 来预测 y(t), y(t+1) 我试图在 to_supervised 函数中设置 n_out = 2,但是当我应用相同的方法时,它返回了错误

train_x, train_y = to_supervised(train_scaled, n_input = 3, n_out = 2)
test_x, test_y =  to_supervised(test_scaled, n_input = 3, n_out = 2)

verbose, epochs, batch_size = 0, 20, 16
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]

model = Sequential()
model.add(LSTM(200, return_sequences= False, input_shape = (train_x.shape[1],train_x.shape[2])))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
history = model.fit(train_x, train_y, epochs=epochs, verbose=verbose, validation_data = (test_x, test_y))
ValueError: Error when checking target: expected dense_27 to have shape (1,) but got array with shape (2,)

Q3(续):我应该在模型设置中添加或更改什么?

Q3(续):return_sequences 是什么?我应该什么时候设置 True?

【问题讨论】:

    标签: python machine-learning keras neural-network lstm


    【解决方案1】:

    第一季度。 LSTM 中的单位是 LSTM 层中的神经元数量。

    第二季度。这取决于您的模型/数据。尝试更换它们以查看效果。

    第三季度。这取决于您采用哪种方法。

    第四季度。理想情况下,您希望每次都预测一个时间步。 一次可以预测多个,但根据我的经验,您会得到更好的结果,如下所述

    例如

    使用 y(t-1), y(t) 预测 y_hat(t+1)

    那么

    使用 y(t), y_hat(t+1) 预测 y_hat(t+2)

    您确定在这种情况下您实际上是在使用 X 来预测 Y 吗? 训练 x/y 和测试 x/y 是什么样子的?

    【讨论】:

    • 你能告诉我更多关于如何在 python 代码中设置的信息吗?如何修改代码使多序列预测生效?
    【解决方案2】:

    关于 Q1:它是 LSTM 单元(=LSTM 单元)的数量,它本身由几个神经元组成,但(在给定的标准情况下)每个只有一个输出。因此,单位数直接对应于输出的维度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2014-11-16
      • 2020-09-15
      • 2021-09-08
      相关资源
      最近更新 更多