import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from keras.layers import merge
from keras.layers.core import *
from keras.layers.recurrent import LSTM
from keras.models import *
'''
获得数据集、attention_column代表我们希望被注意的列
这个数据集是我们人为创建的,目的是为了演示注意力机制,示例如下:
X = [[-21.03816538 1.4249185 ]
[ 3.76040424 -12.83660875]
[ 1. 1. ]
[-10.17242648 5.37333323]
[ 2.97058584 -9.31965078]
[ 3.69295417 8.47650258]
[ -6.91492102 11.00583167]
[ -0.03511656 -1.71475966]
[ 10.9554255 12.47562052]
[ -5.70470182 4.70055424]]
Y = [1]
我们可以看到,当我们将attention_column设置为2的时候
第2个step的输入和当前batch的输出相同,其它step的值是随机设定的
因此网络应该需要去注意第2个step的输入,这就是我们希望他注意的情况。
'''
def get_data_recurrent(n, time_steps, input_dim, attention_column=2):
x = np.random.normal(loc=0, scale=10, size=(n, time_steps, input_dim))
y = np.random.randint(low=0, high=2, size=(n, 1))
x[:, attention_column, :] = np.tile(y[:], (1, input_dim))
return x, y
#-------------------------------------------#
# 对每一个step的注意力权值
#-------------------------------------------#
def get_activations(model, inputs, layer_name=None):
inp = model.input
for layer in model.layers:
if layer.name == layer_name:
Y = layer.output
model = Model(inp,Y)
out = model.predict(inputs)
out = np.mean(out[0],axis=-1)
return out
#------------------------------------------------------------------------------------------------------#
# 注意力模块,主要是实现对step维度的注意力机制
# 在这里大家可能会疑惑,为什么需要先Permute再进行注意力机制的施加。
# 这是因为,如果我们直接进行全连接的话,我们的最后一维是特征维度,这个时候,我们每个step的特征是分开的,
# 此时进行全连接的话,得出来注意力权值每一个step之间是不存在特征交换的,自然也就不准确了。
# 所以在这里我们需要首先将step维度转到最后一维,然后再进行全连接,根据每一个step的特征获得注意力机制的权值。
#------------------------------------------------------------------------------------------------------#
def attention_3d_block(inputs):
# batch_size, time_steps, lstm_units -> batch_size, lstm_units, time_steps
a = Permute((2, 1))(inputs)
# batch_size, lstm_units, time_steps -> batch_size, lstm_units, time_steps
a = Dense(TIME_STEPS, activation='softmax')(a)
# batch_size, lstm_units, time_steps -> batch_size, time_steps, lstm_units
a_probs = Permute((2, 1), name='attention_vec')(a)
# 相当于获得每一个step中,每个特征的权重
output_attention_mul = merge([inputs, a_probs], name='attention_mul', mode='mul')
return output_attention_mul
#-------------------------------------------#
# 建立注意力模型
#-------------------------------------------#
def get_attention_model(time_steps, input_dim, lstm_units = 32):
inputs = Input(shape=(time_steps, input_dim,))
# (batch_size, time_steps, input_dim) -> (batch_size, input_dim, lstm_units)
lstm_out = LSTM(lstm_units, return_sequences=True)(inputs)
attention_mul = attention_3d_block(lstm_out)
# (batch_size, input_dim, lstm_units) -> (batch_size, input_dim*lstm_units)
attention_mul = Flatten()(attention_mul)
output = Dense(1, activation='sigmoid')(attention_mul)
model = Model(input=[inputs], output=output)
return model
if __name__ == '__main__':
N = 100000
INPUT_DIM = 2
TIME_STEPS = 10
#------------------------------------------------------#
# 每一个输入样本的step为10,每一个step的数据长度为2
# X - batch, 10, 2
# Y - batch, 1
#------------------------------------------------------#
X, Y = get_data_recurrent(N, TIME_STEPS, INPUT_DIM)
#------------------------------------------------------#
# 获得模型并进行训练。
#------------------------------------------------------#
model = get_attention_model(TIME_STEPS, INPUT_DIM)
model.summary()
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X, Y, epochs=5, batch_size=64, validation_split=0.1)
attention_vectors = []
#------------------------------------------------------#
# 取三百个样本,将他们通道的平均注意力情况取出来
#------------------------------------------------------#
for i in range(300):
testing_X, testing_Y = get_data_recurrent(1, TIME_STEPS, INPUT_DIM)
attention_vector = get_activations(model,testing_X,layer_name='attention_vec')
print('attention =', attention_vector)
assert (np.sum(attention_vector) - 1.0) < 1e-5
attention_vectors.append(attention_vector)
attention_vector_final = np.mean(np.array(attention_vectors), axis=0)
#------------------------------------------------------#
# 将结果绘制成图
#------------------------------------------------------#
pd.DataFrame(attention_vector_final, columns=['attention (%)']).plot(kind='bar',title='Attention Mechanism as a function of input dimensions.')
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
主要包含了LSTM、卷积神经网络中,注意力机制的实现
共2个文件
py:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 160 浏览量
2024-02-06
11:38:24
上传
评论
收藏 4KB ZIP 举报
温馨提示
主要包含了LSTM、卷积神经网络中,注意力机制的实现 目录 所需环境 Environment LSTM中的注意力机制 Conv中的注意力机制 所需环境 tensorflow-gpu==1.13.1 keras==2.1.5 LSTM中的注意力机制 在本库中,我将注意力机制施加在LSTM的Step上,目的是注意输入进来的样本,每一个Step的重要程度。我们使用的样本数据如下:
资源推荐
资源详情
资源评论
收起资源包目录
Keras-Attention-master.zip (2个子文件)
Keras-Attention-master
Attention_in_conv.py 5KB
Attention_in_LSTM.py 5KB
共 2 条
- 1
资源评论
博士僧小星
- 粉丝: 1760
- 资源: 5875
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 自动驾驶定位系列教程十:闭环修正.pdf
- HM2333-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- Python实现插入排序算法(源代码)
- 123.cpp
- HM2319-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- modbus4j-3.0.4.jar
- 蒙特·卡罗实验、使用蒙特·卡罗方法计算圆周率近似值.docx
- HM2319A-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- JAVA SpringBoot 集成华为云OBS,多镜像配置settings
- 一个文件共享系统,包括前端文件展示系统和后台管理系统,基于SpringBoot + MyBatis实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功