definitialize(n): """ 此函数为w创建一个维度为(n,1)的0向量,并将b初始化为0。 参数: n - 我们想要的w矢量的大小(或者这种情况下的参数数量) 返回: w - 维度为(n,1)的初始化向量。 b - 初始化的标量(对应于偏差) """ w = np.zeros(shape=(n, 1)) b = 0 return (w, b)
实现目标函数计算误差反向传播
1 2 3 4 5 6 7
defj(w, b, X, Y): m = X.shape[1] A = sigmod(z(w, X, b)) J = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A))) dw = (1 / m) * np.dot(X, (A - Y).T) db = (1 / m) * np.sum(A - Y) return (dw,db)
梯度下降更新参数,这里使用批处理梯度下降 θ=θ−αdθ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
defoptimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False): costs = [] for i inrange(num_iterations): dw, db, cost = j(w, b, X, Y) w = w - learning_rate * dw b = b - learning_rate * db # 记录成本 if i % 100==0: costs.append(cost) # 打印成本数据 if (print_cost) and (i % 100==0): print("迭代的次数: %i , 误差值: %f" % (i, cost)) params = { "w": w, "b": b} grads = { "dw": dw, "db": db} return (params, grads, costs)