本文简要介绍python语言中 torch.nn.Transformer
的用法。
用法:
class torch.nn.Transformer(d_model=512, nhead=8, num_encoder_layers=6, num_decoder_layers=6, dim_feedforward=2048, dropout=0.1, activation=<function relu>, custom_encoder=None, custom_decoder=None, layer_norm_eps=1e-05, batch_first=False, norm_first=False, device=None, dtype=None)
d_model-编码器/解码器输入中预期特征的数量(默认值=512)。
nhead-多头注意力模型中的头数(默认值=8)。
num_encoder_layers-编码器中sub-encoder-layers 的数量(默认=6)。
num_decoder_layers-解码器中sub-decoder-layers 的数量(默认=6)。
dim_feedforward-前馈网络模型的维度(默认=2048)。
dropout-辍学值(默认= 0.1)。
activation-编码器/解码器中间层的激活函数,可以是字符串(“relu”或“gelu”)或一元可调用函数。默认值:relu
custom_encoder-自定义编码器(默认=无)。
custom_decoder-自定义解码器(默认=无)。
layer_norm_eps-层标准化组件中的 eps 值(默认值=1e-5)。
batch_first-如果
True
,则输入和输出张量提供为 (batch, seq, feature)。默认值:False
(序列、批处理、特征)。norm_first-如果
True
,编码器和解码器层将在其他注意和前馈操作之前执行LayerNorms,否则在之后。默认值:False
(之后)。
转换器模型。用户可以根据需要修改属性。该架构基于论文“Attention Is All You Need”。 Ashish Vaswani、Noam Shazeer、Niki Parmar、Jakob Uszkoreit、Llion Jones、Aidan N Gomez、Lukasz Kaiser 和 Illia Polosukhin。 2017. 注意力就是你所需要的。在神经信息处理系统的进展中,第 6000-6010 页。用户可以构建带有相应参数的BERT(https://arxiv.org/abs/1810.04805)模型。
- 例子::
>>> 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)
注意:为单词语言模型应用 nn.Transformer 模块的完整示例可在https://github.com/pytorch/examples/tree/master/word_language_model
参数:
相关用法
- Python PyTorch TransformerEncoder用法及代码示例
- Python PyTorch Transformer用法及代码示例
- Python PyTorch TransformerDecoderLayer用法及代码示例
- Python PyTorch TransformerDecoder用法及代码示例
- Python PyTorch Transformer.forward用法及代码示例
- Python PyTorch TransformerEncoderLayer用法及代码示例
- Python PyTorch TransformedDistribution用法及代码示例
- Python PyTorch Transform用法及代码示例
- Python PyTorch TripletMarginLoss用法及代码示例
- Python PyTorch TripletMarginWithDistanceLoss用法及代码示例
- Python PyTorch Tensor.unflatten用法及代码示例
- Python PyTorch Tensor.register_hook用法及代码示例
- Python PyTorch TarArchiveLoader用法及代码示例
- Python PyTorch Tensor.storage_offset用法及代码示例
- Python PyTorch Tensor.to用法及代码示例
- Python PyTorch Tensor.sparse_mask用法及代码示例
- Python PyTorch Timer用法及代码示例
- Python PyTorch TimeMasking用法及代码示例
- Python PyTorch Tacotron2TTSBundle.get_text_processor用法及代码示例
- Python PyTorch Tensor.is_leaf用法及代码示例
- Python PyTorch Tensor.imag用法及代码示例
- Python PyTorch Tensor.unfold用法及代码示例
- Python PyTorch TenCrop用法及代码示例
- Python PyTorch Tensor.real用法及代码示例
- Python PyTorch TwRwSparseFeaturesDist用法及代码示例
注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torch.nn.Transformer。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。