BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / ml-dm / #35775同步于 2019/11/27
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ML_DM机器人发帖

【问题】tensorflow实现点积的self-attention?

littlebean
2019/11/27镜像同步2 回复
需要用tf实现self-attention层(attention is all you need 里面那种注意力函数),网上基本都是keras版本的,tensorflow能直接调用keras的attention层吗?或者应该怎么把keras版本的attention层改成tensorflow的实现???
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
littlebean机器人#1 · 2019/11/27
from keras.preprocessing import sequence from keras.datasets import imdb from matplotlib import pyplot as plt import pandas as pd from keras import backend as K from keras.engine.topology import Layer class Self_Attention(Layer): def __init__(self, output_dim, **kwargs): self.output_dim = output_dim super(Self_Attention, self).__init__(**kwargs) def build(self, input_shape): # 为该层创建一个可训练的权重 #inputs.shape = (batch_size, time_steps, seq_len) self.kernel = self.add_weight(name='kernel', shape=(3,input_shape[2], self.output_dim), initializer='uniform', trainable=True) super(Self_Attention, self).build(input_shape) # 一定要在最后调用它 def call(self, x): WQ = K.dot(x, self.kernel[0]) WK = K.dot(x, self.kernel[1]) WV = K.dot(x, self.kernel[2]) print("WQ.shape",WQ.shape) print("K.permute_dimensions(WK, [0, 2, 1]).shape",K.permute_dimensions(WK, [0, 2, 1]).shape) QK = K.batch_dot(WQ,K.permute_dimensions(WK, [0, 2, 1])) QK = QK / (64**0.5) QK = K.softmax(QK) print("QK.shape",QK.shape) V = K.batch_dot(QK,WV) return V def compute_output_shape(self, input_shape): return (input_shape[0],input_shape[1],self.output_dim)
littlebean机器人#2 · 2019/11/27
网上很多实现为什么都是用additive attention,dot_product attention不好用吗?