import numpy as np
from keras.models import Model
from keras.layers import Input, LSTM, Dense, Permute, Multiply, Activation, Lambda
from keras import backend as K
# 生成模拟数据
def generate_data(n_samples, n_timesteps, n_features):
X = np.random.randn(n_samples, n_timesteps, n_features)
y = np.random.randint(0, 2, size=(n_samples,))
return X, y
# 构建模型
def build_model(n_timesteps, n_features):
inputs = Input(shape=(n_timesteps, n_features))
lstm_out = LSTM(32, return_sequences=True)(inputs)
# Attention层
attention = Dense(1, activation='tanh')(lstm_out)
attention = Permute([2, 1])(attention)
attention = Activation('softmax')(attention)
attention = Multiply()([lstm_out, attention])
attention = Lambda(lambda x: K.sum(x, axis=1))(attention)
outputs = Dense(1, activation='sigmoid')(attention)
model = Model(inputs=inputs, outputs=outputs)
return model
# 生成数据
n_samples = 1000
n_timesteps = 10
n_features = 5
X_train, y_train = generate_data(n_samples, n_timesteps, n_features)
X_test, y_test = generate_data(n_samples // 5, n_timesteps, n_features)
# 构建模型
model = build_model(n_timesteps, n_features)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=64, validation_split=0.2)
# 评估模型
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Loss:", loss)
print("Test Accuracy:", accuracy)
# 模型预测
predictions = model.predict(X_test)
keras 搭建lstm+attention 生成模拟数据模型训练预测
最新推荐文章于 2024-09-03 07:15:52 发布
本文介绍了如何使用Keras库构建一个包含LSTM层和注意力机制的神经网络模型,用于二分类问题,通过生成模拟数据进行训练和评估,展示了模型的性能和预测能力。
摘要由CSDN通过智能技术生成