Yukyukuon / blog

博客的文章
1 stars 0 forks source link

Pytorch笔记 #32

Open Yukyukuon opened 5 months ago

Yukyukuon commented 5 months ago

前言:Paris-Saclay的 Deep learning作业,用Pytorch 编写一些小型的网络的笔记。

笔记部分

这里记录了模型的创建步骤和nn.Module 的相关细节。
今后补全:搭建模型的容器Containers,包括 nn.Sequential, nn.ModuleList, nn.ModuleDict,它们各自有各自的特点和应用场景。

Pytorch模型的创建

模型框架

构建模型的两大要素:

如定义模型:

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(30, 10)
        self.fc2 = nn.Linear(10, 2)

    def forward(self, x):
        h = self.fc1(x)
        h = F.relu(h)
        h = self.fc2(h)
        return h
  1. 构建子模块, 这个是在自己建立的模型(继承 nn.Module )的 init() 方法
  2. 拼接子模块, 这个是在模型的 forward() 方法中

定义层

nn.Conv2d()

torch.nn.Conv2d(
    in_channels, 
    out_channels, 
    kernel_size, 
    stride=1, 
    padding=0, 
    dilation=1, 
    groups=1, 
    bias=True, 
    padding_mode='zeros', 
    device=None, 
    dtype=None
)

nn.Linear()

torch.nn.Linear(in_features, out_features, bias=True)