bojone / attention Goto Github PK
View Code? Open in Web Editor NEWsome attention implements
some attention implements
您好,我在运行您的代码时,出现了错误,在
#计算内积,然后mask,然后softmax
A = K.batch_dot(Q_seq, K_seq, axes=[3,3])
A = K.permute_dimensions(A, (0,3,2,1))
这一块,报了valueError:('You cannot drop a non-broadcastable dimension.'((False,False,False,False,False),(0,3,2,1)
这是因为batch_dot的计算结果是5维,而permute_dimension中参数是4维吗?我的输入格式跟你的是一样的,想请教你这个问题要怎么解决?
ValueError: Dimension must be 5 but is 4 for 'attention_1/transpose_7' (op: 'Transpose') with input shapes: [?,8,?,8,?], [4].
请问这个怎么解决 run example
https://kexue.fm/archives/4765
代码测试
在Keras上对IMDB进行简单的测试
position_ij = tf.concat([tf.cos(position_ij), tf.sin(position_ij)], 1)
论文中sin和cos是交叉的,偶数位置为sin,奇数位置为cos,如果按照你这么运算,前半部分是cos,后半部分是sin,是不是与论文不符,希望帮忙解惑,谢谢
Could you give me a brief expanantion, please? Anyway thanks a lot.
训练完后 加载模型时一直报错,找不到attention层,但我有import attention
原paper里面,每个head拼接之后会乘上Wo,这个实现里好像木有写诶
attention_tf 中的 position_embedding 函数中
position_ij = tf.matmul(position_i, position_j)
应该改为
position_ij = tf.multiply(position_i, position_j)
不然维度不匹配
请问一下,tf版的78,80行的转置操作是什么意思呢? 去掉这两行进行训练对结果也没什么影响。
原文分的是奇偶,这里这么处理好像不对
Position_Embedding中
position_ij = tf.concat([tf.cos(position_ij), tf.sin(position_ij)], 1)
只能将cos和sin前后连接,并不能做到论文中的交叉连接
我认为应该把这行代码替换成:
cos = tf.expand_dims(tf.cos(position_ij), 2)
sin = tf.expand_dims(tf.sin(position_ij), 2)
position_ij = tf.concat([cos, sin], 2)
position_ij = tf.reshape(position_ij, shape=[seq_len, -1])
不知道这样对不对,如果不对请指正
您好,不知道能不能向您请教一下在解码的时候输入的target怎么理解呢?
请问按照这个代码,怎样能得到多头注意力每一次Head的权重呢。
拜读了您的《Attention is All You Need》浅读,对Q,K,V这三个矩阵的来源仍然不理解,我的一种理解是Q是attention layer的输入,K和V是需要训练的两个权重矩阵,这样理解对吗?
您好,非常感谢您的分享,但是我在调用您的模型时:
embeddings=Embedding(len(vocab),200,mask_zero=True)(inputs)
embeddings = Position_Embedding()(embeddings)
发现以下列错误信息
Layer position__embedding_1 does not support masking,
请问一下该如何解决,十分感谢。
我想问一下多头attention那里,通过一次线性映射产生K,Q,V,paper中说要8次不同的映射。代码实现时,是因为,8次不同的映射可以一次完成,只要权重参数的数量已经相当于8次映射的数量了么?
@bojone 您好,看了您的代码(Keras),很赞!
请教个问题:
在输入经过 Word Embedding 之后,又经过了一层 Position Embedding
,这相当于补充了词向量的位置信息。
但由于Keras的Layer中的权值矩阵是默认是可以被训练的(trainable),那么补充的 Position Embedding
的参数信息也会在BP中不断被优化,这样的话增加 Position Embedding
这一层并计算sin
和cos
值只是相当于对 PE 的权值做了一次初始化?随着迭代进行,其权值也会不断变化。。。
还是说在调用Position Embedding
的时候应该要设定trainable=False
呢?
希望得到您的解答!感谢~!
你好,我想把你的Keras版本改写成pytorch版本的,但是K.batch_dot(qw, kw, [4, 4])这个功能好像没有类似pytorch的函数可以转换,请问你有什么建议吗?谢谢
你好,
想请教一下,attention_keras.py中的第21行,
position_i = K.cumsum(K.ones_like(x[:,:,0]), 1)-1 #K.arange不支持变长,只好用这种方法生成
这行代码具体在计算什么?
我翻阅了论文原文和ones_like, cumsum的keras backend资料,对这行代码的用途猜测是计算论文中Position_Embedding公式的pos值,但不太理解具体是如何实现计算的,论文似乎也没有详细涉及。
请问能解释一下吗?谢谢!
Thanks.
您好,我看到您的代码是用在NLP 上的,
我想问下,我可不可以用来做行为识别?
如果可以调用的话,您能不能给点调用的建议呢?
谢谢您的帮助哈
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.