pytorch进行网络构建的详细过程是什么,哪些要注意
Admin 2022-06-28 群英技术资讯 1325 次浏览
今天小编跟大家讲解下有关“pytorch进行网络构建的详细过程是什么,哪些要注意”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。我们可以通过torch.nn包来构建网络,现在你已经看过了autograd,nn在autograd的基础上定义模型和求微分。一个nn.Module包括很多层,forward方法返回output。
一个典型的训练过程包括这么几步:
1.定义一个网络结构包含一些可训练的额参数
2.为数据集制定输入iterata
3.通过网络计算Output
4.计算loss
5.反向传播计算梯度
6.更新权值
weight = weight - learning_rate * gradient
让我们来定义一个网络
import torch
import torch as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__(
#1 input image channel ,6output image channel ,5*5convolytion kernel
self.conv1 = nn.Conv2d(1,6,5)
self.conv2 = nn.Conv2d(6,16,5)
# an affine operation:y = Wx+b
self.fc1 = nn.Linear(16*5*5,120)
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)
def forward(self,x):
#max pooling
x.F.max_pool2d(F.relu(self.conv1(x)),(2,2))
#2 = (2,2)
x.F.max_pool2d(F.relu(self.con2(x)),2)
x = x.view(-1,self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self,x):
size = x.size()[1:]
num_feature = 1
for s in size:
num_features *=s
return num_features
net = Net()
print(net)
out
Net( (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1)) (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (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) )
我们只需定义forward和backward函数,会自动求导通过你定义的函数,你可以使用所有的Tensor操作在forward函数中。
我们使用net.parameters()函数返回可学习的参数
params = list(net.parameters()) print(len(params)) print(params[0].size()) # conv1's .weight
out
10 torch.Size([6, 1, 5, 5])
让我们试试32*32的输入节点,因为lenet网络的输入应该是32*32,为了在MNIST数据集上使用lenet我们需要将图片reshpe成32*32
input = torch.randn(1,1,32,32) oyt = net(input) print(out)
out
tensor([[-0.1346, 0.0581, -0.0396, -0.1136, -0.1128, 0.0180, -0.1226,
-0.0419, -0.1150, 0.0278]])
零化导数buffers所有的参数都会随机求导
net.zero_grad() out.backward(torch.randn(1,10))
torch.nn只支持mini-batch,而不是单个的样本
例如,nn.Conv2d输入是一个4维tensors
nSamples * nChannels * Height * Width
如果你只有单个的样本,使用input.unsqueeze(0)增加一个假的batch维度
在后处理之前,让我们看看都学过什么类
Recap:
torch.Tensor - A multi-dimensional array with support for autograd operations like backward(). Also holds the gradient w.r.t. the tensor.
nn.Module - Neural network module. Convenient way of encapsulating parameters, with helpers for moving them to GPU, exporting, loading, etc.
nn.Parameter - A kind of Tensor, that is automatically registered as a parameter when assigned as an attribute to a Module.
autograd.Function - Implements forward and backward definitions of an autograd operation. Every Tensor operation, creates at least a single Function node, that connects to functions that created a Tensor and encodes its history.
目前,我们学习了:
1.定义一个神经网络
2.处理输入和使用后向传播
我们还需要学习:
1.计算loss
2.更新权值
Loss function接受(output traget)对作为输入,计算一个反映到目标距离的值。
在nn这个包里面有很多loss function ,最简单的是nn.MSELoss,就是那输入与输出的均方误差。
举个例子
output = net(input) target = torch.arrange(1,11) target = target.view(1m-1) criterion = nn.MSELoss() loss = criterion(output,target) print(loss)
Out:
tensor(39.1076)
为了反向传播我们需要做的仅仅是进行loss.backward(),我们需要清除现有的梯度
最简单常用的更新权值的方法就是SGD(Stochastic Gradient Descent )
weight = weight - learning_rata * gradiernt
我们可以通过简单的代码实现上面的公式:
learning_rata = 0.01
for f in net.parameters():
f.data.sib_(f.grad.data * learining_rata)
但是我们也可以使用不同的更新规则,像是 SGD, Nesterov-SGD, Adam, RMSProp, etc.
为了使用这些,我们需要torch.optim包,使用起来也很简单。
import torch.optim as optim #creat you optimizer optimizer = optim.SGD(net.parameters(),lr = 0.01) #in your training loop: optimizer.zero_grad() output = net(input) loss = criterion(output,target) loss.backward() optimizer.step()
注意gradient必须清零
现在我们调用loss.backward(),并且看看con1的bias的前后差别
ner.zero_grad()
print('conv1.bias.grad before backward')
loss.backward()
print('conv1.bias.grad after backward')
piint(net.conv1.bias.grad)
out
conv1.bias.grad before backward tensor([ 0., 0., 0., 0., 0., 0.]) conv1.bias.grad after backward tensor([ 0.1178, -0.0404, -0.0810, 0.0363, -0.0631, 0.1423])
现在,我们看到了如何使用loss function
重要
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
对于Python新手来说,理解python内存分配机制是很有必要的。本文就给大家分享一下实例帮助大家更好的理解python内存分配机制,下面就跟随小编一起来看看吧。
python中怎样做代码性能分析?做代码性能分析能够帮我们了解什么原因导致性能变慢,因此大家学习python代码性能分析也是很有必要的。而python就有提供性能分析工具,下面我们就来具体看看。
这篇文章主要介绍了学好python基本数据类型,学习python基本数据类型我们需要了解基本数据类型有数字int、布尔值bool、字符串str、列表list、元组tuple、字典dict等,其中包括他们的基本用法和其常用的方法,下面来看看文章的具体介绍吧
内容介绍time模块time.sleep()time.time()time.localtime()time.strftime()datetime()random模块random.random()ran
本篇文章给大家带来了关于Python的相关知识,详细介绍了Python实现提取四种不同文本特征的方法,有字典文本特征提取、英文文本特征提取、中文文本特征提取和TF-IDF 文本特征提取,感兴趣的可以了解一下。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008