/usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__/transformer.cpython-36.pyc (20737B)
3
Eg[ @ s d dl Z d dlmZmZmZmZ d dlZd dlmZ ddlm Z
ddlmZ ddl
mZ dd lmZ dd
lmZ ddlmZ ddlmZ dd
lmZ G dd deZG dd deZG dd deZG dd deZG dd deZdd Zdd ZdS ) N)OptionalAnyUnionCallable)Tensor )
functional )Module)MultiheadAttention)
ModuleList)xavier_uniform_)Dropout)Linear) LayerNormc s e Zd ZdZddddddejdddd d ddfeeeeeeee e
egef f ee
ee
eeedd
fdd
Zdeeee ee ee ee ee ee ed
ddZeeedddZdd Z ZS )Transformera< A transformer model. User is able to modify the attributes as needed. The architecture
is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer,
Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and
Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information
Processing Systems, pages 6000-6010. Users can build the BERT(https://arxiv.org/abs/1810.04805)
model with corresponding parameters.
Args:
d_model: the number of expected features in the encoder/decoder inputs (default=512).
nhead: the number of heads in the multiheadattention models (default=8).
num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6).
num_decoder_layers: the number of sub-decoder-layers in the decoder (default=6).
dim_feedforward: the dimension of the feedforward network model (default=2048).
dropout: the dropout value (default=0.1).
activation: the activation function of encoder/decoder intermediate layer, can be a string
("relu" or "gelu") or a unary callable. Default: relu
custom_encoder: custom encoder (default=None).
custom_decoder: custom decoder (default=None).
layer_norm_eps: the eps value in layer normalization components (default=1e-5).
batch_first: If ``True``, then the input and output tensors are provided
as (batch, seq, feature). Default: ``False`` (seq, batch, feature).
norm_first: if ``True``, encoder and decoder layers will perform LayerNorms before
other attention and feedforward operations, otherwise after. Default: ``False`` (after).
Examples::
>>> transformer_model = nn.Transformer(nhead=16, num_encoder_layers=12)
>>> src = torch.rand((10, 32, 512))
>>> tgt = torch.rand((20, 32, 512))
>>> out = transformer_model(src, tgt)
Note: A full example to apply nn.Transformer module for the word language model is available in
https://github.com/pytorch/examples/tree/master/word_language_model
i i g?Ngh㈵>F)
d_modelnheadnum_encoder_layersnum_decoder_layersdim_feedforwarddropout
activationcustom_encodercustom_decoderlayer_norm_epsbatch_first
norm_firstreturnc s |
|d}t t| j |d k r(|| _n