优化算法的实现

2019-10-24  本文已影响0人  此间不留白

前言

优化算法的学习中,已经详细了解了各种优化算法的数学原理和相关公式,现在,可以通过python实现这些优化算法,并将其应用至神经网络中的实践中了。

在开始实现这些优化算法之前,首先需要导入相关python库,具体如下所示;

import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import math
import sklearn
import sklearn.datasets
%matplotlib inline
plt.rcParams['figure.figsize'] = (7.0, 4.0) # 设置图片默认大小
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

梯度下降算法

深度学习中,使用最广法且最简单的优化算法就是梯度下降算法了(Batch Gradient descent),对于一个l层的神经网路而言,梯度下降算法的参数更新如下所示:
W^{[l]} = W^{[l]} - \alpha \text{ } dW^{[l]} \tag{1}
b^{[l]} = b^{[l]} - \alpha \text{ } db^{[l]} \tag{2}

根据以上公式,梯度下降算法的python实现,如下所示:

def update_parameters_with_gd(parameters, grads, learning_rate):
   
    L = len(parameters) 
    for l in range(L):
 
        parameters["W" + str(l+1)] = parameters["W"+str(l+1)] - learning_rate*grads['dW'+str(l+1)]
        parameters["b" + str(l+1)] = parameters["b"+str(l+1)] - learning_rate*grads['db'+str(l+1)]
    

注意:神经网络的相关实现可以参考一步步搭建一个神经网络的相关代码

随机梯度下降算法

与上述梯度下降算法相比,随机梯度下降算法(SGD)是将每次只训练一个样本,等价于mini-batch 梯度下降算法的mini-batch = 1,与常规梯度下降算法的实现相比,如下所示:

# 常规梯度下降算法
X = data_input
Y = labels
parameters = initialize_parameters(layers_dims)
for i in range(0, num_iterations):
    # 前向传播
    a, caches = forward_propagation(X, parameters)
    # 损失计算
    cost = compute_cost(a, Y)
    # 反向传播
    grads = backward_propagation(a, caches, parameters)
    # 参数更新
    parameters = update_parameters(parameters, grads)
        

# 随机梯度下降算法
X = data_input
Y = labels
parameters = initialize_parameters(layers_dims)
for i in range(0, num_iterations):
    for j in range(0, m):   #遍历样本
        # 前向传播算法
        a, caches = forward_propagation(X[:,j], parameters)
        # 计算损失
        cost = compute_cost(a, Y[:,j])
        # 反向传播
        grads = backward_propagation(a, caches, parameters)
        # 参数更新
        parameters = update_parameters(parameters, grads)

因为随机梯度下降算法在参数更新前,只训练一个样本,当训练集较大时,算法的收敛速度更快,但是在收敛过程中是震荡收敛的,而常规的梯度下降算法的收敛过程更加平滑,在二维图形中的示例,如下图所示:


注意:随机梯度下降算法的实现整体上需要3个循环,分别是:

mini-batch 梯度下降

根据以上介绍,具体的代码实现方式,如下所示:


def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):
    """
   创建一个随机的mini-batch (X,Y)
       参数::
    X -- 输入数据集维数为:(特征数量,样本大小)
    Y -- 输出标签,维数为(样本大小,1)
    mini_batch_size -- mini-batch 的数量,为整数
    
    返回值
    mini_batches -- 同步列表 (mini_batch_X, mini_batch_Y)


    """
    
    np.random.seed(seed)       
    m = X.shape[1]                
    mini_batches = []
        
 
    permutation = list(np.random.permutation(m))
    shuffled_X = X[:, permutation]
    shuffled_Y = Y[:, permutation].reshape((1,m))

    # 向下取整,方便计算剩余的数据大小不足 mini-batch
    num_complete_minibatches = math.floor(m/mini_batch_size) 
    for k in range(0, num_complete_minibatches):
       
        mini_batch_X = shuffled_X[:,k*mini_batch_size:(k+1)*mini_batch_size]
        mini_batch_Y = shuffled_Y[:,k*mini_batch_size:(k+1)*mini_batch_size]
     
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    
    # 不能别min_batch_size 整除的数据的处理
    if m % mini_batch_size != 0:
        
        mini_batch_X = shuffled_X[(mini_batch_size*math.floor((m/mini_batch_size))):m]
        mini_batch_Y = shuffled_Y[(mini_batch_size*math.floor((m/mini_batch_size))):m]
       
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    
    return mini_batches,shuffled_X,shuffled_Y

