Ng深度学习 L层神经网络搭建和实例

本文详细介绍了如何从零开始搭建一个L层深度学习神经网络,并通过实例展示了其应用过程,涵盖了网络结构设置、参数初始化、前向传播、反向传播等关键步骤,旨在帮助读者深入理解深度学习的实现原理。
摘要由CSDN通过智能技术生成
L层神经网络
import numpy as np
import h5py
import matplotlib.pyplot as plt

def sigmoid(Z):
    A = 1/(1+np.exp(-Z))
    cache = Z
    return A,cache

def relu(Z):
    A = np.maximum(0,Z)
    cache = Z
    return A,cache

def sigmoid_backward(dA,cache):
    Z = cache
    s = 1/(1+np.exp(-Z))
    dZ = dA*s*(1-s)
    return dZ

def relu_backward(dA,cache):
    Z = cache
    dZ = np.array(dA,copy = True)
    dZ[Z <= 0] = 0
    return dZ

def initialize_parameters_deep(layer_dims):
    L = len(layer_dims)
    parameters={}
    for l in range(1,L):
        #如果此处不使用 “/ np.sqrt(layer_dims[l-1])” 会产生梯度消失
        parameters['W'+str(l)] = np.random.randn(layer_dims[l],layer_dims[l-1]) / np.sqrt(layer_dims[l-1])
        parameters['b'+str(l)] = np.zeros((layer_dims[l],1))
    return parameters

def linear_forward(A_prev, W, b):
    Z = np.dot(W,A_prev)+b
    linear_cache = (A_prev,W,b)
    return Z,linear_cache

def linear_activation_forward(A_prev, W, b, activation):
    Z,linear_cache = linear_forward(A_prev,W,b)
    if activation == 'sigmoid':
        A,activation_cache = sigmoid(Z)#return 2 term and activation_cache caches value 'Z'  
    elif activation == 'relu':
        A,activation_cache = relu(Z)
    cache = (linear_cache,activation_cache)
    return A,cache

def L_model_forward(X,parameters
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值