基于Pytorch的网络设计语法4

import  torch.nn as nn
import  torch.functional as F
import  torch.optim as optim
from collections  import OrderedDict

class Net4(nn.Module):# 从nn.Module  继承
    def __init__(self):# 在类的初始化函数里完成曾的构建
        super(Net4,self).__init__()
        #Sequential里面的顺序 就是前往传播的顺序
        #OrderedDict  是有序字典
        self.block=nn.Sequential(
            OrderedDict(
                [
                    ("conv1", nn.Conv2d(3, 32, 3, 1, 1)),
                    ("relu1", nn.ReLU()),
                    ("conv2", nn.Conv2d(32, 64, 3, 1, 1)),
                    ("relu2", nn.ReLU())
                ]
            )
        )

        self.module = nn.Sequential(
            OrderedDict(
                [
                    ("conv3", nn.Conv2d(3, 32, 3, 1, 1)),
                    ("relu3", nn.ReLU())
                ]
            )
        )

    def forward(self,input_x):# 构建前向传播的流程
        conv_out=self.block(input_x)
        res=conv_out.view(conv_out.size(0),-1)#拉伸处理
        out =self.module(res)
        return  out

gsznet = Net4()
print(gsznet)
if __name__ == '__main__':
    print("XXXXXXXXXXXXXX")

  输出的内容

C:\Users\ai\AppData\Local\Programs\Python\Python38\python.exe E:\yousan.ai-master\computer_vision\projects\classification\pytorch\simpleconv3\设计网络.py
Net4(
(block): Sequential(
(conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu1): ReLU()
(conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu2): ReLU()
)
(module): Sequential(
(conv3): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu3): ReLU()
)
)
XXXXXXXXXXXXXX

进程已结束,退出代码为 0

posted @ 2024-01-03 14:36  wenluderen  阅读(4)  评论(0编辑  收藏  举报