[英]Preparing Time-Series Data for Keras LSTM - Network Trains with Extremely High Loss
我遇到了准備我的數據以便在Keras的LSTM層中使用的問題。 該數據是1,600,000項時間序列csv,包含日期和三個功能:
Date F1 F2 F3 2016-03-01 .252 .316 .690 2016-03-02 .276 .305 .691 2016-03-03 .284 .278 .687 ...
我的目標是預測F1 prediction_period的值未來的時間步。 了解Keras的LSTM層以格式(樣本,時間步長,維度)獲取導入數據我編寫了以下函數,將我的數據轉換為此格式的3D numpy數組(以2016-03-03為例):
[[[。{284,.278,.687],[。276,.305,.691],[。252,.316,.690]],......其他樣本...]
此功能通過堆疊數據副本來創建陣列,每個副本都會向后移動一步。 回顧是堆棧中“層”的數量,而trainpercent是列車/測試拆分:
def loaddata(path):
df = pd.read_csv(path)
df.drop(['Date'], axis=1, inplace=True)
df['label'] = df.F1.shift(periods=-prediction_period)
df.dropna(inplace=True)
df_train, df_test = df.iloc[:int(trainpercent * len(df))], df.iloc[int(trainpercent * len(df)):]
train_X, train_Y = df_train.drop('label', axis=1).copy(), df_train[['label']].copy()
test_X, test_Y = df_test.drop('label', axis=1).copy(), df_test[['label']].copy()
train_X, train_Y, test_X, test_Y = train_X.as_matrix(), train_Y.as_matrix(), test_X.as_matrix(), test_Y.as_matrix()
train_X, train_Y, test_X, test_Y = train_X.astype('float32'), train_Y.astype('float32'), test_X.astype('float32'), test_Y.astype('float32')
train_X, test_X = stackit(train_X), stackit(test_X)
train_X, test_X = train_X[:, lookback:, :], test_X[:, lookback:, :]
train_Y, test_Y = train_Y[lookback:, :], test_Y[lookback:, :]
train_X = np.reshape(train_X, (train_X.shape[1], train_X.shape[0], train_X.shape[2]))
test_X = np.reshape(test_X, (test_X.shape[1], test_X.shape[0], test_X.shape[2]))
train_Y, test_Y = np.reshape(train_Y, (train_Y.shape[0])), np.reshape(test_Y, (test_Y.shape[0]))
return train_X, train_Y, test_X, test_Y
def stackit(thearray):
thelist = []
for i in range(lookback):
thelist.append(np.roll(thearray, shift=i, axis=0))
thelist = tuple(thelist)
thestack = np.stack(thelist)
return thestack
雖然網絡接受了數據並進行了訓練,但損失值非常高,考慮到數據具有明確的周期性趨勢,這是非常令人驚訝的。 為了嘗試找出問題,我用這個例子中的sin-wave數據集和結構替換了我的數據集和網絡結構: http : //www.jakob-aungiers.com/articles/a/LSTM-Neural-Network-for-時間序列預測 。
即使使用sin波數據集,損失仍然高於示例函數產生的數量級。 我使用一列順序數據集逐個瀏覽函數,並將期望值與實際值進行比較。 我沒有發現任何錯誤。
我是否正確地為Keras的LSTM層構建輸入數據? 如果是這樣,這樣做的正確方法是什么? 如果沒有,你會期望在我的功能或其他方面導致這些症狀(極高的損失,不會隨着時間的推移而減少,即使有40多個時期)。
提前感謝您提供的任何建議!
“高損失”是一個非常主觀的事情。 如果沒有看到您的模型,我們無法評估這一點
它可能有多種原因:
你看,有很多可能性。 高損失本身並不意味着什么。 你可以有一個非常小的損失,只需要做到1000,並且在問題解決后你的損失會很高
以下是一些可以改進預測的方法:
首先確保輸入數據居中,即應用一些標准化或標准化。 您可以使用sklearn庫中的MinMaxScaler或StandardScaler,也可以根據數據實現一些自定義縮放。
確保您的網絡(LSTM / GRU / RNN)足夠大,以捕獲數據的復雜性。
使用Keras中的tensorboard回調來監控您的權重矩陣和損失函數。
使用自適應優化器而不是設置自定義學習參數。 也許'adam'或'adagrad'。
使用這些將至少確保您的網絡正在進行培訓。 隨着時間的推移,您應該看到損失逐漸減少。 解決了這個問題之后,您可以自由地嘗試初始的超參數並實現不同的正則化技術
祝好運 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.