代码填空,下面是一段使用NumPy搭建神经网络的代码,损失函数为交叉熵: import numpy as np def sigmoid(x): return 1/(1+math.exp(-x)) def forward(W_1, W_2, X, Y): z_2 = np.dot(X, W_1) a_2 = sigmoid(z_2) y_pred = sigmoid(z_3) J_z_3_grad = J_W_2_grad = a_2.T @ J_z_3_grad J_a_2_grad = J_z_3_grad @ W_2.T a_2_z_2_grad = J_z_2_grad = J_W_1_grad = return y_pred, (J_W_1_grad, J_W_2_grad)
时间: 2024-03-30 20:34:35 浏览: 105
import numpy as np
import math
def sigmoid(x):
return 1/(1+math.exp(-x))
def forward(W_1, W_2, X, Y):
z_2 = np.dot(X, W_1)
a_2 = sigmoid(z_2)
z_3 = np.dot(a_2, W_2)
y_pred = sigmoid(z_3)
J_z_3_grad = (y_pred - Y) * y_pred * (1 - y_pred) # 填空1
J_W_2_grad = a_2.T @ J_z_3_grad
J_a_2_grad = J_z_3_grad @ W_2.T
a_2_z_2_grad = a_2 * (1 - a_2) # 填空2
J_z_2_grad = J_a_2_grad * a_2_z_2_grad # 填空3
J_W_1_grad = X.T @ J_z_2_grad
return y_pred, (J_W_1_grad, J_W_2_grad)
阅读全文