GithubHelp home page GithubHelp logo

luosiqi / self-attention Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zxuu/self-attention

0.0 0.0 0.0 4.66 MB

Transformer的完整实现。详细构建Encoder、Decoder、Self-attention。以实际例子进行展示,有完整的输入、训练、预测过程。可用于学习理解self-attention和Transformer

Python 67.08% Jupyter Notebook 32.92%

self-attention's Introduction

Self-Attention(pytorch实现)

参考这篇博客手撕Transformer Transformer输入输出细节以及代码实现(pytorch)

**OneHead文件夹**下是单头自注意力机制的实现(无Mask) **MultiHead文件夹**下是多头自注意力机制的实现(无Mask) **Encoder文件夹**下是Ecoder编码器的完整实现(有mask) **Decoder文件夹**下是Decoder解码器的完整实现(有mask) **Transformer文件夹**下是把Encoder和Decoder两部分的合并,形成完整的transformer结构

Train文件夹是训练部分
Test文件夹是测试部分
Utils文件夹是工具包,包括一些常量的定义,还有Dataloader

My_Transformer.py是完整的transformer构建、数据输入、测试的例子

torch、transformer版本

torch                          1.13.1+cu117
torchaudio                     0.13.1+cu117
torchvision                    0.14.1+cu117
transformers                   4.26.1

输入例子

             # Encoder_input    Decoder_input        Decoder_output
sentences = [['我 是 学 生 P' , 'S I am a student'   , 'I am a student E'],         # S: 开始符号
             ['我 喜 欢 学 习', 'S I like learning P', 'I like learning P E'],      # E: 结束符号
             ['我 是 男 生 P' , 'S I am a boy'       , 'I am a boy E']]             # P: 占位符号,如果当前句子不足固定长度用P占位 pad补0

My_transformer.py输出(直接运行即可)

......
Epoch: 0050 loss = 0.088758
Epoch: 0050 loss = 0.003786
['', '', '', '', 'P'] -> ['I', 'am', 'a', 'student', 'E']

self-attention's People

Contributors

zxuu avatar

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.