Pytorch學習筆記

2022-03-15 03:49:35 字數 1141 閱讀 8671

非線性回歸問題的引數求解,反向求導基本流程。variable計算時, 它在後台一步步默默地搭建著乙個龐大的系統, 叫做計算圖, computational graph. 這個圖將所有的計算步驟 (節點) 都連線起來, 最後進行誤差反向傳遞的時候, 一次性將所有 variable 裡面的修改幅度 (梯度) 都計算出來, 而 tensor 就沒有這個能力。

1

from torch.autograd import

variable

2 dtype =torch.floattensor

3 n, d_in, h, d_out = 64, 1000, 100, 10

4 x = variable(torch.randn(n, d_in).type(dtype), requires_grad=false)

5 y = variable(torch.randn(n, d_out).type(dtype), requires_grad=false)

6 w1 = variable(torch.randn(d_in, h).type(dtype), requires_grad=true)

7 w2 = variable(torch.randn(h, d_out).type(dtype), requires_grad=true)

8 learning_rate = 1e-6

9for t in range(500):

10 y_pred = x.mm(w1).clamp(min=0).mm(w2)

11 loss = (y_pred - y).pow(2).sum()

12print

(t, loss.data[0])13#

manually zero the gradients before running the backward pass

14if t >0:

15w1.grad.data.zero_()

16w2.grad.data.zero_()

17loss.backward()

18 w1.data -= learning_rate *w1.grad.data

19 w2.data -= learning_rate * w2.grad.data

Pytorch 學習筆記

本渣的pytorch 逐步學習鞏固經歷,希望各位大佬指正,寫這個部落格也為了鞏固下記憶。a a b c 表示從 a 取到 b 步長為 c c 2 則表示每2個數取1個 若為a 1 1 1 即表示從倒數最後乙個到正數第 1 個 最後乙個 1 表示倒著取 如下 陣列的第乙個為 0 啊,第 0 個!彆扭 ...

Pytorch學習筆記

資料集 penn fudan資料集 在學習pytorch官網教程時,作者對penn fudan資料集進行了定義,並且在自定義的資料集上實現了對r cnn模型的微調。此篇筆記簡單總結一下pytorch如何實現定義自己的資料集 資料集必須繼承torch.utils.data.dataset類,並且實現 ...

Pytorch學習筆記

lesson 1.張量 tensor 的建立和常用方法 一 張量 tensor 的基本建立及其型別 import torch 匯入pytorch包 import numpy as np torch.version 檢視版本號1.張量 tensor 函式建立方法 張量 tensor 函式建立方法 t ...