Optimization including Convex Optimization and Gradient Descent

溫馨提示:

    本文將介紹統計學中的優化知識,凸優化和梯度下降,多爲公式推導和圖形化展示,較爲硬核

優化與深度學習

優化與估計

儘管優化方法可以最小化深度學習中的損失函數值,但本質上優化方法達到的目標與深度學習的目標並不相同。

  • 優化方法目標:訓練集損失函數值

  • 深度學習目標:測試集損失函數值(泛化性)

  • 藉助圖形直觀比較

%matplotlib inline
import sys
sys.path.append('path to file storge d2lzh1981')
import d2lzh1981 as d2l
from mpl_toolkits import mplot3d # 三維畫圖
import numpy as np
def f(x): return x * np.cos(np.pi * x)
def g(x): return f(x) + 0.2 * np.cos(5 * np.pi * x)

d2l.set_figsize((5, 3))
x = np.arange(0.5, 1.5, 0.01)
fig_f, = d2l.plt.plot(x, f(x),label="train error")
fig_g, = d2l.plt.plot(x, g(x),'--', c='purple', label="test error")
fig_f.axes.annotate('empirical risk', (1.0, -1.2), (0.5, -1.1),arrowprops=dict(arrowstyle='->'))
fig_g.axes.annotate('expected risk', (1.1, -1.05), (0.95, -0.5),arrowprops=dict(arrowstyle='->'))
d2l.plt.xlabel('x')
d2l.plt.ylabel('risk')
d2l.plt.legend(loc="upper right")

優化在深度學習中的挑戰

  1. 局部最小值
  2. 鞍點
  3. 梯度消失
局部最小值

f(x)=xcosπx f(x) = x\cos \pi x

def f(x):
    return x * np.cos(np.pi * x)

d2l.set_figsize((4.5, 2.5))
x = np.arange(-1.0, 2.0, 0.1)
fig,  = d2l.plt.plot(x, f(x))
fig.axes.annotate('local minimum', xy=(-0.3, -0.25), xytext=(-0.77, -1.0),
                  arrowprops=dict(arrowstyle='->'))
fig.axes.annotate('global minimum', xy=(1.1, -0.95), xytext=(0.6, 0.8),
                  arrowprops=dict(arrowstyle='->'))
d2l.plt.xlabel('x')
d2l.plt.ylabel('f(x)');
鞍點

函數在一階導數爲零處(駐點)的黑塞矩陣爲不定矩陣。

x = np.arange(-2.0, 2.0, 0.1)
fig, = d2l.plt.plot(x, x**3)
fig.axes.annotate('saddle point', xy=(0, -0.2), xytext=(-0.52, -5.0),
                  arrowprops=dict(arrowstyle='->'))
d2l.plt.xlabel('x')
d2l.plt.ylabel('f(x)');

海森矩陣

A=[2fx122fx1x22fx1xn2fx2x12fx222fx2xn2fxnx12fxnx22fxn2] A=\left[\begin{array}{cccc}{\frac{\partial^{2} f}{\partial x_{1}^{2}}} & {\frac{\partial^{2} f}{\partial x_{1} \partial x_{2}}} & {\cdots} & {\frac{\partial^{2} f}{\partial x_{1} \partial x_{n}}} \\ {\frac{\partial^{2} f}{\partial x_{2} \partial x_{1}}} & {\frac{\partial^{2} f}{\partial x_{2}^{2}}} & {\cdots} & {\frac{\partial^{2} f}{\partial x_{2} \partial x_{n}}} \\ {\vdots} & {\vdots} & {\ddots} & {\vdots} \\ {\frac{\partial^{2} f}{\partial x_{n} \partial x_{1}}} & {\frac{\partial^{2} f}{\partial x_{n} \partial x_{2}}} & {\cdots} & {\frac{\partial^{2} f}{\partial x_{n}^{2}}}\end{array}\right]

海森矩陣特徵值和鞍點還有局部極小值的點的關係

偏導數爲零的點

  • 特徵值都大於零是局部極小值點
  • 都爲負數是局部極大指點
  • 有正有負就是鞍點

x, y = np.mgrid[-1: 1: 31j, -1: 1: 31j]
z = x**2 - y**2

