Giter VIP home page Giter VIP logo

transformer_from_scratch's Introduction

🤖Transformer in Details🤔

Transformer
Fig. Transformer Overview


InputEmbeddings:

作用: 将输入序列, 也就是分词后的token,映射到embedding空间(向量空间)。

IE

执行逻辑: 使用一个Embedding层将输入的词索引映射为embedding向量,同时乘以 ( \sqrt{d_{\text{model}}} ) 进行缩放。


PositionalEncoding:

作用: 添加位置编码,以捕捉输入序列中的位置信息。

PE

执行逻辑: 生成一个固定的位置编码矩阵,将其添加到输入中,以捕获序列的位置信息。


MultiHeadAttentionBlock:

作用:

实现多头注意力机制。

multihead_attention

执行逻辑: 将输入通过多个头进行注意力计算,最后将结果拼接并经过线性变换。

att_illus


LayerNormalization:

作用: 实现Layer Normalization,用于对输入进行归一化。

layerNorm

执行逻辑: 通过学习参数 alphabias,对输入进行归一化,并在模型训练过程中学习调整参数。


FeedForwardBlock:

作用: 实现Transformer中的前馈神经网络块。

执行逻辑: 通过两个线性层和一个ReLU激活函数,对输入进行非线性变换。


ResidualConnection:

作用: 实现残差连接。

执行逻辑: 通过残差连接将输入与子层的输出相加,并在残差连接上应用Layer Normalization。


EncoderBlock:

作用: 实现Transformer编码器中的一个块。

执行逻辑: 包含一个自注意力块和一个前馈神经网络块,通过残差连接和Layer Normalization进行整合。


Encoder:

作用: 堆叠多个EncoderBlock,构建整个编码器。

执行逻辑: 对输入序列进行多次编码,通过Layer Normalization整合输出。


DecoderBlock:

作用: 实现Transformer解码器中的一个块。

执行逻辑: 包含一个自注意力块、一个对编码器输出的交叉注意力块和一个前馈神经网络块,通过残差连接和Layer Normalization整合。


Decoder:

作用: 堆叠多个DecoderBlock,构建整个解码器。

执行逻辑: 对目标序列进行多次解码,通过Layer Normalization整合输出。


ProjectionLayer:

作用: 将解码器输出映射到目标词汇空间。

执行逻辑: 通过一个线性层进行映射。


Transformer:

作用: 整合编码器、解码器、嵌入层、位置编码层和投影层,构建整个Transformer模型。

执行逻辑: 对源序列进行编码,然后通过解码器生成目标序列的预测。


build_transformer 函数:

作用: 根据配置参数构建Transformer模型。

执行逻辑: 创建嵌入层、位置编码层、编码器块、解码器块、编码器、解码器和投影层,然后整合它们构成完整的Transformer模型。


以上各个组件共同构成了Transformer模型。通过多层的自注意力机制和前馈神经网络,Transformer模型能够捕捉输入序列的长期依赖关系,适用于各种序列到序列的任务。

transformer_from_scratch's People

Contributors

hkproj avatar ivolazy avatar jackwang0318 avatar kavinask007 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.