PyTorch一小時掌握之a(chǎn)utograd機制篇
概述
PyTorch 干的最厲害的一件事情就是幫我們把反向傳播全部計算好了.
代碼實現(xiàn)
手動定義求導
import torch # 方法一 x = torch.randn(3, 4, requires_grad=True) # 方法二 x = torch.randn(3,4) x.requires_grad = True
b = torch.randn(3, 4, requires_grad=True) t = x + b y = t.sum() print(y) print(y.backward()) print(b.grad) print(x.requires_grad) print(b.requires_grad) print(t.requires_grad)
輸出結(jié)果:
tensor(1.1532, grad_fn=<SumBackward0>)
None
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
True
True
True
計算流量
# 計算流量 x = torch.rand(1) w = torch.rand(1, requires_grad=True) b = torch.rand(1, requires_grad=True) y = w * x z = y + b print(x.requires_grad, w.requires_grad,b.requires_grad, z.requires_grad) print(x.is_leaf, w.is_leaf, b.is_leaf, y.is_leaf,z.is_leaf)
輸出結(jié)果:
False True True True
True True True False False
反向傳播計算
# 反向傳播 z.backward(retain_graph= True) # 如果不清空會累加起來 print(w.grad) print(b.grad)
輸出結(jié)果:
tensor([0.1485])
tensor([1.])
線性回歸
導包
import numpy as np import torch import torch.nn as nn
構(gòu)造 x, y
# 構(gòu)造數(shù)據(jù) X_values = [i for i in range(11)] X_train = np.array(X_values, dtype=np.float32) X_train = X_train.reshape(-1, 1) print(X_train.shape) # (11, 1) y_values = [2 * i + 1 for i in X_values] y_train = np.array(y_values, dtype=np.float32) y_train = y_train.reshape(-1,1) print(y_train.shape) # (11, 1)
輸出結(jié)果:
(11, 1)
(11, 1)
構(gòu)造模型
# 構(gòu)造模型 class LinerRegressionModel(nn.Module): def __init__(self, input_dim, output_dim): super(LinerRegressionModel, self).__init__() self.liner = nn.Linear(input_dim, output_dim) def forward(self, x): out = self.liner(x) return out input_dim = 1 output_dim = 1 model = LinerRegressionModel(input_dim, output_dim) print(model)
輸出結(jié)果:
LinerRegressionModel(
(liner): Linear(in_features=1, out_features=1, bias=True)
)
參數(shù) & 損失函數(shù)
# 超參數(shù) enpochs = 1000 learning_rate = 0.01 # 損失函數(shù) optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) criterion = nn.MSELoss()
訓練模型
# 訓練模型 for epoch in range(enpochs): # 轉(zhuǎn)成tensor inputs = torch.from_numpy(X_train) labels = torch.from_numpy(y_train) # 梯度每次迭代清零 optimizer.zero_grad() # 前向傳播 outputs = model(inputs) # 計算損失 loss = criterion(outputs, labels) # 反向傳播 loss.backward() # 更新參數(shù) optimizer.step() if epoch % 50 == 0: print("epoch {}, loss {}".format(epoch, loss.item()))
輸出結(jié)果:
epoch 0, loss 114.47456359863281
epoch 50, loss 0.00021522105089388788
epoch 100, loss 0.00012275540211703628
epoch 150, loss 7.001651829341426e-05
epoch 200, loss 3.9934264350449666e-05
epoch 250, loss 2.2777328922529705e-05
epoch 300, loss 1.2990592040296178e-05
epoch 350, loss 7.409254521917319e-06
epoch 400, loss 4.227155841363128e-06
epoch 450, loss 2.410347860859474e-06
epoch 500, loss 1.3751249525739695e-06
epoch 550, loss 7.844975016269018e-07
epoch 600, loss 4.4756839656656666e-07
epoch 650, loss 2.5517596213830984e-07
epoch 700, loss 1.4577410922811396e-07
epoch 750, loss 8.30393886985803e-08
epoch 800, loss 4.747753479250605e-08
epoch 850, loss 2.709844615367274e-08
epoch 900, loss 1.5436164346738224e-08
epoch 950, loss 8.783858973515635e-09
完整代碼
import numpy as np import torch import torch.nn as nn # 構(gòu)造數(shù)據(jù) X_values = [i for i in range(11)] X_train = np.array(X_values, dtype=np.float32) X_train = X_train.reshape(-1, 1) print(X_train.shape) # (11, 1) y_values = [2 * i + 1 for i in X_values] y_train = np.array(y_values, dtype=np.float32) y_train = y_train.reshape(-1,1) print(y_train.shape) # (11, 1) # 構(gòu)造模型 class LinerRegressionModel(nn.Module): def __init__(self, input_dim, output_dim): super(LinerRegressionModel, self).__init__() self.liner = nn.Linear(input_dim, output_dim) def forward(self, x): out = self.liner(x) return out input_dim = 1 output_dim = 1 model = LinerRegressionModel(input_dim, output_dim) print(model) # 超參數(shù) enpochs = 1000 learning_rate = 0.01 # 損失函數(shù) optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) criterion = nn.MSELoss() # 訓練模型 for epoch in range(enpochs): # 轉(zhuǎn)成tensor inputs = torch.from_numpy(X_train) labels = torch.from_numpy(y_train) # 梯度每次迭代清零 optimizer.zero_grad() # 前向傳播 outputs = model(inputs) # 計算損失 loss = criterion(outputs, labels) # 反向傳播 loss.backward() # 更新參數(shù) optimizer.step() if epoch % 50 == 0: print("epoch {}, loss {}".format(epoch, loss.item()))
到此這篇關(guān)于PyTorch一小時掌握之a(chǎn)utograd機制篇的文章就介紹到這了,更多相關(guān)PyTorch autograd內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用selenium操作瀏覽器的實現(xiàn)示例
Selenium是一個模擬瀏覽器瀏覽網(wǎng)頁的工具,主要用于測試網(wǎng)站的自動化測試工具,本文主要介紹了python使用selenium操作瀏覽器的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-01-01基于Python實現(xiàn)的百度貼吧網(wǎng)絡爬蟲實例
這篇文章主要介紹了基于Python實現(xiàn)的百度貼吧網(wǎng)絡爬蟲,實例分析了Python實現(xiàn)網(wǎng)絡爬蟲的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04python3中No module named _ssl的問題解決
本文主要介紹了python3中No module named _ssl的問題解決,這個錯誤表示Python導入_ssl模塊時失敗,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-08-08django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫
這篇文章主要給大家介紹了關(guān)于django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用django具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08解決pycharm中opencv-python導入cv2后無法自動補全的問題(不用作任何文件上的修改)
這篇文章主要介紹了解決pycharm中opencv-python導入cv2后無法自動補全的問題(不用作任何文件上的修改),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03利用python Pandas實現(xiàn)批量拆分Excel與合并Excel
今天帶大家學習利用python Pandas實現(xiàn)批量拆分Excel與合并Excel,文中有非常詳細的的代碼示例,對正在學習python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05