/usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__
NameSizeModeActions
activation.cpython-36.pyc476350644editdlrm
adaptive.cpython-36.pyc97250644editdlrm
batchnorm.cpython-36.pyc310390644editdlrm
channelshuffle.cpython-36.pyc19010644editdlrm
container.cpython-36.pyc279700644editdlrm
conv.cpython-36.pyc577890644editdlrm
distance.cpython-36.pyc36840644editdlrm
dropout.cpython-36.pyc103100644editdlrm
flatten.cpython-36.pyc58130644editdlrm
fold.cpython-36.pyc128070644editdlrm
instancenorm.cpython-36.pyc188080644editdlrm
lazy.cpython-36.pyc116900644editdlrm
linear.cpython-36.pyc101880644editdlrm
loss.cpython-36.pyc914650644editdlrm
module.cpython-36.pyc665300644editdlrm
normalization.cpython-36.pyc113600644editdlrm
padding.cpython-36.pyc222170644editdlrm
pixelshuffle.cpython-36.pyc43980644editdlrm
pooling.cpython-36.pyc532030644editdlrm
rnn.cpython-36.pyc446530644editdlrm
sparse.cpython-36.pyc209560644editdlrm
transformer.cpython-36.pyc207370644editdlrm
upsampling.cpython-36.pyc107800644editdlrm
utils.cpython-36.pyc25280644editdlrm
_functions.cpython-36.pyc54410644editdlrm
__init__.cpython-36.pyc52360644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__/transformer.cpython-36.pyc (20737B)
3 Eg[@sddlZddlmZmZmZmZddlZddlmZddlm Z ddl m Z ddl mZdd lmZdd lmZdd lmZdd lmZdd lmZGddde ZGddde ZGddde ZGddde ZGddde ZddZddZdS)N)OptionalAnyUnionCallable)Tensor) functional)Module)MultiheadAttention) ModuleList)xavier_uniform_)Dropout)Linear) LayerNormcseZdZdZddddddejdddd d ddfeeeeeeee e e ge ffe e e e eeedd fd d Zde e e e e e e e e e e e e e e d ddZeee dddZddZZS) 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 iig?Ngh㈵>F) d_modelnheadnum_encoder_layersnum_decoder_layersdim_feedforwarddropout activationcustom_encodercustom_decoderlayer_norm_eps batch_first norm_firstreturnc s| |d}tt|j|dk r(||_n>> output = transformer_model(src, tgt, src_mask=src_mask, tgt_mask=tgt_mask) r z-the batch number of src and tgt must be equalrrz:the feature number of src and tgt must be equal to d_model)maskr9)r7r8r:r;)rsize RuntimeErrorrr&r)) r-r4r5r6r7r8r9r:r;memoryoutputr2r2r3forwardUs0   zTransformer.forward)szr cCstjtj||ftdddS)zGenerate a square mask for the sequence. The masked positions are filled with float('-inf'). Unmasked positions are filled with float(0.0). z-infr )Zdiagonal)torchZtriufullfloat)rBr2r2r3generate_square_subsequent_masksz+Transformer.generate_square_subsequent_maskcCs*x$|jD]}|jdkr t|q WdS)z-Initiate parameters in the transformer model.r N) parametersZdimr )r-pr2r2r3r,s zTransformer._reset_parameters)NNNNNN)__name__ __module__ __qualname____doc__FreluintrErstrrrrrboolr%rA staticmethodrFr, __classcell__r2r2)r1r3rs!J8<rcsFeZdZdZdgZd fdd Zd eeeeeedddZZ S) r(a*TransformerEncoder is a stack of N encoder layers Args: encoder_layer: an instance of the TransformerEncoderLayer() class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6) >>> src = torch.rand(10, 32, 512) >>> out = transformer_encoder(src) normNcs*tt|jt|||_||_||_dS)N)r$r(r% _get_cloneslayers num_layersrT)r-r/rWrT)r1r2r3r%s zTransformerEncoder.__init__)r4r<r9r cCs:|}x|jD]}||||d}q W|jdk r6|j|}|S)aPPass the input through the encoder layers in turn. Args: src: the sequence to the encoder (required). mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. )r6r9N)rVrT)r-r4r<r9r@modr2r2r3rAs    zTransformerEncoder.forward)N)NN) rIrJrKrL __constants__r%rrrArSr2r2)r1r3r(s r(c sTeZdZdZdgZd fdd Zd eeeeeeeeeeedddZZ S) r+a_TransformerDecoder is a stack of N decoder layers Args: decoder_layer: an instance of the TransformerDecoderLayer() class (required). num_layers: the number of sub-decoder-layers in the decoder (required). norm: the layer normalization component (optional). Examples:: >>> decoder_layer = nn.TransformerDecoderLayer(d_model=512, nhead=8) >>> transformer_decoder = nn.TransformerDecoder(decoder_layer, num_layers=6) >>> memory = torch.rand(10, 32, 512) >>> tgt = torch.rand(20, 32, 512) >>> out = transformer_decoder(tgt, memory) rTNcs*tt|jt|||_||_||_dS)N)r$r+r%rUrVrWrT)r-r0rWrT)r1r2r3r%s zTransformerDecoder.__init__)r5r?r7r8r:r;r c Cs@|}x"|jD]}|||||||d}q W|jdk r<|j|}|S)aMPass the inputs (and mask) through the decoder layer in turn. Args: tgt: the sequence to the decoder (required). memory: the sequence from the last layer of the encoder (required). tgt_mask: the mask for the tgt sequence (optional). memory_mask: the mask for the memory sequence (optional). tgt_key_padding_mask: the mask for the tgt keys per batch (optional). memory_key_padding_mask: the mask for the memory keys per batch (optional). Shape: see the docs in Transformer class. )r7r8r:r;N)rVrT) r-r5r?r7r8r:r;r@rXr2r2r3rAs    zTransformerDecoder.forward)N)NNNN) rIrJrKrLrYr%rrrArSr2r2)r1r3r+s r+cseZdZdZddgZddejdddddfdd fd d Zfd d Zde e e e e e dddZ e e e e e e dddZ e e dddZ ZS)r'aTransformerEncoderLayer is made up of self-attn and feedforward network. This standard encoder layer 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 may modify or implement in a different way during application. Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). activation: the activation function of the intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu 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``. norm_first: if ``True``, layer norm is done prior to attention and feedforward operations, respectivaly. Otherwise it's done after. Default: ``False`` (after). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> src = torch.rand(10, 32, 512) >>> out = encoder_layer(src) Alternatively, when ``batch_first`` is ``True``: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8, batch_first=True) >>> src = torch.rand(32, 10, 512) >>> out = encoder_layer(src) rrig?gh㈵>FN)r c s| | d} tt|jt||f||d| |_t||f| |_t||_t||f| |_ ||_ t |fd|i| |_ t |fd|i| |_ t||_t||_t|trt||_n||_dS)N)r!r")rrr#)r$r'r%r self_attnrlinear1rrlinear2rrnorm1norm2dropout1dropout2 isinstancerP_get_activation_fnr) r-rrrrrrrrr!r"r.)r1r2r3r%#s       z TransformerEncoderLayer.__init__cs&d|krtj|d<tt|j|dS)Nr)rMrNr$r' __setstate__)r-state)r1r2r3rc;s z$TransformerEncoderLayer.__setstate__)r4r6r9r cCsh|}|jr8||j|j|||}||j|j|}n,|j||j|||}|j||j|}|S)aQPass the input through the encoder layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. )r _sa_blockr] _ff_blockr^)r-r4r6r9xr2r2r3rA@szTransformerEncoderLayer.forward)rg attn_maskkey_padding_maskr cCs$|j|||||ddd}|j|S)NF)rhri need_weightsr)rZr_)r-rgrhrir2r2r3reYs   z!TransformerEncoderLayer._sa_block)rgr cCs&|j|j|j|j|}|j|S)N)r\rrr[r`)r-rgr2r2r3rfbsz!TransformerEncoderLayer._ff_block)NN)rIrJrKrLrYrMrNr%rcrrrArerfrSr2r2)r1r3r's r'c seZdZdZddgZddejdddddfdd fd d Zfd d Zde e e e e e e e e e e dddZ e e e e e e dddZ e e e e e e e dddZ e e dddZZS)r*ajTransformerDecoderLayer is made up of self-attn, multi-head-attn and feedforward network. This standard decoder layer 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 may modify or implement in a different way during application. Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). activation: the activation function of the intermediate layer, can be a string ("relu" or "gelu") or a unary callable. Default: relu 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``. norm_first: if ``True``, layer norm is done prior to self attention, multihead attention and feedforward operations, respectivaly. Otherwise it's done after. Default: ``False`` (after). Examples:: >>> decoder_layer = nn.TransformerDecoderLayer(d_model=512, nhead=8) >>> memory = torch.rand(10, 32, 512) >>> tgt = torch.rand(20, 32, 512) >>> out = decoder_layer(tgt, memory) Alternatively, when ``batch_first`` is ``True``: >>> decoder_layer = nn.TransformerDecoderLayer(d_model=512, nhead=8, batch_first=True) >>> memory = torch.rand(32, 10, 512) >>> tgt = torch.rand(32, 20, 512) >>> out = decoder_layer(tgt, memory) rrig?gh㈵>FN)r c s| | d} tt|jt||f||d| |_t||f||d| |_t||f| |_t||_ t||f| |_ ||_ t |fd|i| |_ t |fd|i| |_t |fd|i| |_t||_t||_t||_t|trt||_n||_dS)N)r!r")rrr#)r$r*r%r rZmultihead_attnrr[rrr\rrr]r^norm3r_r`dropout3rarPrbr) r-rrrrrrrrr!r"r.)r1r2r3r%s&         z TransformerDecoderLayer.__init__cs&d|krtj|d<tt|j|dS)Nr)rMrNr$r*rc)r-rd)r1r2r3rcs z$TransformerDecoderLayer.__setstate__)r5r?r7r8r:r;r cCs|}|jrR||j|j|||}||j|j||||}||j|j|}nF|j||j|||}|j||j||||}|j||j|}|S)aKPass the inputs (and mask) through the decoder layer. Args: tgt: the sequence to the decoder layer (required). memory: the sequence from the last layer of the encoder (required). tgt_mask: the mask for the tgt sequence (optional). memory_mask: the mask for the memory sequence (optional). tgt_key_padding_mask: the mask for the tgt keys per batch (optional). memory_key_padding_mask: the mask for the memory keys per batch (optional). Shape: see the docs in Transformer class. )rrer] _mha_blockr^rfrl)r-r5r?r7r8r:r;rgr2r2r3rAszTransformerDecoderLayer.forward)rgrhrir cCs$|j|||||ddd}|j|S)NF)rhrirjr)rZr_)r-rgrhrir2r2r3res   z!TransformerDecoderLayer._sa_block)rgmemrhrir cCs$|j|||||ddd}|j|S)NF)rhrirjr)rkr`)r-rgrorhrir2r2r3rns   z"TransformerDecoderLayer._mha_block)rgr cCs&|j|j|j|j|}|j|S)N)r\rrr[rm)r-rgr2r2r3rfsz!TransformerDecoderLayer._ff_block)NNNN)rIrJrKrLrYrMrNr%rcrrrArernrfrSr2r2)r1r3r*gs! ,r*cstfddt|DS)Ncsg|]}tjqSr2)copydeepcopy).0i)moduler2r3 sz_get_clones..)r range)rtNr2)rtr3rUsrUcCs.|dkrtjS|dkrtjStdj|dS)NrNgeluz&activation should be relu/gelu, not {})rMrNrxr>format)rr2r2r3rbs rb) rptypingrrrrrCrrrMrtr rr containerr initr rrZlinearrZ normalizationrrr(r+r'r*rUrbr2r2r2r3s&         ,4ez