d2l.set_figsize((6, 4))
ax = d2l.plt.figure().add_subplot(111, projection='3d')
ax.plot_wireframe(x, y, z, **{'rstride': 2, 'cstride': 2})
ax.plot([0], [0], [0], 'ro', markersize=10)
ticks = [-1,  0, 1]
d2l.plt.xticks(ticks)
d2l.plt.yticks(ticks)
ax.set_zticks(ticks)
d2l.plt.xlabel('x')
d2l.plt.ylabel('y');
梯度消失
x = np.arange(-2.0, 5.0, 0.01)
fig, = d2l.plt.plot(x, np.tanh(x))
d2l.plt.xlabel('x')
d2l.plt.ylabel('f(x)')
fig.axes.annotate('vanishing gradient', (4, 1), (2, 0.0) ,arrowprops=dict(arrowstyle='->'))

梯度下降

%matplotlib inline
import numpy as np
import torch
import time
from torch import nn, optim
import math
import sys
sys.path.append('path to file storge d2lzh1981')
import d2lzh1981 as d2l

一維梯度下降


證明:沿梯度反方向移動自變量可以減小函數值

泰勒展開:

f(x+ϵ)=f(x)+ϵf(x)+O(ϵ2) f(x+\epsilon)=f(x)+\epsilon f^{\prime}(x)+\mathcal{O}\left(\epsilon^{2}\right)

代入沿梯度方向的移動量 ηf(x)\eta f^{\prime}(x)

f(xηf(x))=f(x)ηf2(x)+O(η2f2(x)) f\left(x-\eta f^{\prime}(x)\right)=f(x)-\eta f^{\prime 2}(x)+\mathcal{O}\left(\eta^{2} f^{\prime 2}(x)\right)

f(xηf(x))f(x) f\left(x-\eta f^{\prime}(x)\right) \lesssim f(x)

xxηf(x) x \leftarrow x-\eta f^{\prime}(x)


e.g.

f(x)=x2 f(x) = x^2

def f(x):
    return x**2  # Objective function

def gradf(x):
    return 2 * x  # Its derivative

def gd(eta):
    x = 10
    results = [x]
    for i in range(20):
        # eta 學習率
        x -= eta * gradf(x)
        results.append(x)
    print('epoch 20, x:', x)
    return results

res = gd(0.2)

梯度下降軌跡

def show_trace(res):
    n = max(abs(min(res)), abs(max(res)))
    f_line = np.arange(-n, n, 0.01)
    d2l.set_figsize((3.5, 2.5))
    d2l.plt.plot(f_line, [f(x) for x in f_line],'-')
    d2l.plt.plot(res, [f(x) for x in res],'-o')
    d2l.plt.xlabel('x')
    d2l.plt.ylabel('f(x)')
    

show_trace(res)
學習率

學習率過小 Code:show_trace(gd(0.05))
在這裏插入圖片描述

學習率過大 Code:show_trace(gd(1.1))
在這裏插入圖片描述

局部極小值

f(x)=xcoscx f(x) = x\cos cx

c = 0.15 * np.pi

def f(x):
    return x * np.cos(c * x)

def gradf(x):
    return np.cos(c * x) - c * x * np.sin(c * x)

# 學習率不合適容易導致
show_trace(gd(2))
show_trace(gd(0.5))

多維梯度下降

f(x)=[f(x)x1,f(x)x2,,f(x)xd] \nabla f(\mathbf{x})=\left[\frac{\partial f(\mathbf{x})}{\partial x_{1}}, \frac{\partial f(\mathbf{x})}{\partial x_{2}}, \dots, \frac{\partial f(\mathbf{x})}{\partial x_{d}}\right]^{\top}

f(x+ϵ)=f(x)+ϵf(x)+O(ϵ2) f(\mathbf{x}+\epsilon)=f(\mathbf{x})+\epsilon^{\top} \nabla f(\mathbf{x})+\mathcal{O}\left(\|\epsilon\|^{2}\right)

xxηf(x) \mathbf{x} \leftarrow \mathbf{x}-\eta \nabla f(\mathbf{x})

# 訓練 trainer展示x如何更新
def train_2d(trainer, steps=20):
    x1, x2 = -5, -2
    results = [(x1, x2)]
    for i in range(steps):
        x1, x2 = trainer(x1, x2)
        results.append((x1, x2))
    print('epoch %d, x1 %f, x2 %f' % (i + 1, x1, x2))
    return results
# 垂直於等高線梯度下降
def show_trace_2d(f, results): 
    d2l.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))
    d2l.plt.contour(x1, x2, f(x1, x2), colors='#1f77b4')
    d2l.plt.xlabel('x1')
    d2l.plt.ylabel('x2')

