Skip to content

Commit fb3a822

Browse files
committed
15 / 30 : add cost function plots over the parameter space
1 parent 6205981 commit fb3a822

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

15_optimization/030_Classification_Optimization.ipynb

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,62 @@
325325
"\n"
326326
]
327327
},
328+
{
329+
"cell_type": "markdown",
330+
"metadata": {},
331+
"source": [
332+
"Cost function over parameter space ($w_1 \\times w_2$ plane)<br>\n",
333+
"매개변수공간 ($w_1 \\times w_2$ 평면) 상의 비용함수\n",
334+
"\n"
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": null,
340+
"metadata": {},
341+
"outputs": [],
342+
"source": [
343+
"def plot_cost_surf(cost_function):\n",
344+
"\n",
345+
" w1 = np.linspace(-10.0, 10.0, 20*20+1)\n",
346+
" w2 = np.linspace(-10.0, 10.0, 20*20+1)\n",
347+
"\n",
348+
" W1, W2 = np.meshgrid(w1, w2)\n",
349+
" C = np.zeros_like(W1)\n",
350+
"\n",
351+
" for i_row in range(W1.shape[0]):\n",
352+
" for j_col in range(W1.shape[1]):\n",
353+
" w = np.array([W1[i_row, j_col], W2[i_row, j_col]])\n",
354+
" C[i_row, j_col] = cost_function(w, data)\n",
355+
"\n",
356+
" fig = plt.figure(figsize=(10, 5))\n",
357+
"\n",
358+
" ax0 = fig.add_subplot(1, 2, 1)\n",
359+
" ax0.contour(W1, W2, C, cmap='viridis')\n",
360+
" ax0.set_xlabel('$w_1$')\n",
361+
" ax0.set_ylabel('$w_2$')\n",
362+
" ax0.axis('equal')\n",
363+
" ax0.grid(True)\n",
364+
"\n",
365+
" ax1 = fig.add_subplot(1, 2, 2, projection='3d')\n",
366+
" ax1.plot_surface(W1, W2, C, cmap='viridis')\n",
367+
" ax1.set_xlabel('$w_1$')\n",
368+
" ax1.set_ylabel('$w_2$')\n",
369+
"\n",
370+
" return ax0, ax1\n",
371+
"\n"
372+
]
373+
},
374+
{
375+
"cell_type": "code",
376+
"execution_count": null,
377+
"metadata": {},
378+
"outputs": [],
379+
"source": [
380+
"axs = plot_cost_surf(cost_function_first_attempt)\n",
381+
"\n"
382+
]
383+
},
328384
{
329385
"cell_type": "markdown",
330386
"metadata": {},
@@ -496,6 +552,25 @@
496552
"\n"
497553
]
498554
},
555+
{
556+
"cell_type": "markdown",
557+
"metadata": {},
558+
"source": [
559+
"Cost function over parameter space ($w_1 \\times w_2$ plane)<br>\n",
560+
"매개변수공간 ($w_1 \\times w_2$ 평면) 상의 비용함수\n",
561+
"\n"
562+
]
563+
},
564+
{
565+
"cell_type": "code",
566+
"execution_count": null,
567+
"metadata": {},
568+
"outputs": [],
569+
"source": [
570+
"axs = plot_cost_surf(cost_function_step)\n",
571+
"\n"
572+
]
573+
},
499574
{
500575
"cell_type": "markdown",
501576
"metadata": {},
@@ -638,6 +713,25 @@
638713
"\n"
639714
]
640715
},
716+
{
717+
"cell_type": "markdown",
718+
"metadata": {},
719+
"source": [
720+
"Cost function over parameter space ($w_1 \\times w_2$ plane)<br>\n",
721+
"매개변수공간 ($w_1 \\times w_2$ 평면) 상의 비용함수\n",
722+
"\n"
723+
]
724+
},
725+
{
726+
"cell_type": "code",
727+
"execution_count": null,
728+
"metadata": {},
729+
"outputs": [],
730+
"source": [
731+
"axs = plot_cost_surf(cost_function_sigmoid)\n",
732+
"\n"
733+
]
734+
},
641735
{
642736
"cell_type": "markdown",
643737
"metadata": {},
@@ -729,6 +823,25 @@
729823
"\n"
730824
]
731825
},
826+
{
827+
"cell_type": "markdown",
828+
"metadata": {},
829+
"source": [
830+
"Cost function over parameter space ($w_1 \\times w_2$ plane)<br>\n",
831+
"매개변수공간 ($w_1 \\times w_2$ 평면) 상의 비용함수\n",
832+
"\n"
833+
]
834+
},
835+
{
836+
"cell_type": "code",
837+
"execution_count": null,
838+
"metadata": {},
839+
"outputs": [],
840+
"source": [
841+
"axs = plot_cost_surf(cost_function_cross_entropy)\n",
842+
"\n"
843+
]
844+
},
732845
{
733846
"cell_type": "markdown",
734847
"metadata": {},

0 commit comments

Comments
 (0)