1 什么是注意力机制
当我们看一张图片时,我们会直接聚焦到我们最关心的事情上,比如我们只会看到兔子,而忽略了兔子嘴里的草和蓝天白云。注意力机制是你模仿人脑自动捕捉最重要的信息的能力,这样模型就可以更好地学习关键内容。
[En]
When we look at a picture, we will directly focus on what we are most concerned about, for example, we will only see the rabbit and ignore the grass in the rabbit's mouth and the blue sky and white clouds. The attention mechanism is your ability to imitate the human brain to automatically capture the most important information, so that the model can learn the key content better.
注意力机制适合位置信息和时序信息,和CNN和RNN结合使用是典型的方法,如上图中人的眼睛和耳朵。
下面从encoder-decoder架构角度解释注意力机制:
上图显示了传统encoder-decoder模型的计算方式,encoder输出全部的语义信息c,然后作为输入,decoder计算由c推出的目标信息。decoder模型将注意力放在全部语义信息中,这有时是不合适的。所以引入注意力机制。
下图显示了添加注意力机制后的encoder-decoder模型,输入x会返回注意力权重c,每个c_i记录了要预测的单词y_i在原信息x中的重要性。那么,理解了注意力机制后,就是考虑如何计算c,这也是最不容易理解的部分。
2 注意力机制计算方式
下图就明确的表示了注意力权重的计算方式。
要计算的是某个单词在原句子中和哪些单词相关度高,即计算目标单词的注意力概率分布,例如X=(我是中国人,我爱中国),要翻译成y=(i am chinese, i love china),china对应的概率分布可能是(0.1, 0.001, 0.5, 0.5, 0.2, 0.01, 0.2, 0.5, 0.5),再经过归一化后和原句子做加权求和。
下图中,Query表示要查询的目标单词的位置,暂时这么理解,不同模型Query取值不同;key是输入的原句子;a表示每个目标单词的注意力概率分布。
下面以自编码器为例,详细讲解包括数据流维度在内的关注值的计算,需要一点耐心!
[En]
The following self-encoder as an example, a detailed explanation of the calculation of attention values, including the dimensions of the data stream, requires a little patience!
RNN-based encoder-decoder framework,目标是预测P(y_1, y_2, ..., y_i | x_1, x_2, ..., x_i),x.shape=(b, sentence_x_len, word_x_len),y.shape=(b, sentence_y_len, word_y_len)。训练阶段结构图如下所示。
encoder:
单词序列经过embedding层得到分布式向量表达,由RNN计算得出序列的特征,送到decoder翻译。
h_t = f(W@x_t, h_t-1) 单词序列对应的状态,最后一个h记录了全部状态,shape(b, sentence_x_len, units_encoder);
c = q({h_i}) q为计算概率分布的函数,上下文向量,最简单的是最后一个h,可以是点积、网络...,shape(b, units_encoder);
decoder:
在获得h、上下文向量c和已经预测出的输出y_t-1的条件下,预测下个输出y_t,即条件概率分布。
P(y) = g(y_t | {y_1, y_2, ..., y_t-1}, c, h) g为计算函数,一般为全连接层加softmax,y-i(b, units_decoder)是由RNN得出,即图中的W。
到此非注意力自编码器介绍完毕。下面以RNN-based encoder-decoder with attention framework为模型,解释注意力概率分布的计算过程:
不同之处就在于c的计算方式,不是简单的取最后一个h,而是计算不同目标单词在原句中的重要性概率分布,即注意力概率分布attention vector(b, units_y)。
encoder:
单词序列经过BiLSTM提取特征后全部送到Attention模块中。
inputs:x(b, sentence_len),embedding后为:(b, sentence_len, word_len)
outputs: y(), h(b, sentence_len, units_encoder)
decoder:
输入是将注意力和前一时刻的隐藏状态相加后的语境向量,然后作为目标词输出。
[En]
The input is the context vector after adding attention and the hidden state of the previous moment, and then output as the target word.
inputs:y_t-1(b, sentence_len), context(b, units_encoder)
outputs: s(b, units_decoder)
attention:
inputs:[s_t-1, h]
outputs: [context_t]
计算公式:context_t = h * softmax(f(s_t-1, h))
3 attention调用
3.1 Attention()
Attention模块参数:
use_scale,取值True或者False,它表示是否对计算出来的得分(score)进行权重缩放,如果是True,进行权重缩放,如果False,不进行缩放,默认值是False。通常也使用默认,因为它只是在最后计算出score是乘一个scale。
inputs:
query_seq_decoding(batch_size, Tq, dim),
value_seq_encoding(batch_size, Tv, dim),
key(batch_size, Tv, dim),可选的,如果没有给定,则默认key=value
outputs:
[batch_size, Tq, dim]
计算过程:
可知Attention是非常简单的,没有参数要训练,但是可以自定义分数scores的计算方式,如神经网络。
encoder_out = tf.random.normal([50, 80, 30])
decoder_out = tf.random.normal([50, 1, 30])
attention = layers.Attention()
y = attention([decoder_out, encoder_out])
print(y) # (50, 1, 30)
3.2 自定义attention
attention模块中分数计算部分可以有不同方式,下面实现一种矩阵参数的。
TensorFlow2.0中自定义层,要点如下:
1、如果需要使用到其他Layer结构或者Sequential结构,需要在init()函数里赋值;2、在build()里面根据输入shape构建权重参数, 每个参数需要赋值name,如果参数没有name,当训练到第2个epoch时会报错:AttributeError: 'NoneType' object has no attribute 'replace';3、在call()里写计算逻辑。
class AttentionConcat(layers.Layer):
def __init__(self, bias=True):
"""
scores = query @ W @ key^T => shape(b, Tq, Tv)
W = (dim, dim)
result = scores @ value + bias
# Input shape
[query(batch_size, Tq, dim),
value(batch_size, Tv, dim),
key(batch_size, Tv, dim)]
# Output shape
(batch_size, Tq, dim)
:param bias: shape(b, Tq, dim)
"""
super(AttentionConcat, self).__init__()
self.bias = bias
def build(self, input_shape):
"""
:param input_shape:
:return:
"""
self.batch = input_shape[0][0]
self.Tq = input_shape[0][1]
self.Tv = input_shape[1][1]
self.dim = input_shape[0][-1]
self.W = self.add_weight(
name='{}_W'.format(self.name),
shape=(self.batch, self.dim, self.dim),
initializer='one',
trainable=True
)
if self.bias:
self.b = self.add_weight(
# name='{}_b'.format(self.name),
shape=(self.batch, self.Tq, self.dim),
initializer='zero',
trainable=True
)
else:
self.b = None
def call(self, inputs, **kwargs):
# (
scores = inputs[0] @ self.W @ tf.transpose(inputs[2], perm=[0, 2, 1])
scores_flat = tf.reshape(scores, shape=[-1, self.Tq * self.Tv])
attention_flat = tf.nn.softmax(scores_flat)
attention = tf.reshape(attention_flat, shape=[-1, self.Tq, self.Tv])
# (N, step, d) (N, step, 1) ====> (N, step, d)
result = attention @ inputs[1]
if self.bias:
result += self.bias
return result
Original: https://blog.csdn.net/weixin_67463124/article/details/123856684
Author: 三叶草~
Title: tensorflow调用并实现注意力机制
相关阅读
Title: 用Anaconda安装TensorFlow(Windows10)
用Anaconda安装TensorFlow
本部分分为方法一和方法二,方法一是从清华镜像官网下载速度较快,
方法二是从GitHub下载,速度较慢(有梯子的建议使用)
1.打开Anaconda Prompt
2.输入下面两行命令,打开清华镜像官网
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
3.输入下面命令,利用Anaconda创建一个python3.9的环境,环境名称为tensorflow(这里python版本根据自己的情况如下图)
conda create -n tensorflow python=3.9
如图所示:
4.输入y
5.打开Anaconda Navigator,点击Environments,可以看到名称为tensorflow的环境已经创建好
6.上一步最后我们可以看到,启动Tensorflow,使用下面的命令行:
conda activate tensorflow
关闭tensorflow环境,使用下面的命令行:
7.我们在这里输入下面的命令,进入tensorflow环境
conda activate tensorflow
8.输入下面的命令行,安装cpu版本的tensorflow
conda install tensorflow
此时我们再次查看Anaconda Navigator中的Environments,可以发现tensorflow已经安装好了
第8步注意!!!
如果出现下图情况或者是从GitHub上下载速度极慢,说明清华镜像网址没有切换成功
重新执行步骤二即可
TensorFlow安装方法方法二
方法二速度特别特别慢 (如果有梯子的话建议使用)
1.安装好Anaconda后,在系统栏双击打开Anaconda Navigator
2.点击Environment,Create
3.创建一个tensorflow的新环境,python在这里可以自行选择(根据Anaconda中安装的python版本,我这里是python3.8),然后点击Create
4.右边搜索栏输入tensorflow,选择后点击Apply
5.点击Apply
等待安装完成即可
Original: https://blog.csdn.net/m0_46299185/article/details/124280787
Author: bEstow--
Title: 用Anaconda安装TensorFlow(Windows10)

MATLAB与ROS通信:使用自定义ROS消息custom ros message

K-Means不同含量果汁饮料的聚类(聚类算法)

tensorflow2实现yolov3并使用opencv4.5.5 DNN加载模型预测

(保姆教程)Spyder 配置Tensorflow(2.5.0)和keras(2.4.3)

使用tensorflow出现的错误及其解决方法(numpy、opencv_contrib)

使用opencv分割图像(python实现)

TensorFlow—-Keras库

python3.6安装tensorflow库_Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)

TensorFlow实现梯度下降法求解一元和多元线性回归问题

高斯滤波简介

ubuntu18.04+语音识别

嵌入式习题2

tensorflow笔记(10) 自制数据集Generate(上)

基于Tensorflow实现一个Transformer翻译器
