- https://github.com/huggingface/transformers/tree/master/src/transformers
- https://github.com/facebookresearch/detr/blob/main/models/transformer.py
- https://github.com/yusugomori/deeplearning-keras-tf2-torch/blob/master/6/08_transformer_torch.py
この辺りだろうか。(facebookresearch/detr のは torch.nn.Transformer
の改造版とソースコード内に書いてある。)
或は公式の
- Transformer — PyTorch 1.10.0 documentation
- しかし、Does nn.Transformer include the PositionalEncoding() so far? · Issue #51551 · pytorch/pytorch · GitHub によると何故か位置エンコーディングが
nn.Transformer
には含まれていない・・・。 - Transformer Lack of Embedding Layer and Positional Encodings · Issue #24826 · pytorch/pytorch · GitHub も見ると、重要ではあるもののフーリエ位置エンコーディングなど、他の実装も考えられるし、位置エンコーディングを使用しないモデルもあるので一旦切り離されているようだ。
- detr/position_encoding.py at main · facebookresearch/detr · GitHub でも位置エンコーディングを切り替えられるようにしている。
- しかし、Does nn.Transformer include the PositionalEncoding() so far? · Issue #51551 · pytorch/pytorch · GitHub によると何故か位置エンコーディングが
なので、今のところはオリジナルの実装に合わせるのであれば部品だけもらって、同じく公式のチュートリアルのように組み立てるのが良いのだろうか?
- Language Modeling with nn.Transformer and TorchText — PyTorch Tutorials 1.10.0+cu102 documentation
- examples/model.py at master · pytorch/examples · GitHub
とかだろうか。