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

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

Logistic Regression with a Neural Network mindset


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.


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.

import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset
%matplotlib inline


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.

Let's get more familiar with the dataset. Load the data by running the following code.

# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()


We added "_orig" at the end of image datasets (train and test) because we are going to preprocess them. After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y and test_set_y don't need any preprocessing).

Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code. Feel free also to change the index value and re-run to see other images.

# Example of a picture
index = 25
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  "' picture.")

y = [1], it's a 'cat' picture.

30.png

output_6_1.png

Many software bugs in deep learning come from having matrix/vector dimensions that don't fit. If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many bugs.


Exercise: Find the values for:

- m_train (number of training examples)

- m_test (number of test examples)

- num_px (= height = width of a training image)

Remember that train_set_x_orig is a numpy-array of shape (m_train, num_px, num_px, 3). For instance, you can access m_train by writing train_set_x_orig.shape[0].

### START CODE HERE ### (≈ 3 lines of code)
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
### END CODE HERE ###
print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("Height/Width of each image: num_px = " + str(num_px))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_set_x shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))

Number of training examples: m_train = 209
Number of testing examples: m_test = 50
Height/Width of each image: num_px = 64
Each image is of size: (64, 64, 3)
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)


Expected Output for m_train, m_test and num_px:

<table style="width:15%">

<tr>

<td>m_train</td>

<td> 209 </td>

</tr>

<tr>

<td>m_test</td>

<td> 50 </td>

</tr>

<tr>

<td>num_px</td>

<td> 64 </td>

</tr>

</table>

For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px $$ num_px $$ 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.

Exercise: Reshape the training and test data sets so that images of size (num_px, num_px, 3) are flattened into single vectors of shape (num_px $$ num_px $$ 3, 1).

A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b$$c$$d, a) is to use:

X_flatten = X.reshape(X.shape[0], -1).T      # X.T is the transpose of X

# Reshape the training and test examples
### START CODE HERE ### (≈ 2 lines of code)
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T
### END CODE HERE ###
print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))

train_set_x_flatten shape: (12288, 209)
train_set_y shape: (1, 209)
test_set_x_flatten shape: (12288, 50)
test_set_y shape: (1, 50)
sanity check after reshaping: [17 31 56 22 33]


Expected Output:

<table style="width:35%">

<tr>

<td>train_set_x_flatten shape</td>

<td> (12288, 209)</td>

</tr>

<tr>

<td>train_set_y shape</td>

<td>(1, 209)</td>

</tr>

<tr>

<td>test_set_x_flatten shape</td>

<td>(12288, 50)</td>

</tr>

<tr>

<td>test_set_y shape</td>

<td>(1, 50)</td>

</tr>

<tr>

<td>sanity check after reshaping</td>

<td>[17 31 56 22 33]</td>

</tr>

</table>

To represent color images, the red, green and blue channels (RGB) must be specified for each pixel, and so the pixel value is actually a vector of three numbers ranging from 0 to 255.

One common preprocessing step in machine learning is to center and standardize your dataset, meaning that you substract the mean of the whole numpy array from each example, and then divide each example by the standard deviation of the whole numpy array. But for picture datasets, it is simpler and more convenient and works almost as well to just divide every row of the dataset by 255 (the maximum value of a pixel channel).

Let's standardize our dataset.

train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.


<font color='blue'>

What you need to remember:

Common steps for pre-processing a new dataset are:

  • Figure out the dimensions and shapes of the problem (m_train, m_test, num_px, ...)
  • Reshape the datasets such that each example is now a vector of size (num_px * num_px * 3, 1)
  • "Standardize" the data


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!


Mathematical expression of the algorithm:

For one example $x^{(i)}$:

$$z^{(i)} = w^T x^{(i)} + b \tag{1}$$

$$\hat{y}^{(i)} = a^{(i)} = sigmoid(z^{(i)})\tag{2}$$

$$ \mathcal{L}(a^{(i)}, y^{(i)}) =  - y^{(i)}  \log(a^{(i)}) - (1-y^{(i)} )  \log(1-a^{(i)})\tag{3}$$

The cost is then computed by summing over all training examples:

$$ J = \frac{1}{m} \sum_{i=1}^m \mathcal{L}(a^{(i)}, y^{(i)})\tag{6}$$


Key steps:

In this exercise, you will carry out the following steps:

- Initialize the parameters of the model

- Learn the parameters for the model by minimizing the cost

- Use the learned parameters to make predictions (on the test set)

- Analyse the results and conclude

相关文章
|
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》的翻译与解读