I would like to use 1D-Conv layer following by LSTM layer to classify a 16-channel 400-timestep signal.
The input shape is composed of:
X = (n_samples, n_timesteps, n_features)
, wheren_samples=476
,n_timesteps=400
,n_features=16
are the number of samples, timesteps, and features (or channels) of the signal.y = (n_samples, n_timesteps, 1)
. Each timestep is labeled by either 0 or 1 (binary classification).
I use the 1D-Conv to extract the temporal information, as shown in the figure below. F=32
and K=8
are the filters and kernel_size. 1D-MaxPooling is used after 1D-Conv. 32-unit LSTM is used for signal classification. The model should return a y_pred = (n_samples, n_timesteps, 1)
.
The code snippet is shown as follow:
input_layer = Input(shape=(dataset.n_timestep, dataset.n_feature))
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(1, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)
The model summary is shown below:
However, I got the following error:
ValueError: Error when checking target: expected dense_15 to have 2 dimensions, but got array with shape (476, 400, 1).
I guess the problem was the incorrect shape. Please let me know how to fix it.
Another question is the number of timesteps. Because the input_shape
is assigned in the 1D-Conv, how can we let LSTM know the timestep must be 400?
I would like to add the model graph based on the suggestion of @today. In this case, the timestep of LSTM will be 98. Do we need to use TimeDistributed in this case? I failed to apply the TimeDistributed in the Conv1D.
Is there anyway to perform the convolution among channels, instead of timesteps? For example, a filter (2, 1) traverses each timestep, as shown in figure below.
Thanks.
padding='same'
– sgDysregulationTimeDistributed
thereturn_sequences
argument of LSTM layer must be equal toTrue
. Even after doing thisTimeDistributed(Dense(1))
is the same asDense(1)
. – today