如果您希望为每个时间步预测一个值,我想到了两个略有不同的解决方案:
1)删除MaxPooling1D层,在Conv1D层中添加padding='same'参数,并在LSTM中添加return_sequence=True参数,以便LSTM返回每个时间步的输出:
from keras.layers import Input, Dense, LSTM, MaxPooling1D, Conv1D
from keras.models import Model
input_layer = Input(shape=(400, 16))
conv1 = Conv1D(filters=32,
kernel_size=8,
strides=1,
activation='relu',
padding='same')(input_layer)
lstm1 = LSTM(32, return_sequences=True)(conv1)
output_layer = Dense(1, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)
model.summary()
模型概要如下:
Layer (type) Output Shape Param
=================================================================
input_4 (InputLayer) (None, 400, 16) 0
_________________________________________________________________
conv1d_4 (Conv1D) (None, 400, 32) 4128
_________________________________________________________________
lstm_4 (LSTM) (None, 400, 32) 8320
_________________________________________________________________
dense_4 (Dense) (None, 400, 1) 33
=================================================================
Total params: 12,481
Trainable params: 12,481
Non-trainable params: 0
_________________________________________________________________
2) 仅需将 Dense 层中的 units 数量更改为 400,并将 y
重塑为 (n_samples, n_timesteps)
:
from keras.layers import Input, Dense, LSTM, MaxPooling1D, Conv1D
from keras.models import Model
input_layer = Input(shape=(400, 16))
conv1 = Conv1D(filters=32,
kernel_size=8,
strides=1,
activation='relu')(input_layer)
pool1 = MaxPooling1D(pool_size=4)(conv1)
lstm1 = LSTM(32)(pool1)
output_layer = Dense(400, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)
model.summary()
模型概要如下:
_________________________________________________________________
Layer (type) Output Shape Param
=================================================================
input_6 (InputLayer) (None, 400, 16) 0
_________________________________________________________________
conv1d_6 (Conv1D) (None, 393, 32) 4128
_________________________________________________________________
max_pooling1d_5 (MaxPooling1 (None, 98, 32) 0
_________________________________________________________________
lstm_6 (LSTM) (None, 32) 8320
_________________________________________________________________
dense_6 (Dense) (None, 400) 13200
=================================================================
Total params: 25,648
Trainable params: 25,648
Non-trainable params: 0
_________________________________________________________________
不要忘记,在这两种情况下,你必须使用'binary_crossentropy'(而不是'categorical_crossentropy')作为损失函数。我预计这种解决方案的准确率会低于解决方案#1;但你必须尝试两种方案并尝试更改参数,因为它完全取决于您尝试解决的具体问题和数据的性质。
更新:
你要求一个卷积层只包括一个时间步和k个相邻的特征。是的,你可以使用Conv2D层来实现。
# first add an axis to your data
X = np.expand_dims(X) # now X has a shape of (n_samples, n_timesteps, n_feats, 1)
# adjust input layer shape ...
conv2 = Conv2D(n_filters, (1, k), ...) # covers one timestep and k features
# adjust other layers according to the output of convolution layer...
虽然我不知道你为什么要这么做,但是如果要使用卷积层的输出(即
(?, n_timesteps, n_features, n_filters)
),一种解决方法是使用嵌套在TimeDistributed层中的LSTM层。或者另一种解决方法是将最后两个维度展平。
padding ='same'
。 - sgDysregulationTimeDistributed
,LSTM层的return_sequences
参数必须等于True
。即使这样做了,TimeDistributed(Dense(1))
仍然与Dense(1)
相同。 - today