ATTENTION NETWORK分析
1. TensorFlowTrainable类
1 class TensorFlowTrainable(object):
2 def __init__(self):
3 self.parameters = []
4
5 def get_weights(self, dim_in, dim_out, name, trainable=True):
6 shape = (dim_out, dim_in)
7 weightsInitializer = tf.constant_initializer(
8 self.truncated_normal(shape=shape, stddev=0.01, mean=0.))
9 weights = tf.get_variable(
10 initializer=weightsInitializer, shape=shape, trainable=True, name=name)
11 if trainable:
12 self.parameters.append(weights)
13 return weights
14 def get_4Dweights(self, filter_height, filter_width, in_channels, out_channels, name, trainable=True):
15 shape = (filter_height, filter_width, in_channels, out_channels)
16 weightsInitializer = tf.constant_initializer(
17 self.truncated_normal(shape=shape, stddev=0.01, mean=0))
18 weights = tf.get_variable(
19 initializer=weightsInitializer, shape=shape, trainable=True, name=name)
20 if trainable:
21 self.parameters.append(weights)
22 return weights
23 def get_biases(self, dim_out, name, trainable=True):
24 shape = (dim_out, 1)
25 initialBiases = tf.constant_initializer(np.zeros(shape))
26 biases = tf.get_variable(
27 initializer=initialBiases, shape=shape, trainable=True, name=name)
28 if trainable:
29 self.parameters.append(biases)
30 return biases
31 @staticmethod
32 def truncated_normal(shape, stddev, mean=0.):
33 rand_init = np.random.normal(loc=mean, scale=stddev, size=shape)
34 inf_mask = rand_init < (mean - 2 * stddev)
35 rand_init = rand_init * \
36 np.abs(1 - inf_mask) + inf_mask * (mean - 2 * stddev)
37 sup_mask = rand_init > (mean + 2 * stddev)
38 rand_init = rand_init * \
39 np.abs(1 - sup_mask) + sup_mask * (mean + 2 * stddev)
40 return rand_init
@staticmethod
静态方法,类可以不用实例化就可以调用该方法,当然也可以实例化后调用。
所以要注意这里前面几个函数用到的self.truncated_normal()并不是一开始我以为的tf.truncated_normal()这个正态分布函数(我就奇怪为什么是self.而不是tf.,名字一样的0.0)。
那么这个函数传入参数为shape和stddev,形状和标准差。返回一个形状为shape的截断正态分布数组。
其余函数,get_weights是得到shape=(dim_out, dim_in)的截断正太分布权重,get_4Dweights是得到shape=(filter_height, filter_width, in_channels, out_channels)的截断正态分布权重,get_biases是得到shape=(dim_out, 1)的初始零向量偏置。
2. LSTMCell类
class LSTMCell(TensorFlowTrainable):
def __init__(self, num_units, **kwargs):
super(LSTMCell, self).__init__()
self._num_units = num_units # 单元的个数
self.w_i = self.get_weights(
dim_in=2 * self._num_units, dim_out=self._num_units, name="w_i") # 输入门权重
self.w_f = self.get_weights(dim_in=2 * self._num_units, dim_out=self._num_units, name="w_f") # 忘记门权重
self.w_o = self.get_weights(dim_in=2 * self._num_units, dim_out=self._num_units, name="w_o") # 输出门权重
self.w_c = self.get_weights(dim_in=2 * self._num_units, dim_out=self._num_units, name="w_c") # 数据输入权重
self.b_i = self.get_biases(dim_out=self._num_units, name="b_i") # 输入门偏重
self.b_f = self.get_biases(dim_out=self._num_units, name="b_f") # 忘记门偏重
self.b_o = self.get_biases(dim_out=self._num_units, name="b_o") # 输出门偏重
self.b_c = self.get_biases(dim_out=self._num_units, name="b_c") # 数据输入偏重
self.c = [self.get_biases(dim_out=self._num_units, name="c", trainable=False)] # 记忆细胞状态偏重
def initialize_something(self, input):
# 对输入做一定的变换,包括转置、展开、扩展为度等,并把数值初始化为1
self.batch_size_vector = 1 + 0 * tf.expand_dims(tf.unstack(tf.transpose(input, [1, 0]))[0], 0)
# 初始化
self.h = [self.get_biases(dim_out=self._num_units, name="h", trainable=False) * self.batch_size_vector] def process(self, input, **kwargs):
H = tf.concat([tf.transpose(input, perm=[1, 0]),self.h[-1]], 0) # 将输入数据与上一时刻的记忆信息整合成一个新的输入
i = tf.sigmoid(x=tf.add(tf.matmul(self.w_i, H), self.b_i)) # 经过输入门后的数据
f = tf.sigmoid(x=tf.add(tf.matmul(self.w_f, H), self.b_f)) # 经过忘记门后的数据
o = tf.sigmoid(x=tf.add(tf.matmul(self.w_o, H), self.b_o)) # 经过输出门后的数据
c = f * self.c[-1] + i * tf.tanh(x=tf.add(tf.matmul(self.w_c, H), self.b_c))
# 原代码:h = o * tf.tanh(x=self.c[-1])
h = o * tf.tanh(x=self.c[-1])
self.c.append(c)
self.h.append(h) @property
def features(self):
return self.h[-1] # 将最后一个的向量输出
tf.transpose(input, [dimension_1, dimenaion_2,..,dimension_n]): 这里[1, 0]就是把第0,1维交换位置了。
tf.stack()这是一个矩阵拼接的函数,tf.unstack()则是一个矩阵分解的函数.
stack把两个矩阵按某个轴拼接起来,与tf.concat有所区分。
如拼接两个shape=(4, 3)的矩阵:
concat拼接axis=0后的矩阵是shape=(8, 3),拼接axis=1后,shape=(4,6)
stack拼接axis=0后的矩阵是shape=(2, 4, 3),拼接axis=1后的矩阵是shape=(4, 2, 3),拼接axis=0后的矩阵是shape=(4, 3, 1),
input.shape=(m, n)
H.shape=(2n, m)
i.shape=(n, m)
c.shape=(n, m)
h.shape=(n, m)
@property 装饰器
用装饰器函数把 get/set 方法“装饰”成属性调用:
class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_score(self):
return self.__score
def set_score(self, score):
if score < 0 or score > 100:
raise ValueError('invalid score')
self.__score = score
- >
class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
if score < 0 or score > 100:
raise ValueError('invalid score')
self.__score = score
详见@property装饰器
python super:
Python: 你不知道的 super
小结
- 事实上,
super
和父类没有实质性的关联。 super(cls, inst)
获得的是 cls 在 inst 的 MRO 列表中的下一个类。
ATTENTION NETWORK分析的更多相关文章
- 论文解读(FedGAT)《Federated Graph Attention Network for Rumor Detection》
论文信息 论文标题:Federated Graph Attention Network for Rumor Detection论文作者:Huidong Wang, Chuanzheng Bai, Ji ...
- Dual Attention Network for Scene Segmentation
Dual Attention Network for Scene Segmentation 原始文档 https://www.yuque.com/lart/papers/onk4sn 在本文中,我们通 ...
- 语义分割之Dual Attention Network for Scene Segmentation
Dual Attention Network for Scene Segmentation 在本文中,我们通过 基于自我约束机制捕获丰富的上下文依赖关系来解决场景分割任务. 与之前通过多尺 ...
- Paper | Residual Attention Network for Image Classification
目录 1. 相关工作 2. Residual Attention Network 2.1 Attention残差学习 2.2 自上而下和自下而上 2.3 正则化Attention 最近看了些关于att ...
- Residual Attention Network for Image Classification(CVPR 2017)详解
一.Residual Attention Network 简介 这是CVPR2017的一篇paper,是商汤.清华.香港中文和北邮合作的文章.它在图像分类问题上,首次成功将极深卷积神经网络与人类视觉注 ...
- 5、AFM(Attention+FM)-----Attentional Factorization Machines:Learning the Weight of Feature Interactions via Attention Network
1.摘要: 提出一个Attentional FM,Attention模型+因子分解机,其通过Attention学习到特征交叉的权重.因为很显然不是所有的二阶特征交互的重要性都是一样的,如何通过机器自动 ...
- 《Graph Attention Network》阅读笔记
基本信息 论文题目:GRAPH ATTENTION NETWORKS 时间:2018 期刊:ICLR 主要动机 探讨图谱(Graph)作为输入的情况下如何用深度学习完成分类.预测等问题:通过堆叠这种层 ...
- Keras实现Hierarchical Attention Network时的一些坑
Reshape 对于的张量x,x.shape=(a, b, c, d)的情况 若调用keras.layer.Reshape(target_shape=(-1, c, d)), 处理后的张量形状为(?, ...
- graph attention network(ICLR2018)官方代码详解(tensorflow)-稀疏矩阵版
论文地址:https://arxiv.org/abs/1710.10903 代码地址: https://github.com/Diego999/pyGAT 之前非稀疏矩阵版的解读:https://ww ...
随机推荐
- 【开源】后台权限管理系统升级到aspnetcore3.1
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- java jdk9的特性 jshell
1.进入 jshell 2.推出 /exit() 和python的解释器用法差不多
- 关于SpringDataJpa中测试出现StackOverflowError错误问题
在使用SpringDataJpa进行多表查询时,使用导航查询,每次都出现 StackOverflowError错误, 经过查找资料,网上百度,终于找到原因, StackOverflowError 是栈 ...
- PHP——数组
数组的定义 数组能够在单个变量中存储多个值. 创建空数组: $arr = array();//表示创建一个空数组,并把创建的空数组赋值给变量$arr 数值数组 自动分配 ID 键(ID 键总是从 0 ...
- Linux起源
Linux起源 操作系统出现时间线: Unix1970年诞生 ,71年用C语言重写 Apple II 诞生于1976年 window诞生于1985年 Linux诞生于1991年,由大学生Linus T ...
- 51Nod 1238 最小公倍数之和V3
题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...
- FileZilla 报错“the server's certificate is unknown”
FileZilla 是非常好用的一款FTP SFTP 管理工具. 但是filezilla会报错“the server's certificate is unknown” 并且会在window中看到以下 ...
- Dynamics 365 CRM 在 Connected Field Service 中部署 IoT Central (二)- 匹配设备
上个blog中介绍了我们怎么去部署IoT central和 connected field service做连接. 我们这次介绍怎么把IoT设备在CRM中怎么去注册. 首先我们打开devices,再选 ...
- Git详解之Git起步
前言 本章介绍开始使用 Git 前的相关知识.我们会先了解一些版本控制工具的历史背景,然后试着让 Git 在你的系统上跑起来,直到最后配置好,可以正常开始开发工作.读完本章,你就会明白为什么 Git ...
- centos7+ docker 实践部署docker及配置direct_lvm
转载于博客园:http://www.cnblogs.com/Andrew-XinFei/p/6245330.html 前言 Docker现在在后端是那么的火热..尤其当笔者了解了docker是什么.能 ...