1 模型三要素 三要素其实很简单
必须要继承nn.Module这个类,要让PyTorch知道这个类是一个Module 在__init__(self)中设置好需要的组件,比如conv,pooling,Linear,BatchNorm等等 最后在forward(self,x)中用定义好的组件进行组装,就像搭积木,把网络结构搭建出来,这样一个模型就定义好了 我们来看一个例子: 先看__init__(self)函数
def init(self): super(Net,self).init() self.conv1 = nn.Conv2d(3,6,5) self.pool1 = nn.MaxPool2d(2,2) self.conv2 = nn.Conv2d(6,16,5) self.pool2 = nn.MaxPool2d(2,2) self.fc1 = nn.Linear(1655,120) self.fc2 = nn.Linear(120,84) self.fc3 = nn.Linear(84,10) 第一行是初始化,往后定义了一系列组件。nn.Conv2d就是一般图片处理的卷积模块,然后池化层,全连接层等等。
定义完这些定义forward函数
def forward(self,x): x = self.pool1(F.relu(self.conv1(x))) x = self.pool2(F.relu(self.conv2(x))) x = x.view(-1,1655) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x x为模型的输入,第一行表示x经过conv1,然后经过激活函数relu,然后经过pool1操作 第三行表示对x进行reshape,为后面的全连接层做准备
至此,对一个模型的定义完毕,如何使用呢?
例如:
net = Net() outputs = net(inputs) 其实net(inputs),就是类似于使用了net.forward(inputs)这个函数。
2 参数初始化 简单地说就是设定什么层用什么初始方法,初始化的方法会在torch.nn.init中
话不多说,看一个案例:
def initialize_weights(self): for m in self.modules(): if isinstance(m,nn.Conv2d): torch.nn.init.xavier_normal_(m.weight.data) if m.bias is not None: m.bias.data.zero_() elif isinstance(m,nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m,nn.Linear): torch.nn.init.normal_(m.weight.data,0,0.01) # m.weight.data.normal_(0,0.01) m.bias.data.zero_() 这段代码的基本流程就是,先从self.modules()中遍历每一层,然后判断更曾属于什么类型,是否是Conv2d,是否是BatchNorm2d,是否是Linear的,然后根据不同类型的层,设定不同的权值初始化方法,例如Xavier,kaiming,normal_等等。kaiming也是MSRA初始化,是何恺明大佬在微软亚洲研究院的时候,因此得名。
上面代码中用到了self.modules(),这个是什么东西呢?
def modules(self): for name,module in self.named_modules(): yield module 功能就是:能依次返回模型中的各层,yield是让一个函数可以像迭代器一样可以用for循环不断从里面遍历(可能说的不太明确)。
3 完整运行代码 我们用下面的例子来更深入的理解self.modules(),同时也把上面的内容都串起来(下面的代码块可以运行):
import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import Dataset,DataLoader
class Net(nn.Module): def init(self): super(Net, self).init() self.conv1 = nn.Conv2d(3, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x): x = self.pool1(F.relu(self.conv1(x))) x = self.pool2(F.relu(self.conv2(x))) x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x def initialize_weights(self): for m in self.modules(): if isinstance(m, nn.Conv2d): torch.nn.init.xavier_normal_(m.weight.data) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): torch.nn.init.normal_(m.weight.data, 0, 0.01) # m.weight.data.normal_(0,0.01) m.bias.data.zero_()net = Net() net.initialize_weights() print(net.modules()) for m in net.modules(): print(m) 运行结果:
<generator object Module.modules at 0x0000023BDCA23258>
Net( (conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1)) (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) (fc1): Linear(in_features=400, out_features=120, bias=True) (fc2): Linear(in_features=120, out_features=84, bias=True) (fc3): Linear(in_features=84, out_features=10, bias=True) )
Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1)) MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) Linear(in_features=400, out_features=120, bias=True) Linear(in_features=120, out_features=84, bias=True) Linear(in_features=84, out_features=10, bias=True) 其中呢,并不是每一层都有偏执bias的,有的卷积层可以设置成不要bias的,所以对于卷积网络参数的初始化,需要判断一下是否有bias,(不过我好像记得bias默认初始化为0?不确定,有知道的朋友可以交流)
torch.nn.init.xavier_normal(m.weight.data) if m.bias is not None: m.bias.data.zero_() 上面代码表示用xavier_normal方法对该层的weight初始化,并判断是否存在偏执bias,若存在,将bias初始化为0。
4 尺寸计算与参数计算 我们把上面的主函数部分改成:
net = Net() net.initialize_weights() layers = {} for m in net.modules(): if isinstance(m,nn.Conv2d): print(m) break 这里的输出m就是:
Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1)) 这个卷积层,就是我们设置的第一个卷积层,含义就是:输入3通道,输出6通道,卷积核5×5,步长1,padding=0.
【问题1:输入特征图和输出特征图的尺寸计算】
之前的文章也讲过这个了,
output=input+2×padding−kernelstride+1
用代码来验证一下这个公式:
net = Net() net.initialize_weights() input = torch.ones((16,3,10,10)) output = net.conv1(input) print(input.shape) print(output.shape) 初始结果:
torch.Size([16, 3, 10, 10]) torch.Size([16, 6, 6, 6]) 第一个维度上batch,第二个是通道channel,第三个和第四个是图片(特征图)的尺寸。
10+2×0−51+1=6 算出来的结果没毛病。
【问题2:这个卷积层中有多少的参数?】 输入通道是3通道的,输出是6通道的,卷积核是5×5的,所以理解为6个3×5×5的卷积核,所以不考虑bias的话,参数量是3×5×5×6=450,考虑bais的话,就每一个卷积核再增加一个偏置值。(这是一个一般人会忽略的知识点欸) http://bbs.quyouji.com/topic.asp?id=82 http://bbs.quyouji.com/topic.asp?id=83 http://bbs.quyouji.com/topic.asp?id=84 http://bbs.quyouji.com/topic.asp?id=85 http://bbs.quyouji.com/topic.asp?id=86 http://bbs.quyouji.com/topic.asp?id=87 http://bbs.quyouji.com/topic.asp?id=88 http://bbs.quyouji.com/topic.asp?id=89 http://bbs.quyouji.com/topic.asp?id=90 http://bbs.quyouji.com/topic.asp?id=91 http://bbs.quyouji.com/topic.asp?id=92 http://bbs.quyouji.com/topic.asp?id=93 http://bbs.quyouji.com/topic.asp?id=94 http://bbs.quyouji.com/topic.asp?id=95 http://bbs.quyouji.com/topic.asp?id=96 http://bbs.quyouji.com/topic.asp?id=97 http://bbs.quyouji.com/topic.asp?id=98 http://bbs.quyouji.com/topic.asp?id=99 http://bbs.quyouji.com/topic.asp?id=100 http://bbs.quyouji.com/topic.asp?id=101 http://bbs.quyouji.com/topic.asp?id=102 http://bbs.quyouji.com/topic.asp?id=103 http://bbs.quyouji.com/topic.asp?id=104 http://bbs.quyouji.com/topic.asp?id=105 http://bbs.quyouji.com/topic.asp?id=106 http://bbs.quyouji.com/topic.asp?id=107 http://bbs.quyouji.com/topic.asp?id=108 http://bbs.quyouji.com/topic.asp?id=109 http://bbs.quyouji.com/topic.asp?id=110 http://bbs.quyouji.com/topic.asp?id=111 http://bbs.quyouji.com/topic.asp?id=112 http://bbs.quyouji.com/topic.asp?id=113 http://bbs.quyouji.com/topic.asp?id=114 http://bbs.quyouji.com/topic.asp?id=115 http://bbs.quyouji.com/topic.asp?id=116 http://bbs.quyouji.com/topic.asp?id=117 http://bbs.quyouji.com/topic.asp?id=118 http://bbs.quyouji.com/topic.asp?id=119 http://bbs.quyouji.com/topic.asp?id=120 http://bbs.quyouji.com/topic.asp?id=121 http://bbs.quyouji.com/topic.asp?id=122 http://bbs.quyouji.com/topic.asp?id=123 http://bbs.quyouji.com/topic.asp?id=124 http://bbs.quyouji.com/topic.asp?id=125 http://bbs.quyouji.com/topic.asp?id=126 http://bbs.quyouji.com/topic.asp?id=127 http://bbs.quyouji.com/topic.asp?id=128 http://bbs.quyouji.com/topic.asp?id=129 http://bbs.quyouji.com/topic.asp?id=130 http://bbs.quyouji.com/topic.asp?id=131 http://bbs.quyouji.com/topic.asp?id=132 http://bbs.quyouji.com/topic.asp?id=133 http://bbs.quyouji.com/topic.asp?id=134 http://bbs.quyouji.com/topic.asp?id=135 http://bbs.quyouji.com/topic.asp?id=136 http://bbs.quyouji.com/topic.asp?id=137 http://bbs.quyouji.com/topic.asp?id=138 http://bbs.quyouji.com/topic.asp?id=139 http://bbs.quyouji.com/topic.asp?id=140 http://bbs.quyouji.com/topic.asp?id=141 http://bbs.quyouji.com/topic.asp?id=142 http://bbs.quyouji.com/topic.asp?id=143 http://bbs.quyouji.com/topic.asp?id=144 http://bbs.quyouji.com/topic.asp?id=145 http://bbs.quyouji.com/topic.asp?id=146 http://bbs.quyouji.com/topic.asp?id=147 http://bbs.quyouji.com/topic.asp?id=148 http://bbs.quyouji.com/topic.asp?id=149 http://bbs.quyouji.com/topic.asp?id=150 http://bbs.quyouji.com/topic.asp?id=151 http://bbs.quyouji.com/topic.asp?id=152 http://bbs.quyouji.com/topic.asp?id=153 http://bbs.quyouji.com/topic.asp?id=154 http://bbs.quyouji.com/topic.asp?id=155 http://bbs.quyouji.com/topic.asp?id=156 http://bbs.quyouji.com/topic.asp?id=157 http://bbs.quyouji.com/topic.asp?id=158 http://bbs.quyouji.com/topic.asp?id=159 http://bbs.quyouji.com/topic.asp?id=160 http://bbs.quyouji.com/topic.asp?id=161 http://bbs.quyouji.com/topic.asp?id=162 http://bbs.quyouji.com/topic.asp?id=163 http://bbs.quyouji.com/topic.asp?id=164 http://bbs.quyouji.com/topic.asp?id=165 http://bbs.quyouji.com/topic.asp?id=166 http://bbs.quyouji.com/topic.asp?id=167 http://bbs.quyouji.com/topic.asp?id=168 http://bbs.quyouji.com/topic.asp?id=169 http://bbs.quyouji.com/topic.asp?id=170 http://bbs.quyouji.com/topic.asp?id=171 http://bbs.quyouji.com/topic.asp?id=172 http://bbs.quyouji.com/topic.asp?id=173 http://bbs.quyouji.com/topic.asp?id=174 http://bbs.quyouji.com/topic.asp?id=175 http://bbs.quyouji.com/topic.asp?id=176 http://bbs.quyouji.com/topic.asp?id=177 http://bbs.quyouji.com/topic.asp?id=178 http://bbs.quyouji.com/topic.asp?id=179 http://bbs.quyouji.com/topic.asp?id=180 http://bbs.quyouji.com/topic.asp?id=181 http://bbs.quyouji.com/topic.asp?id=182 http://bbs.quyouji.com/topic.asp?id=183 http://bbs.quyouji.com/topic.asp?id=184 http://bbs.quyouji.com/topic.asp?id=185 http://bbs.quyouji.com/topic.asp?id=186 http://bbs.quyouji.com/topic.asp?id=187 http://bbs.quyouji.com/topic.asp?id=188 http://bbs.quyouji.com/topic.asp?id=189 http://bbs.quyouji.com/topic.asp?id=190 http://bbs.quyouji.com/topic.asp?id=191 http://bbs.quyouji.com/topic.asp?id=192 http://bbs.quyouji.com/topic.asp?id=193 http://bbs.quyouji.com/topic.asp?id=194 http://bbs.quyouji.com/topic.asp?id=195 http://bbs.quyouji.com/topic.asp?id=196 http://bbs.quyouji.com/topic.asp?id=197 http://bbs.quyouji.com/topic.asp?id=198 http://bbs.quyouji.com/topic.asp?id=199 http://bbs.quyouji.com/topic.asp?id=200 http://bbs.quyouji.com/topic.asp?id=201 http://bbs.quyouji.com/topic.asp?id=202 http://bbs.quyouji.com/topic.asp?id=203 http://bbs.quyouji.com/topic.asp?id=204 http://bbs.quyouji.com/topic.asp?id=205 http://bbs.quyouji.com/topic.asp?id=206 http://bbs.quyouji.com/topic.asp?id=207 http://bbs.quyouji.com/topic.asp?id=208 http://bbs.quyouji.com/topic.asp?id=209 http://bbs.quyouji.com/topic.asp?id=210 http://bbs.quyouji.com/topic.asp?id=211 http://bbs.quyouji.com/topic.asp?id=212 http://bbs.quyouji.com/topic.asp?id=213 http://bbs.quyouji.com/topic.asp?id=214 http://bbs.quyouji.com/topic.asp?id=215 http://bbs.quyouji.com/topic.asp?id=216 http://bbs.quyouji.com/topic.asp?id=217 http://bbs.quyouji.com/topic.asp?id=218 http://bbs.quyouji.com/topic.asp?id=219 http://bbs.quyouji.com/topic.asp?id=220 http://bbs.quyouji.com/topic.asp?id=221 http://bbs.quyouji.com/topic.asp?id=222 http://bbs.quyouji.com/topic.asp?id=223 http://bbs.quyouji.com/topic.asp?id=224 http://bbs.quyouji.com/topic.asp?id=225 http://bbs.quyouji.com/topic.asp?id=226 http://bbs.quyouji.com/topic.asp?id=227 http://bbs.quyouji.com/topic.asp?id=228 http://bbs.quyouji.com/topic.asp?id=229 http://bbs.quyouji.com/topic.asp?id=230 http://bbs.quyouji.com/topic.asp?id=231 http://bbs.quyouji.com/topic.asp?id=232 http://bbs.quyouji.com/topic.asp?id=233 http://bbs.quyouji.com/topic.asp?id=234 http://bbs.quyouji.com/topic.asp?id=235 http://bbs.quyouji.com/topic.asp?id=236 http://bbs.quyouji.com/topic.asp?id=237 http://bbs.quyouji.com/topic.asp?id=238 http://bbs.quyouji.com/topic.asp?id=239 http://bbs.quyouji.com/topic.asp?id=240 http://bbs.quyouji.com/topic.asp?id=241 http://bbs.quyouji.com/topic.asp?id=242 http://bbs.quyouji.com/topic.asp?id=243 http://bbs.quyouji.com/topic.asp?id=244 http://bbs.quyouji.com/topic.asp?id=245 http://bbs.quyouji.com/topic.asp?id=246 http://bbs.quyouji.com/topic.asp?id=247 http://bbs.quyouji.com/topic.asp?id=248 http://bbs.quyouji.com/topic.asp?id=249 http://bbs.quyouji.com/topic.asp?id=250 http://bbs.quyouji.com/topic.asp?id=251 http://bbs.quyouji.com/topic.asp?id=252 http://bbs.quyouji.com/topic.asp?id=253 http://bbs.quyouji.com/topic.asp?id=254 http://bbs.quyouji.com/topic.asp?id=255 http://bbs.quyouji.com/topic.asp?id=256 http://bbs.quyouji.com/topic.asp?id=257 http://bbs.quyouji.com/topic.asp?id=258 http://bbs.quyouji.com/topic.asp?id=259 http://bbs.quyouji.com/topic.asp?id=260 http://bbs.quyouji.com/topic.asp?id=261 http://bbs.quyouji.com/topic.asp?id=262 http://bbs.quyouji.com/topic.asp?id=263 http://bbs.quyouji.com/topic.asp?id=264 http://bbs.quyouji.com/topic.asp?id=265 http://bbs.quyouji.com/topic.asp?id=266 http://bbs.quyouji.com/topic.asp?id=267 http://bbs.quyouji.com/topic.asp?id=268 http://bbs.quyouji.com/topic.asp?id=269 http://bbs.quyouji.com/topic.asp?id=270 http://bbs.quyouji.com/topic.asp?id=271 http://bbs.quyouji.com/topic.asp?id=272 http://bbs.quyouji.com/topic.asp?id=273 http://bbs.quyouji.com/topic.asp?id=274 http://bbs.quyouji.com/topic.asp?id=275 http://bbs.quyouji.com/topic.asp?id=276 http://bbs.quyouji.com/topic.asp?id=277 http://bbs.quyouji.com/topic.asp?id=278 http://bbs.quyouji.com/topic.asp?id=279 http://bbs.quyouji.com/topic.asp?id=280 http://bbs.quyouji.com/topic.asp?id=281 http://bbs.quyouji.com/topic.asp?id=282 http://bbs.quyouji.com/topic.asp?id=283 http://bbs.quyouji.com/topic.asp?id=284 http://bbs.quyouji.com/topic.asp?id=285 http://bbs.quyouji.com/topic.asp?id=286 http://bbs.quyouji.com/topic.asp?id=287 http://bbs.quyouji.com/topic.asp?id=288 http://bbs.quyouji.com/topic.asp?id=289 http://bbs.quyouji.com/topic.asp?id=290 http://bbs.quyouji.com/topic.asp?id=291 http://bbs.quyouji.com/topic.asp?id=292 http://bbs.quyouji.com/topic.asp?id=293 http://bbs.quyouji.com/topic.asp?id=294 http://bbs.quyouji.com/topic.asp?id=295 http://bbs.quyouji.com/topic.asp?id=296 http://bbs.quyouji.com/topic.asp?id=297 http://bbs.quyouji.com/topic.asp?id=298 http://bbs.quyouji.com/topic.asp?id=299 http://bbs.quyouji.com/topic.asp?id=300 http://bbs.quyouji.com/topic.asp?id=301 http://bbs.quyouji.com/topic.asp?id=302 http://bbs.quyouji.com/topic.asp?id=303 http://bbs.quyouji.com/topic.asp?id=304 http://bbs.quyouji.com/topic.asp?id=305 http://bbs.quyouji.com/topic.asp?id=306 http://bbs.quyouji.com/topic.asp?id=307 http://bbs.quyouji.com/topic.asp?id=308 http://bbs.quyouji.com/topic.asp?id=309 http://bbs.quyouji.com/topic.asp?id=310 http://bbs.quyouji.com/topic.asp?id=311 http://bbs.quyouji.com/topic.asp?id=312 http://bbs.quyouji.com/topic.asp?id=313 http://bbs.quyouji.com/topic.asp?id=314 http://bbs.quyouji.com/topic.asp?id=315 http://bbs.quyouji.com/topic.asp?id=316 http://bbs.quyouji.com/topic.asp?id=317 http://bbs.quyouji.com/topic.asp?id=318 http://bbs.quyouji.com/topic.asp?id=319 http://bbs.quyouji.com/topic.asp?id=320 http://bbs.quyouji.com/topic.asp?id=321 http://bbs.quyouji.com/topic.asp?id=322 http://bbs.quyouji.com/topic.asp?id=323 http://bbs.quyouji.com/topic.asp?id=324 http://bbs.quyouji.com/topic.asp?id=325 http://bbs.quyouji.com/topic.asp?id=326 http://bbs.quyouji.com/topic.asp?id=327 http://bbs.quyouji.com/topic.asp?id=328 http://bbs.quyouji.com/topic.asp?id=329 http://bbs.quyouji.com/topic.asp?id=330 http://bbs.quyouji.com/topic.asp?id=331 http://bbs.quyouji.com/topic.asp?id=332 http://bbs.quyouji.com/topic.asp?id=333 http://bbs.quyouji.com/topic.asp?id=334 http://bbs.quyouji.com/topic.asp?id=335 http://bbs.quyouji.com/topic.asp?id=336 http://bbs.quyouji.com/topic.asp?id=337 http://bbs.quyouji.com/topic.asp?id=338 http://bbs.quyouji.com/topic.asp?id=339 http://bbs.quyouji.com/topic.asp?id=340 http://bbs.quyouji.com/topic.asp?id=341 http://bbs.quyouji.com/topic.asp?id=342 http://bbs.quyouji.com/topic.asp?id=343 http://bbs.quyouji.com/topic.asp?id=344 http://bbs.quyouji.com/topic.asp?id=345 http://bbs.quyouji.com/topic.asp?id=346 http://bbs.quyouji.com/topic.asp?id=347 http://bbs.quyouji.com/topic.asp?id=348 http://bbs.quyouji.com/topic.asp?id=349 http://bbs.quyouji.com/topic.asp?id=350 http://bbs.quyouji.com/topic.asp?id=351 http://bbs.quyouji.com/topic.asp?id=352 http://bbs.quyouji.com/topic.asp?id=353 http://bbs.quyouji.com/topic.asp?id=354 http://bbs.quyouji.com/topic.asp?id=355 http://bbs.quyouji.com/topic.asp?id=356 http://bbs.quyouji.com/topic.asp?id=357 http://bbs.quyouji.com/topic.asp?id=358 http://bbs.quyouji.com/topic.asp?id=359 http://bbs.quyouji.com/topic.asp?id=360 http://bbs.quyouji.com/topic.asp?id=361 http://bbs.quyouji.com/topic.asp?id=362 http://bbs.quyouji.com/topic.asp?id=363 http://bbs.quyouji.com/topic.asp?id=364 http://bbs.quyouji.com/topic.asp?id=365 http://bbs.quyouji.com/topic.asp?id=366 http://bbs.quyouji.com/topic.asp?id=367 http://bbs.quyouji.com/topic.asp?id=368 http://bbs.quyouji.com/topic.asp?id=369 http://bbs.quyouji.com/topic.asp?id=370 http://bbs.quyouji.com/topic.asp?id=371 http://bbs.quyouji.com/topic.asp?id=372 http://bbs.quyouji.com/topic.asp?id=373 http://bbs.quyouji.com/topic.asp?id=374 http://bbs.quyouji.com/topic.asp?id=375 http://bbs.quyouji.com/topic.asp?id=376 http://bbs.quyouji.com/topic.asp?id=377 http://bbs.quyouji.com/topic.asp?id=378 http://bbs.quyouji.com/topic.asp?id=379 http://bbs.quyouji.com/topic.asp?id=380 http://bbs.quyouji.com/topic.asp?id=381 http://bbs.quyouji.com/topic.asp?id=382 http://bbs.quyouji.com/topic.asp?id=383 http://bbs.quyouji.com/topic.asp?id=384 http://bbs.quyouji.com/topic.asp?id=385 http://bbs.quyouji.com/topic.asp?id=386 http://bbs.quyouji.com/topic.asp?id=387 http://bbs.quyouji.com/topic.asp?id=388 http://bbs.quyouji.com/topic.asp?id=389 http://bbs.quyouji.com/topic.asp?id=390 http://bbs.quyouji.com/topic.asp?id=391 http://bbs.quyouji.com/topic.asp?id=392 http://bbs.quyouji.com/topic.asp?id=393 http://bbs.quyouji.com/topic.asp?id=394 http://bbs.quyouji.com/topic.asp?id=395 http://bbs.quyouji.com/topic.asp?id=396 http://bbs.quyouji.com/topic.asp?id=397 http://bbs.quyouji.com/topic.asp?id=398 http://bbs.quyouji.com/topic.asp?id=399 http://bbs.quyouji.com/topic.asp?id=400 http://bbs.quyouji.com/topic.asp?id=401 http://bbs.quyouji.com/topic.asp?id=402 http://bbs.quyouji.com/topic.asp?id=403 http://bbs.quyouji.com/topic.asp?id=404 http://bbs.quyouji.com/topic.asp?id=405 http://bbs.quyouji.com/topic.asp?id=406 http://bbs.quyouji.com/topic.asp?id=407 http://bbs.quyouji.com/topic.asp?id=408 http://bbs.quyouji.com/topic.asp?id=409 http://bbs.quyouji.com/topic.asp?id=410 http://bbs.quyouji.com/topic.asp?id=411 http://bbs.quyouji.com/topic.asp?id=412 http://bbs.quyouji.com/topic.asp?id=413 http://bbs.quyouji.com/topic.asp?id=414 http://bbs.quyouji.com/topic.asp?id=415 http://bbs.quyouji.com/topic.asp?id=416 http://bbs.quyouji.com/topic.asp?id=417 http://bbs.quyouji.com/topic.asp?id=418 http://bbs.quyouji.com/topic.asp?id=419 http://bbs.quyouji.com/topic.asp?id=420 http://bbs.quyouji.com/topic.asp?id=421 http://bbs.quyouji.com/topic.asp?id=422 http://bbs.quyouji.com/topic.asp?id=423 http://bbs.quyouji.com/topic.asp?id=424 http://bbs.quyouji.com/topic.asp?id=425 http://bbs.quyouji.com/topic.asp?id=426 http://bbs.quyouji.com/topic.asp?id=427 http://bbs.quyouji.com/topic.asp?id=428 http://bbs.quyouji.com/topic.asp?id=429 http://bbs.quyouji.com/topic.asp?id=430 http://bbs.quyouji.com/topic.asp?id=431 http://bbs.quyouji.com/topic.asp?id=432 http://bbs.quyouji.com/topic.asp?id=433 http://bbs.quyouji.com/topic.asp?id=434 http://bbs.quyouji.com/topic.asp?id=435 http://bbs.quyouji.com/topic.asp?id=436 http://bbs.quyouji.com/topic.asp?id=437 http://bbs.quyouji.com/topic.asp?id=438 http://bbs.quyouji.com/topic.asp?id=439 http://bbs.quyouji.com/topic.asp?id=440 http://bbs.quyouji.com/topic.asp?id=441 http://bbs.quyouji.com/topic.asp?id=442 http://bbs.quyouji.com/topic.asp?id=443 http://bbs.quyouji.com/topic.asp?id=444 http://bbs.quyouji.com/topic.asp?id=445 http://bbs.quyouji.com/topic.asp?id=446 http://bbs.quyouji.com/topic.asp?id=447 http://bbs.quyouji.com/topic.asp?id=448 http://bbs.quyouji.com/topic.asp?id=449 http://bbs.quyouji.com/topic.asp?id=450 http://bbs.quyouji.com/topic.asp?id=451 http://bbs.quyouji.com/topic.asp?id=452 http://bbs.quyouji.com/topic.asp?id=453 http://bbs.quyouji.com/topic.asp?id=454 http://bbs.quyouji.com/topic.asp?id=455 http://bbs.quyouji.com/topic.asp?id=456 http://bbs.quyouji.com/topic.asp?id=457 http://bbs.quyouji.com/topic.asp?id=458 http://bbs.quyouji.com/topic.asp?id=459 http://bbs.quyouji.com/topic.asp?id=460 http://bbs.quyouji.com/topic.asp?id=461 http://bbs.quyouji.com/topic.asp?id=462 http://bbs.quyouji.com/topic.asp?id=463 http://bbs.quyouji.com/topic.asp?id=464 http://bbs.quyouji.com/topic.asp?id=465 http://bbs.quyouji.com/topic.asp?id=466 http://bbs.quyouji.com/topic.asp?id=467 http://bbs.quyouji.com/topic.asp?id=468 http://bbs.quyouji.com/topic.asp?id=469 http://bbs.quyouji.com/topic.asp?id=470 http://bbs.quyouji.com/topic.asp?id=471 http://bbs.quyouji.com/topic.asp?id=472 http://bbs.quyouji.com/topic.asp?id=473 http://bbs.quyouji.com/topic.asp?id=474 http://bbs.quyouji.com/topic.asp?id=475 http://bbs.quyouji.com/topic.asp?id=476 http://bbs.quyouji.com/topic.asp?id=477 http://bbs.quyouji.com/topic.asp?id=478 http://bbs.quyouji.com/topic.asp?id=479 http://bbs.quyouji.com/topic.asp?id=480 http://bbs.quyouji.com/topic.asp?id=481 http://bbs.quyouji.com/topic.asp?id=482 http://bbs.quyouji.com/topic.asp?id=483 http://bbs.quyouji.com/topic.asp?id=484 http://bbs.quyouji.com/topic.asp?id=485 http://bbs.quyouji.com/topic.asp?id=486 http://bbs.quyouji.com/topic.asp?id=487 http://bbs.quyouji.com/topic.asp?id=488 http://bbs.quyouji.com/topic.asp?id=489 http://bbs.quyouji.com/topic.asp?id=490 http://bbs.quyouji.com/topic.asp?id=491 http://bbs.quyouji.com/topic.asp?id=492 http://bbs.quyouji.com/topic.asp?id=493 http://bbs.quyouji.com/topic.asp?id=494 http://bbs.quyouji.com/topic.asp?id=495 http://bbs.quyouji.com/topic.asp?id=496 http://bbs.quyouji.com/topic.asp?id=497 http://bbs.quyouji.com/topic.asp?id=498 http://bbs.quyouji.com/topic.asp?id=499 http://bbs.quyouji.com/topic.asp?id=500 http://bbs.quyouji.com/topic.asp?id=501 http://bbs.quyouji.com/topic.asp?id=502 http://bbs.quyouji.com/topic.asp?id=503 http://bbs.quyouji.com/topic.asp?id=504 http://bbs.quyouji.com/topic.asp?id=505 http://bbs.quyouji.com/topic.asp?id=506 http://bbs.quyouji.com/topic.asp?id=507 http://bbs.quyouji.com/topic.asp?id=508 http://bbs.quyouji.com/topic.asp?id=509 http://bbs.quyouji.com/topic.asp?id=510 http://bbs.quyouji.com/topic.asp?id=511 http://bbs.quyouji.com/topic.asp?id=512 http://bbs.quyouji.com/topic.asp?id=513 http://bbs.quyouji.com/topic.asp?id=514 http://bbs.quyouji.com/topic.asp?id=515 http://bbs.quyouji.com/topic.asp?id=516 http://bbs.quyouji.com/topic.asp?id=517 http://bbs.quyouji.com/topic.asp?id=518 http://bbs.quyouji.com/topic.asp?id=519 http://bbs.quyouji.com/topic.asp?id=520 http://bbs.quyouji.com/topic.asp?id=521 http://bbs.quyouji.com/topic.asp?id=522 http://bbs.quyouji.com/topic.asp?id=523 http://bbs.quyouji.com/topic.asp?id=524 http://bbs.quyouji.com/topic.asp?id=525 http://bbs.quyouji.com/topic.asp?id=526 http://bbs.quyouji.com/topic.asp?id=527 http://bbs.quyouji.com/topic.asp?id=528 http://bbs.quyouji.com/topic.asp?id=529 http://bbs.quyouji.com/topic.asp?id=530 http://bbs.quyouji.com/topic.asp?id=531 http://bbs.quyouji.com/topic.asp?id=532 http://bbs.quyouji.com/topic.asp?id=533 http://bbs.quyouji.com/topic.asp?id=534 http://bbs.quyouji.com/topic.asp?id=535 http://bbs.quyouji.com/topic.asp?id=536 http://bbs.quyouji.com/topic.asp?id=537 http://bbs.quyouji.com/topic.asp?id=538 http://bbs.quyouji.com/topic.asp?id=539 http://bbs.quyouji.com/topic.asp?id=540 http://bbs.quyouji.com/topic.asp?id=541 http://bbs.quyouji.com/topic.asp?id=542 http://bbs.quyouji.com/topic.asp?id=543 http://bbs.quyouji.com/topic.asp?id=544 http://bbs.quyouji.com/topic.asp?id=545 http://bbs.quyouji.com/topic.asp?id=546 http://bbs.quyouji.com/topic.asp?id=547 http://bbs.quyouji.com/topic.asp?id=548 http://bbs.quyouji.com/topic.asp?id=549 http://bbs.quyouji.com/topic.asp?id=550 http://bbs.quyouji.com/topic.asp?id=551 http://bbs.quyouji.com/topic.asp?id=552 http://bbs.quyouji.com/topic.asp?id=553 http://bbs.quyouji.com/topic.asp?id=554 http://bbs.quyouji.com/topic.asp?id=555 http://bbs.quyouji.com/topic.asp?id=556 http://bbs.quyouji.com/topic.asp?id=557 http://bbs.quyouji.com/topic.asp?id=558 http://bbs.quyouji.com/topic.asp?id=559 http://bbs.quyouji.com/topic.asp?id=560 http://bbs.quyouji.com/topic.asp?id=561 http://bbs.quyouji.com/topic.asp?id=562 http://bbs.quyouji.com/topic.asp?id=563 http://bbs.quyouji.com/topic.asp?id=564 http://bbs.quyouji.com/topic.asp?id=565 http://bbs.quyouji.com/topic.asp?id=566 http://bbs.quyouji.com/topic.asp?id=567 http://bbs.quyouji.com/topic.asp?id=568 http://bbs.quyouji.com/topic.asp?id=569 http://bbs.quyouji.com/topic.asp?id=570 http://bbs.quyouji.com/topic.asp?id=571 http://bbs.quyouji.com/topic.asp?id=572 http://bbs.quyouji.com/topic.asp?id=573 http://bbs.quyouji.com/topic.asp?id=574 http://bbs.quyouji.com/topic.asp?id=575 http://bbs.quyouji.com/topic.asp?id=576 http://bbs.quyouji.com/topic.asp?id=577 http://bbs.quyouji.com/topic.asp?id=578 http://bbs.quyouji.com/topic.asp?id=579 http://bbs.quyouji.com/topic.asp?id=580 http://bbs.quyouji.com/topic.asp?id=581
下面用代码来验证:
net = Net() net.initialize_weights() for m in net.modules(): if isinstance(m,nn.Conv2d): print(m) print(m.weight.shape) print(m.bias.shape) break 输出结果是:
Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1)) torch.Size([6, 3, 5, 5]) torch.Size([6]) 都和预料中一样。