吳恩達深度學習學習筆記——C1W2——神經網絡基礎——作業2——用神經網絡的思路實現Logistic迴歸

可以明確的說,如果不自己一步步調試作業代碼,很難看懂作業內容。

這裏主要梳理一下作業的主要內容和思路,完整作業文件可參考:

http://localhost:8888/tree/Andrew-Ng-Deep-Learning-notes/assignments/C1W2

作業完整截圖,參考本文結尾:作業完整截圖。

 

作業指導及目標

Logistic Regression with a Neural Network mindset(用神經網絡的思路實現Logistic迴歸)

Welcome to your first (required) programming assignment! You will build a logistic regression classifier to recognize cats. This assignment will step you through how to do this with a Neural Network mindset, and so will also hone your intuitions about deep learning.

Instructions:

  • Do not use loops (for/while) in your code, unless the instructions explicitly ask you to do so.

You will learn to:

  • Build the general architecture of a learning algorithm, including:
    • Initializing parameters
    • Calculating the cost function and its gradient
    • Using an optimization algorithm (gradient descent)
  • Gather all three functions above into a main model function, in the right order.

...

 

主要軟件包(numpy, h5py, matplotlib, PIL, scipy等)

1 - Packages

First, let's run the cell below to import all the packages that you will need during this assignment.

  • numpy is the fundamental package for scientific computing with Python.
  • h5py is a common package to interact with a dataset that is stored on an H5 file.
  • matplotlib is a famous library to plot graphs in Python.
  • PIL and scipy are used here to test your model with your own picture at the end.

...

 

問題概要(要解決什麼問題?創建一個簡單的圖像識別算法(Logistic迴歸),判斷給定的一幅圖片中是否包含一隻貓)

2 - Overview of the Problem set

Problem Statement: You are given a dataset ("data.h5") containing: - a training set of m_train images labeled as cat (y=1) or non-cat (y=0) - a test set of m_test images labeled as cat or non-cat - each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).

You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-cat.

...

 

算法的粗略結構

3 - General Architecture of the learning algorithm

It's time to design a simple algorithm to distinguish cat images from non-cat images.

You will build a Logistic Regression, using a Neural Network mindset. The following Figure explains why Logistic Regression is actually a very simple Neural Network!

...

 

構造算法的零件

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 𝑠𝑖𝑔𝑚𝑜𝑖𝑑(𝑤𝑇𝑥+𝑏)=11+𝑒−(𝑤𝑇𝑥+𝑏)sigmoid(wTx+b)=11+e−(wTx+b) to make predictions. Use np.exp().

...

 

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.

...

 

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.

...

 

4.4 - 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.

...

 

重點:

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 - Merge all functions into a model(將定義好的函數組成模型)

You will now see how the overall model is structured by putting together all the building blocks (functions implemented in the previous parts) together, in the right order.

Exercise: Implement the model function. Use the following notation: - Y_prediction for your predictions on the test set - Y_prediction_train for your predictions on the train set - w, costs, grads for the outputs of optimize()

...

 

6 - Further analysis (optional/ungraded exercise)(進一步分析)

Congratulations on building your first image classification model. Let's analyze it further, and examine possible choices for the learning rate 𝛼α.

...

 

7 - Test with your own image (optional/ungraded exercise)(使用自己的圖片進行測試)

Congratulations on finishing this assignment. You can use your own image and see the output of your model. To do that: 1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub. 2. Add your image to this Jupyter Notebook's directory, in the "images" folder 3. Change your image's name in the following code 4. Run the code and check if the algorithm is right (1 = cat, 0 = non-cat)!

重點:

What to remember from this assignment:
1. Preprocessing the dataset is important.
2. You implemented each function separately: initialize(), propagate(), optimize(). Then you built a model().
3. Tuning the learning rate (which is an example of a "hyperparameter") can make a big difference to the algorithm. You will see more examples of this later in this course!


 

作業完整截圖:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章