[英]Understanding multivariate time series classification with Keras
我試圖了解如何正確地將數據輸入我的 keras 模型,以使用 LSTM 神經網絡將多元時間序列數據分為三類。
我已經查看了不同的資源 - 主要是 Jason Brownlee post1 、 post2 、 post3 的這三篇優秀的博客文章)、其他 SO 問題和不同的論文,但是那里提供的信息都沒有完全適合我的問題案例,我無法弄清楚如果我的數據預處理/將其輸入模型是正確的,那么我想如果我在這里指定我的確切條件,我可能會得到一些幫助。
我想要做的是對多元時間序列數據進行分類,其原始形式的結構如下:
我有 200 個樣品
一個示例是一個 csv 文件。
一個樣本可以有 1 到 50 個特征(即 csv 文件有 1 到 50 列)。
每個特征都有其在固定時間步長內“跟蹤”的值,假設為 100(即每個 csv 文件正好有 100 行)。
每個 csv 文件都有三個類別之一(“好”、“太小”、“太大”)
所以我目前的狀態如下:
我有一個具有以下結構的 numpy 數組“樣本” :
# array holding all samples
[
# sample 1
[
# feature 1 of sample 1
[ 0.1, 0.2, 0.3, 0.2, 0.3, 0.1, 0.2, 0.4, 0.5, 0.1, ... ], # "time series" of feature 1
# feature 2 of sample 1
[ 0.5, 0.6, 0.7, 0.6, 0.4, 0.3, 0.2, 0.1, -0.1, -0.2, ... ], # "time series" of feature 2
... # up to 50 features
],
# sample 2
[
# feature 1 of sample 2
[ 0.1, 0.2, 0.3, 0.2, 0.3, 0.1, 0.2, 0.4, 0.5, 0.1, ... ], # "time series" of feature 1
# feature 2 of sample 2
[ 0.5, 0.6, 0.7, 0.6, 0.4, 0.3, 0.2, 0.1, -0.1, -0.2, ... ], # "time series" of feature 2
... # up to 50 features
],
... # up to sample no. 200
]
我還有一個 numpy 數組“標簽” ,其長度與“樣本”數組(即 200)相同。 標簽按以下方式編碼:
[0, 2, 2, 1, 0, 1, 2, 0, 0, 0, 1, 2, ... ] # up to label no. 200
然后使用 keras 的to_categorical
函數對這個“標簽”數組進行編碼
to_categorical(labels, len(np.unique(labels)))
我的模型定義目前看起來像這樣:
max_nb_features = 50
nb_time_steps = 100
model = Sequential()
model.add(LSTM(5, input_shape=(max_nb_features, nb_time_steps)))
model.add(Dense(3, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
然后我將數據拆分為訓練/測試數據:
samples_train, samples_test, labels_train, labels_test = train_test_split(samples, labels, test_size=0.33)
這給我們留下了 134 個用於訓練的樣本和 66 個用於測試的樣本。
我目前遇到的問題是以下代碼不起作用:
model.fit(samples_train, labels_train, epochs=1, batch_size=1)
錯誤如下:
Traceback (most recent call last):
File "lstm_test.py", line 152, in <module>
model.fit(samples_train, labels_train, epochs=1, batch_size=1)
File "C:\Program Files\Python36\lib\site-packages\keras\models.py", line 1002, in fit
validation_steps=validation_steps)
File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1630, in fit
batch_size=batch_size)
File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data
exception_prefix='input')
File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (134, 1)
對我來說,它似乎不起作用,因為我的樣本可以具有可變數量的特征。 如果我使用“假”(生成的)數據,其中所有參數都相同,除了每個樣本具有完全相同數量的特征 (50),代碼工作。
現在我想了解的是:
batch_size
、 input_shape
)是否正確/合理?我相信 Keras 的輸入形狀應該是:
input_shape=(number_of_samples, nb_time_steps, max_nb_features)。
最常見的是 nb_time_steps = 1
PS:我嘗試為實習職位解決一個非常相似的問題(但結果證明我的結果是錯誤的)。 你可以看看這里: https : //github.com/AbbasHub/Deep_Learning_LSTM/blob/master/2018-09-22_Multivariate_LSTM.ipynb (看看你能不能發現我的錯誤!)
LSTM 模型需要 [樣本、時間步長、特征] 形式的 3D 輸入
在定義我們的 LSTM 模型的第一層時,我們只需要指定時間步長和特征。 盡管這看起來是 2D,但實際上是 3D,因為樣本大小,即批量大小是在模型擬合時指定的。
features = x_train_d.shape[1]
因此,我們首先需要以 3D 格式重塑我們的輸入:
x_train_d = np.reshape(x_train_d, (x_train_d.shape[0], 1, x_train_d.shape[1]))
這是 LSTM 第一層:
model.add(LSTM(5,input_shape=(1, features),activation='relu'))
並且模型擬合指定了 LSTM 預期的樣本=50
model.fit(x_train_d,y_train_d.values,batch_size=50,epochs=100)
對於使用可變長度輸入提出的問題,在https://datascience.stackexchange.com/questions/26366/training-an-rnn-with-examples-of-different-lengths-in-keras發起了討論
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.