吴恩达深度学习第一课第三周课程笔记和课后习题答案

更新时间:2023-05-07 11:57:44 阅读: 评论:0

吴恩达深度学习第⼀课第三周课程笔记和课后习题答案1,课程⼩结
主要讲了只有⼀层隐层的浅层神经⽹络。
假设样本有三个特征,隐层有4个节点,则神经⽹络的结构如上图所⽰。正向传播计算有四个公式。
对于多样本,向量化计算如下:
常⽤的激活函数:
⼀般来说,tanh函数好过sigmod函数,因为其中⼼为0,除⾮是⼆元分类,⼀般不⽤sigmod函数;⼀般情况下,激活函数选⽤Relu函数。
反向传播计算梯度如下:
⼀般w初始化是采⽤随机数⽽不能采⽤全零处理,全零处理会导致每个节点演化相同,没有意义。
课后作业,建⽴⼀个神经⽹络:
导⼊需要的库:
# Package imports
import numpy as np
import matplotlib.pyplot as plt
from testCas import *
import sklearn
import sklearn.datats
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_datat, load_extra_datats
%matplotlib inline
np.random.ed(1) # t a ed so that the results are consistent
得到层数:
# GRADED FUNCTION: layer_sizes
def layer_sizes(X, Y):
"""
Arguments:
X -- input datat of shape (input size, number of examples)
Y -- labels of shape (output size, number of examples)
Returns:
n_x -- the size of the input layer
n_h -- the size of the hidden layer
n_y -- the size of the output layer
"""
### START CODE HERE ### (≈ 3 lines of code)
n_x = X.shape[0] # size of input layer
n_h = 4
n_y = Y.shape[0] # size of output layer
### END CODE HERE ###
return (n_x, n_h, n_y)
初始化参数:
# 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:
params -- 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.ed(2) # we t up a ed so that your output matches ours although the initialization is random.
### 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 ###
asrt (W1.shape == (n_h, n_x))
asrt (b1.shape == (n_h, 1))
asrt (W2.shape == (n_y, n_h))
asrt (b2.shape == (n_y, 1))
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2}
return parameters
前向传播:
# GRADED FUNCTION: forward_propagation
def forward_propagation(X, parameters):
"""
Argument:
X -- input data of size (n_x, m)
parameters -- python dictionary containing your parameters (output of initialization function)
Returns:
A2 -- The sigmoid output of the cond activation
cache -- a dictionary containing "Z1", "A1", "Z2" and "A2"
"""
# Retrieve each parameter from the dictionary "parameters"
### START CODE HERE ### (≈ 4 lines of code)
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
### END CODE HERE ###
# Implement Forward Propagation to calculate A2 (probabilities)
### START CODE HERE ### (≈ 4 lines of code)
Z1 = np.dot(W1,X)+b1
A1 = np.tanh(Z1)
Z2 = np.dot(W2,A1)+b2
A2 = sigmoid(Z2)
### END CODE HERE ###
asrt(A2.shape == (1, X.shape[1]))
cache = {"Z1": Z1,
"A1": A1,
"Z2": Z2,
"A2": A2}
return A2, cache
计算损失函数:

本文发布于:2023-05-07 11:57:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/99228.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:计算   函数   节点   处理
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图