吴恩达 深度学习 编程作业(1-4)- Building your DNN & DNN for Image Classification: Application

【深度学习】吴恩达-课后作业-搭建多层神经网络以及应用 【代码】【深度学习】吴恩达-课后作业-搭建多层神经网络以及应用。 阅读详情

吴恩达Coursera课程 DeepLearning.ai 编程作业系列,本文为《神经网络与深度学习》部分的第四周“深层神经网络”的课程作业,增加了一些辅助的测试函数。
另外,本节课程笔记在此:《吴恩达Coursera深度学习课程 DeepLearning.ai 提炼笔记(1-4)》,如有任何建议和问题,欢迎留言。


Part 1:Building your Deep Neural Network: Step by Step

1 - Packages

Let’s first import all the packages that you will need during this assignment.
- numpy is the main package for scientific computing with Python.
- matplotlib is a library to plot graphs in Python.
- dnn_utils provides some necessary functions for this notebook.
- testCases provides some test cases to assess the correctness of your functions
- np.random.seed(1) is used to keep all the random function calls consistent. It will help us grade your work. Please don’t change the seed.

import numpy as np
import h5py
import matplotlib.pyplot as plt
from testCases_v3 import *
from dnn_utils_v2 import sigmoid, sigmoid_backward, relu, relu_backward

%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

%load_ext autoreload
%autoreload 2

np.random.seed(1)

You can get the support code from here.

the sigmoid function:

def sigmoid(Z):
    """
    Implements the sigmoid activation in numpy

    Arguments:
    Z -- numpy array of any shape

    Returns:
    A -- output of sigmoid(z), same shape as Z
    cache -- returns Z as well, useful during backpropagation
    """

    A = 1/(1+np.exp(-Z))
    cache = Z

    return A, cache

the sigmoid_backward function:

def sigmoid_backward(dA, cache):
    """
    Implement the backward propagation for a single SIGMOID unit.

    Arguments:
    dA -- post-activation gradient, of any shape
    cache -- 'Z' where we store for computing backward propagation efficiently

    Returns:
    dZ -- Gradient of the cost with respect to Z
    """

    Z = cache

    s = 1/(1+np.exp(-Z))
    dZ = dA * s * (1-s)

    assert (dZ.shape == Z.shape)

    return dZ

the relu function:

def relu(Z):
    """
    Implement the RELU function.

    Arguments:
    Z -- Output of the linear layer, of any shape

    Returns:
    A -- Post-activation parameter, of the same shape as Z
    cache -- a python dictionary containing "A" ; stored for computing the backward pass efficiently
    """

    A = np.maximum(0,Z)

    assert(A.shape == Z.shape)

    cache = Z 
    return A, cache

the relu_backward function:

def relu_backward(dA, cache):
    """
    Implement the backward propagation for a single RELU unit.

    Arguments:
    dA -- post-activation gradient, of any shape
    cache -- 'Z' where we store for computing backward propagation efficiently

    Returns:
    dZ -- Gradient of the cost with respect to Z
    """

    Z = cache
    dZ = np.array(dA, copy=True) # just converting dz to a correct object.

    # When z <= 0, you should set dz to 0 as well. 
    dZ[Z <= 0] = 0

    assert (dZ.shape == Z.shape)

    return dZ

2 - Outline of the Assignment

To build your neural network, you will be implementing several “helper functions”. These helper functions will be used in the next assignment to build a two-layer neural network and an L-layer neural network. Each small helper function you will implement will have detailed instructions that will walk you through the necessary steps. Here is an outline of this assignment, you will:

  • Initialize the parameters for a two-layer network and for an L L -layer neural network.
  • Implement the forward propagation module (shown in purple in the figure below).
    • Complete the LINEAR part of a layer’s forward propagation step (resulting in Z [ l ] ).
    • We give you the ACTIVATION function (relu/sigmoid).
    • Combine the previous two steps into a new [LINEAR->ACTIVATION] forward function.
    • Stack the [LINEAR->RELU] forward function L-1 time (for layers 1 through L-1) and add a [LINEAR->SIGMOID] at the end (for the final layer L L ). This gives you a new L_model_forward function.
  • Compute the loss.
  • Implement the backward propagation module (denoted in red in the figure below).
    • Complete the LINEAR part of a layer’s backward propagation step.
    • We give you the gradient of the ACTIVATE function (relu_backward/sigmoid_backward)
    • Combine the previous two steps into a new [LINEAR->ACTIVATION] backward function.
    • Stack [LINEAR->RELU] backward L-1 times and add [LINEAR->SIGMOID] backward in a new L_model_backward function
  • Finally update the parameters.

