2017-07-31 17 views
0

私は深い学習の初心者です。私はkerasライブラリを使ってLSTMを実装しています。気象データを予測するために、私は列車とテストデータを持っています。いくつかの変数を削除した後、私の入力データは次のような形をしています。ケラスのLSTMによる時系列予測

('X_train', (117, 22)) 
('y_train', (117,)) 
('X_test', (13, 22)) 
('y_test', (13,)) 

今、私は以下のLSTMコードにこのデータを供給していますが、私はトラブルに直面しています入力形状を、見つけ出すことができませんでした。以下に、私が申請しているLSTMの完全なコードを示します。

import os 
print os.getcwd() 
import pandas 
import numpy 
import matplotlib.pyplot as plt 
import math 
from sklearn.metrics import mean_squared_error 


train = pandas.read_excel('./data/train.xlsx', sheetname = 'temp4') 
print train.head() 
print train.shape 

test = pandas.read_excel('./data/test.xlsx', sheetname = 'temp4') 
print test.head() 
print test.shape 

# lagsp has 7 misssing values in train data and rest is tha in all entries and also drop un-necessary variable 
train = train.drop(['WEEK_NBR', 'DOS_YEAR', 'sorted row','lagsp'], axis = 1) 
test = test.drop(['WEEK_NBR', 'DOS_YEAR', 'sorted row','lagsp'], axis = 1) 

print train.shape 
print test.shape 

train = train.values 
test = test.values 

X_train = train[:,0:22] 
y_train = train[:,22] 

X_test = test[:,0:22] 
y_test = test[:,22] 

print("X_train", X_train.shape) 
print("y_train", y_train.shape) 
print("X_test", X_test.shape) 
print("y_test", y_test.shape) 




timesteps = X_train.shape[0] 
features = X_train.shape[1] 

X_train = X_train.reshape(1, timesteps, features) 
X_test = X_test.reshape(1, X_test.shape[0], X_test.shape[1]) 



print 'timesteps', timesteps 
print 'features', features 



numpy.random.seed(7) 


from keras.models import Sequential 
from keras.layers import Dense, Dropout 


########################### 
# RNN 
########################### 
from keras.layers.recurrent import LSTM 

d = 0.2 
rnn_model = Sequential() 
rnn_model.add(LSTM(64, input_shape= (117,22), return_sequences=True)) 
rnn_model.add(Dropout(d)) 
rnn_model.add(Dense(16,kernel_initializer='uniform',activation='relu')) 
rnn_model.add(Dense(1,kernel_initializer='uniform',activation='linear')) 
rnn_model.compile(loss='mse',optimizer='rmsprop',metrics=['accuracy']) 

#batch_input_shape=(batch_size, timesteps, data_dim) 

rnn_model.fit(
    X_train, 
    numpy.array(y_train), 
    batch_size=10, 
    epochs=10) 

# make predictions 
trainPredict_rnn = rnn_model.predict(X_train) 
testPredict_rnn = rnn_model.predict(X_test) 
# calculate root mean squared error 
trainScore_rnn = math.sqrt(mean_squared_error(y_train, trainPredict_rnn)) 
print('Train Score: %.2f RMSE' % (trainScore_rnn)) 
testScore_rnn = math.sqrt(mean_squared_error(y_test, testPredict_rnn)) 
print('Test Score: %.2f RMSE' % (testScore_rnn)) 

# plot predictions 

plt.figure(figsize=(20,10)) 
plt.plot(y_train) # blue # orange 
plt.plot(trainPredict_rnn) 
plt.show() 

plt.plot(y_test) # blue # orange 
plt.plot(testPredict_rnn) 
plt.show() 

これは私は、モデルは、入力形状上で、X_trainとy_trainをフィッティングされ、コードを実行した後geting AM、エラーです。

Traceback (most recent call last): 
    File "/home/shivampanchal/PycharmProjects/WeatherPrediction/try.py", line 81, in <module> 
    epochs=10) 
    File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 856, in fit 
    initial_epoch=initial_epoch) 
    File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1429, in fit 
    batch_size=batch_size) 
    File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1309, in _standardize_user_data 
    exception_prefix='target') 
    File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 127, in _standardize_input_data 
    str(array.shape)) 
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (117, 1) 
+0

[シーケンスが異なるケラスのlstm入力形状を理解する]の可能な複製(https://stackoverflow.com/questions/43234504/understanding-lstm-input-shape-in​​-keras-with-disferent-sequence) – gionni

+0

Ifあなたは周りを見回すので、あなたの質問に対する多くの答えを見つけることができます。あなたの質問もあまり明確ではありません。間違った入力シェイプ(ヒント、入力シェイプが間違っている)を使用しているためコードが壊れていますか、または別のものを明確にしたいのですか?よい質問にどのように質問すればよいかを見るには、[here](https://stackoverflow.com/questions/43234504/understanding-lstm-input-shape-in​​-keras-with-different-sequence)をお読みください。 – gionni

+0

これはエラーです。私は取得しています。 –

答えて

1

X_trainは三次元である必要があります。最初のレイヤーにinput_shapeを指定する場合は、(timesteps, features)を指定しています。次に、実際のデータをfit()に渡す場合は、各サンプルの形状が(timesteps, features)の3次元配列を渡す必要があります。

timesteps = X_train.shape[0] 
features = X_train.shape[1] 

X_train = X_train.reshape(1, timesteps, features) 

これは1つのトレーニングサンプルを残していますが、これはあなたが望むものではありません。あなたのデータが実際にどのように見えるのかわからなければ、さらに助けになることは難しいです!データセットを固定量のタイムステップのシーケンスに分割したいと思う可能性が高いようです。さらに、同様の変換をX_testに適用したいと考えています。

+0

ありがとう、私はこれを試してみます。データセットには22の変数が含まれています。すでにy-列車に入っている1つの変数を予測する必要があります。このように処理した後、入力の形状をどのようにするべきか。 –

+0

あなたの現在の入力形状は問題ありません。 –

+0

ニコール、私はコードを変更しました。 y_trainにエラーが発生しました。トレーニング中。 –

関連する問題