山西网站建设网站,wordpress gpl协议,摄影设计师招聘,梅县区住房和城乡规划建设局网站目录 构建模型模型中的可训练参数假设输入尺寸为32*32损失函数反向传播更新网络参数 构建模型
import torch
import torch.nn as nn
import torch.nn.functional as Fclass Net(nn.Module):def __init__(self):super(Net,self).__init__()#定义第一层卷积层#xff0c;输入维… 目录 构建模型模型中的可训练参数假设输入尺寸为32*32损失函数反向传播更新网络参数 构建模型
import torch
import torch.nn as nn
import torch.nn.functional as Fclass Net(nn.Module):def __init__(self):super(Net,self).__init__()#定义第一层卷积层输入维度1输出维度6卷积核大小3*3self.conv1nn.Conv2d(1,6,3)self.conv2nn.Conv2d(6,16,3)self.fc1nn.Linear(16*6*6,120)self.fc2nn.Linear(120,84)self.fc3nn.Linear(84,10)def forward(self,x):#注意任意卷积层后面要加激活层池化层xF.max_pool2d(F.relu(self.conv1(x),(2,2)))xF.max_pool2d(F.relu(self.conv2(x),2))xx.view(-1,self.num_flat_features(x))xF.relu(self.fc1(x))xF.relu(self.fc2(x))xself.fc3(x)return xdef num_flat_features(self,x):sizex.size()[1:]num_features1for s in size:num_features*sreturn num_featuresnetNet()
print(net)模型中的可训练参数
paramslist(net.parameters())
print(len(params))
print(params[0].size()) #conv1的参数假设输入尺寸为32*32
inputtorch.randn(1,1,32,32) #个数通道数长宽
outnet(input)
print(out)
print(out.size())注意
损失函数 targettorch.randn(10)
targettarget.view(1,-1)
criterionnn.MSELoss()
losscriterion(out,target)
print(loss)print(loss.grad_fn)
print(loss.grad_fn.next_functions[0][0]) #上一层的grad_fn
print(loss.grad_fn.next_functions[0][0].next_functions[0][0]) #上上一层的grad_fn反向传播 #首先要执行梯度清零的操作
net.zero_grad()print(conv1.bisa.grad before backward)
print(net.conv1.bias.grad)#实现一次反向传播
loss.backward()print(conv1.bisa.grad after backward)
print(net.conv1.bias.grad)更新网络参数 #导入优化器包
import torch.optim as optim
#构建优化器
optimizeroptim.SGD(net.parameters(),lr0.01)
#优化器梯度清零
optimizer.zero_grad()
#进行网络计算并计算损失值
outputnet(input)
losscriterion(output,target)
#执行反向传播
loss.backward()
#更新参数
optimizer.step()
文章转载自: http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.coatingonline.com.cn.gov.cn.coatingonline.com.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.zydr.cn.gov.cn.zydr.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.krklj.cn.gov.cn.krklj.cn