Giter Site home page Giter Site logo

attention's Introduction

attention

some attention implements

《Attention is All You Need》中的Attention机制的实现

http://kexue.fm/archives/4765/

测试环境

python 2.7 + tensorflow 1.8+ + keras 2.2.4

注:本项目不再跟进新版本的tensorflow/keras,如果有新版本的需求,可以到bert4keras的layers.py处复制对应的函数。

交流

QQ交流群:67729435,微信群请加机器人微信号spaces_ac_cn

attention's People

Contributors

bojone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

attention's Issues

K.permute_dimensions(A, (0,3,2,1))运行时报错

您好,我在运行您的代码时,出现了错误,在
#计算内积,然后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维吗?我的输入格式跟你的是一样的,想请教你这个问题要怎么解决?

tf中position

position_ij = tf.concat([tf.cos(position_ij), tf.sin(position_ij)], 1)

论文中sin和cos是交叉的,偶数位置为sin,奇数位置为cos,如果按照你这么运算,前半部分是cos,后半部分是sin,是不是与论文不符,希望帮忙解惑,谢谢

请问 Position Embedding 可以被训练吗?

@bojone 您好,看了您的代码(Keras),很赞!
请教个问题:
在输入经过 Word Embedding 之后,又经过了一层 Position Embedding,这相当于补充了词向量的位置信息。
但由于Keras的Layer中的权值矩阵是默认是可以被训练的(trainable),那么补充的 Position Embedding 的参数信息也会在BP中不断被优化,这样的话增加 Position Embedding 这一层并计算sincos值只是相当于对 PE 的权值做了一次初始化?随着迭代进行,其权值也会不断变化。。。
还是说在调用Position Embedding的时候应该要设定trainable=False呢?

希望得到您的解答!感谢~!

K,Q,V映射

我想问一下多头attention那里,通过一次线性映射产生K,Q,V,paper中说要8次不同的映射。代码实现时,是因为,8次不同的映射可以一次完成,只要权重参数的数量已经相当于8次映射的数量了么?

不支持mask

您好,非常感谢您的分享,但是我在调用您的模型时:
embeddings=Embedding(len(vocab),200,mask_zero=True)(inputs)
embeddings = Position_Embedding()(embeddings)
发现以下列错误信息
Layer position__embedding_1 does not support masking,
请问一下该如何解决,十分感谢。

模型加载问题

训练完后 加载模型时一直报错,找不到attention层,但我有import attention

Position_Embedding肯能有些问题

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])

不知道这样对不对,如果不对请指正

行为识别 用attention

您好,我看到您的代码是用在NLP 上的,
我想问下,我可不可以用来做行为识别?
如果可以调用的话,您能不能给点调用的建议呢?
谢谢您的帮助哈

Keras的batch_dot()如何用pytorch改写

你好,我想把你的Keras版本改写成pytorch版本的,但是K.batch_dot(qw, kw, [4, 4])这个功能好像没有类似pytorch的函数可以转换,请问你有什么建议吗?谢谢

attention_keras.py

请问按照这个代码,怎样能得到多头注意力每一次Head的权重呢。

attention_keras.py中,Position_Embedding的pos的计算

你好,

想请教一下,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值,但不太理解具体是如何实现计算的,论文似乎也没有详细涉及。

请问能解释一下吗?谢谢!

请问Q,K,V分别是从哪里输入的呢?

拜读了您的《Attention is All You Need》浅读,对Q,K,V这三个矩阵的来源仍然不理解,我的一种理解是Q是attention layer的输入,K和V是需要训练的两个权重矩阵,这样理解对吗?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.