nn.Sequential
时间: 2023-10-23 14:09:54 浏览: 84
nn.Sequential is a container module provided by the PyTorch library that allows you to stack multiple layers sequentially. It is an ordered list of neural network modules where each module is connected to the next one. This module provides a convenient way to build sequential models in PyTorch.
For example, if you want to build a simple feedforward neural network with three fully connected layers, you can use the nn.Sequential module as follows:
```
import torch.nn as nn
model = nn.Sequential(
nn.Linear(784, 256),
nn.ReLU(),
nn.Linear(256, 64),
nn.ReLU(),
nn.Linear(64, 10),
nn.LogSoftmax(dim=1)
)
```
In this example, the first layer is a linear layer with 784 input neurons and 256 output neurons. The second layer applies the ReLU activation function to the output of the first layer. The third layer is another linear layer with 256 input neurons and 64 output neurons, followed by another ReLU activation function. The fourth layer is a linear layer with 64 input neurons and 10 output neurons, and the final layer applies the LogSoftmax activation function to the output of the previous layer.
Using nn.Sequential, you can easily build complex models by stacking layers sequentially, without having to manually connect each layer to the next one.
阅读全文