cba CNN-LSTM-Attention模型

文章展示了如何利用TensorFlow的Keras库建立一个包含卷积层、MaxPooling、Dropout、双向LSTM以及Attention机制的深度学习模型。模型主要用于序列数据的处理,通过LSTM学习时间序列特征,并用Attention机制增强模型对关键信息的捕捉能力。
部署运行你感兴趣的模型镜像

_________________________________________________________________

from tensorflow.keras.layers import *
from tensorflow.keras.models import *

window = 5
input_size = 10
lstm_units = 16
dropout = 0.01
#建立LSTM模型 训练
inputs=Input(shape=(window, input_size))
model=Conv1D(filters = lstm_units, kernel_size = 1, activation = 'sigmoid')(inputs)#卷积层
model=MaxPooling1D(pool_size = window)(model)#池化层
model=Dropout(dropout)(model)#droupout层
model=Bidirectional(LSTM(lstm_units, activation='tanh'), name='bilstm')(model)#双向LSTM层
attention=Dense(lstm_units*2, activation='sigmoid', name='attention_vec')(model)#求解Attention权重
model=Multiply()([model, attention])#attention与LSTM对应数值相乘
outputs = Dense(1, activation='tanh')(model)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='mse',optimizer='adam',metrics=['accuracy'])
model.summary()#展示模型结构

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

input_1 (InputLayer) [(None, 20, 13)] 0

_________________________________________________________________

conv1d (Conv1D) (None, 20, 64) 896

_________________________________________________________________

dropout (Dropout) (None, 20, 64) 0

_________________________________________________________________

bidirectional (Bidirectional (None, 20, 128) 66048

_________________________________________________________________

dropout_1 (Dropout) (None, 20, 128) 0

_________________________________________________________________

attention_vec (Dense) (None, 20, 128) 16512

_________________________________________________________________

flatten (Flatten) (None, 2560) 0

_________________________________________________________________

dense (Dense) (None, 1) 2561

=================================================================

Total params: 86,017

Trainable params: 86,017

Non-trainable params: 0

_________________________________________________________________

定义函数的方法

from tensorflow.keras.layers import *
from tensorflow.keras.models import *


# def attention_model(INPUT_DIMS = 13,TIME_STEPS = 20,lstm_units = 64):
INPUT_DIMS = 13
TIME_STEPS = 20
lstm_units = 64
inputs = Input(shape=(TIME_STEPS, INPUT_DIMS))

x = Conv1D(filters=64, kernel_size=1, activation='relu')(inputs)  # padding = 'same'
x = Dropout(0.3)(x)

# lstm_out = Bidirectional(LSTM(lstm_units, activation='relu'), name='bilstm')(x)
lstm_out = Bidirectional(LSTM(lstm_units, return_sequences=True))(x)
lstm_out = Dropout(0.3)(lstm_out)
# attention_mul = attention_3d_block(lstm_out)
attention_mul = Dense(lstm_units * 2, activation='sigmoid', name='attention_vec')(lstm_out)
attention_mul = Flatten()(attention_mul)

output = Dense(1, activation='sigmoid')(attention_mul)
model = Model(inputs=[inputs], outputs=output)
# return model
model.summary()  # 展示模型结构

Layer (type) Output Shape Param #

=================================================================

input_1 (InputLayer) [(None, 20, 13)] 0

_________________________________________________________________

conv1d (Conv1D) (None, 20, 64) 896

_________________________________________________________________

dropout (Dropout) (None, 20, 64) 0

_________________________________________________________________

bidirectional (Bidirectional (None, 20, 128) 66048

_________________________________________________________________

dropout_1 (Dropout) (None, 20, 128) 0

_________________________________________________________________

attention_vec (Dense) (None, 20, 128) 16512

_________________________________________________________________

flatten (Flatten) (None, 2560) 0

_________________________________________________________________

dense (Dense) (None, 1) 2561

=================================================================

Total params: 86,017

Trainable params: 86,017

Non-trainable params: 0

_________________________________________________________________

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值