Transformer-pytorch
A PyTorch implementation of Transformer from "Attention is All You Need" (https://arxiv.org/abs/1706.03762).
This repo focuses on clean, readable, and modular implementation of the paper.
$ docker build --tag transformer --rm .
$ docker run --name $CONTAINER_NAME -it --gpus all --shm-size 16G --volume $(pwd):/transformer transformer bash
python main.py v1 prepare multi30k data/Multi30k/
python -m spacy download en
python -m spacy download de
python main.py v1 build-vocab data/Multi30k/train.json results/vocabs/shared_vocab.tsv --source-language de --target-language en --min-freq 3
Vocabulary 9521 lines written to results/vocabs/shared_vocab.tsv
CUDA_VISIBLE_DEVICES=0 python main.py v1 train data/Multi30k/train.json data/Multi30k/val.json results/vocabs/shared_vocab.tsv results/vocabs/shared_vocab.tsv results/runs playground
python main.py v1 prepare aihub data/AIHub/
Requirements
- Python 3.6+
- PyTorch 4.1+
- NumPy
- NLTK
- tqdm
Usage
Prepare datasets
This repo comes with example data in data/ directory. To begin, you will need to prepare datasets with given data as follows:
$ python prepare_datasets.py --train_source=data/example/raw/src-train.txt --train_target=data/example/raw/tgt-train.txt --val_source=data/example/raw/src-val.txt --val_target=data/example/raw/tgt-val.txt --save_data_dir=data/example/processed
The example data is brought from OpenNMT-py. The data consists of parallel source (src) and target (tgt) data for training and validation. A data file contains one sentence per line with tokens separated by a space. Below are the provided example data files.
src-train.txttgt-train.txtsrc-val.txttgt-val.txt
Train model
To train model, provide the train script with a path to processed data and save files as follows:
$ python train.py --data_dir=data/example/processed --save_config=checkpoints/example_config.json --save_checkpoint=checkpoints/example_model.pth --save_log=logs/example.log
This saves model config and checkpoints to given files, respectively.
You can play around with hyperparameters of the model with command line arguments.
For example, add --epochs=300 to set the number of epochs to 300.
Translate
To translate a sentence in source language to target language:
$ python predict.py --source="There is an imbalance here ." --config=checkpoints/example_config.json --checkpoint=checkpoints/example_model.pth
Candidate 0 : Hier fehlt das Gleichgewicht .
Candidate 1 : Hier fehlt das das Gleichgewicht .
Candidate 2 : Hier fehlt das das das Gleichgewicht .
It will give you translation candidates of the given source sentence. You can adjust the number of candidates with command line argument.
Evaluate
To calculate BLEU score of a trained model:
$ python evaluate.py --save_result=logs/example_eval.txt --config=checkpoints/example_config.json --checkpoint=checkpoints/example_model.pth
BLEU score : 0.0007947
File description
models.pyincludes Transformer's encoder, decoder, and multi-head attention.embeddings.pycontains positional encoding.losses.pycontains label smoothing loss.optimizers.pycontains Noam optimizer.metrics.pycontains accuracy metric.beam.pycontains beam search.datasets.pyhas code for loading and processing data.trainer.pyhas code for training model.prepare_datasets.pyprocesses data.train.pytrains model.predict.pytranslates given source sentence with a trained model.evaluate.pycalculates BLEU score of a trained model.
Reference
Author
Deploy
python3 setup.py sdist bdist_wheel
python3 -m twine upload --repository testpypi dist/*
python3 -m twine upload --repository dist/*
Metadata
Release files for transformer-pytorch 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| transformer-pytorch-0.0.1.tar.gz | 14.7 kB | Without provenance | Details for transformer-pytorch-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| transformer_pytorch-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for transformer_pytorch-0.0.1-py3-none-any.whl |
Total release size: 35.2 kB
Release files / transformer-pytorch-0.0.1.tar.gz
| Download URL | transformer-pytorch-0.0.1.tar.gz |
|---|---|
| Size | 14.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e1defa5623fe184a9265ca71d5611650087d3f54eb513cb62ec44d948ae7b14
|
|
BLAKE2b-256 checksum How to use checksums |
5da8f887edcb3fbbdaa9482ba5497d7500545960d932600270baff26e670b4f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4
|
Release files / transformer_pytorch-0.0.1-py3-none-any.whl
| Download URL | transformer_pytorch-0.0.1-py3-none-any.whl |
|---|---|
| Size | 20.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b34986f9fad422a71c8953c0cb0c7b08b811aafbb6755faa89d6066d05882f98
|
|
BLAKE2b-256 checksum How to use checksums |
3aee48b50a868973b123b869e2db3092a0bc36405eb9e8cf297154a0f729f303
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4
|
Security for transformer-pytorch 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.