DeepLearning(AndrewNg)-Course1-week1&week2

Neural Networks and Deep Learning

DeepLearning-Course1-week1&week2

一、Week1 content

1、What is a Neural NetWork

  • ReLU (Rectified Linear Unite)

2、Supervised Learning with Neural Networks

  • Common Neural Networks

    • SNN(Standard Neural Network)
    • CNN(Convolution Neural Network)
    • RNN(Recurrent Neural Network)
  • The difference between Structed Data and Unstructed Data

    • Structed Data is storage in a database.
    • Unstructed Data includes audio,image,text,etc.

3、Why is Deep Learning take off

  • Scale drives deep learning process.(Data,Computation,Algorithm)
  • ReLU has better performance than Sigmoid because gradient descent quickly increases the computation speed.

二、Week2 content

1、Binary Classification

2、Logistic Regression

3、Logistic Regression Cost Function

4、Gradient Descent

5、Detrivaties

6、More Detrivaties Examples

7、Computation Graph

8、Detrivaties with a Computation Graph

9、Logistic Regression Gradient Descent

10、Gradient Descent on M Example

11、Vectorization

12、More Vectorization Example

13、Vectorization Logistic Regression

14、Vectorization Logistic Regression’s Gradient Computation

15、Broadcasting in Python(Numpy)

16、A Note on Python/Numpy Vectors

17、Jupyter/Ipython Notebooks

18、Explanation of Logistic Regression Cost Function

三、Programming assignment(Logistic Regression in a Neural Network mindset)

1、Main Content

  • 加载数据,对训练/测试集的输入部分进行预处理
    • 将图片三通道像素值转换成列向量,并按列进行排列成矩阵
    • 对输入矩阵进行标准化
  • 使用Numpy以及Python中的广播实现sigmoid函数
  • 实现前向传播和后向传播
  • 计算梯度下降
  • 检测图片

2、Problem Solving

  • 使用代码检测png 图像,存在像素矩阵转换问题

    png图像使用PIL读入是四通道图像,即RGBA

    图像检测时检测三通道,需要对图像模式进行切换

    from PIL import Image
    
    img = Image.open('*.png')
    
    img = img.convert("RGB")
    
  • 学习率设置为0.05时,程序报错:ERROR:RuntimeWarning: divide by zero encountered in log

    使用np.log对matrix/vector进行广播处理,由于计算时数字太小,计算过程出现inf。

    import numpy as np
    
    np.log(A)
    
    ERROR:RuntimeWarning: divide by zero encountered in log
    # 改变浮点数的精度位
    np.log(A+1e-5)
    

3、Complete Code

# Simple image-recognition learning algorithm
# Initializing parameters
# Caculating the cost function and its gradient
# Using optimization algorithm(gradient descent)	

# Scientific computing with Python
import numpy as np
import copy
# Plot graphs in Python
import matplotlib.pyplot as plt
# Interact with a dataset that is stored on an H5 file
import h5py
import scipy
from PIL import Image
from scipy import ndimage

%matplotlib inline
%load_ext autoreload
%autoreload 2

# Load data
def load_dataset():
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', 'r')
    train_set_x_orig = np.array(train_dataset['train_set_x'][:])
    train_set_y = np.array(train_dataset['train_set_y'][:])
    
    test_dataset = h5py.File('datasets/test_catvnoncat.h5', 'r')
    test_set_x_orig = np.array(test_dataset['test_set_x'][:])
    test_set_y = np.array(test_dataset['test_set_y'][:])
    
    classes = np.array(test_dataset['list_classes'][:])
    
    train_set_y = train_set_y.reshape((1, train_set_y.shape[0]))
    test_set_y = test_set_y.reshape((1, test_set_y.shape[0]))
    
    return train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes
    
# Loading the data(cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes=load_dataset()

m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

# Reshape the train and test set
train_set_x_flatten = train_set_x_orig.reshape(m_train,-1).T
test_set_x_flatten = test_set_x_orig.reshape(m_test, -1).T

# Standardize the dataset
train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255

# Graded function:sigmoid

def sigmoid(z):
    """
    compute the sigmoid of z
    """
    s = 1/(1+np.exp(-z))
    return s;
    
# GRADED FUNCTION: initialize_with_zeros

def initialize_with_zero(dim):
    w = np.zeros([dim,1])
    b = 0.0
    return w,b
    
# GRADED FUNCTION: propagate

def propagate(w, b, X, Y):
    m = X.shape[1]
    # forward
    A = sigmoid(np.dot(w.T,X)+b)
    cost = -(1/m)*(np.dot(Y,(np.log(A+1e-5)).T)+np.dot(1-Y,(np.log((1-A)+1e-5).T)))
    cost = np.squeeze(np.array(cost))
    
    #backward
    dw = (1/m)*np.dot(X,(A-Y).T)
    db = (1/m)*np.sum(A-Y)
    
    grads ={
        "dw":dw,
        "db":db
    }
    
    return grads, cost
    
# GRADED FUNCTION: optimize

def optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False):
    w = copy.deepcopy(w)
    b = copy.deepcopy(b)
    
    costs = []
    
    for i in range(num_iterations):
        
        grads, cost = propagate(w, b, X, Y)
        
        dw = grads['dw']
        db = grads['db']
        
        w = w - learning_rate*dw
        b = b - learning_rate*db
        
        if i % 100 == 0:
            costs.append(cost)
            
            if print_cost:
                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)
    
    A = sigmoid(np.dot(w.T, X)+b)
    
    for i in range(A.shape[1]):
        if A[0,i] > 0.5:
            Y_prediction[0,i] = 1
        else:
            Y_prediction[0,i] = 0
    return Y_prediction
    
# GRADED FUNCTION:model

def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
    # Initilize hyperparameters with zeros
    w, b = initialize_with_zero(X_train.shape[0])
    
    # Gradient descent
    params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
    
    # Retrieve w, b from the dictionary "params"
    w = params['w']
    b = params['b']
    
    # predict
    Y_prediction_train = predict(w, b, X_train)
    Y_prediction_test = predict(w, b, X_test)
    
    # print train/test accuracy
    if print_cost:
        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_train":Y_prediction_train,
        "Y_prediction_test":Y_prediction_test,
        "w":w,
        "b":b,
        "learning_rate":learning_rate,
        "num_iterations":num_iterations}
    return d
    
# Logistic_regression_model
logistic_regression_model = model(train_set_x, train_set_y, test_set_x, test_set_y, 2000, 0.05, True)

# wrongly classifed
index = 44
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
print("y= "+str(test_set_y[0, index])+", you predicted that it is a ""+classes[int(logistic_regression_model['Y_prediction_test'][0, index])].decode("utf-8")+""picture")

# plot learning curve(with costs)
costs = np.squeeze(logistic_regression_model['costs'])
plt.plot(costs)
print(costs)
plt.ylabel('cost')
plt.xlabel('iterations(per hundreds)')
plt.title("learning rate ="+str(logistic_regression_model["learning_rate"]))
plt.show()

# test you image
my_image = 'LogReg_kiank.png'

# We preprocess the image to fit the algorithm
fname ="images/"+my_image
image = Image.open(fname)
image = image.convert("RGB")
image = np.array(image.resize((num_px,num_px)))
plt.imshow(image)
image = image/255
image = image.reshape(1,num_px*num_px*3).T

my_prediction_image = predict(logistic_regression_model['w'], logistic_regression_model['b'], image)

print("y= "+str(np.squeeze(my_prediction_image))+", your algorithm predicts a ""+classes[int(np.squeeze(my_prediction_image)),].decode('utf-8')+"" picture")

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>