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 ...
随机推荐
- React16源码解读:开篇带你搞懂几个面试考点
引言 如今,主流的前端框架React,Vue和Angular在前端领域已成三足鼎立之势,基于前端技术栈的发展现状,大大小小的公司或多或少也会使用其中某一项或者多项技术栈,那么掌握并熟练使用其中至少一种 ...
- 枚举 xor
题意:输入整数n(1<=n<=3千万),有多少对整数(a,b)满足:1<=b<=a<=n,且gcd(a,b)=a XOR b.例如:n=7时,有4对:(3,2),(5,4 ...
- 从网上下载DLL
1,微软官网 2:https://www.zhaodll.com/ 3:http://www.dllzj.com/
- 12.方法重载overload
方法重载:overload 重载就是在一个类中,有相同的函数名称,但形参不同的函数 方法重载的规则: 方法名称必须相同 参数列表必须不同(个数不同.或类型不同.参数排列顺序不同等) 方法的返回值类型可 ...
- 「 从0到1学习微服务SpringCloud 」01 一起来学呀!
有想学微服务的小伙伴没?一起来从0开始学习微服务SpringCloud,我会把学习成果总结下来,供大家参考学习,有兴趣可以一起来学!如有错误,望指正! Spring .SpringBoot.Sprin ...
- isinstance 和type
推荐使用 isinstance 判断对象类型. isinstance 的用法: 语法: isinstance(object, classinfo) 其中,object 是变量,classinfo 是类 ...
- MyBatis-Plus学习笔记(1):环境搭建以及基本的CRUD操作
MyBatis-Plus是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,使用MyBatis-Plus时,不会影响原来Mybatis方式的使用. SpringBoot+M ...
- Java练习题1
题目1: 编程实现,现在有如下的一个数组: int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的 ...
- mybatis入门案例分析
mybatis入门案例分析 一.设计模式分析 public class MybatisTest { public static void main(String[] args) throws Exce ...
- Spring IoC 容器和 bean 对象
程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...