第二周编程作业 -Logistic Regression with a Neural Network mindset(二)

简介: 第二周编程作业 -Logistic Regression with a Neural Network mindset(二)

4 - Building the parts of our algorithm


The main steps for building a Neural Network are:

  1. Define the model structure (such as number of input features)
  2. Initialize the model's parameters
  3. Loop:
  • Calculate current loss (forward propagation)
  • Calculate current gradient (backward propagation)
  • Update parameters (gradient descent)

You often build 1-3 separately and integrate them into one function we call model().


4.1 - Helper functions


Exercise: Using your code from "Python Basics", implement sigmoid(). As you've seen in the figure above, you need to compute $sigmoid( w^T x + b) = \frac{1}{1 + e{-(wT x + b)}}$ to make predictions. Use np.exp().

# GRADED FUNCTION: sigmoid
def sigmoid(z):
    """
    Compute the sigmoid of z
    Arguments:
    z -- A scalar or numpy array of any size.
    Return:
    s -- sigmoid(z)
    """
    ### START CODE HERE ### (≈ 1 line of code)
    s = 1.0/(1+np.exp(-z))
    ### END CODE HERE ###
    return s

print ("sigmoid([0, 2]) = " + str(sigmoid(np.array([0,2]))))

sigmoid([0, 2]) = [ 0.5         0.88079708]


Expected Output:

<table>

<tr>

<td>sigmoid([0, 2])</td>

<td> [ 0.5         0.88079708]</td>

</tr>

</table>


4.2 - Initializing parameters


Exercise: Implement parameter initialization in the cell below. You have to initialize w as a vector of zeros. If you don't know what numpy function to use, look up np.zeros() in the Numpy library's documentation.

# GRADED FUNCTION: initialize_with_zeros
def initialize_with_zeros(dim):
    """
    This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0.
    Argument:
    dim -- size of the w vector we want (or number of parameters in this case)
    Returns:
    w -- initialized vector of shape (dim, 1)
    b -- initialized scalar (corresponds to the bias)
    """
    ### START CODE HERE ### (≈ 1 line of code)
    w, b = np.zeros((dim,1)), 0
    ### END CODE HERE ###
    assert(w.shape == (dim, 1))
    assert(isinstance(b, float) or isinstance(b, int))
    return w, b

dim = 2
w, b = initialize_with_zeros(dim)
print ("w = " + str(w))
print ("b = " + str(b))

w = [[ 0.]
 [ 0.]]
b = 0


Expected Output:

<table style="width:15%">

<tr>

<td>  ** w **  </td>

<td> [[ 0.]

[ 0.]] </td>

</tr>

<tr>

<td>  ** b **  </td>

<td> 0 </td>

</tr>

</table>

For image inputs, w will be of shape (num_px $\times$ num_px $\times$ 3, 1).


4.3 - Forward and Backward propagation


Now that your parameters are initialized, you can do the "forward" and "backward" propagation steps for learning the parameters.


Exercise: Implement a function propagate() that computes the cost function and its gradient.


Hints:

Forward Propagation:

  • You get X
  • You compute $A = \sigma(w^T X + b) = (a^{(0)}, a^{(1)}, ..., a^{(m-1)}, a^{(m)})$
  • You calculate the cost function: $J = -\frac{1}{m}\sum_{i=1}{m}y{(i)}\log(a{(i)})+(1-y{(i)})\log(1-a^{(i)})$

Here are the two formulas you will be using:

$$ \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T\tag{7}$$

$$ \frac{\partial J}{\partial b} = \frac{1}{m} \sum_{i=1}^m (a{(i)}-y{(i)})\tag{8}$$

# GRADED FUNCTION: propagate
def propagate(w, b, X, Y):
    """
    Implement the cost function and its gradient for the propagation explained above
    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of size (num_px * num_px * 3, number of examples)
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)
    Return:
    cost -- negative log-likelihood cost for logistic regression
    dw -- gradient of the loss with respect to w, thus same shape as w
    db -- gradient of the loss with respect to b, thus same shape as b
    Tips:
    - Write your code step by step for the propagation. np.log(), np.dot()
    """
    m = X.shape[1]
    # FORWARD PROPAGATION (FROM X TO COST)
    ### START CODE HERE ### (≈ 2 lines of code)
    A = sigmoid(np.dot(w.T,X)+b) # A
    cost = -(1/m)*np.sum(Y*np.log(A) + (1-Y)*np.log(1-A))                            # compute cost
    ### END CODE HERE ###
    # BACKWARD PROPAGATION (TO FIND GRAD)
    ### START CODE HERE ### (≈ 2 lines of code)
    dw = 1/m*np.dot(X,(A-Y).T)
    db = 1/m*np.sum(A-Y)
    ### END CODE HERE ###
    assert(dw.shape == w.shape)
    assert(db.dtype == float)
    cost = np.squeeze(cost)
    assert(cost.shape == ())
    grads = {"dw": dw,
             "db": db}
    return grads, cost

