Git Product home page Git Product logo

text-attentionpooling-visualization's Introduction

text-attentionpooling-visualization

AttentionPooling和MultiHeadAttentionPooling在分类和匹配问题上的可视化分析。这里提供丰富的可视化例子。

GlobalMaxPool请看text-globalmaxpool-visualization

AttentionPooling1D中call函数:

class AttentionPooling1D(tf.keras.layers.Layer):

    def call(self, inputs, mask=None):
        if mask is None:
            mask = 1
        else:
            # 扩展维度便于广播
            mask = tf.expand_dims(tf.cast(mask, tf.float32), -1)
        x0 = inputs
        # 计算每个 time steps 权重
        w = self.k_dense(inputs)
        w = self.o_dense(w)
        # 处理 mask
        w = w - (1 - mask) * 1e12
        # 权重归一化
        w = tf.math.softmax(w, axis=1) # 有mask位置对应的权重变为很小的值
        # 加权平均
        x = tf.reduce_sum(w * x0, axis=1)
        return x, w

在这个基础上可以进行多头化操作,称为MultiHeadAttentionPooling1D,以扩展关键信息的捕获能力。

分类问题示例

示例一:

新闻标题分类,

示例二:

正负情感分类,

示例三:

四情感分类,

示例四:

长文分类,

示例五:

把分类问题化为文本与标签的匹配问题,

匹配问题示例

示例五:

文本匹配Attentionpooling可视化示例一,

文本匹配Attentionpooling可视化示例二,

文本匹配Attentionpooling可视化示例三,使用triplet loss,

基于交互的匹配示例

示例一,

示例二,

多头Pooling示例

MultiHeadAttentionPooling可视化示例一(情感分析),

其对应的Attention权重可视化,

MultiHeadAttentionPooling可视化示例二(酒店评论一),

其对应的Attention权重可视化,

MultiHeadAttentionPooling可视化示例二(酒店评论二),

其对应的Attention权重可视化,

MultiHeadAttentionPooling可视化示例三(新闻分类),

其对应的Attention权重可视化,

数据来源

[1] http://thuctc.thunlp.org/

[2] https://github.com/SophonPlus/ChineseNlpCorpus/blob/master/datasets/weibo_senti_100k/intro.ipynb

[3] https://github.com/SophonPlus/ChineseNlpCorpus/blob/master/datasets/simplifyweibo_4_moods/intro.ipynb

[4] https://www.aclweb.org/anthology/C18-1166/

text-attentionpooling-visualization's People

Contributors

allenwind avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

al-yan

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.