Couresra-吴恩达-深度学习-第一章:Neural Network and Deep Learning- 第四周
本周学习内容
- Key Concepts on Deep Neural Network
- Deep L-layer neural network (深层神经网络)
- Forward Propagation in a Deep Network (前向算法)
- Getting your matrix dimensions right (核对矩阵的维数)
- Why deep representations (为什么用深层表示)
- Building blocks of deep neural networks (搭建深层神经网络)
- Forward and Backward Propagation (前向和后向算法)
- Parameters vs Hyperparameters (参数 vs 超参数)
- What does this have to do with brain (这和大脑有什么关系)
测验
本周题目都比较简单,主要考察参数的维度:
W [ l ] : ( n [ l ] , n [ l − 1 ] ) W^{[l]} : (n^{[l]},n^{[l-1]}) W[l]:(n[l],n[l−1])
b [ l ] : ( n [ l ] , 1 ) b^{[l]} : (n^{[l]},1) b[l]:(n[l],1)
代码练习
任务1:Building your Deep Neural Network: Step by Step
目标
- Develop an intuition of the over all structure of a neural network.
- Write functions (e.g. forward propagation, backward propagation, logistic loss, etc…) that would help you decompose your code and ease the process of building a neural network.
- Initialize/update parameters according to your desired structure.
大纲(Outline of the Assignment)
我们将构建两个神经网络,一个是两层的,一个是多层的。必须的步骤有:
- 初始化参数
- 前向传播(图中紫色部分)
- LINEAR part - 生成 Z [ l [ Z^{[l[} Z[l[
Z = np.dot(W,A) + b
- LINEAR > ACTIVATION forward function
- LINEAR > ReLU (L-1层)
A = relu(Z)
- LINEAR > Sigmoid(最后一层L)
A = sigmoid(Z)
- LINEAR > ReLU (L-1层)
- LINEAR part - 生成 Z [ l [ Z^{[l[} Z[l[
- 计算损失函数
- 反向传播 (图中红色部分)
- 线性部分的反向传播
- 激活函数部分的反向传播
- 结合线性部分与激活函数的反向传播公式
- 更新参数
1. 初始化参数(Initialization)
两层的:
def initialize_parameters(n_x,n_h,n_y):
"""
此函数是为了初始化两层网络参数而使用的函数。
参数:
n_x - 输入层节点数量
n_h - 隐藏层节点数量
n_y - 输出层节点数量
返回:
parameters - 包含你的参数的python字典:
W1 - 权重矩阵,维度为(n_h,n_x)
b1 - 偏向量,维度为(n_h,1)
W2 - 权重矩阵,维度为(n_y,n_h)
b2 - 偏向量,维度为(n_y,1)
"""
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))
#使用断言确保我的数据格式是正确的
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
L层的参数初始化稍复杂,另外涉及到一点broadcasting,但只要记住 w , b w,b w,b的维度即可
def initialize_parameters_deep(layers_dims):
"""
此函数是为了初始化多层网络参数而使用的函数。
参数:
layers_dims - 包含我们网络中每个图层的节点数量的列表
返回:
parameters - 包含参数“W1”,“b1”,...,“WL”,“bL”的字典:
W1 - 权重矩阵,维度为(layers_dims [1],layers_dims [1-1])
bl - 偏向量,维度为(layers_dims [1],1)
"""
np.<