

Transformer模型是一種採用自注意力機制的深度學習模型,這一機制可以按輸入數據各部分重要性的不同而分配不同的權重。該模型主要用於自然語言處理與計算機視覺領域。 與循環神經網絡一樣,Transformer模型旨在處理自然語言等順序輸入數據,可應用於翻譯、文本摘要等任務。
這邊為了練習,盡可能使用乾淨的內容去實現。實驗檔案Colab

模型定義
ntoken # 字詞數量
d_model # 嵌入層的維度
nhead # nn.MultiheadAttention 中的頭數
d_hid # nn.TransformerEncoder 中前饋網絡模型的維度
nlayers # nn.TransformerEncoder 中的 nn.TransformerEncoderLayer 的數量
dropout # 捨棄概率


訓練腳本
