這是Tonsorflow 2.x變壓器模型的實現(您需要注意的是您需要的)神經機器翻譯(NMT)。

與RNN相比,變壓器具有更靈活的表示上下文的方式。
變形金剛是用於序列建模的深度神經網絡體系結構,這是根據其文本上下文估算序列中令牌可能性的任務。儘管復發性神經網絡將上下文令牌的整個歷史記錄的嵌入到單個向量中,但無論上下文跨越多遠,變形金剛都可以訪問每個單個令牌的嵌入向量。這使其非常適合建模長距離依賴關係,這是文本表示學習方法(例如BERT和GPT-2)的最新突破的關鍵。
變形金剛的核心是自我發揮作用機制,其目標是通過讓他們彼此“注意”來計算每個令牌的上下文表示。 Given the initial vector representations e[i] for all positions i , it first applies linear projections to obtain vectors q[i] , k[i] , v[i] , where k 's and v 's play the role of the key and value of a knowledge base about the sequence content, which is to be queried by q[i] to determine which tokens are most similar to the token at index i .查詢的結果僅僅是Q q[i] ]和k (通常是點產品)之間的相似性得分,這些分數被用作權重,以計算v的加權平均值作為e[i]的新表示。請注意, q , k和v源自相同的序列,這意味著該序列有效地查詢自己(因此名稱自我注意)。

自我注意機制。




變壓器網絡體系結構。
該實現基於Tensorflow 2.x和Python3。此外,需要NLTK來計算BLEU評分進行評估。
您可以通過運行來克隆此存儲庫
git clone [email protected]:chao-ji/tf-transformer.git然後克隆並通過運行來更新子模塊
cd tf-transformer
git submodule update --init --recursive培訓語料庫應採用源語言的文本文件列表的形式,並與目標語言中的文本文件列表配對,其中源語言文本文件中的行(IE句子)具有與目標語言文本文件中的行的一對一信件
source_file_1.txt target_file_1.txt
source_file_2.txt target_file_2.txt
...
source_file_n.txt target_file_n.txt
首先,您需要通過運行將原始文本文件轉換為tfrecord文件
python commons/create_tfrecord_machine_translation.py
--source_filenames=source_file_1.txt,source_file_2.txt,...,source_file_2.txt
--target_filenames=target_file_1.txt,target_file_2.txt,...,target_file_2.txt
--output_dir=/path/to/tfrecord/directory
--vocab_name=vocab注意:此過程涉及從培訓語料庫中“學習”子字代幣的詞彙,該子詞將其保存到vocab.subtokens和vocab.alphabet中。詞彙將稍後用於將原始文本字符串編碼為子字代幣ID,或將其解碼回原始文本字符串。
有關詳細的用法信息,請運行
python commons/create_tfrecord_machine_translation.py --help有關示例數據,請參閱data_sources.txt
訓練模型,運行
python run_trainer.py
--data_dir=/path/to/tfrecord/directory
--vocab_path=/path/to/vocab/files
--model_dir=/path/to/directory/storing/checkpoints data_dir is the directory storing the TFRecord files, vocab_path is the path to the basename of the vocabulary files vocab.subtokens and vocab.alphabet (ie path to vocab ) generated by running create_tfrecord_machine_translation.py , and model_dir is the directory that checkpoint files will be saved to (or loaded from if training is resumed from a previous checkpoint).
有關詳細的用法信息,請運行
python run_trainer.py --help評估涉及將源序列轉換為目標序列,併計算預測目標和地面目標序列之間的BLEU評分。
為了評估預貼模型,運行
python run_evaluator.py
--source_text_filename=/path/to/source/text/file
--target_text_filename=/path/to/target/text/file
--vocab_path=/path/to/vocab/files
--model_dir=/path/to/directory/storing/checkpoints source_text_filename和target_text_filename分別是保存源和目標序列的文本文件的路徑。
請注意,命令行參數target_text_filename是可選的 - 如果排除在外,則評估器將在推理模式下運行,其中僅將翻譯寫入輸出文件。
有關更詳細的用法信息,請運行
python run_evaluator.py --help請注意,注意機制計算了可以可視化的令牌與犯罪的相似性,以了解如何在不同令牌上分佈注意力。當您運行python run_evaluator.py時,注意力矩陣將被保存到attention_xxxx.npy ,其中存儲以下條目:
src :形狀的numpy陣列[batch_size, src_seq_len] ,其中每一行都是以1( EOS_ID )結尾並用零填充的令牌ID序列。tgt :形狀的numpy陣列[batch_size, tgt_seq_len] ,其中每一行都是以1( EOS_ID )結尾的令牌ID序列,並用零填充。src_src_attention :形狀的numpy陣列[batch_size, num_heads, src_seq_len, src_seq_len]tgt_src_attention :形狀的numpy陣列[batch_size, num_heads, tgt_seq_len, src_seq_len]tgt_tgt_attention :形狀的numpy陣列[batch_size, num_heads, tgt_seq_len, tgt_seq_len]注意力可以通過運行來顯示:
python run_visualizer.py
--attention_file=/path/to/attention_xxxx.npy
--head=attention_head
--index=seq_index
--vocab_path=/path/to/vocab/files其中, head是[0, num_heads - 1]中的整數,而index是[0, batch_size - 1]中的整數。
下面顯示的是英語(源語言)的三個句子及其用德語(目標語言)的翻譯。
來源langauge中的輸入句子
1. It is in this spirit that a majority of American governments have passed new laws since 2009 making the registration or voting process more difficult.
2. Google's free service instantly translates words, phrases, and web pages between English and over 100 other languages.
3. What you said is completely absurd.
用目標語言翻譯句子
1. In diesem Sinne haben die meisten amerikanischen Regierungen seit 2009 neue Gesetze verabschiedet, die die Registrierung oder das Abstimmungsverfahren schwieriger machen.
2. Der kostenlose Service von Google übersetzt Wörter, Phrasen und Webseiten zwischen Englisch und über 100 anderen Sprachen.
3. Was Sie gesagt haben, ist völlig absurd.
變壓器模型計算三種類型的注意力:

來源的注意力重量。
請注意,從more_和difficult_ making_注意力的重量 - 在嘗試完成短語“使...更困難”時,它們是“在監視”中。

目標到源的注意力重量。
請注意, übersetz (目標)到translat (源)以及從Webseiten (Target)到web (源)等的注意力重量。這可能是由於它們在德語和英語中的同義詞所致。

目標到目標的注意力重量。
請注意, Was Sie_ , gesagt , haben_關注 - 當解碼器吐出這些微動物Was ,它需要“意識到”該條款的範圍Was Sie gesagt haben (意思是“您所說的”)。