PyTorch 深度學習實踐 第2 3講

2021-10-13 15:21:06 字數 3203 閱讀 6139

第2講 linear_model 源**

import numpy as np

import matplotlib.pyplot as plt

x_data = [1.0, 2.0, 3.0]

y_data = [2.0, 4.0, 6.0]

def forward(x):

return x*w

def loss(x, y):

y_pred = forward(x)

return (y_pred - y)**2

# 窮舉法

w_list =

mse_list =

for w in np.arange(0.0, 4.1, 0.1):

print("w=", w)

l_sum = 0

for x_val, y_val in zip(x_data, y_data):

y_pred_val = forward(x_val)

loss_val = loss(x_val, y_val)

l_sum += loss_val

print('\t', x_val, y_val, y_pred_val, loss_val)

print('mse=', l_sum/3)

plt.plot(w_list,mse_list)

plt.ylabel('loss')

plt.xlabel('w')

plt.show()

第3講 梯度下降法 源**

import matplotlib.pyplot as plt

# prepare the training set

x_data = [1.0, 2.0, 3.0]

y_data = [2.0, 4.0, 6.0]

# initial guess of weight

w = 1.0

# define the model linear model y = w*x

def forward(x):

return x*w

#define the cost function mse

def cost(xs, ys):

cost = 0

for x, y in zip(xs,ys):

y_pred = forward(x)

cost += (y_pred - y)**2

return cost / len(xs)

# define the gradient function gd

def gradient(xs,ys):

grad = 0

for x, y in zip(xs,ys):

grad += 2*x*(x*w - y)

return grad / len(xs)

epoch_list =

cost_list =

print('predict (before training)', 4, forward(4))

for epoch in range(100):

cost_val = cost(x_data, y_data)

grad_val = gradient(x_data, y_data)

w-= 0.01 * grad_val # 0.01 learning rate

print('epoch:', epoch, 'w=', w, 'loss=', cost_val)

print('predict (after training)', 4, forward(4))

plt.plot(epoch_list,cost_list)

plt.ylabel('cost')

plt.xlabel('epoch')

plt.show()

隨機梯度下降法 源**

隨機梯度下降法在神經網路中被證明是有效的。效率較低(時間複雜度較高),學習效能較好。

隨機梯度下降法和梯度下降法的主要區別在於:

1、損失函式由cost()更改為loss()。cost是計算所有訓練資料的損失,loss是計算乙個訓練函式的損失。對應於源**則是少了兩個for迴圈。

2、梯度函式gradient()由計算所有訓練資料的梯度更改為計算乙個訓練資料的梯度。

3、本演算法中的隨機梯度主要是指,每次拿乙個訓練資料來訓練,然後更新梯度引數。本演算法中梯度總共更新100(epoch)x3 = 300次。梯度下降法中梯度總共更新100(epoch)次。

import matplotlib.pyplot as plt

x_data = [1.0, 2.0, 3.0]

y_data = [2.0, 4.0, 6.0]

w = 1.0

def forward(x):

return x*w

# calculate loss function

def loss(x, y):

y_pred = forward(x)

return (y_pred - y)**2

# define the gradient function sgd

def gradient(x, y):

return 2*x*(x*w - y)

epoch_list =

loss_list =

print('predict (before training)', 4, forward(4))

for epoch in range(100):

for x,y in zip(x_data, y_data):

grad = gradient(x,y)

w = w - 0.01*grad # update weight by every grad of sample of training set

print("\tgrad:", x, y,grad)

l = loss(x,y)

print("progress:",epoch,"w=",w,"loss=",l)

print('predict (after training)', 4, forward(4))

plt.plot(epoch_list,loss_list)

plt.ylabel('loss')

plt.xlabel('epoch')

plt.show()

PyTorch 深度學習實踐 第2講

第2講 linear model 源 b站 劉二大人 傳送門 pytorch深度學習實踐 線性模型 說明 1 函式forward 中,有乙個變數w。這個變數最終的值是從for迴圈中傳入的。2 for迴圈中,使用了np.arange。若對numpy不太熟悉,傳送門numpy資料計算從入門到實戰 3 p...

PyTorch 深度學習實踐 第9講

第9講 多分類問題 源 b站 劉二大人 傳送門pytorch深度學習實踐 多分類問題 說明 1 softmax的輸入不需要再做非線性變換,也就是說softmax之前不再需要啟用函式 relu softmax兩個作用,如果在進行softmax前的input有負數,通過指數變換,得到正數。所有類的概率求...

PyTorch深度學習實踐 Overview

pytorch是乙個基於torch的python開源機器學習庫,用於自然語言處理等應用程式。它主要由facebookd的人工智慧小組開發,不僅能夠 實現強大的gpu加速,同時還支援動態神經網路。pytorch是乙個動態的框架,而tensorflow是靜態框架 2.x版本也為動態框架優先 靜態框架就是...