e.g.

f(x)=x12+2x22 f(x) = x_1^2 + 2x_2^2

eta = 0.1

def f_2d(x1, x2):  # 目標函數
    return x1 ** 2 + 2 * x2 ** 2

def gd_2d(x1, x2):
    return (x1 - eta * 2 * x1, x2 - eta * 4 * x2)

show_trace_2d(f_2d, train_2d(gd_2d))

自適應方法

牛頓法

優勢 :

梯度下降“步幅”的確定比較困難

而牛頓法相當於可以通過Hessian矩陣來調整“步幅”。

在牛頓法中,局部極小值也可以通過調整學習率來解決。

x+ϵx + \epsilon 處泰勒展開:

f(x+ϵ)=f(x)+ϵf(x)+12ϵf(x)ϵ+O(ϵ3) f(\mathbf{x}+\epsilon)=f(\mathbf{x})+\epsilon^{\top} \nabla f(\mathbf{x})+\frac{1}{2} \epsilon^{\top} \nabla \nabla^{\top} f(\mathbf{x}) \epsilon+\mathcal{O}\left(\|\epsilon\|^{3}\right)

最小值點處滿足: f(x)=0\nabla f(\mathbf{x})=0, 即我們希望 f(x+ϵ)=0\nabla f(\mathbf{x} + \epsilon)=0, 對上式關於 ϵ\epsilon 求導,忽略高階無窮小,有:

f(x)+Hfϵ=0 and hence ϵ=Hf1f(x) \nabla f(\mathbf{x})+\boldsymbol{H}_{f} \boldsymbol{\epsilon}=0 \text { and hence } \epsilon=-\boldsymbol{H}_{f}^{-1} \nabla f(\mathbf{x})

牛頓法需要計算Hessian矩陣的逆,計算量比較大。

c = 0.5

def f(x):
    return np.cosh(c * x)  # Objective

def gradf(x):
    return c * np.sinh(c * x)  # Derivative

def hessf(x):
    return c**2 * np.cosh(c * x)  # Hessian

# Hide learning rate for now
def newton(eta=1):
    x = 10
    results = [x]
    for i in range(10):
        x -= eta * gradf(x) / hessf(x)
        results.append(x)
    print('epoch 10, x:', x)
    return results

show_trace(newton())
# 牛頓法對於有局部極小值的情況
# 和梯度下降的方法有一樣的效果
# 正確的方法還是降低學習率
c = 0.15 * np.pi

def f(x):
    return x * np.cos(c * x)

def gradf(x):
    return np.cos(c * x) - c * x * np.sin(c * x)

def hessf(x):
    return - 2 * c * np.sin(c * x) - x * c**2 * np.cos(c * x)

show_trace(newton())

show_trace(newton(0.5))
在這裏插入圖片描述

收斂性分析

只考慮在函數爲凸函數, 且最小值點上 f(x)>0f''(x^*) > 0 時的收斂速度:

xkx_k 爲第 kk 次迭代後 xx 的值, ek:=xkxe_{k}:=x_{k}-x^{*} 表示 xkx_k 到最小值點 xx^{*} 的距離,由 f(x)=0f'(x^{*}) = 0:

0=f(xkek)=f(xk)ekf(xk)+12ek2f(ξk)for some ξk[xkek,xk] 0=f^{\prime}\left(x_{k}-e_{k}\right)=f^{\prime}\left(x_{k}\right)-e_{k} f^{\prime \prime}\left(x_{k}\right)+\frac{1}{2} e_{k}^{2} f^{\prime \prime \prime}\left(\xi_{k}\right) \text{for some } \xi_{k} \in\left[x_{k}-e_{k}, x_{k}\right]

兩邊除以 f(xk)f''(x_k), 有:

ekf(xk)/f(xk)=12ek2f(ξk)/f(xk) e_{k}-f^{\prime}\left(x_{k}\right) / f^{\prime \prime}\left(x_{k}\right)=\frac{1}{2} e_{k}^{2} f^{\prime \prime \prime}\left(\xi_{k}\right) / f^{\prime \prime}\left(x_{k}\right)

代入更新方程 xk+1=xkf(xk)/f(xk)x_{k+1} = x_{k} - f^{\prime}\left(x_{k}\right) / f^{\prime \prime}\left(x_{k}\right), 得到:

