日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pytorch 神经网络训练过程

發(fā)布時間:2024/7/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytorch 神经网络训练过程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 定義模型
      • 1.1 繪制模型
      • 1.2 模型參數
    • 2. 前向傳播
    • 3. 反向傳播
    • 4. 計算損失
    • 5. 更新參數
    • 6. 完整簡潔代碼

參考 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 模型參數

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)) # 使用隨機的梯度反向傳播

4. 計算損失

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>)
  • 測試 .zero_grad() 清零梯度緩存作用
model.zero_grad() print(model.conv1.bias.grad) loss.backward() print(model.conv1.bias.grad)

輸出:

tensor([0., 0., 0., 0., 0., 0.]) tensor([-0.0067, 0.0114, 0.0033, -0.0013, 0.0076, 0.0010])

5. 更新參數

learning_rate = 0.01 for f in model.parameters():f.data.sub_(f.grad.data*learning_rate)

6. 完整簡潔代碼

criterion = nn.MSELoss() # 定義損失類型 import torch.optim as optim optimizer = optim.SGD(model.parameters(), lr=0.1)# 優(yōu)化目標,學習率# 循環(huán)執(zhí)行以下內容 訓練 optimizer.zero_grad() # 清空梯度緩存 output = model(input) # 輸入,輸出,前向傳播loss = criterion(output, target) # 計算損失loss.backward() # 反向傳播optimizer.step() # 更新參數

總結

以上是生活随笔為你收集整理的Pytorch 神经网络训练过程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。