简体   繁体   English

用于多元时间序列的LSTM输入形状?

[英]LSTM input shape for multivariate time series?

I know this question is asked many times, but I truly can't fix this input shape issue for my case. 我知道这个问题已经问过很多次了,但是我确实无法解决我的情况下的这种输入形状问题。

My x_train shape == (5523000, 13) // (13 timeseries of length 5523000) 我的x_train shape ==(5523000,13)//(13个时间序列的长度5523000)

My y_train shape == (5523000, 1) 我的y_train形状==(5523000,1)

number of classes == 2 类数== 2

To reshape the x_train and y_train: 重塑x_train和y_train:

x_train= x_train.values.reshape(27615,200,13)  # 5523000/200 = 27615
y_train= y_train.values.reshape((5523000,1))   # I know I have a problem here but I dont know how to fix it

Here is my lstm network : 这是我的lstm网络:

def lstm_baseline(x_train, y_train):
    batch_size=200
    model = Sequential()
    model.add(LSTM(batch_size, input_shape=(27615,200,13),
                   activation='relu', return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(128, activation='relu'))
    model.add(Dropout(0.1))

    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(1, activation='softmax'))

    model.compile(
        loss='categorical_crossentropy',
        optimizer='rmsprop',
        metrics=['accuracy'])

    model.fit(x_train,y_train, epochs= 15)

    return model

Whenever I run the code I get this error : 每当我运行代码时,都会出现此错误:

ValueError: Input 0 is incompatible with layer lstm_10: expected ndim=3, found ndim=4 ValueError:输入0与层lstm_10不兼容:预期ndim = 3,找到的ndim = 4

My question is what I am missing here? 我的问题是我在这里想念的是什么?

PS: The idea of the project is that I have 13 signals coming from the 13 points of the human body, I want to use them to detect a certain type of diseases (an arousal). PS:该项目的想法是,我有13个来自人体13个点的信号,我想用它们来检测某种类型的疾病(一种唤醒)。 By using the LSTM, I want my model to locate the regions where I have that arousal based on these 13 signals. 通过使用LSTM,我希望我的模型根据这13个信号来定位具有唤醒功能的区域。

.

The whole data is 993 patients, for each one I use 13 signals to detect the disorder regions. 整个数据为993位患者,每位患者我都使用13个信号来检测疾病区域。

if you want me to put the data in 3D dimensions: 如果要我将数据放入3D尺寸中:

(500000 ,13, 993) # (nb_recods, nb_signals, nb_patient) (500000 ,13, 993) 500000,13,993 (500000 ,13, 993) #(nb_recods,nb_signals,nb_ Patient)

for each patient I have 500000 observations of 13 signals. 对于每个患者,我对13个信号有500000次观察。 nb_patient is 993 nb_ Patient是993

It worth noting that the 500000 size doesn't matter ! 值得注意的是,500000大小无关紧要! as i can have patients with more observations or less than that. 因为我可以让患者得到更多或更少的观察结果。

Update: here is a sample data of one patient. 更新:这是一名患者的样本数据。

Here is a chunk of my data first 2000 rows 这是我的数据前2000行的一大块

You may try some modifications like this below: 您可以尝试以下修改:

x_train = x_train.reshape(1999, 1, 13)
# double-check dimensions
x_train.shape

def lstm_baseline(x_train, y_train, batch_size):
    model = Sequential()
    model.add(LSTM(batch_size, input_shape=(None, 13),
                   activation='relu', return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(128, activation='relu'))
    model.add(Dropout(0.1))

    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(1, activation='softmax'))

    model.compile(
        loss='binary_crossentropy',
        optimizer='adam',
        metrics=['accuracy'])

    return model    

Ok, I did some changes to your code. 好的,我对您的代码做了一些更改。 First, I still don't now what the "200" in your attempt to reshape your data means, so I'm gonna give you a working code and let's see if you can use it or you can modify it to make your code work. 首先,我现在仍然不了解尝试重塑数据时使用“ 200”表示什么,因此,我将为您提供一个有效的代码,让我们看看是否可以使用它或可以对其进行修改以使代码正常工作。 The size of your input data and your targets, have to match. 输入数据的大小和目标必须匹配。 You can not have an input x_train with 27615 rows (which is the meaning of x_train[0] = 27615) and a target set y_train with 5523000 values. 输入x_train的行数为27615(x_train [0] = 27615的含义),目标集y_train的值为5523000。

I took the first two rows from the data example that you provided for this example: 我从您为该示例提供的数据示例中获取了前两行:

x_sample = [[-17,  -7, -7,  0, -5, -18, 73, 9, -282, 28550, 67],
            [-21, -16, -7, -6, -8,  15, 60, 6, -239, 28550, 94]]

y_sample = [0, 0]

Let's reshape x_sample : 让我们重塑x_sample

x_train = np.array(example)

#Here x_train.shape = (2,11), we want to reshape it to (2,11,1) to
#fit the network's input dimension
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)

You are using a categorical loss, so you have to change your targets to categorical (chek https://keras.io/utils/ ) 您正在使用分类损失,因此必须将目标更改为分类(检查https://keras.io/utils/

y_train = np.array(target)
y_train = to_categorical(y_train, 2)

Now you have two categories, I assumed two categories as in the data that you provided all the targets values are 0, so I don't know how many possible values your target can take. 现在您有两个类别,在您提供的所有目标值均为0的数据中,我假设有两个类别,所以我不知道目标可以采用多少个可能的值。 If your target can take 4 possible values, then the number of categories in the to_categorical function will be 4. Every output of your last dense layer will represent a category and the value of that output, the probability of your input to belong to that category. 如果目标可以取4个可能的值,则to_categorical函数中的类别数将为4。最后一个密集层的每个输出将代表一个类别,并且该输出的值即您的输入属于该类别的概率。

Now, we just have to slightly modify your LSTM model: 现在,我们只需要稍微修改您的LSTM模型即可:

def lstm_baseline(x_train, y_train):
   batch_size = 200
   model = Sequential()
   #We are gonna change input shape for input_dim
   model.add(LSTM(batch_size, input_dim=1,
                  activation='relu', return_sequences=True))
   model.add(Dropout(0.2))

   model.add(LSTM(128, activation='relu'))
   model.add(Dropout(0.1))

   model.add(Dense(32, activation='relu'))
   model.add(Dropout(0.2))

   #We are gonna set the number of outputs to 2, to match with the
   #number of categories
   model.add(Dense(2, activation='softmax'))

   model.compile(
       loss='categorical_crossentropy',
       optimizer='rmsprop',
       metrics=['accuracy'])

   model.fit(x_train, y_train, epochs=15)

return model

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

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