w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]), np.array([[1,0]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))

dw = [[ 0.99993216]
 [ 1.99980262]]
db = 0.499935230625
cost = 6.00006477319


Expected Output:

<table style="width:50%">

<tr>

<td>  ** dw **  </td>

<td> [[ 0.99993216]

[ 1.99980262]]</td>

</tr>

<tr>

<td>  ** db **  </td>

<td> 0.499935230625 </td>

</tr>

<tr>

<td>  ** cost **  </td>

<td> 6.000064773192205</td>

</tr>

</table>


d) Optimization


  • You have initialized your parameters.
  • You are also able to compute a cost function and its gradient.
  • Now, you want to update the parameters using gradient descent.

Exercise: Write down the optimization function. The goal is to learn $w$ and $b$ by minimizing the cost function $J$. For a parameter $\theta$, the update rule is $ \theta = \theta - \alpha \text{ } d\theta$, where $\alpha$ is the learning rate.

# GRADED FUNCTION: optimize
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
    """
    This function optimizes w and b by running a gradient descent algorithm
    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of shape (num_px * num_px * 3, number of examples)
    Y -- true "label" vector (containing 0 if non-cat, 1 if cat), of shape (1, number of examples)
    num_iterations -- number of iterations of the optimization loop
    learning_rate -- learning rate of the gradient descent update rule
    print_cost -- True to print the loss every 100 steps
    Returns:
    params -- dictionary containing the weights w and bias b
    grads -- dictionary containing the gradients of the weights and bias with respect to the cost function
    costs -- list of all the costs computed during the optimization, this will be used to plot the learning curve.
    Tips:
    You basically need to write down two steps and iterate through them:
        1) Calculate the cost and the gradient for the current parameters. Use propagate().
        2) Update the parameters using gradient descent rule for w and b.
    """
    costs = []
    for i in range(num_iterations):
        # Cost and gradient calculation (≈ 1-4 lines of code)
        ### START CODE HERE ### 
        grads, cost = propagate(w, b, X, Y)
        costs.append(cost)
        ### END CODE HERE ###
        # Retrieve derivatives from grads
        dw = grads["dw"]
        db = grads["db"]
        # update rule (≈ 2 lines of code)
        ### START CODE HERE ###
        w = w - learning_rate*dw
        b = b - learning_rate*db
        ### END CODE HERE ###
        # Record the costs
        if i % 100 == 0:
            costs.append(cost)
        # Print the cost every 100 training examples
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
    params = {"w": w,
              "b": b}
    grads = {"dw": dw,
             "db": db}
    return params, grads, costs

params, grads, costs = optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009, print_cost = False)
print ("w = " + str(params["w"]))
print ("b = " + str(params["b"]))
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))

w = [[ 0.1124579 ]
 [ 0.23106775]]
b = 1.55930492484
dw = [[ 0.90158428]
 [ 1.76250842]]
db = 0.430462071679


Expected Output:

<table style="width:40%">

<tr>

<td> w </td>

<td>[[ 0.1124579 ]

[ 0.23106775]] </td>

</tr>

<tr>
   <td> **b** </td>
   <td> 1.55930492484 </td>
</tr>
<tr>
   <td> **dw** </td>
   <td> [[ 0.90158428]


[ 1.76250842]] </td>

</tr>

<tr>

<td> db </td>

<td> 0.430462071679 </td>

</tr>

</table>


Exercise: The previous function will output the learned w and b. We are able to use w and b to predict the labels for a dataset X. Implement the predict() function. There is two steps to computing predictions:

  1. Calculate $\hat{Y} = A = \sigma(w^T X + b)$
  2. Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector Y_prediction. If you wish, you can use an if/else statement in a for loop (though there is also a way to vectorize this).

