练习一:X Y的维度
numpy.ndarray.shape使用方法
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: incompatible shape for a non-contiguous array
练习二:定义神经网络结构
4.1 - Defining the neural network structure
使用.shape方法取出x、y的维度
4.2 - Initialize the model’s parameters
initialize_parameters(n_x, n_h, n_y)
返回:
parameters = {“W1”: W1,
“b1”: b1,
“W2”: W2,
“b2”: b2}
根据提示使用 np.random.randn(a,b) * 0.01 to randomly initialize a matrix of shape (a,b).
Use: np.zeros((a,b)) to initialize a matrix of shape (a,b) with zeros.
4.3 - The Loop
-
forward_propagation(X, parameters) #前向传播
使用parameters[“W1”]来获取W1的数据返回:A2, cache
cache = {“Z1”: Z1,
“A1”: A1,
“Z2”: Z2,
“A2”: A2} -
compute_cost(A2, Y, parameters)# 计算损失
返回:
cost -
backward_propagation(parameters, cache, X, Y) #反向传播
返回:
grads = {“dW1”: dW1,
“db1”: db1,
“dW2”: dW2,
“db2”: db2} -
update_parameters(parameters, grads, learning_rate = 1.2)# 更新所有参数
4.4 - Integrate parts 4.1, 4.2 and 4.3 in nn_model()
综合前面定义的函数到一起:
nn_model(X, Y, n_h, num_iterations = 10000, print_cost=False)