还在路上,稍等...

Note that for every forward function, there is a corresponding backward function. That is why at every step of your forward module you will be storing some values in a cache. The cached values are useful for computing gradients. In the backpropagation module you will then use the cache to calculate the gradients. This assignment will show you exactly how to carry out each of these steps.

3 - Initialization

You will write two helper functions that will initialize the parameters for your model. The first function will be used to initialize parameters for a two layer model. The second one will generalize this initialization process to L layers.

3.1 - 2-layer Neural Network

Exercise: Create and initialize the parameters of the 2-layer neural network.

Instructions:
- The model’s structure is: LINEAR -> RELU -> LINEAR -> SIGMOID.
- Use random initialization for the weight matrices. Use np.random.randn(shape)*0.01 with the correct shape.
- Use zero initialization for the biases. Use np.zeros(shape).

# GRADED FUNCTION: initialize_parameters

def initialize_parameters(n_x, n_h, n_y):
    """
    Argument:
    n_x -- size of the input layer
    n_h -- size of the hidden layer
    n_y -- size of the output layer

    Returns:
    parameters -- python dictionary containing your parameters:
                    W1 -- weight matrix of shape (n_h, n_x)
                    b1 -- bias vector of shape (n_h, 1)
                    W2 -- weight matrix of shape (n_y, n_h)
                    b2 -- bias vector of shape (n_y, 1)
    """

    np.random.seed(1)

    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = np.random.randn(n_h, n_x)*0.01
    b1 = np.zeros((n_h, 1))
    W2 = np.random.randn(n_y, n_h)*0.01
    b2 = np.zeros((n_y, 1))
    ### END CODE HERE ###

    assert(W1.shape == (n_h, n_x))
    assert(b1.shape == (n_h, 1))
    assert(W2.shape == (n_y, n_h))
    assert(b2.shape == (n_y, 1))

    parameters = {
  
  "W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters    
parameters = initialize_parameters(3,2,1)
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))
W1 = [[ 0.01624345 -0.00611756 -0.00528172]
 [-0.01072969  0.00865408 -0.02301539]]
b1 = [[ 0.]
 [ 0.]]
W2 = [[ 0.01744812 -0.00761207]]
b2 = [[ 0.]]

3.2 - L-layer Neural Network

The initialization for a deeper L-layer neural network is more complicated because there are many more weight matrices and bias vectors. When completing the initialize_parameters_deep, you should make sure that your dimensions match between each layer. Recall that n[l] n [ l ] is the number of units in layer l l . Thus for example if the size of our input X is (12288,209) ( 12288 , 209 ) (with m=209 m = 209 examples) then:

**Shape of W** **Shape of b** **Activation** **Shape of Activation**
**Layer 1** (n[1],12288) ( n [ 1 ] , 12288 ) (n[1],1) ( n [ 1 ] , 1 ) Z[1]=W[1]X+b[1] Z [ 1 ] = W [ 1 ] X + b [ 1 ] (n[1],209) ( n [ 1 ] , 209 )
**Layer 2** (n[2],n[1]) ( n [ 2 ] , n [ 1 ] ) (n[2],1) ( n [ 2 ] , 1 ) Z[2]=W[2]A[1]+b[2] Z [ 2 ] = W [ 2 ] A [ 1 ] + b [ 2 ] (n[2],209) ( n [ 2 ] , 209 )
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
**Layer L-1** (n[L−1],n[L−2]) ( n [ L − 1 ] , n [ L − 2 ] ) (n[L−1],1) ( n [ L − 1 ] , 1 ) Z[L−1]=W[L−1]A[L−2]+b[L−1] Z [ L − 1 ] = W [ L − 1 ] A [ L − 2 ] + b [ L − 1 ] (n[L−1],209) ( n [ L − 1 ] , 209 )
**Layer L** (n[L],n[L−1]) ( n [ L ] , n [ L − 1 ] ) (n[L],1) ( n [ L ] , 1 ) Z[L]=W[L]A[L−1]+b[L] Z [ L ] = W [ L ] A [ L − 1 ] + b [ L ] (n[L],209) ( n [ L ] , 209 )

