torch.Sequential()
时间: 2023-11-12 11:07:54 浏览: 29
torch.Sequential() is a class in the PyTorch library that allows you to create a neural network model by adding individual layers sequentially. It is a convenient way to define a model architecture in a readable and concise manner.
To use the Sequential() class, you can simply instantiate an object and add layers to it using the .add_module() method. For example:
```
import torch.nn as nn
model = nn.Sequential()
model.add_module('layer1', nn.Linear(10, 20))
model.add_module('relu', nn.ReLU())
model.add_module('layer2', nn.Linear(20, 5))
```
In this example, we create a neural network model with three layers: a linear layer with 10 input features and 20 output features, followed by a ReLU activation function, and then another linear layer with 20 input features and 5 output features.
You can also pass a list of layers to the Sequential() constructor to add them all at once. For example:
```
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 5)
)
```