Coder Social home page Coder Social logo

njunlp / selectivekd Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 1.72 MB

Implementation for our AAAI-23 paper "Selective Knowledge Distillation for Non-Autoregressive Neural Machine Translation".

License: MIT License

Python 97.59% C++ 0.62% Cuda 1.13% Cython 0.39% Shell 0.14% Lua 0.12%

selectivekd's Introduction

Selective KD

Implementation for our AAAI-23 paper "Selective Knowledge Distillation for Non-Autoregressive Neural Machine Translation" (arXiv).

Preparation

Train an autoregressive Transformer according to the instructions in Fairseq.

Use the trained autoregressive Transformer to generate target sentences for the training set.

You should make sure that the order of source sentences in distilled data matches that in raw data.

Then binarize raw and distilled data respectively.

python3 fairseq_cli/preprocess.py \
    --source-lang ${SRC} --target-lang ${TGT} \
    --trainpref ${INPUT_DIR}/train --validpref ${INPUT_DIR}/valid --testpref ${INPUT_DIR}/test \
    --destdir ${DATA_DIR} \
    --srcdict ${INPUT_DIR}/dict.${SRC}.txt --tgtdict ${INPUT_DIR}/dict.${TGT}.txt

Train an Evaluator

python3 fairseq_cli/train.py ${KD_DATA_DIR} \
    --arch glat_ctc --noise full_mask --share-all-embeddings \
    --criterion ctc_loss --label-smoothing 0.1 --lr 5e-4 --warmup-init-lr 1e-7 --stop-min-lr 1e-9 \
    --lr-scheduler inverse_sqrt --warmup-updates 4000 --optimizer adam --adam-betas '(0.9,0.999)' \
    --adam-eps 1e-6 --task translation_lev_modified --max-tokens 8192 --weight-decay 0.01 --dropout 0.1 \
    --encoder-layers 6 --encoder-embed-dim 512 --decoder-layers 6 --decoder-embed-dim 512 --fp16 \
    --max-source-positions 1000 --max-target-positions 1000 --max-update 100000 --seed 0 --clip-norm 2\
    --length-loss-factor 0 --log-interval 100 \
    --eval-bleu --eval-bleu-args '{"iter_decode_max_iter":0,"iter_decode_with_beam":1}' \
    --eval-tokenized-bleu --eval-bleu-remove-bpe --best-checkpoint-metric bleu \
    --maximize-best-checkpoint-metric --decoder-learned-pos --encoder-learned-pos \
    --apply-bert-init --activation-fn gelu --user-dir plugins --keep-best-checkpoints 5 \
    --save-dir ${EVALUATOR_DIR}

Train a Model Using Selective KD

python3 fairseq_cli/train.py ${RAW_DATA_DIR} --kd-data ${KD_DATA_DIR} \
    --arch glat_ctc --noise full_mask --share-all-embeddings --num-workers 4 \
    --criterion ctc_loss --label-smoothing 0.1 --lr 5e-4 --warmup-init-lr 1e-7 --stop-min-lr 1e-9 \
    --lr-scheduler inverse_sqrt --warmup-updates 4000 --optimizer adam --adam-betas '(0.9,0.999)' \
    --adam-eps 1e-6 --task translation_selective_kd --max-tokens 8192 --weight-decay 0.01 --dropout 0.1 \
    --encoder-layers 6 --encoder-embed-dim 512 --decoder-layers 6 --decoder-embed-dim 512 --fp16 \
    --max-source-positions 1000 --max-target-positions 1000 --max-update 300000 --seed 0 --clip-norm 2\
    --length-loss-factor 0 --log-interval 100 \
    --eval-bleu --eval-bleu-args '{"iter_decode_max_iter":0,"iter_decode_with_beam":1}' \
    --eval-tokenized-bleu --eval-bleu-remove-bpe --best-checkpoint-metric bleu \
    --maximize-best-checkpoint-metric --decoder-learned-pos --encoder-learned-pos \
    --apply-bert-init --activation-fn gelu --user-dir plugins \
    --keep-best-checkpoints 5 --save-dir ${MODEL_DIR} \
    --evaluator ${EVALUATOR_PATH} --load-from-checkpoint ${INITIALIZATION_MODEL} \
    --kd-threshold 0.4 --kd-threshold-offset 0.6

Inference

python3 fairseq_cli/generate.py ${RAW_DATA_DIR} \
    --user-dir plugins --gen-subset test \
    --task translation_selective_kd --remove-bpe --max-sentences 20 \
    --iter-decode-max-iter 0 --iter-decode-eos-penalty 0 --iter-decode-with-beam 1 \
    --path ${CHECKPOINT_PATH} --quiet

Citation

Please consider citing our paper if your find our research or this codebase helpful:

@article{liu2023selective,
  title={Selective Knowledge Distillation for Non-Autoregressive Neural Machine Translation},
  author={Liu, Min and Bao, Yu and Zhao, Chengqi and Huang, Shujian},
  journal={arXiv preprint arXiv:2303.17910},
  year={2023}
}

selectivekd's People

Contributors

damianliumin avatar

Stargazers

 avatar  avatar

Watchers

 avatar

selectivekd's Issues

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.