Remember that when we compute WX+b W X + b in python, it carries out broadcasting. For example, if:

W=⎡⎣⎢jmpknqlor⎤⎦⎥X=⎡⎣⎢
人工智能高效学习路径:从基础数学到实战项目的体系化指南 机器学习作为人工智能的核心技术,其原理基于数据驱动的算法模型,通过从数据中学习规律来实现预测和决策。从基础的线性代数、概率统计到深度学习神经网络,这些数学概念构成了AI技术的理论基础。在工程实践中,Python编程语言配合Scikit-learn、TensorFlow等框架,使得机器学习算法能够高效应用于实际场景。通过监督学习、无监督学习等核心算法,AI技术在图像识别、自然语言处理、推荐系统等领域展现出巨大价值。本文基于交互式学习和可视化工具,提供了一套从数学基础到项目实战的完整学习路径,帮助学习者避免常见 阅读详情

相关推荐

吴恩达深度学习编程作业

吴恩达深度学习编程作业,入门深度学习的绝佳资源,包含非常优秀的代码资源.

吴恩达-深度学习-课后作业-答案与总结

吴恩达-深度学习-课后作业-答案与总结

Codex登录要手机验证码?Cockpit Tools跳过验证保姆级教程

摘要:OpenAI升级风控后,Codex登录需手机验证,增加批量账号使用成本。CockpitTools(GitHub 8.3k star)可绕过验证,支持多AI编程平台账号管理。操作步骤:1) 获取ChatGPT网页Session数据;2) 通过转换工具生成CPA格式JSON;3) 导入工具启动Codex。注意:仅适用于未绑定手机号的账号,Session会过期,且需防范数据泄露风险。商业账号通常直接提供认证文件。(150字)

5847

不同网段共享打印机的方法

不同网段怎么共享打印机,跨路由怎么共享打印机

weixin_44686104的博客 1万+

STM32 FATFS文件系统按行读取SD卡数据

STM32 SD数据实现按行读取

qq_32216815的博客 7826

第38节:cesium 风场效果(含源码+视频)

cesium 风场效果

qq_29384639的博客 2330

YOLOv8 智能交通流量监测完整解决方案

YOLOv8智能交通流量监测系统基于先进的目标检测技术,提供实时车辆识别与统计分析功能。该系统通过虚拟检测线实现多方向车流量统计,支持多种车辆类型分类,并具备数据可视化、异常事件预警等核心功能。完整解决方案包含模型训练、实时检测、批量处理、结果分析等模块,并提供Flask API和Docker两种部署方案。系统采用YOLOv8算法实现高效检测,支持GPU加速,可通过量化、剪枝等技术进一步优化性能,为智能交通管理提供可靠的数据支持和技术保障。

u010117029的博客 118

Uniapp返回上一页并刷新:亲测有效的 goBack 实现技巧!

这个技巧在实际开发中非常常用,特别适合表单提交后返回列表刷新这类场景。比起依赖onShow或复杂的vuex状态同步,这种方式更直接、好维护,也不容易出 bug。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1843

【图像去噪】基于梯度为0法实现图像去噪附matlab代码

近几年,徐立提出的 L0 梯度最小化模型在图像平滑领域得到了广泛的应用和发展[11]. 和以往的局部滤波模型不同的是该模型可以全局的估计图像的像素值,因此,L0 梯度最小化模型在处理图像的同时能够很好的保护图像的边缘.​。

qq_59747472的博客 184

QT入门开发一个时钟

本文讲述了时钟开发的具体过程以及代码。 涉及到一下几个知识点: 1. 定时器使用 2. 本地时间获取 3. resource资源读取 4. 图片绘制 5. 图片按照中心旋转 6. sin或者cos角度计算。

啊渊的专栏 5147

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an &ldquo;advanced algorithms&rdquo; book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex&trade;-M4F 内核的信息,请参见《Cortex&trade;-M4F 技术参考手册》。

上一篇: 吴恩达Coursera深度学习课程 DeepLearning.ai 提炼笔记(1-4)-- 深层神经网络
下一篇: 吴恩达Coursera深度学习课程 DeepLearning.ai 提炼笔记(2-1)-- 深度学习的实践方面
大树先生的博客
博客等级 码龄9年 2692粉丝 200原创
评论 61
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值