第一种语法:
层层堆叠
import torch.nn as nn import torch.functional as F import torch.optim as optim from collections import OrderedDict class Net1(nn.Module):# 从nn.Module 继承 def __init__(self):# 在类的初始化函数里完成曾的构建 super(Net1,self).__init__() self.conv1=nn.Conv2d(3,32,3,1,1) self.dense1=nn.Linear(32*3*3,128)#全连接层 self.dense2 = nn.Linear(128,10)#全连接层 def forward(self,x):# 构建前向传播的流程 x=F.max_pool2d(F.relu(self.conv(x)),2)# 先卷积, 然后激活, 然后最大池化 x=x.view(x.size(0),-1)#拉伸 为1维 x=F.relu(self.dense1(x))# 全链接 ,然后激活 x=self.dense2(x)# 再次全链接 return x gsznet = Net1() print(gsznet) if __name__ == '__main__': print("XXXXXXXXXXXXXX")
标签:__,基于,nn,self,torch,语法,Pytorch,import,Net1 From: https://www.cnblogs.com/wenluderen/p/17942981