赞
踩
b站小土堆pytorch教程学习笔记
复现CIFAR10网络结构
from torch import nn from torch.nn import Conv2d, MaxPool2d, Flatten, Linear class Han(nn.Module): def __init__(self): super(Han, self).__init__() self.conv1=Conv2d(in_channels=3,out_channels=32, kernel_size=5,padding=2,stride=1) self.maxpool1=MaxPool2d(kernel_size=2) self.conv2=Conv2d(in_channels=32,out_channels=32, kernel_size=5,padding=2,stride=1) self.maxpool2=MaxPool2d(kernel_size=2) self.conv3=Conv2d(in_channels=32,out_channels=64, kernel_size=5,padding=2,stride=1) self.maxpool3=MaxPool2d(kernel_size=2) self.flatten=Flatten() self.linear1=Linear(1024,64) self.linear2=Linear(64,10) def forward(self,x): x = self.conv1(x) x = self.maxpool1(x) x = self.conv2(x) x = self.maxpool2(x) x = self.conv3(x) x = self.maxpool3(x) x = self.flatten(x) x = self.linear1(x) x = self.linear2(x) return x han=Han() print(han)
Han(
(conv1): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv3): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear1): Linear(in_features=1024, out_features=64, bias=True)
(linear2): Linear(in_features=64, out_features=10, bias=True)
)
检查网络正确性:
假定输入
#测试网络结构正确性
input=torch.ones((64,3,32,32))#产生都是1的输入
output=han(input)
print(output)
tensor([[ 0.0063, -0.0712, 0.0809, -0.0330, -0.1598, -0.0949, 0.0303, 0.0632,
0.0453, 0.0606]…
Sequential:
class Han(nn.Module): def __init__(self): super(Han, self).__init__() # self.conv1=Conv2d(in_channels=3,out_channels=32, # kernel_size=5,padding=2,stride=1) # self.maxpool1=MaxPool2d(kernel_size=2) # self.conv2=Conv2d(in_channels=32,out_channels=32, # kernel_size=5,padding=2,stride=1) # self.maxpool2=MaxPool2d(kernel_size=2) # self.conv3=Conv2d(in_channels=32,out_channels=64, # kernel_size=5,padding=2,stride=1) # self.maxpool3=MaxPool2d(kernel_size=2) # self.flatten=Flatten() # self.linear1=Linear(1024,64) # self.linear2=Linear(64,10) self.model1=Sequential( Conv2d(3,32,5,padding=2), MaxPool2d(2), Conv2d(32,32,5,padding=2), MaxPool2d(2), Conv2d(32,64,5,padding=2), MaxPool2d(2), Flatten(), Linear(1024,64), Linear(64,10) ) def forward(self,x): # x = self.conv1(x) # x = self.maxpool1(x) # x = self.conv2(x) # x = self.maxpool2(x) # x = self.conv3(x) # x = self.maxpool3(x) # x = self.flatten(x) # x = self.linear1(x) # x = self.linear2(x) x=self.model1(x) return x han=Han() # print(han) #测试网络结构正确性 input=torch.ones((64,3,32,32))#产生都是1的输入 output=han(input) # print(output) writer=SummaryWriter('logs/seq') writer.add_graph(han,input) writer.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。