随机梯度下降法与mini-batch 梯度下降法的收敛形式在二维平面上如下所示,与常规的梯度下降法相比,mini-batch梯度下降,不仅能够加快算法收敛速度,还能在一定程度上能够消除震荡。

动量梯度下降法

与mini-batch梯度下降法相比,动量梯度下降法能够使算法收敛更加平滑,动量梯度下降法采用了指数加权平均的参数更新方法,之前更新的梯度会对本次的梯度更新起到修正作用,所以,能够在很大程度上减小震荡。

实现动量梯度下降算法之前,首先需要对指数加权平均参数进行初始化,其初始化的实现代码如下所示:


def initialize_velocity(parameters):
   
    L = len(parameters) // 神经网络的层数
    v = {}
  
    for l in range(L):
     
        v["dW" + str(l+1)] = np.zeros(parameters["W"+str(l+1)].shape)
        v["db" + str(l+1)] = np.zeros(parameters["b"+str(l+1)].shape)
         
    return v

利用指数加权平均更新参数的公式如下所示:
\begin{cases} v_{dW^{[l]}} = \beta v_{dW^{[l]}} + (1 - \beta) dW^{[l]} \\ W^{[l]} = W^{[l]} - \alpha v_{dW^{[l]}} \end{cases}\tag{3}

\begin{cases} v_{db^{[l]}} = \beta v_{db^{[l]}} + (1 - \beta) db^{[l]} \\ b^{[l]} = b^{[l]} - \alpha v_{db^{[l]}} \end{cases}\tag{4}

根据以上公式,实现代码如下所示:



def update_parameters_with_momentum(parameters, grads, v, beta, learning_rate):
    L = len(parameters) 
    for l in range(L):
      
        v["dW" + str(l+1)] = beta*v["dW"+str(l+1)] + (1-beta)*grads["dW"+str(l+1)]
        v["db" + str(l+1)] = beta*v["db"+str(l+1)] + (1-beta)*grads["db"+str(l+1)]
       
        parameters["W" + str(l+1)] = parameters["W"+str(l+1)] - learning_rate*v["dW"+str(l+1)]
        parameters["b" + str(l+1)] = parameters["b"+str(l+1)] - learning_rate*v["db"+str(l+1)]
     
        
    return parameters, v

\beta的值越大,则算法收敛越平滑,当\beta = 0时,相当于常规的梯度下降算法,\beta的取值在可以在0.8-0.999之间,一般情况下,默认取值设置为0.9

Adam优化算法的实现

adam优化算法是将RMSprop算法和动量梯度下降法结合在一起的算法,其具体实现方式可以分为以下几个步骤:

具体公式如下所示:

\begin{cases} v_{dW^{[l]}} = \beta_1 v_{dW^{[l]}} + (1 - \beta_1) \frac{\partial \mathcal{J} }{ \partial W^{[l]} } \\ v^{corrected}_{dW^{[l]}} = \frac{v_{dW^{[l]}}}{1 - (\beta_1)^t} \\ s_{dW^{[l]}} = \beta_2 s_{dW^{[l]}} + (1 - \beta_2) (\frac{\partial \mathcal{J} }{\partial W^{[l]} })^2 \\ s^{corrected}_{dW^{[l]}} = \frac{s_{dW^{[l]}}}{1 - (\beta_1)^t} \\ W^{[l]} = W^{[l]} - \alpha \frac{v^{corrected}_{dW^{[l]}}}{\sqrt{s^{corrected}_{dW^{[l]}}} + \varepsilon} \end{cases}

