Python AssertionErrorの解決
解決したいこと
AssertionErrorの解決
CourseraのMachine Learning専門講座を受講しており、
week3の提出課題に取り組んでいるのですが、ヒントをみてその通りにしたり、ネットで調べてもわかりません。
解決方法を教えていただきたいです。
発生している問題・エラー
出ているエラーメッセージを入力
AssertionError: Wrong value for dj_db. Expected: 0.28936094 got: -0.07142857142857142
### 該当するソースコード
```python(jupyter)
●1セル目
---------------------------------------------------------------------------
# UNQ_C3
# GRADED FUNCTION: compute_gradient
def compute_gradient(X, y, w, b, lambda_=None):
"""
Computes the gradient for logistic regression
Args:
X : (ndarray Shape (m,n)) variable such as house size
y : (array_like Shape (m,1)) actual value
w : (array_like Shape (n,1)) values of parameters of the model
b : (scalar) value of parameter of the model
lambda_: unused placeholder.
Returns
dj_dw: (array_like Shape (n,1)) 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.
"""
m, n = X.shape
dj_dw = np.zeros(w.shape)
dj_db = 0.0
### START CODE HERE ###
for i in range(m):
z_wb = 0
#z_wb += b
f_wb = sigmoid(z_wb)
dj_db_i = f_wb - y[i]
dj_db += dj_db_i
# get dj_dw for each attribute
for j in range(n):
z_wb_ij = X[i, j] * w[j]
z_wb += z_wb_ij
# You code here to calculate the gradient from the i-th example for j-th attribute
dj_dw_ij = (f_wb - y[i])* X[i][j]
dj_dw[j] += dj_dw_ij
# divide dj_db and dj_dw by total number of examples
dj_dw = dj_dw / m
dj_db = dj_db / m
### END CODE HERE ###
return dj_db, dj_dw
---------------------------------------------------------------------------
●2セル目
---------------------------------------------------------------------------
# Compute and display gradient with w initialized to zeroes
initial_w = np.zeros(n)
initial_b = 0.0
dj_db, dj_dw = compute_gradient(X_train, y_train, initial_w, initial_b)
print(f'dj_db at initial w (zeros):{dj_db}' )
print(f'dj_dw at initial w (zeros):{dj_dw.tolist()}' )
---------------------------------------------------------------------------
結果
dj_db at initial w (zeros):-0.1
dj_dw at initial w (zeros):[0.0, -11.262842205513591, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
●3セル目
---------------------------------------------------------------------------
# Compute and display cost and gradient with non-zero w
test_w = np.array([ 0.2, -0.5])
test_b = -24
dj_db, dj_dw = compute_gradient(X_train, y_train, test_w, test_b)
print('dj_db at test_w:', dj_db)
print('dj_dw at test_w:', dj_dw.tolist())
# UNIT TESTS
compute_gradient_test(compute_gradient)
---------------------------------------------------------------------------
結果
dj_db at test_w: -0.1
dj_dw at test_w: [0.0, -11.262842205513591]
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-60-0697eff9bbd0> in <module>
8
9 # UNIT TESTS
---> 10 compute_gradient_test(compute_gradient)
~/work/public_tests.py in compute_gradient_test(target)
51 dj_db, dj_dw = target(X, y, test_w, test_b)
52
---> 53 assert np.isclose(dj_db, 0.28936094), f"Wrong value for dj_db. Expected: {0.28936094} got: {dj_db}"
54 assert dj_dw.shape == test_w.shape, f"Wrong shape for dj_dw. Expected: {test_w.shape} got: {dj_dw.shape}"
55 assert np.allclose(dj_dw, [-0.11999166, 0.41498775, -0.71968405]), f"Wrong values for dj_dw. Got: {dj_dw}"
AssertionError: Wrong value for dj_db. Expected: 0.28936094 got: -0.07142857142857142
### 自分で試したこと・エラー内容
●エラー内容
AssertionError: Wrong value for dj_db. Expected: 0.28936094 got: -0.07142857142857142
0 likes