前言
通过定义代码来按需⽣成任意复杂度的块,我们可以通过简洁的代码实现复杂的神经⽹络。
1.引入库
import torch
from torch import nn
from torch.nn import functional as F
2.实现方式
2.1无名称
net1 = nn.Sequential(nn.Linear(10,20),
nn.ReLU(),
nn.Linear(20,3))
print(net1)
"""
输出结果:
Sequential(
(0): Linear(in_features=10, out_features=20, bias=True)
(1): ReLU()
(2): Linear(in_features=20, out_features=3, bias=True)
)
"""
这种方式直接在Sequential中按顺序添加层,每一层没有名字只有索引
X