1. 程式人生 > >python資料科學基礎和吳恩達作業補缺補漏(二)

python資料科學基礎和吳恩達作業補缺補漏(二)

Optimization Methods


1-梯度下降法在機器學習中的一個簡單的優化方法是梯度下降(GD)。當你對每一步的所有mm例子採取梯度步驟時,它也被稱為批量梯度下降。

  • (Batch) Gradient Descent:
X = data_input
Y = labels
parameters = initialize_parameters(layers_dims)
for i in range(0, num_iterations):
    # Forward propagation
    a, caches = forward_propagation(X, parameters
) # Compute cost. cost = compute_cost(a, Y) # Backward propagation. grads = backward_propagation(a, caches, parameters) # Update parameters. parameters = update_parameters(parameters, grads)
  • Stochastic Gradient Descent:
X = data_input
Y = labels
parameters = initialize_parameters
(layers_dims) for i in range(0, num_iterations): for j in range(0, m): # Forward propagation a, caches = forward_propagation(X[:,j], parameters) # Compute cost cost = compute_cost(a, Y[:,j]) # Backward propagation grads = backward_propagation(a, caches
, parameters) # Update parameters. parameters = update_parameters(parameters, grads)
在隨機梯度下降中,在更新梯度之前,只使用一個訓練示例。當訓練集很大時,SGD可以更快。但是這些引數會“振盪”到最小值,而不是平滑地收斂。這裡有一個例子

您應該記住的是:梯度下降、小批量梯度下降和隨機梯度下降之間的區別是您用來執行一個更新步驟的示例數量。你必須調整學習速率超引數。有了一個良好的小批量大小,通常它的效能優於梯度下降或隨機梯度下降(特別是當訓練集很大時)。 



def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):
    """
    Creates a list of random minibatches from (X, Y)
    
    Arguments:
    X -- input data, of shape (input size, number of examples)
    Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (1, number of examples)
    mini_batch_size -- size of the mini-batches, integer
    
    Returns:
    mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)
    """
    
    np.random.seed(seed)            # To make your "random" minibatches the same as ours
    m = X.shape[1]                  # number of training examples
    mini_batches = []
        
    # Step 1: Shuffle (X, Y)
    permutation = list(np.random.permutation(m))
    shuffled_X = X[:, permutation]
    shuffled_Y = Y[:, permutation].reshape((1,m))


    # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
    num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning
    for k in range(0, num_complete_minibatches):
        ### START CODE HERE ### (approx. 2 lines)
        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]
        ### END CODE HERE ###
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    
    # Handling the end case (last mini-batch < mini_batch_size)
    if m % mini_batch_size != 0:
        ### START CODE HERE ### (approx. 2 lines)
        mini_batch_X = shuffled_X[:, num_complete_minibatches * mini_batch_size : m]
        mini_batch_Y = shuffled_Y[:, num_complete_minibatches * mini_batch_size : m]
        ### END CODE HERE ###
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    

    return mini_batches

您應該記住的是:移動(洗牌)和分割槽是構建小批量的兩個步驟所需的兩個步驟,通常被選擇為小批量大小,例如,16、32、64,128。


3 - Momentum


因為小批量梯度下降在看到一個例子的一個子集後,會進行一個引數更新,更新的方向有一些差異,所以小批量梯度下降的路徑將會“振盪”趨向收斂。使用動量可以減少這些振盪。動量考慮了過去的漸變,以平滑更新。我們將在變數vv中儲存先前梯度的“方向”。形式上,這將是之前步驟的指數加權平均。你也可以把vv看作是一個滾下山的球的“速度”,根據山的坡度/坡度的方向建立速度(和動量)。


圖3:紅色箭頭顯示了一個帶動量的小批量梯度下降的方向。藍色的點顯示了每個步驟的梯度(關於當前的小批)的方向。我們不只是遵循梯度,而是讓梯度影響vv,然後在vv的方向上邁出一步。






Adam




動量通常是有幫助的,但是考慮到小的學習速率和簡單的資料集,它的影響幾乎是不可能的。另外,你在成本中看到的巨大振盪來自於這樣一個事實,即一些小批量對於優化演算法來說更加困難。另一方面,亞當明顯優於小批量梯度下降和動量。如果您在這個簡單的資料集上執行這個模型,那麼這三種方法都將帶來非常好的結果。然而,你已經看到亞當收斂得快得多。亞當的一些優點包括:相對較低的記憶體需求(儘管高於梯度下降和隨動量的梯度下降)通常可以很好地工作,即使對超引數進行微調(除了)