将相关参数存放在字典中,并初始化这个字典的代码如下所示:


def initialize_adam(parameters) :
  
    L = len(parameters) // 2 # number of layers in the neural networks
    v = {}
    s = {}
    for l in range(L):
  
        v["dW" + str(l+1)] = np.zeros((parameters["W"+str(l+1)]).shape)
        v["db" + str(l+1)] = np.zeros((parameters["b"+str(l+1)]).shape)
        s["dW" + str(l+1)] = np.zeros((parameters["W"+str(l+1)]).shape)
        s["db" + str(l+1)] = np.zeros((parameters["b"+str(l+1)]).shape)

    
    return v, s

初始化参数完成之后,根据以上公式,实现优化算法的代码如下所示:




def update_parameters_with_adam(parameters, grads, v, s, t, learning_rate = 0.01,
                                beta1 = 0.9, beta2 = 0.999,  epsilon = 1e-8):
 
    L = len(parameters) // 2                 #神经网络的层数
    v_corrected = {}                       #初始化字典
    s_corrected = {}                   
    
    
    for l in range(L):
    
        v["dW" + str(l+1)] = beta1*v["dW"+str(l+1)] +(1-beta1)*grads["dW"+str(l+1)]
        v["db" + str(l+1)] = beta1*v["db"+str(l+1)] +(1-beta1)*grads["db"+str(l+1)]
     
        v_corrected["dW" + str(l+1)] = v["dW"+str(l+1)]/(1-beta1**2)
        v_corrected["db" + str(l+1)] = v["db"+str(l+1)]/(1-beta1**2)
      
        s["dW" + str(l+1)] = beta2*s["dW"+str(l+1)]+(1-beta2)*(grads["dW"+str(l+1)]**2)
        s["db" + str(l+1)] = beta2*s["db"+str(l+1)]+(1-beta2)*(grads["db"+str(l+1)]**2)
     

        s_corrected["dW" + str(l+1)] = s["dW"+str(l+1)]/(1-beta2**2)
        s_corrected["db" + str(l+1)] = s["db"+str(l+1)]/(1-beta2**2)
       
        parameters["W" + str(l+1)] = parameters["W"+str(l+1)] - learning_rate*(v_corrected["dW"+str(l+1)]/(np.sqrt(s_corrected["dW"+str(l+1)])+epsilon))
        parameters["b" + str(l+1)] =parameters["b"+str(l+1)] - learning_rate*(v_corrected["db"+str(l+1)]/(np.sqrt(s_corrected["db"+str(l+1)])+epsilon))
      
     
    return parameters, v, s

为模型使用不同的优化算法

实现以上各种优化算法之后,现在就可以在模型中使用不同的优化算法来比较模型性能和收敛速度。

首先,加载数据集并可视化,实现方式如下所示



def load_dataset():
    np.random.seed(3)
    train_X, train_Y = sklearn.datasets.make_moons(n_samples=300, noise=.2) #300 #0.2 
    # 可视化数据
    plt.scatter(train_X[:, 0], train_X[:, 1], c=train_Y, s=40, cmap=plt.cm.Spectral);
    train_X = train_X.T
    train_Y = train_Y.reshape((1, train_Y.shape[0]))
    
    return train_X, train_Y
train_X, train_Y = load_dataset()

调用模型中不同的算法实现方式如下所示:



