Keras Conv1D处理不平衡时间序列分类时维度错误求助
Hey there! Let's break down what's going wrong with your model and how to fix it step by step.
错误根源:Conv1D输入与卷积核不匹配
The error you're seeing (Negative dimension size caused by subtracting 9 from 1) comes from a fundamental mismatch between your input's time step length and the Conv1D kernel size.
Conv1D expects input in the shape (batch_size, time_steps, features). When you call cnn_1d(1, X_train.shape[1]), you're setting window_size=1 (meaning each sample has 1 time step) and nb_input_series=9 (9 features per time step).
Your first Conv1D layer uses a kernel size of 9:
model.add(Conv1D(32, 9, activation='relu', input_shape=(window_size, nb_input_series)))
The output time steps after convolution are calculated as:time_steps - kernel_size + 1
Here that's 1 - 9 + 1 = -7 — which is mathematically impossible, hence the error.
你的两个问题解答
1. 是否需要使用Embedding层?
No, you don't need an Embedding layer. Embedding layers are designed for discrete, categorical input (like word indices in text processing). Your input is numerical time series features, so you can feed them directly into Conv1D once you adjust the shape correctly.
2. 是否需要对输入进行reshape操作?
Yes, but first you need to clarify your time sequence structure:
There are two common scenarios for your (90000, 9) input:
Scenario 1: Each sample is a time sequence of 9 steps, with 1 feature per step
In this case, reshape your input to(90000, 9, 1)(batch_size, time_steps, features), then call your model withwindow_size=9andnb_input_series=1:# Reshape the training data X_train_reshaped = X_train.reshape((X_train.shape[0], 9, 1)) # Initialize the model correctly model = cnn_1d(9, 1)Now your first Conv1D layer with kernel size 9 will process the entire 9-step sequence, resulting in 1 valid output time step (9-9+1=1).
Scenario 2: Each sample is 1 time step with 9 features
If this is your case, using a kernel size of 9 is invalid (you only have 1 time step to process). You'll need to:- Reduce the kernel size to 1 (so it operates on each feature independently at the single time step)
- Adjust or remove MaxPooling1D layers (pooling with size 2 on 1 time step will also cause dimension errors)
Here's a modified model for this scenario:
def cnn_1d(window_size, nb_input_series): model = Sequential() # Use kernel_size=1 for single time step input model.add(Conv1D(32, 1, activation='relu', input_shape=(window_size, nb_input_series))) model.add(Conv1D(32, 1, activation='relu')) # Remove MaxPooling1D or set pool_size=1 to avoid dimension issues model.add(Dropout(0.25)) model.add(Conv1D(64, 1, activation='relu')) model.add(Conv1D(64, 1, activation='relu')) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(50, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid'))Call it with your original input shape (no reshape needed):
model = cnn_1d(1, 9)
额外提示:不平衡分类任务优化
Since you're working on an imbalanced time series classification task, don't forget to:
- Use the
class_weightparameter inmodel.fit()to assign higher weights to the minority class - Evaluate your model using metrics like F1-score, AUC-ROC, or precision-recall curve instead of just accuracy (accuracy can be misleading for imbalanced data)
内容的提问来源于stack exchange,提问作者Rayadurai

