Untitled conversation
- User
{ "cell_type": "markdown", "metadata": {}, "source": [ "## 4. L2 Regularization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\hat{y}^{(i)} = \sigma(w^T x^{(i)} + b)$$\n", "\n", "$$BCE(w, b) = - \sum_{i=1}^{N} y^{(i)} \cdot log(\hat{y}^{(i)}) + (1-y^{(i)}) \cdot log(1-\hat{y}^{(i)})$$\n", "\n", "$$L(w, b) = BCE(w, b) + \frac{\lambda}{2} \| w\|^2$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### <span style="color:#ffd33d"> Exercise 5 : Complete the below codes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\> Here, the <span style="color:skyblue">compute_l2_reg_loss() method computes the l2 regularized loss of the logistic regression model.\n", "\n", "Feel free to change or add variables, but do not modify the method name and its arguements." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def compute_l2_reg_loss(data, label, w, b, l2_lambda=0.5):\n", " """\n", " Compute the bceloss of the logistic regression model, given data and label.\n", "\n", " Args:\n", " data (np.ndarray): Shape (N, ) Input to the model\n", " label (np.ndarray): Shape (N, ) Label of the data\n", " w (np.ndarray): Weight of the logistic regression model\n", " b (float): Bias of the logistic regression model\n", " \n", " Return:\n", " total_loss (float): Total bceloss of the logistic regression model with l2 regularization, given data and label\n", " """\n", " total_loss = 0\n", "\n", " #[NOTE] write your code here!\n", "\n", " return total_loss" ] }
- Assistant
To complete the
compute_l2_reg_lossfunction, we will perform the following steps:- Calculate the predicted probability (\hat{y}^{(i)}) using the logistic regression model.