def model(X, Y, layers_dims, optimizer, learning_rate = 0.0007, mini_batch_size = 64, beta = 0.9,
          beta1 = 0.9, beta2 = 0.999,  epsilon = 1e-8, num_epochs = 10000, print_cost = True):
    """
  
    参数
    X -- 输入数据
    Y -- 输出标签
    layers_dims -- 神经网络的层数
    learning_rate -- 学习率
    mini_batch_size -- mini-batch大小
    beta -- 动量梯度超参数
    beta1 -- 梯度指数加权平均的超参数
    beta2 -- 梯度平方指数加权平均的超参数
    epsilon -- adam优化算法的被除数的修正值
    num_epochs -- 迭代次数
    print_cost -- 为真的情况下打印出每1000次迭代的损失

    返回值:
    parameters -- 包含在字典中的参数值
    """

    L = len(layers_dims)             # 神经网络的层数 
    costs = []                       
    t = 0                            # adma算法初始化的计数器
    seed = 10                        # 固定随机种子
    
    # 初始化参数
    parameters = initialize_parameters(layers_dims)

    
    if optimizer == "gd":
        pass #不需要任何优化
    elif optimizer == "momentum":
        v = initialize_velocity(parameters)
    elif optimizer == "adam":
        v, s = initialize_adam(parameters)
    
    # Optimization loop
    for i in range(num_epochs):
        
        # Define the random minibatches. We increment the seed to reshuffle differently the dataset after each epoch
        seed = seed + 1
        minibatches = random_mini_batches(X, Y, mini_batch_size, seed)

        for minibatch in minibatches:

            # 选择一个mini-batch
            (minibatch_X, minibatch_Y) = minibatch

            # 前向传播
            a3, caches = forward_propagation(minibatch_X, parameters)

            # 计算损失
            cost = compute_cost(a3, minibatch_Y)

            # 反向传播
            grads = backward_propagation(minibatch_X, minibatch_Y, caches)

           # 更新参数
            if optimizer == "gd":
                parameters = update_parameters_with_gd(parameters, grads, learning_rate)
            elif optimizer == "momentum":
                parameters, v = update_parameters_with_momentum(parameters, grads, v, beta, learning_rate)
            elif optimizer == "adam":
                t = t + 1 
                parameters, v, s = update_parameters_with_adam(parameters, grads, v, s,
                                                               t, learning_rate, beta1, beta2,  epsilon)
        
        #打印出每1000次迭代的损失
        if print_cost and i % 1000 == 0:
            print ("Cost after epoch %i: %f" %(i, cost))
        if print_cost and i % 100 == 0:
            costs.append(cost)
                
    # 绘制模型损失图
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('epochs (per 100)')
    plt.title("Learning rate = " + str(learning_rate))
    plt.show()

    return parameters

mini-batch梯度下降

通过mini-batch梯度下降法的实现代码如下所示


# 训练一个3层神经网络
layers_dims = [train_X.shape[0], 5, 2, 1]
parameters = model(train_X, train_Y, layers_dims, optimizer = "gd")

# 预测
predictions = predict(train_X, train_Y, parameters)

