第二周编程作业 -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
相关文章
|
2月前
|
机器学习/深度学习 存储 数据可视化
基于机器学习的地震预测(Earthquake Prediction with Machine Learning)(上)
基于机器学习的地震预测(Earthquake Prediction with Machine Learning)
96 0
|
2月前
|
机器学习/深度学习 存储 算法
基于机器学习的地震预测(Earthquake Prediction with Machine Learning)(下)
基于机器学习的地震预测(Earthquake Prediction with Machine Learning)
64 0
|
4月前
|
机器学习/深度学习 算法 调度
【博士每天一篇文献-算法】Neurogenesis Dynamics-inspired Spiking Neural Network Training Acceleration
NDSNN(Neurogenesis Dynamics-inspired Spiking Neural Network)是一种受神经发生动态启发的脉冲神经网络训练加速框架,通过动态稀疏性训练和新的丢弃与生长策略,有效减少神经元连接数量,降低训练内存占用并提高效率,同时保持高准确性。
52 3
|
7月前
|
vr&ar
R语言如何做马尔可夫转换模型markov switching model
R语言如何做马尔可夫转换模型markov switching model
|
7月前
|
vr&ar
R语言如何做马尔科夫转换模型markov switching model
R语言如何做马尔科夫转换模型markov switching model
|
机器学习/深度学习 算法
周志华《Machine Learning》学习笔记(4)--线性模型
笔记的前一部分主要是对机器学习预备知识的概括。
157 0
周志华《Machine Learning》学习笔记(4)--线性模型
|
机器学习/深度学习 算法 数据挖掘
周志华《Machine Learning》学习笔记(12)--降维与度量学习
样本的特征数称为维数(dimensionality),当维数非常大时,也就是现在所说的“维数灾难”,具体表现在:在高维情形下,数据样本将变得十分稀疏
230 0
周志华《Machine Learning》学习笔记(12)--降维与度量学习
|
Python
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(下)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(下)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(下)
|
BI 容器
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(上)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)
Machine Learning-L9-贝叶斯分类器(涉及贝叶斯的全在这了)(上)
|
机器学习/深度学习 索引
cs224w(图机器学习)2021冬季课程学习笔记11 Theory of Graph Neural Networks
cs224w(图机器学习)2021冬季课程学习笔记11 Theory of Graph Neural Networks
cs224w(图机器学习)2021冬季课程学习笔记11 Theory of Graph Neural Networks