xkxf(xk)/f(xk)=12ek2f(ξk)/f(xk) x_k - x^{*} - f^{\prime}\left(x_{k}\right) / f^{\prime \prime}\left(x_{k}\right) =\frac{1}{2} e_{k}^{2} f^{\prime \prime \prime}\left(\xi_{k}\right) / f^{\prime \prime}\left(x_{k}\right)

xk+1x=ek+1=12ek2f(ξk)/f(xk) x_{k+1} - x^{*} = e_{k+1} = \frac{1}{2} e_{k}^{2} f^{\prime \prime \prime}\left(\xi_{k}\right) / f^{\prime \prime}\left(x_{k}\right)

12f(ξk)/f(xk)c\frac{1}{2} f^{\prime \prime \prime}\left(\xi_{k}\right) / f^{\prime \prime}\left(x_{k}\right) \leq c 時,有:

ek+1cek2 e_{k+1} \leq c e_{k}^{2}

預處理 (Heissan陣輔助梯度下降)

xxηdiag(Hf)1x \mathbf{x} \leftarrow \mathbf{x}-\eta \operatorname{diag}\left(H_{f}\right)^{-1} \nabla \mathbf{x}

梯度下降與線性搜索(共軛梯度法)

隨機梯度下降

隨機梯度下降參數更新

對於有 nn 個樣本對訓練數據集,設 fi(x)f_i(x) 是第 ii 個樣本的損失函數, 則目標函數爲:

f(x)=1ni=1nfi(x) f(\mathbf{x})=\frac{1}{n} \sum_{i=1}^{n} f_{i}(\mathbf{x})

其梯度爲:

f(x)=1ni=1nfi(x) \nabla f(\mathbf{x})=\frac{1}{n} \sum_{i=1}^{n} \nabla f_{i}(\mathbf{x})

每一個樣本的梯度是對整體的梯度的無偏估計

使用該梯度的一次更新的時間複雜度爲 O(n)\mathcal{O}(n)

隨機梯度下降更新公式 O(1)\mathcal{O}(1):

xxηfi(x) \mathbf{x} \leftarrow \mathbf{x}-\eta \nabla f_{i}(\mathbf{x})

且有:

Eifi(x)=1ni=1nfi(x)=f(x) \mathbb{E}_{i} \nabla f_{i}(\mathbf{x})=\frac{1}{n} \sum_{i=1}^{n} \nabla f_{i}(\mathbf{x})=\nabla f(\mathbf{x})
e.g.

f(x1,x2)=x12+2x22 f(x_1, x_2) = x_1^2 + 2 x_2^2

def f(x1, x2):
    return x1 ** 2 + 2 * x2 ** 2  # Objective

def gradf(x1, x2):
    return (2 * x1, 4 * x2)  # Gradient

def sgd(x1, x2):  # Simulate noisy gradient
    global lr  # Learning rate scheduler
    (g1, g2) = gradf(x1, x2)  # Compute gradient
    (g1, g2) = (g1 + np.random.normal(0.1), g2 + np.random.normal(0.1))
    eta_t = eta * lr()  # Learning rate at time t
    return (x1 - eta_t * g1, x2 - eta_t * g2)  # Update variables

eta = 0.1
lr = (lambda: 1)  # Constant learning rate
show_trace_2d(f, train_2d(sgd, steps=50))

動態學習率

  • 在最開始學習率設計比較大,加速收斂
  • 學習率可以設計爲指數衰減或多項式衰減
  • 在優化進行一段時間後可以適當減小學習率來避免振盪

η(t)=ηi if titti+1 piecewise constant η(t)=η0eλt exponential η(t)=η0(βt+1)α polynomial  \begin{array}{ll}{\eta(t)=\eta_{i} \text { if } t_{i} \leq t \leq t_{i+1}} & {\text { piecewise constant }} \\ {\eta(t)=\eta_{0} \cdot e^{-\lambda t}} & {\text { exponential }} \\ {\eta(t)=\eta_{0} \cdot(\beta t+1)^{-\alpha}} & {\text { polynomial }}\end{array}

def exponential():
    global ctr
    ctr += 1
    return math.exp(-0.1 * ctr)

ctr = 1
lr = exponential  # Set up learning rate
show_trace_2d(f, train_2d(sgd, steps=1000))
def polynomial():
    # 迭代次數
    global ctr
    ctr += 1
    return (1 + 0.1 * ctr)**(-0.5)

ctr = 1
lr = polynomial  # Set up learning rate
show_trace_2d(f, train_2d(sgd, steps=50))

