簡體   English   中英

Keras LSTM培訓。 如何調整我的輸入數據?

[英]Keras LSTM training. How to shape my input data?

我有3000個觀測值的數據集。 每個觀察包括3個時間序列,長度為200個樣本。 作為輸出,我有5個類標簽。

因此,我將火車構建為測試集,如下所示:

test_split = round(num_samples * 3 / 4)
X_train = X_all[:test_split, :, :] # Start upto just before test_split
y_train = y_all[:test_split]
X_test = X_all[test_split:, :, :] # From test_split to end
y_test = y_all[test_split:]

# Print shapes and class labels
print(X_train.shape)
print(y_train.shape)


> (2250, 200, 3)
> (22250, 5)

我使用Keras功能API構建網絡:

from keras.models import  Model
from keras.layers import Dense, Activation, Input, Dropout, concatenate
from keras.layers.recurrent import LSTM
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping

series_len = 200
num_RNN_neurons = 64
ch1 = Input(shape=(series_len, 1), name='ch1')
ch2 = Input(shape=(series_len, 1), name='ch2')
ch3 = Input(shape=(series_len, 1), name='ch3')

ch1_layer = LSTM(num_RNN_neurons, return_sequences=False)(ch1)
ch2_layer = LSTM(num_RNN_neurons, return_sequences=False)(ch2)
ch3_layer = LSTM(num_RNN_neurons, return_sequences=False)(ch3)


visible = concatenate([
    ch1_layer,
    ch2_layer,
    ch3_layer])


hidden1 = Dense(30, activation='linear', name='weighted_average_channels')(visible)
output = Dense(num_classes, activation='softmax')(hidden1)

model = Model(inputs= [ch1, ch2, ch3], outputs=output)

# Compile model
model.compile(loss='categorical_crossentropy', optimizer=SGD(), metrics=['accuracy'])
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-4, patience=5, verbose=1, mode='auto')

然后,我嘗試擬合模型:

# Fit the model
model.fit(X_train, y_train, 
          epochs=epochs, 
          batch_size=batch_size,
          validation_data=(X_test, y_test),
          callbacks=[monitor],
          verbose=1)

我收到以下錯誤:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays...

我應該如何重塑數據以解決問題?

您可以神奇地假設具有3個時間序列的單個輸入X_train將分為4個通道,並分配給不同的輸入。 好吧,這不會發生,這就是錯誤所抱怨的。 您有1個輸入:

ch123_in = Input(shape=(series_len, 3), name='ch123')
latent = LSTM(num_RNN_neurons)(ch123_in)
hidden1 = Dense(30, activation='linear', name='weighted_average_channels')(latent)

通過將序列合並到單個LSTM中,該模型也可以跨時間序列獲得關系。 現在您的目標形狀必須為y_train.shape == (2250, 5) ,第一個尺寸必須與X_train.shape[0]匹配。

還有一點是,您具有具有線性激活的Dense層,這幾乎沒有用,因為它不提供任何非線性。 您可能需要使用諸如relu之類的非線性激活函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM