# UNQ_C2
# GRADED FUNCTION: compute_gradient
def compute_gradient(x, y, w, b):
"""
Computes the gradient for linear regression
Args:
x (ndarray): Shape (m,) Input to the model (Population of cities)
y (ndarray): Shape (m,) Label (Actual profits for the cities)
w, b (scalar): Parameters of the model
Returns
dj_dw (scalar): The gradient of the cost w.r.t. the parameters w
dj_db (scalar): The gradient of the cost w.r.t. the parameter b
"""
# Number of training examples
n = x.shape[0]
# You need to return the following variables correctly
dj_dw = 0
dj_db = 0
### START CODE HERE ###
for i in range (n):
f_wb = w*x[i] + b
dj_db_i = f_wb - y[i]
dj_db += dj_db_i
dj_dw_i = (f_wb - y[i]) * [i]
dj_dw += dj_dw_i
dj_dw = dj_dw / m
dj_db = dj_db / m
### END CODE HERE ###
return dj_dw, dj_db我试图运行这个代码的计算梯度dj/wb,dj/db,我得到了名称错误:名称X没有定义在这个程序中,如果有人有解决我的问题,他们可以张贴在下面。
如果有人能解决我的问题,我真的会很感激,我从过去的几天开始就被困在这个问题上了。
发布于 2022-11-21 04:22:51
在这里,x必须是您在任何地方都没有指定的输入。当您尝试对尚未设置/定义但却遇到此错误的对象执行形状时。在对x=您的输入数组/矩阵进行赋值之前,尝试一些类似于赋值的方法。
https://stackoverflow.com/questions/74514182
复制相似问题