{"id":18887143,"url":"https://github.com/thunlp-mt/document-transformer","last_synced_at":"2026-03-04T15:30:49.021Z","repository":{"id":80458569,"uuid":"125013345","full_name":"THUNLP-MT/Document-Transformer","owner":"THUNLP-MT","description":"Improving the Transformer translation model with document-level context","archived":false,"fork":false,"pushed_at":"2020-07-07T13:26:52.000Z","size":305,"stargazers_count":170,"open_issues_count":6,"forks_count":21,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-02T16:48:33.726Z","etag":null,"topics":["document-level-translation","neural-machine-translation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/THUNLP-MT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-13T07:57:22.000Z","updated_at":"2025-05-03T07:23:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"dde5f585-d97a-409f-9553-39adabe00818","html_url":"https://github.com/THUNLP-MT/Document-Transformer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/THUNLP-MT/Document-Transformer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/THUNLP-MT%2FDocument-Transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/THUNLP-MT%2FDocument-Transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/THUNLP-MT%2FDocument-Transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/THUNLP-MT%2FDocument-Transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/THUNLP-MT","download_url":"https://codeload.github.com/THUNLP-MT/Document-Transformer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/THUNLP-MT%2FDocument-Transformer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30084953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T13:22:36.021Z","status":"ssl_error","status_checked_at":"2026-03-04T13:20:45.750Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["document-level-translation","neural-machine-translation"],"created_at":"2024-11-08T07:34:44.996Z","updated_at":"2026-03-04T15:30:49.003Z","avatar_url":"https://github.com/THUNLP-MT.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Improving the Transformer Translation Model with Document-Level Context\n## Contents\n* [Introduction](#introduction)\n* [Usage](#usage)\n* [Citation](#citation)\n* [FAQ](#faq)\n\n## Introduction\n\nThis is the implementation of our work, which extends Transformer to integrate document-level context \\[[paper](https://arxiv.org/abs/1810.03581)\\]. The implementation is on top of [THUMT](https://github.com/thumt/THUMT)\n\n## Usage\n\nNote: The usage is not user-friendly. May improve later.\n\n1. Train a standard Transformer model, please refer to the user manual of [THUMT](https://github.com/thumt/THUMT). Suppose that model_baseline/model.ckpt-30000 performs best on validation set.\n\n2. Generate a dummy improved Transformer model with the following command:\n\n\u003cpre\u003e\u003ccode\u003epython THUMT/thumt/bin/trainer_ctx.py --inputs [source corpus] [target corpus] \\\n                                      --context [context corpus] \\\n                                      --vocabulary [source vocabulary] [target vocabulary] \\\n                                      --output model_dummy --model contextual_transformer \\\n                                      --parameters train_steps=1\n\u003c/code\u003e\u003c/pre\u003e\n\n3. Generate the initial model by merging the standard Transformer model into the dummy model, then create a checkpoint file:\n\n\u003cpre\u003e\u003ccode\u003epython THUMT/thumt/scripts/combine_add.py --model model_dummy/model.ckpt-0 \\\n                                         --part model_baseline/model.ckpt-30000 --output train\nprintf 'model_checkpoint_path: \"new-0\"\\nall_model_checkpoint_paths: \"new-0\"' \u003e train/checkpoint\n\u003c/code\u003e\u003c/pre\u003e\n\n\n4. Train the improved Transformer model with the following command:\n\n\u003cpre\u003e\u003ccode\u003epython THUMT/thumt/bin/trainer_ctx.py --inputs [source corpus] [target corpus] \\\n                                      --context [context corpus] \\\n                                      --vocabulary [source vocabulary] [target vocabulary] \\\n                                      --output train --model contextual_transformer \\\n                                      --parameters start_steps=30000,num_context_layers=1\n\u003c/code\u003e\u003c/pre\u003e\n\n5. Translate with the improved Transformer model:\n\n\u003cpre\u003e\u003ccode\u003epython THUMT/thumt/bin/translator_ctx.py --inputs [source corpus] --context [context corpus] \\\n                                         --output [translation result] \\\n                                         --vocabulary [source vocabulary] [target vocabulary] \\\n                                         --model contextual_transformer --checkpoints [model path] \\\n                                         --parameters num_context_layers=1\n\u003c/code\u003e\u003c/pre\u003e\n\n## Citation\n\nPlease cite the following paper if you use the code:\n\n\u003cpre\u003e\u003ccode\u003e@InProceedings{Zhang:18,\n  author    = {Zhang, Jiacheng and Luan, Huanbo and Sun, Maosong and Zhai, Feifei and Xu, Jingfang and Zhang, Min and Liu, Yang},\n  title     = {Improving the Transformer Translation Model with Document-Level Context},\n  booktitle = {Proceedings of EMNLP},\n  year      = {2018},\n}\n\u003c/code\u003e\u003c/pre\u003e\n\n\n## FAQ\n\n1. What is the context corpus?\n\nThe context corpus file contains one context sentence each line. Normally, context sentence is the several preceding source sentences within a document. For example, if the origin document-level corpus is:\n\n\u003cpre\u003e\u003ccode\u003e==== source ====\n\u0026lt;document id=XXX\u003e\n\u0026lt;seg id=1\u003esource sentence #1\u0026lt;/seg\u003e\n\u0026lt;seg id=2\u003esource sentence #2\u0026lt;/seg\u003e\n\u0026lt;seg id=3\u003esource sentence #3\u0026lt;/seg\u003e\n\u0026lt;seg id=4\u003esource sentence #4\u0026lt;/seg\u003e\n\u0026lt;/document\u003e\n\n==== target ====\n\u0026lt;document id=XXX\u003e\n\u0026lt;seg id=1\u003etarget sentence #1\u0026lt;/seg\u003e\n\u0026lt;seg id=2\u003etarget sentence #2\u0026lt;/seg\u003e\n\u0026lt;seg id=3\u003etarget sentence #3\u0026lt;/seg\u003e\n\u0026lt;seg id=4\u003etarget sentence #4\u0026lt;/seg\u003e\n\u0026lt;/document\u003e\u003c/code\u003e\u003c/pre\u003e\n\nThe inputs to our system should be processed as (suppose that 2 preceding source sentences are used as context):\n\n\u003cpre\u003e\u003ccode\u003e==== train.src ==== (source corpus)\nsource sentence #1\nsource sentence #2\nsource sentence #3\nsource sentence #4\n\n==== train.ctx ==== (context corpus)\n(the first line is empty)\nsource sentence #1\nsource sentence #1 source sentence #2 (there is only a space between the two sentence)\nsource sentence #2 source sentence #3\n\n==== train.trg ==== (target corpus)\ntarget sentence #1\ntarget sentence #2\ntarget sentence #3\ntarget sentence #4\u003c/code\u003e\u003c/pre\u003e\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthunlp-mt%2Fdocument-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthunlp-mt%2Fdocument-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthunlp-mt%2Fdocument-transformer/lists"}