# GRADED FUNCTION: predict
def predict(w, b, X):
    '''
    Predict whether the label is 0 or 1 using learned logistic regression parameters (w, b)
    Arguments:
    w -- weights, a numpy array of size (num_px * num_px * 3, 1)
    b -- bias, a scalar
    X -- data of size (num_px * num_px * 3, number of examples)
    Returns:
    Y_prediction -- a numpy array (vector) containing all predictions (0/1) for the examples in X
    '''
    m = X.shape[1]
    Y_prediction = np.zeros((1,m))
    w = w.reshape(X.shape[0], 1)
    # Compute vector "A" predicting the probabilities of a cat being present in the picture
    ### START CODE HERE ### (≈ 1 line of code)
    A =sigmoid(np.dot(w.T,X)+b)
    ### END CODE HERE ###
    for i in range(A.shape[1]):
        # Convert probabilities A[0,i] to actual predictions p[0,i]
        ### START CODE HERE ### (≈ 4 lines of code)
        if A[0,i]<=0.5:
            Y_prediction[0,i] = 0
        else:
            Y_prediction[0,i] = 1
        ### END CODE HERE ###
    assert(Y_prediction.shape == (1, m))
    return Y_prediction

print ("predictions = " + str(predict(w, b, X)))

predictions = [[ 1.  1.]]


Expected Output:

<table style="width:30%">

<tr>

<td>


predictions

</td>

<td>

[[ 1.  1.]]

</td>

</tr>

</table>

<font color='blue'>


What to remember:

You've implemented several functions that:

  • Initialize (w,b)
  • Optimize the loss iteratively to learn parameters (w,b):
  • computing the cost and its gradient
  • updating the parameters using gradient descent
  • Use the learned (w,b) to predict the labels for a given set of examples
相关文章
|
5月前
|
vr&ar
R语言如何做马尔科夫转换模型markov switching model
R语言如何做马尔科夫转换模型markov switching model
|
机器学习/深度学习 自然语言处理 算法
【论文泛读】 知识蒸馏:Distilling the knowledge in a neural network
【论文泛读】 知识蒸馏:Distilling the knowledge in a neural network
【论文泛读】 知识蒸馏:Distilling the knowledge in a neural network
|
机器学习/深度学习 算法
瞎聊机器学习——LR(Logistic Regression)逻辑斯蒂回归(一)
瞎聊机器学习——LR(Logistic Regression)逻辑斯蒂回归(一)
瞎聊机器学习——LR(Logistic Regression)逻辑斯蒂回归(一)
|
机器学习/深度学习 算法 数据挖掘
周志华《Machine Learning》学习笔记(12)--降维与度量学习
样本的特征数称为维数(dimensionality),当维数非常大时,也就是现在所说的“维数灾难”,具体表现在:在高维情形下,数据样本将变得十分稀疏
184 0
周志华《Machine Learning》学习笔记(12)--降维与度量学习
|
BI 容器
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(上)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(上)
|
Python
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(下)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(下)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(下)
|
机器学习/深度学习 数据可视化 PyTorch
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
1146 0
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
|
机器学习/深度学习 人工智能 搜索推荐
【推荐系统论文精读系列】(十一)--DeepFM A Factorization-Machine based Neural Network for CTR Prediction
在推荐系统领域最大化CTR最关键就是要学习用户举止背后复杂的特征交互。尽管现在已经有了一些大的进展,但是现存的方式仍然是只能捕捉低阶或者高阶特征,或者需要专业的特征工程。本篇论文中,我们提出了一种端到端的学习模型,能够同时学习到低阶和高阶的交互特征。我们将这个模型命名为DeepFM,它结合了分解机的能力和深度学习捕捉高阶特征的能力。对比最新谷歌提出的Wide & Deep模型,我们的DeepFM模型不需要任何特征工程,而且会共享特征输入。
244 0
|
机器学习/深度学习 自然语言处理 搜索推荐
【推荐系统论文精读系列】(十三)--Attentional Factorization Machines Learning the Weight of Feature Interactions
有监督学习在机器学习中是最基本的任务之一。它的目标就是推断出一个函数能够预测给定变量的标签。例如,实值标签对于回归问题,而分类标签用于分类问题。他已经广泛的应用于各大应用,包括推荐系统,在线广告,图像识别等。
209 0
【推荐系统论文精读系列】(十三)--Attentional Factorization Machines Learning the Weight of Feature Interactions
|
机器学习/深度学习 存储 算法
Paper:《CatBoost: unbiased boosting with categorical features》的翻译与解读
Paper:《CatBoost: unbiased boosting with categorical features》的翻译与解读
Paper:《CatBoost: unbiased boosting with categorical features》的翻译与解读