# 绘制决策边界
plt.title("Model with Gradient Descent optimization")
axes = plt.gca()
axes.set_xlim([-1.5,2.5])
axes.set_ylim([-1,1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

经过10000次迭代后,模型的分类精确度为76%,绘制出迭代过程中的损失值下降情况和决策边界如下所示:

动量梯度下降法


layers_dims = [train_X.shape[0], 5, 2, 1]
parameters = model(train_X, train_Y, layers_dims, beta = 0.9, optimizer = "momentum")


predictions = predict(train_X, train_Y, parameters)


plt.title("Model with Momentum optimization")
axes = plt.gca()
axes.set_xlim([-1.5,2.5])
axes.set_ylim([-1,1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

分类精确度为79%,损失值的变化和决策边界如下所示:

adam优化算法


layers_dims = [train_X.shape[0], 5, 2, 1]
parameters = model(train_X, train_Y, layers_dims, optimizer = "adam")


predictions = predict(train_X, train_Y, parameters)


plt.title("Model with Adam optimization")
axes = plt.gca()
axes.set_xlim([-1.5,2.5])
axes.set_ylim([-1,1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

通过adam的优化算法,分类精确度接近94%,损失值和决策边界如下所示

附录

吴恩达老师深度学习的作业中,一些搭建神经网络函数实现所示:


import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy.io
import sklearn
import sklearn.datasets

def sigmoid(x):
    """
    Compute the sigmoid of x

    Arguments:
    x -- A scalar or numpy array of any size.

    Return:
    s -- sigmoid(x)
    """
    s = 1/(1+np.exp(-x))
    return s

def relu(x):
    """
    Compute the relu of x

    Arguments:
    x -- A scalar or numpy array of any size.

    Return:
    s -- relu(x)
    """
    s = np.maximum(0,x)
    
    return s

def load_params_and_grads(seed=1):
    np.random.seed(seed)
    W1 = np.random.randn(2,3)
    b1 = np.random.randn(2,1)
    W2 = np.random.randn(3,3)
    b2 = np.random.randn(3,1)

    dW1 = np.random.randn(2,3)
    db1 = np.random.randn(2,1)
    dW2 = np.random.randn(3,3)
    db2 = np.random.randn(3,1)
    
    return W1, b1, W2, b2, dW1, db1, dW2, db2


def initialize_parameters(layer_dims):
    """
    Arguments:
    layer_dims -- python array (list) containing the dimensions of each layer in our network
    
    Returns:
    parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
                    W1 -- weight matrix of shape (layer_dims[l], layer_dims[l-1])
                    b1 -- bias vector of shape (layer_dims[l], 1)
                    Wl -- weight matrix of shape (layer_dims[l-1], layer_dims[l])
                    bl -- bias vector of shape (1, layer_dims[l])
                    
    Tips:
    - For example: the layer_dims for the "Planar Data classification model" would have been [2,2,1]. 
    This means W1's shape was (2,2), b1 was (1,2), W2 was (2,1) and b2 was (1,1). Now you have to generalize it!
    - In the for loop, use parameters['W' + str(l)] to access Wl, where l is the iterative integer.
    """
    
    np.random.seed(3)
    parameters = {}
    L = len(layer_dims) # number of layers in the network

    for l in range(1, L):
        parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1])*  np.sqrt(2 / layer_dims[l-1])
        parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
        
        assert(parameters['W' + str(l)].shape == layer_dims[l], layer_dims[l-1])
        assert(parameters['W' + str(l)].shape == layer_dims[l], 1)
        
    return parameters


def compute_cost(a3, Y):
    
    """
    Implement the cost function
    
    Arguments:
    a3 -- post-activation, output of forward propagation
    Y -- "true" labels vector, same shape as a3
    
    Returns:
    cost - value of the cost function
    """
    m = Y.shape[1]
    
    logprobs = np.multiply(-np.log(a3),Y) + np.multiply(-np.log(1 - a3), 1 - Y)
    cost = 1./m * np.sum(logprobs)
    
    return cost

def forward_propagation(X, parameters):
    """
    Implements the forward propagation (and computes the loss) presented in Figure 2.
    
    Arguments:
    X -- input dataset, of shape (input size, number of examples)
    parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3":
                    W1 -- weight matrix of shape ()
                    b1 -- bias vector of shape ()
                    W2 -- weight matrix of shape ()
                    b2 -- bias vector of shape ()
                    W3 -- weight matrix of shape ()
                    b3 -- bias vector of shape ()
    
    Returns:
    loss -- the loss function (vanilla logistic loss)
    """
    
    # retrieve parameters
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    W3 = parameters["W3"]
    b3 = parameters["b3"]
    
    # LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
    z1 = np.dot(W1,X) + b1
    a1 = relu(z1)
    z2 = np.dot(W2, a1) + b2
    a2 = relu(z2)
    z3 = np.dot(W3, a2) + b3
    a3 = sigmoid(z3)
    
    cache = (z1, a1, W1, b1, z2, a2, W2, b2, z3, a3, W3, b3)
    
    return a3, cache

def backward_propagation(X, Y, cache):
    """
    Implement the backward propagation presented in figure 2.
    
    Arguments:
    X -- input dataset, of shape (input size, number of examples)
    Y -- true "label" vector (containing 0 if cat, 1 if non-cat)
    cache -- cache output from forward_propagation()
    
    Returns:
    gradients -- A dictionary with the gradients with respect to each parameter, activation and pre-activation variables
    """
    m = X.shape[1]
    (z1, a1, W1, b1, z2, a2, W2, b2, z3, a3, W3, b3) = cache
    
    dz3 = 1./m * (a3 - Y)
    dW3 = np.dot(dz3, a2.T)
    db3 = np.sum(dz3, axis=1, keepdims = True)
    
    da2 = np.dot(W3.T, dz3)
    dz2 = np.multiply(da2, np.int64(a2 > 0))
    dW2 = np.dot(dz2, a1.T)
    db2 = np.sum(dz2, axis=1, keepdims = True)
    
    da1 = np.dot(W2.T, dz2)
    dz1 = np.multiply(da1, np.int64(a1 > 0))
    dW1 = np.dot(dz1, X.T)
    db1 = np.sum(dz1, axis=1, keepdims = True)
    
    gradients = {"dz3": dz3, "dW3": dW3, "db3": db3,
                 "da2": da2, "dz2": dz2, "dW2": dW2, "db2": db2,
                 "da1": da1, "dz1": dz1, "dW1": dW1, "db1": db1}
    
    return gradients

def predict(X, y, parameters):
    """
    This function is used to predict the results of a  n-layer neural network.
    
    Arguments:
    X -- data set of examples you would like to label
    parameters -- parameters of the trained model
    
    Returns:
    p -- predictions for the given dataset X
    """
    
    m = X.shape[1]
    p = np.zeros((1,m), dtype = np.int)
    
    # Forward propagation
    a3, caches = forward_propagation(X, parameters)
    
    # convert probas to 0/1 predictions
    for i in range(0, a3.shape[1]):
        if a3[0,i] > 0.5:
            p[0,i] = 1
        else:
            p[0,i] = 0

    # print results

    #print ("predictions: " + str(p[0,:]))
    #print ("true labels: " + str(y[0,:]))
    print("Accuracy: "  + str(np.mean((p[0,:] == y[0,:]))))
    
    return p

def load_2D_dataset():
    data = scipy.io.loadmat('datasets/data.mat')
    train_X = data['X'].T
    train_Y = data['y'].T
    test_X = data['Xval'].T
    test_Y = data['yval'].T

    plt.scatter(train_X[0, :], train_X[1, :], c=train_Y, s=40, cmap=plt.cm.Spectral);
    
    return train_X, train_Y, test_X, test_Y

def plot_decision_boundary(model, X, y):
    # Set min and max values and give it some padding
    x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
    y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # Predict the function value for the whole grid
    Z = model(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.ylabel('x2')
    plt.xlabel('x1')
    plt.scatter(X[0, :], X[1, :], c=np.squeeze(y), cmap=plt.cm.Spectral)
    plt.show()
    
def predict_dec(parameters, X):
    """
    Used for plotting decision boundary.
    
    Arguments:
    parameters -- python dictionary containing your parameters 
    X -- input data of size (m, K)
    
    Returns
    predictions -- vector of predictions of our model (red: 0 / blue: 1)
    """
    
    # Predict using forward propagation and a classification threshold of 0.5
    a3, cache = forward_propagation(X, parameters)
    predictions = (a3 > 0.5)
    return predictions

def load_dataset():
    np.random.seed(3)
    train_X, train_Y = sklearn.datasets.make_moons(n_samples=300, noise=.2) #300 #0.2 
    # Visualize the data
    plt.scatter(train_X[:, 0], train_X[:, 1], c=train_Y, s=40, cmap=plt.cm.Spectral);
    train_X = train_X.T
    train_Y = train_Y.reshape((1, train_Y.shape[0]))
    
    return train_X, train_Y
上一篇下一篇

猜你喜欢

热点阅读