GithubHelp home page GithubHelp logo

Comments (4)

CyberZHG avatar CyberZHG commented on June 10, 2024

The attention module uses both the history_only parameter and the given mask.

https://github.com/CyberZHG/keras-self-attention/blob/f3341547271068243866b1c0ff2512baddf92068/keras_self_attention/scaled_dot_attention.py#L69-L70

from keras-bert.

lwyeah avatar lwyeah commented on June 10, 2024

Yes, the attention module in the keras_self_attention has parameter mask. But the _attention_builder returned by attention_builder only receives one parameters x (i.e. the parameter inputs in ScaledDotProductAttention) and the parameter mask is None. So only the history_only parameter controls the self-attention mask in transformer encoder.

Below is the code in keras-transformer/keras_transformer/transformer.py

def attention_builder(name,
head_num,
activation,
history_only,
trainable=True):
"""Get multi-head self-attention builder.
:param name: Prefix of names for internal layers.
:param head_num: Number of heads in multi-head self-attention.
:param activation: Activation for multi-head self-attention.
:param history_only: Only use history data.
:param trainable: Whether the layer is trainable.
:return:
"""
def _attention_builder(x):
return MultiHeadAttention(
head_num=head_num,
activation=activation,
history_only=history_only,
trainable=trainable,
name=name,
)(x)
return _attention_builder

from keras-bert.

CyberZHG avatar CyberZHG commented on June 10, 2024

Note that the second () in MultiHeadAttention(...)(x) calls __call__(...) but not call(...).

from keras-bert.

lwyeah avatar lwyeah commented on June 10, 2024

Oh, god, thand you very much. I have read the <Understanding masking & padding> in the keras docs, and understand the _keras_mask. Thank you again taking time out to answer my questions.

from keras-bert.

Related Issues (20)

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.