小批量隨機梯度下降

讀取數據

讀取數據

def get_data_ch7():
    data = np.genfromtxt('/home/kesci/input/airfoil4755/airfoil_self_noise.dat', delimiter='\t')
    data = (data - data.mean(axis=0)) / data.std(axis=0) # 標準化
    return torch.tensor(data[:1500, :-1], dtype=torch.float32), \
           torch.tensor(data[:1500, -1], dtype=torch.float32) # 前1500個樣本(每個樣本5個特徵)

features, labels = get_data_ch7()
features.shape

數據可視化

import pandas as pd
df = pd.read_csv('path to airfoil_self_noise.dat', delimiter='\t', header=None)
df.head(10)

Stochastic Gradient Descent (SGD)函數

def sgd(params, states, hyperparams):
    for p in params:
        p.data -= hyperparams['lr'] * p.grad.data

訓練

def train_ch7(optimizer_fn, states, hyperparams, features, labels,
              batch_size=10, num_epochs=2):
    # 初始化模型
    net, loss = d2l.linreg, d2l.squared_loss
    
    w = torch.nn.Parameter(torch.tensor(np.random.normal(0, 0.01, size=(features.shape[1], 1)), dtype=torch.float32),
                           requires_grad=True)
    b = torch.nn.Parameter(torch.zeros(1, dtype=torch.float32), requires_grad=True)

    def eval_loss():
        return loss(net(features, w, b), labels).mean().item()

    ls = [eval_loss()]
    data_iter = torch.utils.data.DataLoader(
        torch.utils.data.TensorDataset(features, labels), batch_size, shuffle=True)
    
    for _ in range(num_epochs):
        start = time.time()
        for batch_i, (X, y) in enumerate(data_iter):
            l = loss(net(X, w, b), y).mean()  # 使用平均損失
            
            # 梯度清零
            if w.grad is not None:
                w.grad.data.zero_()
                b.grad.data.zero_()
                
            l.backward()
            optimizer_fn([w, b], states, hyperparams)  # 迭代模型參數
            if (batch_i + 1) * batch_size % 100 == 0:
                ls.append(eval_loss())  # 每100個樣本記錄下當前訓練誤差
    # 打印結果和作圖
    print('loss: %f, %f sec per epoch' % (ls[-1], time.time() - start))
    d2l.set_figsize()
    d2l.plt.plot(np.linspace(0, num_epochs, len(ls)), ls)
    d2l.plt.xlabel('epoch')
    d2l.plt.ylabel('loss')

測試

def train_sgd(lr, batch_size, num_epochs=2):
    train_ch7(sgd, None, {'lr': lr}, features, labels, batch_size, num_epochs)

Result

  • train_sgd(1, 1500, 6)
    在這裏插入圖片描述
  • train_sgd(0.005, 1)
    在這裏插入圖片描述
  • train_sgd(0.05, 10)
    在這裏插入圖片描述

簡化模型

def train_pytorch_ch7(optimizer_fn, optimizer_hyperparams, features, labels,
                    batch_size=10, num_epochs=2):
    # 初始化模型
    net = nn.Sequential(
        nn.Linear(features.shape[-1], 1)
    )
    loss = nn.MSELoss()
    optimizer = optimizer_fn(net.parameters(), **optimizer_hyperparams)

    def eval_loss():
        return loss(net(features).view(-1), labels).item() / 2

    ls = [eval_loss()]
    data_iter = torch.utils.data.DataLoader(
        torch.utils.data.TensorDataset(features, labels), batch_size, shuffle=True)

    for _ in range(num_epochs):
        start = time.time()
        for batch_i, (X, y) in enumerate(data_iter):
            # 除以2是爲了和train_ch7保持一致, 因爲squared_loss中除了2
            l = loss(net(X).view(-1), y) / 2 
            
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            if (batch_i + 1) * batch_size % 100 == 0:
                ls.append(eval_loss())
    # 打印結果和作圖
    print('loss: %f, %f sec per epoch' % (ls[-1], time.time() - start))
    d2l.set_figsize()
    d2l.plt.plot(np.linspace(0, num_epochs, len(ls)), ls)
    d2l.plt.xlabel('epoch')
    d2l.plt.ylabel('loss')

train_pytorch_ch7(optim.SGD, {“lr”: 0.05}, features, labels, 10)
在這裏插入圖片描述

發佈了46 篇原創文章 · 獲贊 21 · 訪問量 3426
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章