我在 Coursera 上注册了 Andrew Ng 的机器学习专业课程,在那里我遇到了实现梯度下降算法的这个函数。
def gradient_descent(x, y, w_in, b_in, alpha, num_iters, cost_function, gradient_function):
w = copy.deepcopy(w_in) # avoid modifying global w_in
# An array to store cost J and w's at each iteration primarily for graphing later
J_history = []
p_history = []
b = b_in
w = w_in
for i in range(num_iters):
# Calculate the gradient and update the parameters using gradient_function
dj_dw, dj_db = gradient_function(x, y, w , b)
# Update Parameters using equation (3) above
b = b - alpha * dj_db
w = w - alpha * dj_dw
# Save cost J at each iteration
if i<100000: # prevent resource exhaustion
J_history.append( cost_function(x, y, w , b))
p_history.append([w,b])
# Print cost every at intervals 10 times or as many iterations if < 10
if i% math.ceil(num_iters/10) == 0:
print(f"Iteration {i:4}: Cost {J_history[-1]:0.2e} ",
f"dj_dw: {dj_dw: 0.3e}, dj_db: {dj_db: 0.3e} ",
f"w: {w: 0.3e}, b:{b: 0.5e}")
return w, b, J_history, p_history #return w and J,w history for graphing`
谁能给我解释一下 for 循环中的第二个 if 语句?
我正在了解该条件语句的实际目的?我确实理解是在控制台上打印一些东西,但是在这种情况下,以下条件表示什么?
if i% math.ceil(num_iters/10) == 0:
如果你解构
i% math.ceil(num_iters/10) == 0
:
num_iters/10
是迭代次数除以 10.math.ceil
返回向上舍入的数字,以使其成为整数。%
是模运算符,因此它返回除法的余数。== 0
,如果除法余数为0,则表示i
是num_iters/10
的倍数。总的来说,当
True
在num_iters的十分位数时,这个表达式是i
例如,如果 num_iters = 200,这将打印十次,当 i = 20, 40, 60, ... , 180, 200