Deeplearning.ai Course-1 Week-2 Programming Assignment1

前言:

文章以Andrew Ng 的 deeplearning.ai 视频课程为主线,记录Programming Assignments 的实现过程。相对于斯坦福的CS231n课程,Andrew的视频课程更加简单易懂,适合深度学习的入门者系统学习!

这一部分主要涉及到python与numpy的一些基础知识,最重要的两个概念分别是广播和矩阵向量化的实现。具体代码如下:

1.1-sigmoid function, np.exp()

sigmoid(x) is sometimes also known as the logistic function. It is a non-linear function used not only in Machine Learning (Logistic Regression), but also in Deep Learning.


Deeplearning.ai Course-1 Week-2 Programming Assignment1_第1张图片

Before using np.exp(), you will use math.exp() to implement the sigmoid function. You will then see why np.exp() is preferable to math.exp()

import math

def basic_sigmoid(x):

s = 1./(1+math.exp(-x))

return s

Actually, we rarely use the "math" library in deep learning because the inputs of the functions are real numbers. In deep learning we mostly use matrices and vectors. This is why numpy is more useful.

import numpy as np

def sigmoid(x):

s = 1./(1+np.exp(-x))

return s

1.2-sigmoid grdient

The formula is:  sigmoid_derivative(x)=σ′(x)=σ(x)(1−σ(x))

def sigmoid_derivative(x):

s = 1./(1+np.exp(-x))

ds = s*(1-s)

return ds

1.3 - Reshaping arrays

in computer science, an image is represented by a 3D array of shape(length,height,depth=3)(length,height,depth=3). However, when you read an image as the input of an algorithm you convert it to a vector of shape(length∗height∗3,1)(length∗height∗3,1). In other words, you "unroll", or reshape, the 3D array into a 1D vector.


Deeplearning.ai Course-1 Week-2 Programming Assignment1_第2张图片

def image2vector(image):

v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)

return v

1.4 - Normalizing rows

Another common technique we use in Machine Learning and Deep Learning is to normalize our data. It often leads to a better performance because gradient descent converges faster after normalization.

def normalizeRows(x):

x_norm = np.linalg.norm(x,axis=1,keepdims=True)

x = x/x_norm

return x

1.5 - Broadcasting and the softmax function

A very important concept to understand in numpy is "broadcasting". It is very useful for performing mathematical operations between arrays of different shapes.


Deeplearning.ai Course-1 Week-2 Programming Assignment1_第3张图片

def softmax(

x_exp = np.exp(x)

x_sum = np.sum(x_exp,axis=1,keepdims=True)

s = x_exp/x_sum

return s

1.6-Implement the L1 and L2 loss functions

The loss is used to evaluate the performance of your model. The bigger your loss is, the more different your predictions (ŷy^) are from the true values (yy). In deep learning, you use optimization algorithms like Gradient Descent to train your model and to minimize the cost.

      L1 loss is defined as: L1(ŷ,y)=∑|y(i)−ŷ(i)|

          def L1(yhat, y):

                loss = np.sum(np.abs(yhat-y))

                return loss

      L2 loss is defined as:L2(ŷ,y)=∑(y(i)−ŷ(i))**2

            def L2(yhat, y):

                loss = np.sum((yhat-y)**2)               

                return loss

最后附上第一份作业的得分,表示我的程序没有问题,如果觉得我的文章对您有用,请随意打赏,我将持续更新Deeplearning.ai的作业!

Deeplearning.ai Course-1 Week-2 Programming Assignment1_第4张图片

你可能感兴趣的:(Deeplearning.ai Course-1 Week-2 Programming Assignment1)