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.
這邊的主要任務就是構建一個logistics迴歸的二分分類器去分類貓,主要分爲如下幾步:
1)導入所需要的package,對data進行preprocessing
2)編寫必要的Basic function,比如sigmoid、initialize_with_zeros等等
3)最後構建model,測試
下面詳細展開:
1.數據的導入以及預處理
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
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
#label_1
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
#label_2
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
train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.
label_1處可以對訓練集和測試集的大小有具體印象,文中是這樣闡述的: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]
.
label_2處就是數據的預處理過程,先試把原始的數據集(m_train,num_px,num_px,3)reshape爲(num_px*num_px*3,m_train),規範數據表示(矩陣的形式)方便後面的計算,原文也給了轉變的具體方式,如下:
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
2.基礎函數的編寫
# GRADED FUNCTION: sigmoid
def sigmoid(z):
s = 1/(1+np.exp(-z))
return s
# GRADED FUNCTION: initialize_with_zeros
def initialize_with_zeros(dim):
w = np.zeros((dim,1))
b = 0
assert(w.shape == (dim, 1))
assert(isinstance(b, float) or isinstance(b, int))
return w, b
# GRADED FUNCTION: propagate
def propagate(w, b, X, Y):
m = X.shape[1]
A = sigmoid(np.dot(w.T,X)+b) # compute activation
cost = -np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))/m # compute cost
dw = np.dot(X,(A-Y).T)/m
db = np.sum(A-Y)/m
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
return grads, cost
# GRADED FUNCTION: optimize
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
costs = []
for i in range(num_iterations):
# Cost and gradient calculation (≈ 1-4 lines of code)
grads,cost = propagate(w,b,X,Y)
# Retrieve derivatives from grads
dw = grads["dw"]
db = grads["db"]
# update rule (≈ 2 lines of code)
w = w-learning_rate*dw
b = b-learning_rate*db
# 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
# GRADED FUNCTION: predict
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1,m))
w = w.reshape(X.shape[0], 1) #保證dot運算不出錯
# Compute vector "A" predicting the probabilities of a cat being present in the picture
A = sigmoid(np.dot(w.T,X)+b)
for i in range(A.shape[1]):
# Convert probabilities A[0,i] to actual predictions p[0,i]
if A[0][i]<=0.5:
Y_prediction[0][i] = 0
else:
Y_prediction[0][i] = 1
assert(Y_prediction.shape == (1, m))
return Y_prediction
這邊需要說明的幾點是,在predict函數中,給出的w = w.reshape(X.shape[0],1),這邊很細節,時刻確保矩陣正確的維度是很重要的。這樣在下面的dot運算中,保證w.T與X的矩陣乘法運算滿足矩陣乘法規則,即爲([1,X.shape[0]]*[X.shape[0],X.shape[1]]=[1,X.shape[1]]),這樣我們就能很清楚的知道A就是1*m的矩陣,所以下面code時就可以確定寫A[0][i]
3.構建model,測試
# GRADED FUNCTION: model
def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):
# initialize parameters with zeros (≈ 1 line of code)
dim = X_train.shape[0]
w,b = initialize_with_zeros(dim)
# Gradient descent (≈ 1 line of code)
params,grads,costs = optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost)
# Retrieve parameters w and b from dictionary "parameters"
w = params["w"]
b = params["b"]
# Predict test/train set examples (≈ 2 lines of code)
Y_prediction_train = predict(w,b,X_train)
Y_prediction_test = predict(w,b,X_test)
# Print train/test Errors
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.01, print_cost = True)
這邊我注意了一下正確率的計算方式如下: