本文簡要介紹python語言中 torch.nn.Sequential
的用法。
用法:
class torch.nn.Sequential(*args)
一個順序容器。模塊將按照它們在構造函數中傳遞的順序添加到其中。或者,可以傳入模塊的
OrderedDict
。Sequential
的forward()
方法接受任何輸入並將其轉發到它包含的第一個模塊。然後它“chains” 依次為每個後續模塊輸出到輸入,最後返回最後一個模塊的輸出。與手動調用一係列模塊相比,
Sequential
提供的值在於,它允許將整個容器視為單個模塊,這樣對Sequential
執行的轉換適用於它存儲的每個模塊(每個模塊都是一個模塊)。Sequential
的注冊子模塊)。Sequential
和torch.nn.ModuleList
有什麽區別?ModuleList
正是它聽起來的 like-a 列表,用於存儲Module
s!另一方麵,Sequential
中的層以級聯方式連接。例子:
# Using Sequential to create a small model. When `model` is run, # input will first be passed to `Conv2d(1,20,5)`. The output of # `Conv2d(1,20,5)` will be used as the input to the first # `ReLU`; the output of the first `ReLU` will become the input # for `Conv2d(20,64,5)`. Finally, the output of # `Conv2d(20,64,5)` will be used as input to the second `ReLU` model = nn.Sequential( nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU() ) # Using Sequential with OrderedDict. This is functionally the # same as the above code model = nn.Sequential(OrderedDict([ ('conv1', nn.Conv2d(1,20,5)), ('relu1', nn.ReLU()), ('conv2', nn.Conv2d(20,64,5)), ('relu2', nn.ReLU()) ]))
相關用法
- Python PyTorch SequentialLR用法及代碼示例
- Python PyTorch SequenceEmbeddingAllToAll用法及代碼示例
- Python PyTorch ScaledDotProduct.__init__用法及代碼示例
- Python PyTorch Sigmoid用法及代碼示例
- Python PyTorch ShardedEmbeddingBagCollection.named_parameters用法及代碼示例
- Python PyTorch SummaryWriter.add_histogram用法及代碼示例
- Python PyTorch ScriptModule.state_dict用法及代碼示例
- Python PyTorch Softmin用法及代碼示例
- Python PyTorch SummaryWriter.add_pr_curve用法及代碼示例
- Python PyTorch Softmax2d用法及代碼示例
- Python PyTorch ShardedEmbeddingBag.named_parameters用法及代碼示例
- Python PyTorch ScriptModule.register_full_backward_hook用法及代碼示例
- Python PyTorch SummaryWriter.add_custom_scalars用法及代碼示例
- Python PyTorch ScriptModule.parameters用法及代碼示例
- Python PyTorch ShardedEmbeddingBag.state_dict用法及代碼示例
- Python PyTorch SummaryWriter.add_image用法及代碼示例
- Python PyTorch Store.num_keys用法及代碼示例
- Python PyTorch ShardedEmbeddingBagCollection.named_modules用法及代碼示例
- Python PyTorch SummaryWriter.add_hparams用法及代碼示例
- Python PyTorch ScriptModule.register_forward_hook用法及代碼示例
- Python PyTorch ShardedEmbeddingBagCollection.state_dict用法及代碼示例
- Python PyTorch ScriptModule.modules用法及代碼示例
- Python PyTorch SummaryWriter.__init__用法及代碼示例
- Python PyTorch SparseArch用法及代碼示例
- Python PyTorch ShardedEmbeddingCollection.named_parameters用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.nn.Sequential。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。