參考梯度下降和随機梯度下降
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPB9ENNRVT1kEROBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zROBlL1QzM5MzNxITM3AjMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
以f(x) = x2為例
import matplotlib.pyplot as plt
import numpy as np
import torch
import math
def gd(eta,iter):
x = 10
results = []
for i in range(iter):
x -= eta * 2 * x # f(x) = x * x的導數為f'(x) = 2 * x
results.append(x)
return results
res = gd(0.2,iter = 10)
print(res)
# 繪制出自變量x的疊代軌迹
def show_trace(res):
n = max(abs(min(res)), abs(max(res)), 10)
f_line = np.arange(-n, n, 0.1)
plt.plot(f_line, [x * x for x in f_line])
plt.plot(res, [x * x for x in res], '-o')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.figure(1)
show_trace(res)
# 學習率
res = gd(0.05,10) #學習率過小疊代10次不能達到最優解
plt.figure(2)
show_trace(res)
res = gd(0.05,100) #疊代100次達到最優解,100是我自己随意設的,隻是100次肯定與最優解誤差不大
plt.figure(3)
print(res,'\n')
show_trace(res)
res = gd(1.1,10) #學習率過大
plt.figure(4)
print(res)
show_trace(res)
# 多元梯度下降------------------------------------------------------
def train_2d(trainer):
x1, x2, s1, s2 = -5, -2, 0, 0 # s1和s2是自變量狀态,本章後續幾節會使用
results = [(x1, x2)]
for i in range(20):
x1, x2, s1, s2 = trainer(x1, x2, s1, s2)
results.append((x1, x2))
print('epoch %d, x1 %f, x2 %f' % (i + 1, x1, x2))
return results
def show_trace_2d(f, results):
plt.plot(*zip(*results), '-o', color='#ff7f0e')
x1, x2 = np.meshgrid(np.arange(-5.5, 1.0, 0.1), np.arange(-3.0, 1.0, 0.1))
plt.contour(x1, x2, f(x1, x2), colors='#1f77b4')
plt.xlabel('x1')
plt.ylabel('x2')
def f_2d(x1, x2): # 目标函數
return x1 ** 2 + 2 * x2 ** 2
def gd_2d(x1, x2, s1, s2):
return (x1 - eta * 2 * x1, x2 - eta * 4 * x2, 0, 0)
eta = 0.1
plt.subplot(3,2,5)
show_trace_2d(f_2d, train_2d(gd_2d))
# 随機梯度下降-----------------------------
# 通過在梯度中添加均值為0的随機噪聲來模拟随機梯度下降,以此來比較它與梯度下降的差別
def sgd_2d(x1, x2, s1, s2):
return (x1 - eta * (2 * x1 + np.random.normal(0.1)),
x2 - eta * (4 * x2 + np.random.normal(0.1)), 0, 0)
plt.subplot(3,2,6)
show_trace_2d(f_2d, train_2d(sgd_2d))
#當訓練資料集的樣本較多時,梯度下降每次疊代的計算開銷較大,因而随機梯度下降通常更受青睐。```