Deeplearning.ai Course-2 Week-1 Programming Assignment1


.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.前言:

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

本次作业主要针对网络权重的初始化,对于不同的初始化方式可能最后对网络的训练结果产生不同的影响!

1.1 Dataset

首先我们看一下样本的集合

train_X, train_Y, test_X, test_Y = load_dataset()

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

1.2 Neural Network model

我们定义了一个3层的神经网络,对于weight的初始化方式有3种:Zeros initialization,Random initialization,He initialization,对于bias的初始化一般都置为0,,代码如下:

def model(X, Y, learning_rate = 0.01, num_iterations = 15000, print_cost = True, initialization = "he"):

grads = {}

costs = [] 

m = X.shape[1] 

layers_dims = [X.shape[0], 10, 5, 1]

if initialization == "zeros":

parameters = initialize_parameters_zeros(layers_dims)

elif initialization == "random":

parameters = initialize_parameters_random(layers_dims)

elif initialization == "he":

parameters = initialize_parameters_he(layers_dims)

for i in range(0, num_iterations):

a3, cache = forward_propagation(X, parameters)

cost = compute_loss(a3, Y)

grads = backward_propagation(X, Y, cache)

parameters = update_parameters(parameters, grads, learning_rate)

if print_cost and i % 1000 == 0:

print("Cost after iteration {}: {}".format(i, cost))

costs.append(cost)

plt.plot(costs)

plt.ylabel('cost')

plt.xlabel('iterations (per hundreds)')

plt.title("Learning rate =" + str(learning_rate))

plt.show()

return parameters

1.3 Zero Initialization

对于全0的weight初始化方式为:

def initialize_parameters_zeros(layers_dims):

L=len(layers_dims)

parameters={}

for i in range(1,L):

parameters["W"+str(i)]=np.zeros((layers_dims[i],layers_dims[i-1]))

parameters["b"+str(i)]=np.zeros((layers_dims[i],1))

return parameters

在全0初始化的情况下训练结果如下:

parameters = model(train_X, train_Y, initialization = "zeros")

print ("On the train set:")

predictions_train = predict(train_X, train_Y, parameters)

print ("On the test set:")

predictions_test = predict(test_X, test_Y, parameters)

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

从训练结果可以看出在全零初始化的情况下,权重不能进行有效的学习,因为梯度都是0,学不到东西,在这种情况下可见准确率也是最差的。

我们看一下这种情况下的decision boundary

plt.title("Model with Zeros initialization")

axes = plt.gca()

axes.set_xlim([-1.5,1.5])

axes.set_ylim([-1.5,1.5])

plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

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

可以看出对数据集并没有好的拟合。

1.4 Random Initialization

我们再看一下随机初始化的方式,在这种方式下将会产生均值为0的标准正态分布数据

def initialize_parameters_random(layers_dims):

np.random.seed(3)              

parameters = {}

L = len(layers_dims)           

for i in range(1, L):

parameters["W"+str(i)]=np.random.randn(layers_dims[i],layers_dims[i-1])*10

parameters["b"+str(i)]=np.zeros((layers_dims[i],1))

return parameters

训练过程如下:

parameters = model(train_X, train_Y, initialization = "random")

print ("On the train set:")

predictions_train = predict(train_X, train_Y, parameters)

print ("On the test set:")

predictions_test = predict(test_X, test_Y, parameters)

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

在这种情况下准确率能达到86%,较好的拟合了数据,我们看一下形成的decision boundary

plt.title("Model with large random initialization")

axes = plt.gca()

axes.set_xlim([-1.5,1.5])

axes.set_ylim([-1.5,1.5])

plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

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

从decision boundary中我们也可以看出,随机初始化较好的拟合了数据

1.5 He Initialization

He Initialization 是一种较新的初始化方式,类似于随机初始化,但是差别在于在随机初始化的基础上乘以 sqrt(2/dimension of the previous layer),实现代码如下:

def initialize_parameters_he(layers_dims):

np.random.seed(3)

parameters = {}

L = len(layers_dims) # integer representing the number of layers

for i in range(1, L):

parameters["W" + str(i)] = np.random.randn(layers_dims[i], layers_dims[i - 1]) * np.sqrt(2 / layers_dims[i - 1])

parameters["b" + str(i)] = np.zeros((layers_dims[i], 1))

return parameters

训练结果如下:

parameters = model(train_X, train_Y, initialization = "he")

print ("On the train set:")

predictions_train = predict(train_X, train_Y, parameters)

print ("On the test set:")

predictions_test = predict(test_X, test_Y, parameters)

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

我们再看一下decision boundary的情况:

plt.title("Model with He initialization")

axes = plt.gca()

axes.set_xlim([-1.5,1.5])

axes.set_ylim([-1.5,1.5])

plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

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

在这种情况下能达到最高的准确率,数据拟合程度最好。

当然针对于不同的target,权重初始化的方式可能不同,不一定在所有的任务中He Initialization的方式都是完美的方式。

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

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

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