在keras.io上运行LSTM以获取多变量时间序列数据(LONG)

时间:2017-03-07 03:50:32

标签: python-3.x machine-learning neural-network keras lstm

我有一个季度经济变量的数据集,我想通过利用LSTM框架来尝试预测一些二进制变量(当天的政府)。

数据集看起来像:

Date           X1   X2   X3   X4    X5  Y1
------------------------------------------
01-Jan-1954    0.1  2.4  5.5  0.66  0.2  1
01-April-1954  0.3  2.0  5.0  0.67  0.4  1
                    .
                    .
                    .
01-July-2016   0.4  5.0  1.0  0.88  0.4  0

即:我想在句号X1-X5中使用0,...,t,以便能够预测句点Y1中的t+1

到目前为止,我已经使用了顺序模型...

import pandas as pd  
from random import random
import numpy as np
from keras.models import Sequential  
from keras.layers import Input, Embedding, LSTM, Dense, merge 
import scipy
from sklearn.preprocessing import MinMaxScaler
from keras.layers import Input, Embedding, LSTM, Dense, merge

Macro=pd.read_csv('MACROUS2.csv')
Macro=Macro.ix[:,Macro.columns!= 'DATE']    

'''
The model architecture is made to pull in 
'''

def _load_data(Macro, n_prev = 16):  
    """
    data should be pd.DataFrame()
    """

    docX, docY = [], []
    for i in range(len(Macro)-n_prev):
        docX.append(Macro.iloc[i:i+n_prev].as_matrix())
        docY.append(Macro.iloc[i+n_prev].as_matrix())
    alsX = np.array(docX)
    alsY = np.array(docY)

    return alsX, alsY

def train_test_split(df, test_size=0.1):  
    """
    This just splits data to training and testing parts
    """
    ntrn = round(len(df) * (1 - test_size))

    X_train, y_train = _load_data(df.iloc[0:ntrn])
    X_test, y_test = _load_data(df.iloc[ntrn:])


  return (X_train, y_train[:][0]), (X_test, y_test[:][0])
 (X_train, y_train), (X_test, y_test) = train_test_split(Macro)

我的数据形状为:

X_train has shape:  (209, 16, 6)
y_train has shape:  (6,)
X_test has shape:  (9, 16, 6)
y_test has shape:  (6,)

我有这个形状来反映一个'时间片'是16个季度(4年)。

我按以下方式定义架构:

in_neurons = np.shape(X_train)[2]   
out_neurons = np.shape(y_train)[0]
hidden_neurons = 20

model = Sequential()  
model.add(LSTM(output_dim=hidden_neurons, input_dim=in_neurons, return_sequences=False)) 
model.add(Dense(output_dim=out_neurons, input_dim=hidden_neurons))
model.add(Activation("linear"))  
model.compile(loss="mse", optimizer="adam")  

然而,在尝试训练模型时,我遇到了一些问题:

(X_train, y_train), (X_test, y_test) = train_test_split(Macro)  # retrieve data
model.fit(X_train, y_train, batch_size=450, nb_epoch=1, validation_split=0.05)  

predicted = model.predict(X_test)  
rmse = np.sqrt(((predicted - y_test) ** 2).mean(axis=0))

我得到以下异常:

Error when checking model target: expected activation_1 to have shape (None, 6) but got array with shape (6, 1)

如果我对我的问题一无所知或者我是如何尝试这个问题的话,我很擅长让RNN这么开心。任何有关如何继续和解决此异常的建议将不胜感激!

归功于http://danielhnyk.cz/predicting-sequences-vectors-keras-using-rnn-lstm/作为我尝试这样做的主要来源。

0 个答案:

没有答案