cnn-lstm预测模型代码
时间: 2023-09-28 20:09:26 浏览: 129
Sure! Here's an example code for a CNN-LSTM prediction model using Keras with TensorFlow backend:
```python
from keras.models import Sequential
from keras.layers import Conv1D, LSTM, Dense, Flatten
# Define the model
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(timesteps, input_dim)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(LSTM(128, return_sequences=True))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=64)
# Make predictions
predictions = model.predict(X_test)
```
In this code, we first import the necessary libraries. Then we define the model architecture using `Sequential` model from Keras. The model starts with a 1D convolutional layer (`Conv1D`) followed by a LSTM layer (`LSTM`). We then flatten the output of the LSTM layer and add a dense layer with sigmoid activation for binary classification.
After defining the model architecture, we compile it by specifying the optimizer, loss function, and metrics. Next, we train the model using the `fit` function, providing the training data, validation data, number of epochs, and batch size.
Finally, we can use the trained model to make predictions by calling the `predict` function on the test data.
Please note that this is just a basic example and you may need to modify it based on your specific requirements and data.
阅读全文