Giter VIP home page Giter VIP logo

transformers_from_scratch's Introduction

Transformers_from_scratch

This implementation follows the step-by-step guide provided in Umar Jamil's YouTube video: Transformers from Scratch

Transformer Model in PyTorch

This repository contains the implementation of a Transformer model in PyTorch, including scripts for training the model on bilingual datasets.

Table of Contents

Introduction

This implementation provides a modular and extensible structure for the Transformer model. Each component of the model is implemented as a separate class, making it easy to understand and modify individual parts of the architecture.

Transformer Diagram

The Transformer model architecture is a complex structure best understood visually. Below is the diagram to help illustrate the concept:

Technical Diagram

For a detailed technical diagram of the Transformer architecture, please refer to this image:

Transformer Architecture

This diagram shows the intricate components of the Transformer model, including the encoder and decoder stacks, multi-head attention mechanisms, and feed-forward networks.

Pop Culture Reference

For a lighter take on "transformers", here's an image from the popular movie franchise:

Transformers Movie

While not technically related to our NLP model, this image serves as a fun reminder of the term "transformer" in popular culture!

Our implementation focuses on the technical architecture shown in the first image, not the movie characters. The model transforms input sequences into output sequences, hence the name "Transformer".

Components

model.py

Contains the implementation of various components of the Transformer model such as:

  • LayerNormalization
  • FeedForwardBlock
  • InputEmbeddings
  • PositionalEncoding
  • ResidualConnection
  • MultiHeadAttentionBlock
  • EncoderBlock
  • Encoder
  • DecoderBlock
  • Decoder
  • ProjectionLayer
  • Transformer

dataset.py

Contains the implementation of:

  • BilingualDataset: Handles the preparation of bilingual data for the Transformer model, including tokenization, padding, and masking.
  • causal_mask: Creates a causal mask for the decoder to ensure that the model does not attend to future positions in the sequence.

train.py

Handles the training of the Transformer model. Includes functions such as:

  • greedy_decode: Performs greedy decoding to generate translations from the model.
  • run_validation: Runs the validation process and calculates evaluation metrics.
  • get_all_sentences: Yields all sentences in a dataset for a given language.
  • get_or_build_tokenizer: Builds or loads a tokenizer.
  • get_ds: Loads the dataset, builds tokenizers, and creates dataloaders.
  • get_model: Builds the Transformer model.
  • train_model: Orchestrates the training process.

Usage

To build and use the Transformer model, you need to follow these steps:

  1. Prepare the dataset: Ensure you have a bilingual dataset to train on.
  2. Configure the model: Set up the configuration parameters for the model and training process.
  3. Run the training script: Execute train.py to start the training process.

Training

To train the model, execute the train.py script:

python train.py

License

MIT

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.