Skip to content

A numpy implementation of the Transformer model in "Attention is All You Need"

Notifications You must be signed in to change notification settings

AkiRusProd/numpy-transformer

Repository files navigation

numpy-transformer

What is this?

This is a fast numpy implementation of the Transformer (Seq2Seq) model in original paper "Attention is All You Need", that runs at CPU with numpy or GPU with cupy (very fast).

Some methods were borrowed from my numpy-nn-model repository.

Training

The model was trained for 10 epochs. On my machine, training one epoch with CPU takes 3 hours, but with GPU takes 2 minutes!!!! Thus training of all epochs takes 20 minutes.
Note: You can`t open pre-trained model if CuPy is not installed, because this model was trained on GPU.

Dataset:

The dataset on which the model was trained is Multi30k dataset. To import this just run extract_data.py script.

Project Structure

Transformer model components:

Functional components:

  • activations.py - list of activation functions for the model
  • losses.py - list of loss functions for the model. But used only torch like Cross Entropy Loss
  • optimizers.py - list of gradient optimizers for the model. In this case, Adam is used with the Noam scheduler

Special utils:

Others:

  • tests - junk scripts. Do not pay attention to them

Examples

Examples of translated sentences of validation set:

Example №1
Input sentence: people sitting in a circle outside a large building
Decoded sentence: personen sitzen im freien vor einem großen gebäude
Target sentence: menschen die vor einem großen gebäude im kreis sitzen

Example №2
Input sentence: a woman sings in a club with a guitarist behind her
Decoded sentence: eine frau singt in einem club mit einem club
Target sentence: eine frau singt in einem klub mit einem gitarristen hinter ihr

Example №3
Input sentence: construction workers having a discussion by the tracks
Decoded sentence: bauarbeiter diskutieren einige von den bahngleisen
Target sentence: arbeiter diskutieren neben den schiene

Example №4
Input sentence: a man with graying hair shaves his beard
Decoded sentence: ein bärtiger mann bart trägt
Target sentence: ein mann mit ergrauenden haaren rasiert seinen bart

Example №5
Input sentence: a man in a black shirt and jeans standing on the sidewalk looking at the camera
Decoded sentence: ein mann in einem schwarzen oberteil und jeans steht auf dem gehweg und blickt auf die kamera
Target sentence: ein mann in einem schwarzen hemd und jeans steht auf dem gehsteig und blickt in die kamera

Example №6
Input sentence: a woman is laying down on a sofa laughing
Decoded sentence: eine frau liegt auf einem sofa und lacht
Target sentence: eine frau liegt auf einem sofa und lacht

Example №7
Input sentence: two males seem to be conversing while standing in front of a truck's back and behind a metal item while four people stand around them
Decoded sentence: vier personen stehen hinter ihnen unterhalten sich während hinter ihnen und hinter ihnen unterhalten und unterhalten und hinter ihnen stehen
Target sentence: zwei männer stehen vor dem heck eines lasters und hinter einem metallgegenstand und unterhalten sich anscheinend während vier weitere personen um sie herum stehen

Example №8
Input sentence: two rockers are singing and playing on a dark stage
Decoded sentence: zwei singen und spielen auf einer bühne
Target sentence: zwei rocker singen und spielen auf einer dunklen bühne

Example №9
Input sentence: a man fixing a little girl's bicycle
Decoded sentence: ein mann repariert ein kleines fahrrad
Target sentence: ein mann repariert das fahrrad eines kleinen mädchens

Example №10
Input sentence: a man eating a sandwich with his baby daughter in his lap
Decoded sentence: ein mann isst ein baby auf seiner tochter ein sandwich
Target sentence: ein mann isst ein sandwich und hält seine kleine tochter auf dem schoß

Attention plots:

References:

TODO:

  1. clean up and refactor code