Pytorch 神经网络训练过程
生活随笔
收集整理的這篇文章主要介紹了
Pytorch 神经网络训练过程
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1. 定義模型
- 1.1 繪制模型
- 1.2 模型參數(shù)
- 2. 前向傳播
- 3. 反向傳播
- 4. 計(jì)算損失
- 5. 更新參數(shù)
- 6. 完整簡(jiǎn)潔代碼
參考 http://pytorch123.com/
1. 定義模型
import torch import torch.nn as nn import torch.nn.functional as Fclass Net_model(nn.Module):def __init__(self):super(Net_model, self).__init__()self.conv1 = nn.Conv2d(1,6,5) # 卷積# in_channels, out_channels, kernel_size, stride=1,# padding=0, dilation=1, groups=1,# bias=True, padding_mode='zeros'self.conv2 = nn.Conv2d(6,16,5)self.fc1 = nn.Linear(16*5*5, 120) # FC層self.fc2 = nn.Linear(120, 84)self.fc3 = nn.Linear(84, 10)def forward(self, x):x = self.conv1(x)x = F.relu(x)x = F.max_pool2d(x, (2,2))x = self.conv2(x)x = F.relu(x)x = F.max_pool2d(x, 2)x = x.view(-1, self.num_flat_features(x)) # 展平x = self.fc1(x)x = F.relu(x)x = self.fc2(x)x = F.relu(x)x = self.fc3(x)return xdef num_flat_features(self, x):size = x.size()[1:] # 除了batch 維度外的維度num_features = 1for s in size:num_features *= sreturn num_featuresmodel = Net_model() print(model)輸出:
Net_model((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) )1.1 繪制模型
from torchviz import make_dot vis_graph = make_dot(model(input),params=dict(model.named_parameters())) vis_graph.view()1.2 模型參數(shù)
params = list(model.parameters()) print(len(params)) for i in range(len(params)):print(params[i].size())輸出:
10 torch.Size([6, 1, 5, 5]) torch.Size([6]) torch.Size([16, 6, 5, 5]) torch.Size([16]) torch.Size([120, 400]) torch.Size([120]) torch.Size([84, 120]) torch.Size([84]) torch.Size([10, 84]) torch.Size([10])2. 前向傳播
input = torch.randn(1,1,32,32) out = model(input) print(out)輸出:
tensor([[-0.1100, 0.0273, 0.1260, 0.0713, -0.0744, -0.1442, -0.0068, -0.0965,-0.0601, -0.0463]], grad_fn=<AddmmBackward>)3. 反向傳播
# 清零梯度緩存器 model.zero_grad() out.backward(torch.randn(1,10)) # 使用隨機(jī)的梯度反向傳播4. 計(jì)算損失
output = model(input) target = torch.randn(10) # 舉例用 target = target.view(1,-1) # 形狀匹配 output criterion = nn.MSELoss() # 定義損失類型 loss = criterion(output, target) print(loss) # tensor(0.5048, grad_fn=<MseLossBackward>)- 測(cè)試 .zero_grad() 清零梯度緩存作用
輸出:
tensor([0., 0., 0., 0., 0., 0.]) tensor([-0.0067, 0.0114, 0.0033, -0.0013, 0.0076, 0.0010])5. 更新參數(shù)
learning_rate = 0.01 for f in model.parameters():f.data.sub_(f.grad.data*learning_rate)6. 完整簡(jiǎn)潔代碼
criterion = nn.MSELoss() # 定義損失類型 import torch.optim as optim optimizer = optim.SGD(model.parameters(), lr=0.1)# 優(yōu)化目標(biāo),學(xué)習(xí)率# 循環(huán)執(zhí)行以下內(nèi)容 訓(xùn)練 optimizer.zero_grad() # 清空梯度緩存 output = model(input) # 輸入,輸出,前向傳播loss = criterion(output, target) # 計(jì)算損失loss.backward() # 反向傳播optimizer.step() # 更新參數(shù)總結(jié)
以上是生活随笔為你收集整理的Pytorch 神经网络训练过程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode MySQL 1364.
- 下一篇: LeetCode MySQL 1149.