lstm模型二分类代码
时间: 2023-09-02 07:08:51 浏览: 111
以下是一个使用LSTM模型进行二分类的示例代码:
```python
import numpy as np
import tensorflow as tf
# 加载数据
X_train = ... # shape: (num_samples, sequence_length, input_dim)
y_train = ... # shape: (num_samples, 1)
# 创建LSTM模型
model = tf.keras.Sequential([
tf.keras.layers.LSTM(units=64, input_shape=(X_train.shape[1], X_train.shape[2])),
tf.keras.layers.Dense(units=1, activation='sigmoid')
])
# 编译模型
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=32)
# 预测新数据
X_test = ... # shape: (num_samples, sequence_length, input_dim)
y_pred = model.predict(X_test)
# 将预测结果转换为二分类结果
y_pred_binary = np.round(y_pred).astype(int)
```
阅读全文