Couresra-吴恩达-深度学习-第一章:Neural Network and Deep Learning- 第三周

本文介绍了在Coursera上的吴恩达深度学习课程中,关于浅层神经网络的学习内容,包括神经网络的基本概念、激活函数、梯度下降及反向传播算法的解释,以及通过实例演示了如何构建并训练一个单隐藏层的神经网络模型,并讨论了调整隐藏节点数量对模型性能的影响。
摘要由CSDN通过智能技术生成

本周学习内容

  • Shallow Neural Network (浅层神经网络)
    • Neural Network Overview
    • Neural Network Representation (神经网络的表示)
    • Computing a Neural Network’s Output (计算神经网络的输出)
    • Vectorizing across multiple examples (多个例子的向量化)
    • Explanation for Vectorized Implementation (向量化实现的解释)
    • Activation Functions (激活函数)
    • Why need non-linear activation functions (为什么需要非线性激活函数)
    • Derivatives of activation functions (激活函数的导数)
    • Gradient Descent for Neural Network (神经网络的梯度下降法)
    • Backpropagation intuition (反向传播算法的直观理解)
    • Random Initialization (随机初始化)

测验

2: The tanh activation usually works better than sigmoid activation function for hidden units because the mean of its output is closer to zero, and so it centers the data better for the next layer. True/False?
(tanh激活函数通常比隐藏层单元的sigmoid激活函数效果更好,因为其输出的平均值更接近于零,因此它将数据集中在下一层是更好的选择,请问正确吗?)
【★】True
【 】False
解析:对于隐藏层的激活函数,tanh比simoid表现更好。因为tanh函数的取值范围在 [-1,1]之间,隐藏层的输出被限定在[-1,1]之间,可以看成是在0值附近分布,均值为0.这样从隐藏层到输出层,数据起到了归一化(均值为0)的效果。
对于输出层的激活函数,因为二分类问题的输出取值为{0,1},所以一般选择sigmoid作为激活函数。

9: 看一下下面的单隐层神经网络:
在这里插入图片描述
【★】b[1] 的维度是(4, 1)
【★】W[1] 的维度是 (4, 2)
【★】W[2] 的维度是 (1, 4)
【★】b[2] 的维度是 (1, 1)
10: 在和上一个相同的网络中,z[1] 和 A[1]的维度是多少?

【★】z[1] 和 A[1] 的维度都是 (4,m)
解析:可以理解为:行表示神经元个数,列表示样本数目m。


代码练习

任务名称:Planar data classification with one hidden layer

任务描述:In this notebook you will generate red and blue points to form a flower. You will then fit a neural network to correctly classify the points. You will try different layers and see the results. (生成红色和蓝色的点以组成一朵花,然后让神经网络正确分类这些点。在此任务中会尝试不同层数的神经网络)

  1. 目标:
    • Develop an intuition of back-propagation and see it work on data.
    • Recognize that the more hidden layers you have the more complex structure you could capture.
    • Building all the helper functions to implement a full model with one hidden layer.
  2. 在这个任务中我们会涉及到以下知识
    • Implement a 2-class classification neural network with a single hidden layer.(构建具有单隐藏层的2类分类神经网络)
    • Use units with a non-linear activation function, such as tanh.
    • Compute the cross entropy loss
    • Implement forward and backward propagation

1. 准备package

  • testCases: 提供了一些测试示例来评估函数的正确性
  • planar_utils: 提供了在这个任务中使用的各种有用的功能
import numpy as np
import matplotlib.pyplot as plt
from testCases_v2 import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets

%matplotlib inline

np.random.seed(1) # set a seed so that the results are consistent 设置一个固定的随机种子,以保证接下来的步骤中我们的结果是一致的

2.加载和查看数据集

首先查看将使用的数据集:将一个花图案的2-class数据集加载到变量X,Y中。

X,Y = load_planar_dataset()

数据加载后,可视化。

plt.scatter(X[0,:],X[1,:],c=Y,s=40,cmap=plt.cm.Spectral)

plt.scatter 参数解释
plt.cm.Spectral实现的功能是给label为1的点一种颜色,给label为0的店另一种颜色。
在这里插入图片描述
数据是由红色(y=0)和蓝色(y=1)的数据点组成的一朵花。我们的目标是建立模型来适应这些数据。现在我们有了以下东西:

  • X - 包含了数据点的特征(x1, x2)
  • Y - 对应X的标签(red:0, blue:1)

Exercise: 我们有多少个训练样本? X,Y的shape是怎样的?

shape_X = X.shape
shape_Y = Y.shape
m = Y.shape[1]  # 训练集里面的数量

print ("X的维度为: " + str(shape_X))
print ("Y的维度为: " + str(shape_Y))
print ("数据集里面的数据有:" + str(m) + " 个")

运行结果:

X的维度为: (2, 400)
Y的维度为: (1, 400)
数据集里面的数据有:400

3.查看简单的逻辑回归的分类效果

在构建完整的神经网络之前,我们先看看逻辑回归在这个问题上的表现。我们可以用sklearn的内置函数来坐到这一点。

clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X.T, Y.T)

绘制决策边界:

plot.decison_boundary(lamda x: clf.predict(x), X, Y)
plt.title("Logistic Regression")

#print accuracy
LR_predictions = clf.predict(X.T)
print ("逻辑回归的准确性: %d " % float((np.dot(Y, LR_predictions) + 
		np.dot(1 - Y,1 - LR_predictions)) / float(Y.size) * 100) +
       "% " + "(正确标记的数据点所占的百分比)")

lamda函数(匿名函数的用法)
运行结果:
在这里插入图片描述

逻辑回归的准确性: 47 % (正确标记的数据点所占的百分比)

解析:准确性只有47%的原因是数据集是线性不可分的,所以逻辑回归表现不佳,下面开始正式构建神经网络。


4. 神经网络

以下是我们要搭建的网络:
在这里插入图片描述
数学形式:
在这里插入图片描述
注意:方括号上标 [i] 表示第 i层; 圆括号上标(i) 表示第i个样本。
构建神经网络的一般方法:

  1. 定义神经网络结构(输入单元数量,隐藏单元数量等)
  2. 初始化模型参数
  3. 循环:
    • 前向传播
    • 计算损失函数
    • 后向传播以得到梯度
    • 更新参数(梯度下降)
      可以建立helper functions 来计算步骤1-3,然后将它们合并到一个nn_model()函数中。

4.1 定义神经网络结构:

def layer_sizes(X , Y):
    """
    参数:
     X - 输入数据集,维度为(输入的数量,训练/测试的数量)
     Y - 标签,维度为(输出的数量,训练/测试数量)
    
    返回:
     n_x - 输入层的数量
     n_h - 隐藏层的数量
     n_y - 输出层的数量
    """
    n_x = X.shape[0] #输入层
    n_h = 4 #,隐藏层,硬编码为4
    n_y = Y.shape[0] #输出层
    
    return (n_x,n_h,n_y)

测试一下:

#测试layer_sizes
print("=========================测试layer_sizes=========================")
X_asses , Y_asses = layer_sizes_test_case()
(n_x,n_h,n_y) =  layer_sizes(X_asses,Y_asses)
print("输入层的节点数量为: n_x = " + str(n_x))
print("隐藏层的节点数量为: n_h = " + str(n_h))
print("输出层的节点数量为: n_y = " + str(n_y))

结果:

=========================测试layer_sizes=========================
输入层的节点数量为: n_x = 5
隐藏层的节点数量为: n_h 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值