• pytorch建模的三种方式


    # 可以使用以下3种方式构建模型:
    #
    # 1,继承nn.Module基类构建自定义模型。
    #
    # 2,使用nn.Sequential按层顺序构建模型。
    #
    # 3,继承nn.Module基类构建模型并辅助应用模型容器进行封装(nn.Sequential,nn.ModuleList,nn.ModuleDict)。
    #
    # 其中 第1种方式最为常见,第2种方式最简单,第3种方式最为灵活也较为复杂。
    # 一、继承nn.Module基类构建自定义模型
    1. from torch import nn
    2. class Net(nn.Module):
    3. def __init__(self):
    4. super(Net, self).__init__()
    5. self.conv1 = nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3)
    6. self.pool1 = nn.MaxPool2d(kernel_size = 2,stride = 2)
    7. self.conv2 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5)
    8. self.pool2 = nn.MaxPool2d(kernel_size = 2,stride = 2)
    9. self.dropout = nn.Dropout2d(p = 0.1)
    10. self.adaptive_pool = nn.AdaptiveMaxPool2d((1,1))
    11. self.flatten = nn.Flatten()
    12. self.linear1 = nn.Linear(64,32)
    13. self.relu = nn.ReLU()
    14. self.linear2 = nn.Linear(32,1)
    15. def forward(self,x):
    16. x = self.conv1(x)
    17. x = self.pool1(x)
    18. x = self.conv2(x)
    19. x = self.pool2(x)
    20. x = self.dropout(x)
    21. x = self.adaptive_pool(x)
    22. x = self.flatten(x)
    23. x = self.linear1(x)
    24. x = self.relu(x)
    25. y = self.linear2(x)
    26. return y
    27. net = Net()
    28. print(net)
    29. #查看参数
    30. from torchkeras import summary
    31. summary(net,input_shape= (3,32,32));

     # 二、使用nn.Sequential按层顺序构建模型 # 利用add_module方法

    1. net = nn.Sequential()
    2. net.add_module("conv1",nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3))
    3. net.add_module("pool1",nn.MaxPool2d(kernel_size = 2,stride = 2))
    4. net.add_module("conv2",nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5))
    5. net.add_module("pool2",nn.MaxPool2d(kernel_size = 2,stride = 2))
    6. net.add_module("dropout",nn.Dropout2d(p = 0.1))
    7. net.add_module("adaptive_pool",nn.AdaptiveMaxPool2d((1,1)))
    8. net.add_module("flatten",nn.Flatten())
    9. net.add_module("linear1",nn.Linear(64,32))
    10. net.add_module("relu",nn.ReLU())
    11. net.add_module("linear2",nn.Linear(32,1))
    12. print(net)
    # 利用变长参数
    1. net = nn.Sequential(
    2. nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),
    3. nn.MaxPool2d(kernel_size = 2,stride = 2),
    4. nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),
    5. nn.MaxPool2d(kernel_size = 2,stride = 2),
    6. nn.Dropout2d(p = 0.1),
    7. nn.AdaptiveMaxPool2d((1,1)),
    8. nn.Flatten(),
    9. nn.Linear(64,32),
    10. nn.ReLU(),
    11. nn.Linear(32,1)
    12. )
    13. print(net)
    # 三、继承nn.Module基类构建模型并辅助应用模型容器进行封装
    # nn.Sequential作为模型容器
    1. class Net(nn.Module):
    2. def __init__(self):
    3. super(Net, self).__init__()
    4. self.conv = nn.Sequential(
    5. nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),
    6. nn.MaxPool2d(kernel_size = 2,stride = 2),
    7. nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),
    8. nn.MaxPool2d(kernel_size = 2,stride = 2),
    9. nn.Dropout2d(p = 0.1),
    10. nn.AdaptiveMaxPool2d((1,1))
    11. )
    12. self.dense = nn.Sequential(
    13. nn.Flatten(),
    14. nn.Linear(64,32),
    15. nn.ReLU(),
    16. nn.Linear(32,1)
    17. )
    18. def forward(self,x):
    19. x = self.conv(x)
    20. y = self.dense(x)
    21. return y
    22. net = Net()
    23. print(net)
    # nn.ModuleList作为模型容器
    # 注意下面中的ModuleList不能用Python中的列表代替。(即不用省略)
    1. class Net(nn.Module):
    2. def __init__(self):
    3. super(Net, self).__init__()
    4. self.layers = nn.ModuleList([
    5. nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),
    6. nn.MaxPool2d(kernel_size = 2,stride = 2),
    7. nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),
    8. nn.MaxPool2d(kernel_size = 2,stride = 2),
    9. nn.Dropout2d(p = 0.1),
    10. nn.AdaptiveMaxPool2d((1,1)),
    11. nn.Flatten(),
    12. nn.Linear(64,32),
    13. nn.ReLU(),
    14. nn.Linear(32,1)]
    15. )
    16. def forward(self,x):
    17. for layer in self.layers:
    18. x = layer(x)
    19. return x
    20. net = Net()
    21. print(net)
    # nn.ModuleDict作为模型容器
    1. class Net(nn.Module):
    2. def __init__(self):
    3. super(Net, self).__init__()
    4. self.layers_dict = nn.ModuleDict({"conv1":nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3),
    5. "pool": nn.MaxPool2d(kernel_size = 2,stride = 2),
    6. "conv2":nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5),
    7. "dropout": nn.Dropout2d(p = 0.1),
    8. "adaptive":nn.AdaptiveMaxPool2d((1,1)),
    9. "flatten": nn.Flatten(),
    10. "linear1": nn.Linear(64,32),
    11. "relu":nn.ReLU(),
    12. "linear2": nn.Linear(32,1)
    13. })
    14. def forward(self,x):
    15. layers = ["conv1","pool","conv2","pool","dropout","adaptive",
    16. "flatten","linear1","relu","linear2","sigmoid"]
    17. for layer in layers:
    18. x = self.layers_dict[layer](x) # 只找有的 sigmoid是没有的
    19. return x
    20. net = Net()
    21. print(net)

  • 相关阅读:
    黄州科目三
    LeetCode_优先级队列_中等_373.查找和最小的 K 对数字
    每天一点C++——C++中的强制类型转换
    【Proteus仿真】【STM32单片机】智能加湿器设计
    蓝桥杯day7——DFS&&BFS
    半回文数【Python】
    TypeError: ‘float‘ object is not subscriptable 已解决
    MyBatis逆向工程 —【解决生成多个实体——解决映射文件重复出现BaseResultMap】
    球的状态判定技巧
    MySQL-数据类型和DDL
  • 原文地址:https://blog.csdn.net/chehec2010/article/details/136201470