g = 1 / (1 + np.exp(-z))
\n",
" sigmoid(0) | \n", "0.5 | \n", "
sigmoid([-1, 0, 1, 2]) | \n", "[0.26894142 0.5 0.73105858 0.88079708] | \n", "
z_wb_ij = w[j]*X[i][j]
\n",
"f_wb = sigmoid(z_wb)
\n",
" loss = -y[i] * np.log(f_wb) - (1 - y[i]) * np.log(1 - f_wb)
\n",
"Cost at initial w and b (zeros) | \n", "0.693 | \n", "
Cost at test w and b (non-zeros): | \n", "0.218 | \n", "
compute_cost
above — for detailed hints on how to calculate each intermediate term, check out the hints section below that exercise\n",
" \n", " for i in range(m): \n", " # Calculate f_wb (exactly how you did it in the compute_cost function above)\n", " z_wb = 0\n", " # Loop over each feature\n", " for j in range(n): \n", " # Add the corresponding term to z_wb\n", " z_wb_ij = X[i, j] * w[j]\n", " z_wb += z_wb_ij\n", " \n", " # Add bias term \n", " z_wb += b\n", " \n", " # Calculate the prediction from the model\n", " f_wb = sigmoid(z_wb)\n", "
dj_db_i = f_wb - y[i]
\n",
" dj_dw_ij = (f_wb - y[i])* X[i][j]
\n",
" dj_db at initial w and b (zeros) | \n", "-0.1 | \n", "
dj_dw at initial w and b (zeros): | \n", "[-12.00921658929115, -11.262842205513591] | \n", "
dj_db at test w and b (non-zeros) | \n", "-0.5999999999991071 | \n", "
dj_dw at test w and b (non-zeros): | \n", "[-44.8313536178737957, -44.37384124953978] | \n", "
compute_cost
above — for detailed hints on how to calculate each intermediate term, check out the hints section below that exercise\n",
" \n", " for i in range(m): \n", " # Calculate f_wb (exactly how you did it in the compute_cost function above)\n", " z_wb = 0\n", " # Loop over each feature\n", " for j in range(n): \n", " # Add the corresponding term to z_wb\n", " z_wb_ij = X[i, j] * w[j]\n", " z_wb += z_wb_ij\n", " \n", " # Add bias term \n", " z_wb += b\n", " \n", " # Calculate the prediction from the model\n", " f_wb = sigmoid(z_wb)\n", "
x = y < 3
. Now do the same for p[i] = 1 if f_wb >= 0.5 and 0 otherwise. \n",
" p[i] = f_wb >= 0.5
\n",
" Output of predict: shape (4,),value [0. 1. 1. 1.] | \n", "
Train Accuracy (approx): | \n", "92.00 | \n", "
reg_cost_j = w[j]**2
\n",
" Regularized cost : | \n", "0.6618252552483948 | \n", "
dj_dw_j_reg = (lambda_ / m) * w[j]
\n",
" dj_db:0.07138288792343 |
First few elements of regularized dj_dw: |
[[-0.010386028450548], [0.011409852883280], [0.0536273463274], [0.003140278267313]] | \n", "
Train Accuracy:~ 80% |
Important Note: Please only do this when you've already passed the assignment to avoid problems with the autograder.\n", "
Here's a short demo of how to do the steps above: \n",
"
\n",
" \n",
"