How to set up LSTM network for predict multi-sequence?

断了今生、忘了曾经 提交于 2019-12-13 03:18:49

问题


I am learning how to set up the RNN-LSTM network for prediction. I have created the dataset with one input variable.

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

By the following python code, I have created the window data, like [x(t-2), x(t-1), x(t)] to predict [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))

However, I have other questions about this:

Q1: What is the meaning of units in LSTM? [model.add(LSTM(units, ...))]

(I have tried different units for the model, it would be more accurate as units increased.)

Q2: How many layers should I set?

Q3: How can I predict multi-steps ? e.g base on (x(t),x(t-1)) to predict y(t), y(t+1) I have tried to set the n_out = 2 in the to_supervised function, but when I applied the same method, it returned the error

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(cont): What should I add or change in the model setting?

Q3(cont): What is the return_sequences ? When should I set True?


回答1:


Q1. Units in LSTM is the number of neurons in your LSTM layer.

Q2. That depends on your model / data. Try changing them around to see the effect.

Q3. That depends which apporach you take.

Q4. Ideally you'll want to predict a single time step every time. It is possible to predict several at a time, but in my experience you will get better results like as i have described below

e.g

use y(t-1), y(t) to predict y_hat(t+1)

THEN

use y(t), y_hat(t+1) to predict y_hat(t+2)

Are you sure you're actually using X to predict Y in this case? how does train x/y and test x/y look like?




回答2:


Re Q1: It is the number of LSTM cells (=LSTM units), which consist of several neurons themselves but have (in the standard case as given) only one output each. Thus, the number of units corresponds directly to the dimensionality of your output.



来源:https://stackoverflow.com/questions/58004563/how-to-set-up-lstm-network-for-predict-multi-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!