简体   繁体   English

如何在时间序列分类中结合 LSTM 和 CNN

[英]How to combine LSTM and CNN in timeseries classification

Most commonly CNN is used when there are images as data.当有图像作为数据时,最常使用 CNN。 However, I have seen that CNN are sometines used for timeseries.但是,我已经看到 CNN 是用于时间序列的 sometines。 Therefore, I tried both LSTM and CNN models seperately for my timeseries classification problem.因此,我针对我的时间序列分类问题分别尝试了 LSTM 和 CNN 模型。 My two models are as follows.我的两个模型如下。

LSTM: LSTM:

model = Sequential()
model.add(LSTM(200, input_shape=(25,3)))
model.add(Dense(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

CNN:美国有线电视新闻网:

model = Sequential()
model.add(Conv1D(200, kernel_size=3, input_shape=(25,3)))
model.add(Conv1D(200, kernel_size=2))
model.add(GlobalMaxPooling1D())
model.add(Dense(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

I think LSTM and CNN has there unique characteristics and combining these two in my prediction will produce better results.我认为 LSTM 和 CNN 有其独特的特点,在我的预测中将这两者结合起来会产生更好的结果。 However, I am struggling to find a suitable resource that suits my problem.但是,我正在努力寻找适合我的问题的合适资源。

Is it possible to do this for my problem?是否可以针对我的问题执行此操作? If so how I can do it?如果是这样,我该怎么做? Will it produce better results?它会产生更好的结果吗?

I am happy to provide more details if needed.如果需要,我很乐意提供更多详细信息。

EDIT:编辑:

My problem setting is as follows.我的问题设置如下。 I have a dataset with about 5000 data points.我有一个包含大约 5000 个数据点的数据集。 Each data point has 3 time-series data that are exactly 25 in size.每个数据点有 3 个大小正好为 25 的时间序列数据。 My labeled data is 1 or 0 (ie binary classification).我的标记数据是10 (即二元分类)。 More specifically my dataset looks as follows.更具体地说,我的数据集如下所示。

node, time-series1, time_series2, time_series3, Label
n1, [1.2, 2.5, 3.7, 4.2, ... 5.6, 8.8], [6.2, 5.2, 4.7, 3.2, ... 2.6, 1.8], [1.0, 2.8, 3.9, 4.1, ... 5.2, 8.6] …, 1
n2, [5.2, 4.5, 3.7, 2.2, ... 1.6, 0.8], [8.2, 7.5, 6.7, 5.2, ... 4.6, 1.8], …, [1.2, 2.5, 3.7, 4.2, ... 5.2, 8.5] 0
and so on.

I input these data to my LSTM and CNN models.我将这些数据输入到我的 LSTM 和 CNN 模型中。

Have you tried to just put one layer after another?你有没有试过一层又一层地放? This sounds pretty standard...这听起来很标准...

model = Sequential()
model.add(Conv1D(200, kernel_size=3, activation = useSomething, input_shape=(25,3)))
model.add(LSTM(200))
model.add(Dense(100))
model.add(Dense(1, activation='sigmoid'))

Do you want to try the opposite?你想反其道而行之吗?

model = Sequential()
model.add(LSTM(200, return_sequences=True, input_shape=(25,3)))
model.add(Conv1D(200, kernel_size=3, activation = useSomething))
model.add(GlobalMaxPooling1D())
model.add(Dense(100))
model.add(Dense(1, activation='sigmoid'))

Do you want a huge model?你想要一个巨大的模型吗?

model = Sequential()
model.add(Conv1D(15, kernel_size=3, activation = useSomething, input_shape=(25,3)))
model.add(LSTM(30, return_sequences=True))
model.add(Conv1D(70, kernel_size=3, activation = useSomething))
............
model.add(LSTM(100))
model.add(Dense(100))
model.add(Dense(1, activation='sigmoid'))

Try many things:尝试很多事情:

  • Conv, LSTM转换,LSTM
  • LSTM, Conv LSTM,转换
  • Conv, Conv, .., Conv, LSTM, ..., LSTM Conv, Conv, .., Conv, LSTM, ..., LSTM
  • LSTM, LSTM, ..., Conv, Conv, .... LSTM, LSTM, ..., Conv, Conv, ....
  • C, L, C, L, C, L, .... C, L, C, L, C, L, ....
  • L, C, L, C, L, C, .... L, C, L, C, L, C, ....
  • C, C, L, L, C, C, .... C, C, L, L, C, C, ....

A two sided model?双面模型?

inputs = Input((25,3))

side1 = Bidirectional(LSTM(100, return_sequences=True))(inputs) #200 total units
side2 = Conv1D(200, activation = 'tanh', padding = 'same')(inputs) #same activation 
                                                                   #same length

merged = Add()([side1, side2]) 
     #or Concatenate()([side1, side2]) if different number of units/channels/features

outputs = Conv1D(200)(merged)
outputs = GlobalMaxPooling1D()(outputs)
outputs = Dense(100)(outputs)
outputs = Dense(1, activation='sigmoid')(outputs)

model = Model(inputs, outputs)

您可能想看看 LSTNet,它就是这样做的 - https://arxiv.org/abs/1703.07015https://github.com/laiguokun/LSTNet

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

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