简体   繁体   English

Keras LSTM培训。 如何调整我的输入数据?

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

I have a dataset of 3000 observations. 我有3000个观测值的数据集。 Each observation consists of 3 timeseries of length 200 samples. 每个观察包括3个时间序列,长度为200个样本。 As the output I have 5 class labels. 作为输出,我有5个类标签。

So I build train as test sets as follows: 因此,我将火车构建为测试集,如下所示:

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)

I build my network using Keras functional API: 我使用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')

Then, I try to fit the model: 然后,我尝试拟合模型:

# 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)

and I get the following error: 我收到以下错误:

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...

How should I reshape my data, to solve the issue? 我应该如何重塑数据以解决问题?

You magically assume a single input with 3 time series X_train will split into 4 channels and be assigned to different inputs. 您可以神奇地假设具有3个时间序列的单个输入X_train将分为4个通道,并分配给不同的输入。 Well this doesn't happen and that is what the error is complaining about. 好吧,这不会发生,这就是错误所抱怨的。 You have 1 input: 您有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)

By merging the series together into single LSTM, the model might pickup relations across time series as well. 通过将序列合并到单个LSTM中,该模型也可以跨时间序列获得关系。 Now your target shape has to be y_train.shape == (2250, 5) , the first dimension must match X_train.shape[0] . 现在您的目标形状必须为y_train.shape == (2250, 5) ,第一个尺寸必须与X_train.shape[0]匹配。

Another point is you have Dense layer with linear activation, that is almost useless as it doesn't provide any non-linearity. 还有一点是,您具有具有线性激活的Dense层,这几乎没有用,因为它不提供任何非线性。 You might want to use a non-linear activation function like relu . 您可能需要使用诸如relu之类的非线性激活函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM